Skip to content

Commit 6812061

Browse files
committed
refactoring
1 parent 014b40e commit 6812061

File tree

5 files changed

+277
-109
lines changed

5 files changed

+277
-109
lines changed

Editor/ExtendedEditor.cs

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#if UNITY_EDITOR
2+
namespace DCFApixels.Notes.Editors
3+
{
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Reflection;
7+
using Unity.Collections.LowLevel.Unsafe;
8+
using UnityEditor;
9+
using UnityEngine;
10+
using UnityObject = UnityEngine.Object;
11+
12+
internal abstract class ExtendedEditor : Editor
13+
{
14+
private bool _isStaticInit = false;
15+
private bool _isInit = false;
16+
17+
protected bool IsMultipleTargets { get { return targets.Length > 1; } }
18+
protected float OneLineHeight { get => EditorGUIUtility.singleLineHeight; }
19+
protected float Spacing { get => EditorGUIUtility.standardVerticalSpacing; }
20+
protected virtual bool IsStaticInit { get { return _isStaticInit; } }
21+
protected virtual bool IsInit { get { return _isInit; } }
22+
protected NotesSettings Settings { get { return NotesSettings.Instance; } }
23+
24+
protected void StaticInit()
25+
{
26+
if (IsStaticInit) { return; }
27+
_isStaticInit = true;
28+
OnStaticInit();
29+
}
30+
protected void Init()
31+
{
32+
if (IsInit) { return; }
33+
_isInit = true;
34+
OnInit();
35+
}
36+
protected virtual void OnStaticInit() { }
37+
protected virtual void OnInit() { }
38+
39+
public sealed override void OnInspectorGUI()
40+
{
41+
EditorGUI.BeginChangeCheck();
42+
StaticInit();
43+
Init();
44+
DrawCustom();
45+
if (EditorGUI.EndChangeCheck())
46+
{
47+
serializedObject.ApplyModifiedProperties();
48+
}
49+
}
50+
51+
protected abstract void DrawCustom();
52+
protected void DrawDefault()
53+
{
54+
base.OnInspectorGUI();
55+
}
56+
57+
protected SerializedProperty FindProperty(string name)
58+
{
59+
return serializedObject.FindProperty(name);
60+
}
61+
}
62+
internal abstract class ExtendedEditor<T> : ExtendedEditor
63+
{
64+
public T Target
65+
{
66+
get
67+
{
68+
var obj = target;
69+
return UnsafeUtility.As<UnityObject, T>(ref obj);
70+
}
71+
}
72+
public T[] Targets
73+
{
74+
get
75+
{
76+
var obj = targets;
77+
return UnsafeUtility.As<UnityObject[], T[]>(ref obj);
78+
}
79+
}
80+
}
81+
internal abstract class ExtendedPropertyDrawer : PropertyDrawer
82+
{
83+
private bool _isStaticInit = false;
84+
private bool _isInit = false;
85+
86+
private IEnumerable<Attribute> _attributes = null;
87+
88+
protected IEnumerable<Attribute> Attributes
89+
{
90+
get
91+
{
92+
if (_attributes == null)
93+
{
94+
_attributes = fieldInfo.GetCustomAttributes();
95+
}
96+
return _attributes;
97+
}
98+
}
99+
protected float OneLineHeight { get => EditorGUIUtility.singleLineHeight; }
100+
protected float Spacing { get => EditorGUIUtility.standardVerticalSpacing; }
101+
protected virtual bool IsStaticInit { get { return _isStaticInit; } }
102+
protected virtual bool IsInit { get { return _isInit; } }
103+
protected NotesSettings Settings { get { return NotesSettings.Instance; } }
104+
105+
106+
protected void StaticInit()
107+
{
108+
if (IsStaticInit) { return; }
109+
_isStaticInit = true;
110+
OnStaticInit();
111+
}
112+
protected void Init()
113+
{
114+
if (IsInit) { return; }
115+
_isInit = true;
116+
OnInit();
117+
}
118+
protected virtual void OnStaticInit() { }
119+
protected virtual void OnInit() { }
120+
121+
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
122+
{
123+
EditorGUI.BeginChangeCheck();
124+
StaticInit();
125+
Init();
126+
DrawCustom(position, property, label);
127+
if (EditorGUI.EndChangeCheck())
128+
{
129+
property.serializedObject.ApplyModifiedProperties();
130+
}
131+
}
132+
protected abstract void DrawCustom(Rect position, SerializedProperty property, GUIContent label);
133+
}
134+
internal abstract class ExtendedPropertyDrawer<TAttribute> : ExtendedPropertyDrawer
135+
{
136+
protected TAttribute Attribute
137+
{
138+
get
139+
{
140+
var obj = attribute;
141+
return UnsafeUtility.As<PropertyAttribute, TAttribute>(ref obj);
142+
}
143+
}
144+
}
145+
}
146+
#endif

Editor/LazyNoteEditor.cs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,50 @@ namespace DCFApixels.Notes.Editors
66
{
77
using static NotesConsts;
88
[CustomEditor(typeof(LazyNote))]
9-
internal class LazyNoteEditor : Editor
9+
internal class LazyNoteEditor : ExtendedEditor<LazyNote>
1010
{
1111
private Rect rect = new Rect();
1212
private Texture2D _lineTex;
1313
private bool _IsInit = false;
1414

15-
private LazyNote Target => target as LazyNote;
15+
private SerializedProperty _heightProp;
16+
private SerializedProperty _textProp;
17+
private SerializedProperty _drawIconProp;
18+
private SerializedProperty _colorProp;
1619

17-
private void Init()
20+
#region Init
21+
protected override void OnInit()
1822
{
19-
if (_IsInit) return;
2023
_lineTex = CreateTexture(2, 2, Color.black);
21-
_IsInit = true;
24+
25+
_heightProp = FindProperty("_height");
26+
_textProp = FindProperty("_text");
27+
_drawIconProp = FindProperty("_drawIcon");
28+
_colorProp = FindProperty("_color");
2229
}
30+
private static Texture2D CreateTexture(int width, int height, Color32 color32)
31+
{
32+
var pixels = new Color32[width * height];
33+
for (var i = 0; i < pixels.Length; ++i)
34+
pixels[i] = color32;
2335

24-
public override void OnInspectorGUI()
36+
var result = new Texture2D(width, height);
37+
result.SetPixels32(pixels);
38+
result.Apply();
39+
return result;
40+
}
41+
#endregion
42+
43+
#region Draw
44+
protected override void DrawCustom()
2545
{
26-
Init();
2746
Color defaultColor = GUI.color;
2847
Color defaultBackgroundColor = GUI.backgroundColor;
2948

30-
EditorGUI.BeginChangeCheck();
31-
SerializedProperty heightProp = serializedObject.FindProperty("_height");
32-
SerializedProperty textProp = serializedObject.FindProperty("_text");
33-
SerializedProperty colorProp = serializedObject.FindProperty("_color");
34-
SerializedProperty drawIconProp = serializedObject.FindProperty("_drawIcon");
35-
36-
Color color = colorProp.colorValue;
49+
Color color = _colorProp.colorValue;
3750

3851
Color elemcolor = NormalizeBackgroundColor(color);
39-
rect = new Rect(0, 0, EditorGUIUtility.currentViewWidth, EditorGUIUtility.singleLineHeight * 2 + heightProp.floatValue + 5);
52+
rect = new Rect(0, 0, EditorGUIUtility.currentViewWidth, EditorGUIUtility.singleLineHeight * 2 + _heightProp.floatValue + 5);
4053

4154
EditorGUI.DrawRect(rect, color);
4255

@@ -53,7 +66,7 @@ public override void OnInspectorGUI()
5366
GUIStyle gUIStyle = new GUIStyle(EditorStyles.label);
5467
gUIStyle.normal.textColor = new Color(0.1f, 0.1f, 0.1f, 0.2f);
5568

56-
drawIconProp.boolValue = EditorGUILayout.Toggle(drawIconProp.boolValue, GUILayout.MaxWidth(16));
69+
_drawIconProp.boolValue = EditorGUILayout.Toggle(_drawIconProp.boolValue, GUILayout.MaxWidth(16));
5770
GUILayout.Label("", gUIStyle);
5871

5972
float originalValue = EditorGUIUtility.labelWidth;
@@ -62,24 +75,21 @@ public override void OnInspectorGUI()
6275
GUI.backgroundColor = Color.white;
6376

6477
GUIStyle gUIStylex = new GUIStyle(EditorStyles.helpBox);
65-
heightProp.floatValue = EditorGUILayout.FloatField("↕", heightProp.floatValue, gUIStylex, GUILayout.MaxWidth(58));
66-
heightProp.floatValue = Mathf.Max(MIN_NOTE_HEIGHT, heightProp.floatValue);
78+
_heightProp.floatValue = EditorGUILayout.FloatField("↕", _heightProp.floatValue, gUIStylex, GUILayout.MaxWidth(58));
79+
_heightProp.floatValue = Mathf.Max(MIN_NOTE_HEIGHT, _heightProp.floatValue);
6780
GUI.color = defaultColor;
6881
EditorGUIUtility.labelWidth = originalValue;
6982

70-
Color newColor = EditorGUILayout.ColorField(colorProp.colorValue, GUILayout.MaxWidth(40));
83+
Color newColor = EditorGUILayout.ColorField(_colorProp.colorValue, GUILayout.MaxWidth(40));
7184
newColor.a = 1f;
72-
colorProp.colorValue = newColor;
85+
_colorProp.colorValue = newColor;
7386

7487
EditorGUILayout.EndHorizontal();
7588

7689
GUILayout.Box(_lineTex, GUILayout.Height(1), GUILayout.ExpandWidth(true));
7790

78-
textProp.stringValue = EditorGUILayout.TextArea(textProp.stringValue, areastyle, GUILayout.Height(heightProp.floatValue));
91+
_textProp.stringValue = EditorGUILayout.TextArea(_textProp.stringValue, areastyle, GUILayout.Height(_heightProp.floatValue));
7992
GUI.backgroundColor = defaultBackgroundColor;
80-
81-
serializedObject.ApplyModifiedProperties();
82-
EditorGUI.EndChangeCheck();
8393
}
8494
public override void DrawPreview(Rect previewArea)
8595
{
@@ -92,17 +102,7 @@ private static Color NormalizeBackgroundColor(Color color)
92102
S -= S * 0.62f;
93103
return Color.HSVToRGB(H, S, V) * 3f;
94104
}
95-
private static Texture2D CreateTexture(int width, int height, Color32 color32)
96-
{
97-
var pixels = new Color32[width * height];
98-
for (var i = 0; i < pixels.Length; ++i)
99-
pixels[i] = color32;
100-
101-
var result = new Texture2D(width, height);
102-
result.SetPixels32(pixels);
103-
result.Apply();
104-
return result;
105-
}
105+
#endregion
106106
}
107107
}
108108
#endif

Editor/NoteArrowEditor.cs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,10 @@ namespace DCFApixels.Notes.Editors
77
{
88
[CustomEditor(typeof(NoteArrow))]
99
[InitializeOnLoad]
10-
public class NoteArrowEditor : Editor
10+
internal class NoteArrowEditor : ExtendedEditor<NoteArrow>
1111
{
12-
private struct Segment
13-
{
14-
public float halfHeight;
15-
public float lerpT;
16-
public Segment(float halfHeight, float lerpT)
17-
{
18-
this.halfHeight = halfHeight;
19-
this.lerpT = lerpT;
20-
}
21-
}
2212
private static float _heightMultiplier = 0.0215f;
23-
private static Segment[] _segments = new Segment[] {
13+
private static Segment[] _segments = new Segment[] {
2414
new Segment(0.00f / 2f, 0.00f * 0.99f),
2515
new Segment(0.66f / 2f, 0.01f * 0.97f),
2616
new Segment(1.00f / 2f, 0.04f * 0.94f),
@@ -30,21 +20,25 @@ public Segment(float halfHeight, float lerpT)
3020
//new Segment(0.00f / 2f, 1.00f),
3121
};
3222

33-
[DrawGizmo(GizmoType.Active | GizmoType.NonSelected)]
34-
static void DrawGizmo(NoteArrow obj, GizmoType type)
35-
{
36-
if (obj.Target == null)
37-
return;
38-
39-
if(!_arrows.Contains(obj))
40-
_arrows.Add(obj);
41-
}
4223
private static HashSet<NoteArrow> _arrows = new HashSet<NoteArrow>();
4324
private static List<NoteArrow> _removedArrows = new List<NoteArrow>();
25+
4426
static NoteArrowEditor()
4527
{
4628
SceneView.duringSceneGui += SceneView_duringSceneGui;
4729
}
30+
31+
32+
#region Draw Gizmo
33+
[DrawGizmo(GizmoType.Active | GizmoType.NonSelected)]
34+
static void DrawGizmo(NoteArrow obj, GizmoType type)
35+
{
36+
if (obj.Target == null) { return; }
37+
if (!_arrows.Contains(obj))
38+
{
39+
_arrows.Add(obj);
40+
}
41+
}
4842
private static void SceneView_duringSceneGui(SceneView scene)
4943
{
5044
if (Event.current.type == EventType.Repaint)
@@ -124,15 +118,19 @@ private static void SceneView_duringSceneGui(SceneView scene)
124118
}
125119
}
126120
}
121+
#endregion
127122

128-
public override void OnInspectorGUI()
123+
#region Draw Inspector
124+
protected override void DrawCustom()
129125
{
130126
var targetProp = serializedObject.FindProperty("_target");
131127

132128
NoteArrow target = this.target as NoteArrow;
133129
Color color = Color.white;
134130
if (target.TryGetComponent(out INote inote))
131+
{
135132
color = inote.Color;
133+
}
136134

137135
Color defaultBackgroundColor = GUI.backgroundColor;
138136
GUI.backgroundColor = color;
@@ -143,10 +141,26 @@ public override void OnInspectorGUI()
143141
EditorGUI.BeginChangeCheck();
144142
EditorGUILayout.PropertyField(targetProp);
145143
if (EditorGUI.EndChangeCheck())
144+
{
146145
serializedObject.ApplyModifiedProperties();
146+
}
147147

148148
GUI.backgroundColor = defaultBackgroundColor;
149149
}
150+
#endregion
151+
152+
#region Utils
153+
private struct Segment
154+
{
155+
public float halfHeight;
156+
public float lerpT;
157+
public Segment(float halfHeight, float lerpT)
158+
{
159+
this.halfHeight = halfHeight;
160+
this.lerpT = lerpT;
161+
}
162+
}
163+
#endregion
150164
}
151165
}
152166
#endif

0 commit comments

Comments
 (0)