Skip to content

Commit 4062b26

Browse files
committed
Minor refactor changes
1 parent e65bf81 commit 4062b26

File tree

4 files changed

+72
-99
lines changed

4 files changed

+72
-99
lines changed

Assets/Editor Toolbox/Editor/Internal/ReorderableListBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,8 @@ protected static class Style
882882
internal static readonly float sizeAreaWidth = 19.0f;
883883
internal static readonly float minEmptyHeight = 8.0f;
884884

885+
internal static readonly Color selectionColor = new Color(0.3f, 0.47f, 0.75f);
886+
885887
internal static readonly GUIContent sizePropertyContent;
886888
internal static readonly GUIContent iconToolbarAddContent;
887889
internal static readonly GUIContent iconToolbarDropContent;

Assets/Editor Toolbox/Editor/Internal/ToolboxEditorList.cs

Lines changed: 64 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
namespace Toolbox.Editor.Internal
88
{
9-
using Toolbox.Editor.Drawers;
10-
119
/// <summary>
1210
/// Version of the <see cref="ReorderableList"/> dedicated for <see cref="ToolboxDrawer"/>s.
1311
/// Can be used only together with the internal layouting system.
@@ -75,61 +73,65 @@ private void DrawElementRow(int index, bool isActive, bool isTarget, bool hasFoc
7573
var elementRowRect = elementRowGroup.rect;
7674
var isSelected = isActive || isTarget;
7775
elementsRects[index] = elementRowRect;
78-
//draw element background (handle selection and targeting)
79-
if (drawElementBackgroundCallback != null)
80-
{
81-
drawElementBackgroundCallback(elementRowRect, index, isSelected, hasFocus);
82-
}
83-
else
84-
{
85-
DrawStandardElementBackground(elementRowRect, index, isSelected, hasFocus, Draggable);
86-
}
87-
88-
//prepare handle-related properties
89-
var handleRect = GetHandleRect(elementRowRect);
90-
var drawHandle = !IsDragging;
91-
//draw handles only for static array or currently dragged element
92-
if (isSelected && IsDragging)
93-
{
94-
drawHandle = true;
95-
handleRect = GetHandleRect(draggedY);
96-
}
97-
98-
if (drawHandle)
99-
{
100-
if (drawElementHandleCallback != null)
101-
{
102-
drawElementHandleCallback(handleRect, index, isActive, hasFocus);
103-
}
104-
else
105-
{
106-
DrawStandardElementHandle(handleRect, index, isActive, hasFocus, Draggable);
107-
}
108-
}
10976

77+
DrawElementBackground(elementRowRect, index, isSelected, hasFocus);
78+
DrawElementDragHandle(elementRowRect, index, isSelected, hasFocus);
11079
//draw the real property in separate vertical group
11180
using (var elementGroup = new EditorGUILayout.VerticalScope(Style.contentGroupStyle))
11281
{
11382
var elementRect = elementGroup.rect;
11483
//adjust label width to the known dragging area
11584
EditorGUIUtility.labelWidth -= Style.dragAreaWidth;
116-
if (drawElementCallback != null)
117-
{
118-
drawElementCallback(elementRect, index, isActive, hasFocus);
119-
}
120-
else
121-
{
122-
DrawStandardElement(elementRect, index, isActive, hasFocus, Draggable);
123-
}
124-
85+
DrawElement(index);
12586
EditorGUIUtility.labelWidth += Style.dragAreaWidth;
12687
}
12788

128-
//create additional space between element and right margin
89+
//create additional space between item and right margin
12990
DrawEmptySpace(Style.spacing);
13091
}
13192
}
13293

94+
private void DrawElementBackground(Rect rect, int index, bool isSelected, bool hasFocus)
95+
{
96+
if (drawElementBackgroundCallback != null)
97+
{
98+
drawElementBackgroundCallback(rect, index, isSelected, hasFocus);
99+
}
100+
else
101+
{
102+
DrawStandardElementBackground(rect, index, isSelected, hasFocus, Draggable);
103+
}
104+
}
105+
106+
private void DrawElementDragHandle(Rect rect, int index, bool isSelected, bool hasFocus)
107+
{
108+
DrawHandleArea();
109+
//draw handles only for static array or currently dragged element
110+
if (IsDragging && !isSelected)
111+
{
112+
return;
113+
}
114+
115+
var handleRect = IsDragging
116+
? GetHandleRect(draggedY)
117+
: GetHandleRect(rect);
118+
if (drawElementHandleCallback != null)
119+
{
120+
drawElementHandleCallback(handleRect, index, isSelected, hasFocus);
121+
}
122+
else
123+
{
124+
DrawStandardElementHandle(handleRect, index, isSelected, hasFocus, Draggable);
125+
}
126+
}
127+
128+
private void DrawElement(int index)
129+
{
130+
var element = List.GetArrayElementAtIndex(index);
131+
var content = GetElementContent(element, index);
132+
ToolboxEditorGui.DrawToolboxProperty(element, content);
133+
}
134+
133135
/// <summary>
134136
/// Creates empty space in a layout-based structure.
135137
/// </summary>
@@ -138,14 +140,18 @@ private void DrawEmptySpace(float space)
138140
GuiLayoutUtility.CreateSpace(space);
139141
}
140142

143+
private void DrawHandleArea()
144+
{
145+
DrawEmptySpace(Style.dragAreaWidth);
146+
}
147+
141148
/// <summary>
142149
/// Creates empty space for the dragging area and returns adjusted <see cref="Rect"/>.
143150
/// </summary>
144151
private Rect GetHandleRect(Rect rowRect)
145152
{
146153
var handleRect = new Rect(rowRect);
147154
handleRect.xMax = handleRect.xMin + Style.dragAreaWidth;
148-
DrawEmptySpace(Style.dragAreaWidth);
149155
return handleRect;
150156
}
151157

@@ -171,26 +177,15 @@ private Rect GetHandleRect(float draggedY)
171177
/// <summary>
172178
/// Creates small, colored rect to visualize the dragging target (gap between elements).
173179
/// </summary>
174-
private Rect DrawTargetGap(int targetIndex, int draggingIndex, Color color, float width, float margin)
180+
private void DrawTargetGap(int targetIndex, int draggingIndex)
175181
{
176-
//TODO: refactor + custom texture/method for the gap
177-
var targetsRect = elementsRects[targetIndex];
178-
var rect = new Rect(targetsRect);
179-
//handle differently situation above and under target
180-
if (targetIndex < draggingIndex)
181-
{
182-
rect.yMax = rect.yMin;
183-
rect.yMin = rect.yMax - width;
184-
}
185-
else
186-
{
187-
rect.yMin = rect.yMax;
188-
rect.yMax = rect.yMin + width;
189-
}
190-
191-
rect.xMin += margin;
192-
EditorGUI.DrawRect(rect, color);
193-
return rect;
182+
var rect = elementsRects[targetIndex];
183+
rect.yMax = targetIndex < draggingIndex
184+
? rect.yMin
185+
: rect.yMax + Style.spacing;
186+
rect.yMin = rect.yMax - Style.spacing;
187+
rect.xMin += Style.dragAreaWidth;
188+
EditorGUI.DrawRect(rect, Style.selectionColor);
194189
}
195190

196191

@@ -229,7 +224,9 @@ protected override void DoListMiddle(Rect middleRect)
229224
return;
230225
}
231226

232-
DrawEmptySpace(Style.padding);
227+
var upperPadding = Style.padding - ElementSpacing;
228+
var lowerPadding = Style.padding;
229+
DrawEmptySpace(upperPadding);
233230
//if there are elements, we need to draw them - we will do
234231
//this differently depending on if we are dragging or not
235232
for (var i = 0; i < arraySize; i++)
@@ -240,25 +237,15 @@ protected override void DoListMiddle(Rect middleRect)
240237
var isTarget = (i == lastCoveredIndex && !isActive);
241238
var isEnding = (i == arraySize - 1);
242239

243-
//draw current array element
240+
DrawEmptySpace(ElementSpacing);
244241
DrawElementRow(i, isActive, isTarget, hasFocus);
245-
246-
//draw dragging element gap
247242
if (isTarget)
248243
{
249-
DrawTargetGap(i, Index, GapColor, GapWidth, Style.dragAreaWidth);
250-
}
251-
252-
if (isEnding)
253-
{
254-
continue;
244+
DrawTargetGap(i, Index);
255245
}
256-
257-
//create spacing for elements
258-
DrawEmptySpace(ElementSpacing);
259246
}
260247

261-
DrawEmptySpace(Style.padding);
248+
DrawEmptySpace(lowerPadding);
262249
}
263250

264251
protected override bool DoListHeader()
@@ -339,18 +326,6 @@ protected override int GetCoveredElementIndex(Vector2 mousePosition)
339326
}
340327

341328

342-
#region Methods: Default interaction/draw calls
343-
344-
/// <inheritdoc/>
345-
public override void DrawStandardElement(Rect rect, int index, bool selected, bool focused, bool draggable)
346-
{
347-
var element = List.GetArrayElementAtIndex(index);
348-
var content = GetElementContent(element, index);
349-
ToolboxEditorGui.DrawToolboxProperty(element, content);
350-
}
351-
352-
#endregion
353-
354329
/// <inheritdoc/>
355330
public override void DoList()
356331
{
@@ -380,9 +355,5 @@ public Rect LastFooterRect
380355
{
381356
get => footerRect;
382357
}
383-
384-
public Color GapColor { get; set; } = new Color(0.3f, 0.47f, 0.75f);
385-
386-
public float GapWidth { get; set; } = 2.0f;
387358
}
388359
}

Assets/Editor Toolbox/Editor/Utilities/InspectorUtility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ internal static void SimulateOnValidate(Object target)
138138

139139
internal static partial class InspectorUtility
140140
{
141-
private static List<Component> copiedComponents = new List<Component>();
141+
private static readonly List<Component> copiedComponents = new List<Component>();
142142

143143

144144
[MenuItem("CONTEXT/Component/Copy Components", false, priority = 200)]

Assets/Editor Toolbox/Editor/Utilities/PropertyUtility.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ internal static void SetProperValue(this SerializedProperty property, FieldInfo
193193
fieldInfo.SetValue(targetParent, value);
194194
}
195195

196-
EnsureOnValidateBroadcast(targetObject, callOnValidate);
197-
EnsureTargetSerialization(targetObject);
196+
ForceOnValidateBroadcast(targetObject, callOnValidate);
197+
ForceTargetSerialization(targetObject);
198198
}
199199
}
200200

@@ -388,17 +388,17 @@ internal static void EnsureReflectionSafeness(SerializedProperty property)
388388
}
389389
}
390390

391-
internal static void EnsureTargetSerialization(Object targetObject)
391+
internal static void ForceTargetSerialization(Object targetObject)
392392
{
393393
if (AssetDatabase.Contains(targetObject))
394394
{
395395
EditorUtility.SetDirty(targetObject);
396396
}
397397
}
398398

399-
internal static void EnsureOnValidateBroadcast(Object targetObject, bool callNeeded)
399+
internal static void ForceOnValidateBroadcast(Object targetObject, bool isForced)
400400
{
401-
if (callNeeded)
401+
if (isForced)
402402
{
403403
InspectorUtility.SimulateOnValidate(targetObject);
404404
}

0 commit comments

Comments
 (0)