Skip to content

Commit c3aff33

Browse files
committed
Minor refactor changes
1 parent b3a1893 commit c3aff33

2 files changed

Lines changed: 100 additions & 115 deletions

File tree

Assets/Editor Toolbox/Editor/SceneView/ToolboxEditorSceneViewObjectSelector.cs

Lines changed: 91 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ namespace Toolbox.Editor.SceneView
66
{
77
public class ToolboxEditorSceneViewObjectSelector : EditorWindow
88
{
9-
private List<GameObject> gameObjects;
10-
private List<string> gameObjectPaths;
9+
private static readonly Color selectionColor = new Color(0.50f, 0.70f, 1.00f);
1110

1211
private const float sizeXPadding = 2f;
1312
private const float sizeYPadding = 2f;
1413
private const float buttonYSpacing = 0.0f;
1514
private const float buttonYSize = 20f;
1615
private const float sizeXOffset = -30f;
1716

17+
private List<GameObject> gameObjects;
18+
private List<string> gameObjectPaths;
19+
1820
private GameObject highlightedObject;
1921
private Vector2 size;
2022
private Vector2 buttonSize;
21-
private static readonly Color selectionColor = new Color(0.50f, 0.70f, 1.00f);
2223

2324
private int shiftMinSelectionId = -1;
2425
private int shiftMaxSelectionId = -1;
@@ -37,54 +38,6 @@ public static void Show(List<GameObject> gameObjects, Vector2 position)
3738
window.ShowAsDropDown(rect, window.size);
3839
}
3940

40-
private Vector2 CalculateSize()
41-
{
42-
size = Vector2.zero;
43-
44-
foreach (var go in gameObjects)
45-
{
46-
GUIContent content = EditorGUIUtility.ObjectContent(go, typeof(GameObject));
47-
Vector2 currentSize = Style.buttonStyle.CalcSize(content);
48-
if (currentSize.x > size.x)
49-
{
50-
size.x = currentSize.x;
51-
}
52-
}
53-
54-
//This is needed because CalcSize calculates content drawing with icon at full size.
55-
size.x += sizeXOffset;
56-
57-
buttonSize.x = size.x;
58-
buttonSize.y = buttonYSize;
59-
60-
size.y = gameObjects.Count * buttonYSize + sizeYPadding * 2.0f + buttonYSpacing * gameObjects.Count - 1;
61-
size.x += sizeXPadding * 2.0f;
62-
63-
return size;
64-
}
65-
66-
private void InitializeGameObjectPaths()
67-
{
68-
gameObjectPaths = new List<string>();
69-
var pathStack = new Stack<string>();
70-
71-
for (int i = 0; i < gameObjects.Count; i++)
72-
{
73-
pathStack.Clear();
74-
Transform transform = gameObjects[i].transform;
75-
pathStack.Push(transform.gameObject.name);
76-
77-
while (transform.parent != null)
78-
{
79-
transform = transform.parent;
80-
pathStack.Push(transform.gameObject.name);
81-
}
82-
83-
string path = string.Join("/", pathStack.ToArray());
84-
gameObjectPaths.Add(path);
85-
}
86-
}
87-
8841
private void OnGUI()
8942
{
9043
if (Event.current.type == EventType.Layout)
@@ -95,41 +48,59 @@ private void OnGUI()
9548
switch (Event.current.type)
9649
{
9750
case EventType.MouseMove:
98-
{
99-
OnGUIMouseMove();
51+
OnGuiMouseMove();
10052
break;
101-
}
102-
case EventType.MouseLeaveWindow:
103-
{
104-
OnGUIMouseLeave();
53+
case EventType.MouseLeaveWindow:
54+
OnGuiMouseLeave();
10555
break;
106-
}
10756
default:
108-
{
109-
OnGUINormal();
57+
OnGuiNormal();
11058
break;
111-
}
11259
}
11360
}
11461

115-
private void OnGUINormal()
62+
private void OnGuiMouseLeave()
11663
{
117-
Rect rect = new Rect(sizeXPadding, sizeYPadding, buttonSize.x, buttonSize.y);
64+
HighlightedObject = null;
65+
}
11866

119-
for (int i = 0; i < gameObjects.Count; i++)
67+
private void OnGuiMouseMove()
68+
{
69+
var rect = new Rect(sizeXPadding, sizeYPadding, buttonSize.x, buttonSize.y);
70+
for (var i = 0; i < gameObjects.Count; i++)
12071
{
12172
var gameObject = gameObjects[i];
122-
123-
if(gameObject == null)
73+
if (gameObject == null)
12474
{
12575
//Can happen when something removes the gameobject during the window display.
12676
continue;
12777
}
12878

12979
var content = EditorGUIUtility.ObjectContent(gameObject, typeof(GameObject));
80+
GUI.Button(rect, content, Style.buttonStyle);
81+
if (rect.Contains(Event.current.mousePosition))
82+
{
83+
HighlightedObject = gameObject;
84+
}
13085

131-
bool objectSelected = Selection.Contains(gameObject);
86+
rect.y += buttonYSize + buttonYSpacing;
87+
}
88+
}
13289

90+
private void OnGuiNormal()
91+
{
92+
var rect = new Rect(sizeXPadding, sizeYPadding, buttonSize.x, buttonSize.y);
93+
for (var i = 0; i < gameObjects.Count; i++)
94+
{
95+
var gameObject = gameObjects[i];
96+
if (gameObject == null)
97+
{
98+
//Can happen when something removes the gameobject during the window display.
99+
continue;
100+
}
101+
102+
var content = EditorGUIUtility.ObjectContent(gameObject, typeof(GameObject));
103+
var objectSelected = Selection.Contains(gameObject);
133104
if (objectSelected)
134105
{
135106
GUI.backgroundColor = selectionColor;
@@ -141,42 +112,61 @@ private void OnGUINormal()
141112
}
142113

143114
GUI.backgroundColor = Color.white;
144-
145115
rect.y += buttonYSize + buttonYSpacing;
146116
}
147117
}
148118

149-
private void OnGUIMouseMove()
119+
private Vector2 CalculateSize()
150120
{
151-
var rect = new Rect(sizeXPadding, sizeYPadding, buttonSize.x, buttonSize.y);
121+
size = Vector2.zero;
152122

153-
for (int i = 0; i < gameObjects.Count; i++)
123+
foreach (var go in gameObjects)
154124
{
155-
var gameObject = gameObjects[i];
156-
157-
if (gameObject == null)
125+
var content = EditorGUIUtility.ObjectContent(go, typeof(GameObject));
126+
var currentSize = Style.buttonStyle.CalcSize(content);
127+
if (currentSize.x > size.x)
158128
{
159-
//Can happen when something removes the gameobject during the window display.
160-
continue;
129+
size.x = currentSize.x;
161130
}
131+
}
162132

163-
var content = EditorGUIUtility.ObjectContent(gameObject, typeof(GameObject));
133+
//This is needed because CalcSize calculates content drawing with icon at full size.
134+
size.x += sizeXOffset;
164135

165-
GUI.Button(rect, content, Style.buttonStyle);
136+
buttonSize.x = size.x;
137+
buttonSize.y = buttonYSize;
166138

167-
if (rect.Contains(Event.current.mousePosition))
139+
size.y = gameObjects.Count * buttonYSize + sizeYPadding * 2.0f + buttonYSpacing * gameObjects.Count - 1;
140+
size.x += sizeXPadding * 2.0f;
141+
142+
return size;
143+
}
144+
145+
private void InitializeGameObjectPaths()
146+
{
147+
gameObjectPaths = new List<string>();
148+
var pathStack = new Stack<string>();
149+
150+
for (var i = 0; i < gameObjects.Count; i++)
151+
{
152+
pathStack.Clear();
153+
var transform = gameObjects[i].transform;
154+
pathStack.Push(transform.gameObject.name);
155+
156+
while (transform.parent != null)
168157
{
169-
HighlightedObject = gameObject;
158+
transform = transform.parent;
159+
pathStack.Push(transform.gameObject.name);
170160
}
171161

172-
rect.y += buttonYSize + buttonYSpacing;
162+
var path = string.Join("/", pathStack.ToArray());
163+
gameObjectPaths.Add(path);
173164
}
174165
}
175166

176167
private void GameObjectButtonPress(int id)
177168
{
178169
SelectObject(id, Event.current.control, Event.current.shift);
179-
180170
if (Event.current.control || Event.current.shift)
181171
{
182172
return;
@@ -187,12 +177,12 @@ private void GameObjectButtonPress(int id)
187177

188178
private void UpdateShiftSelectionIDs(int id)
189179
{
190-
if(shiftLastId == -1)
180+
if (shiftLastId == -1)
191181
{
192182
shiftLastId = id;
193183
}
194184

195-
if(shiftMinSelectionId == -1)
185+
if (shiftMinSelectionId == -1)
196186
{
197187
shiftMinSelectionId = id;
198188
}
@@ -202,18 +192,18 @@ private void UpdateShiftSelectionIDs(int id)
202192
shiftMaxSelectionId = id;
203193
}
204194

205-
if(id < shiftMinSelectionId)
195+
if (id < shiftMinSelectionId)
206196
{
207197
shiftMinSelectionId = id;
208198
}
209-
else if(id >= shiftMaxSelectionId)
199+
else if (id >= shiftMaxSelectionId)
210200
{
211201
shiftMaxSelectionId = id;
212202
}
213-
else if(id > shiftMinSelectionId)
203+
else if (id > shiftMinSelectionId)
214204
{
215205
//ID is between min and max.
216-
if(shiftLastId < id)
206+
if (shiftLastId < id)
217207
{
218208
shiftMaxSelectionId = id;
219209
}
@@ -238,16 +228,13 @@ private void SelectObject(int id, bool control, bool shift)
238228
else if (control)
239229
{
240230
UpdateShiftSelectionIDs(id);
241-
242231
if (Selection.Contains(gameObject))
243232
{
244-
//Deselect
245-
RemoveObjectFromSelection(gameObject);
233+
RemoveSelectedObject(gameObject);
246234
}
247235
else
248236
{
249-
//Select
250-
AddObjectToSelection(gameObject);
237+
AppendSelectedObject(gameObject);
251238
}
252239
}
253240
else
@@ -256,14 +243,14 @@ private void SelectObject(int id, bool control, bool shift)
256243
}
257244
}
258245

259-
private void SelectObjects(int minID, int maxID)
246+
private void SelectObjects(int minId, int maxId)
260247
{
261-
var size = maxID - minID + 1;
248+
var size = maxId - minId + 1;
262249
var newSelection = new Object[size];
263250

264-
int index = 0;
251+
var index = 0;
265252

266-
for (int i = minID; i <= maxID; i++)
253+
for (var i = minId; i <= maxId; i++)
267254
{
268255
newSelection[index] = gameObjects[i];
269256
index++;
@@ -272,7 +259,7 @@ private void SelectObjects(int minID, int maxID)
272259
Selection.objects = newSelection;
273260
}
274261

275-
private void AddObjectToSelection(GameObject gameObject)
262+
private void AppendSelectedObject(GameObject gameObject)
276263
{
277264
var currentSelection = Selection.objects;
278265
var newSelection = new Object[currentSelection.Length + 1];
@@ -283,14 +270,14 @@ private void AddObjectToSelection(GameObject gameObject)
283270
Selection.objects = newSelection;
284271
}
285272

286-
private void RemoveObjectFromSelection(GameObject gameObject)
273+
private void RemoveSelectedObject(GameObject gameObject)
287274
{
288275
var currentSelection = Selection.objects;
289276
var newSelection = new Object[currentSelection.Length - 1];
290277

291278
var index = 0;
292279

293-
for (int i = 0; i < currentSelection.Length; i++)
280+
for (var i = 0; i < currentSelection.Length; i++)
294281
{
295282
if (currentSelection[i] == gameObject)
296283
{
@@ -304,11 +291,6 @@ private void RemoveObjectFromSelection(GameObject gameObject)
304291
Selection.objects = newSelection;
305292
}
306293

307-
private void OnGUIMouseLeave()
308-
{
309-
HighlightedObject = null;
310-
}
311-
312294
private GameObject HighlightedObject
313295
{
314296
set
@@ -334,8 +316,10 @@ private static class Style
334316

335317
static Style()
336318
{
337-
buttonStyle = new GUIStyle(GUI.skin.button);
338-
buttonStyle.alignment = TextAnchor.MiddleLeft;
319+
buttonStyle = new GUIStyle(GUI.skin.button)
320+
{
321+
alignment = TextAnchor.MiddleLeft
322+
};
339323
}
340324
}
341325
}

Assets/Editor Toolbox/Editor/ToolboxEditorSceneView.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ static ToolboxEditorSceneView()
1616

1717
private static List<GameObject> GetObjectsUnderCursor()
1818
{
19-
List<GameObject> hitObjects = new List<GameObject>();
20-
GameObject[] hitObjectsArray;
19+
const int maxIterations = 50;
2120

22-
int maxIterations = 50;
23-
for (int i = 0; i < maxIterations; i++)
21+
var mousePosition = Event.current.mousePosition;
22+
var hitObjects = new List<GameObject>();
23+
GameObject[] hitObjectsArray;
24+
for (var i = 0; i < maxIterations; i++)
2425
{
2526
hitObjectsArray = hitObjects.ToArray();
2627

27-
GameObject go = HandleUtility.PickGameObject(Event.current.mousePosition, true, hitObjectsArray);
28+
var go = HandleUtility.PickGameObject(mousePosition, true, hitObjectsArray);
2829
if (go == null)
2930
{
3031
break;
@@ -48,13 +49,13 @@ private static void UpdateEventCallback()
4849

4950
private static void SceneViewDuringSceneGUI(UnityEditor.SceneView sceneView)
5051
{
51-
if (Event.current.type != EventType.KeyDown
52-
|| Event.current.keyCode != SelectorKey)
52+
if (Event.current.type != EventType.KeyDown ||
53+
Event.current.keyCode != SelectorKey)
5354
{
5455
return;
5556
}
5657

57-
List<GameObject> objectsUnderCursor = GetObjectsUnderCursor();
58+
var objectsUnderCursor = GetObjectsUnderCursor();
5859
if (objectsUnderCursor.Count > 0)
5960
{
6061
ToolboxEditorSceneViewObjectSelector.Show(objectsUnderCursor, Event.current.mousePosition + sceneView.position.position);

0 commit comments

Comments
 (0)