Skip to content
This repository was archived by the owner on May 9, 2023. It is now read-only.

Commit fcdfeb2

Browse files
committed
Redesign mouse inspector class, add UI results panel
1 parent a1d0b64 commit fcdfeb2

File tree

7 files changed

+358
-228
lines changed

7 files changed

+358
-228
lines changed

src/Inspectors/InspectUnderMouse.cs

Lines changed: 51 additions & 227 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using UnityExplorer.Core;
99
using UnityExplorer.Core.Input;
1010
using UnityExplorer.Core.Runtime;
11+
using UnityExplorer.Inspectors.MouseInspectors;
1112
using UnityExplorer.UI;
1213
using UnityExplorer.UI.Panels;
1314

@@ -23,17 +24,27 @@ public class InspectUnderMouse : UIPanel
2324
{
2425
public static InspectUnderMouse Instance { get; private set; }
2526

26-
public InspectUnderMouse() { Instance = this; }
27+
private readonly WorldInspector worldInspector;
28+
private readonly UiInspector uiInspector;
2729

28-
public static void OnDropdownSelect(int index)
30+
public static bool Inspecting { get; set; }
31+
public static MouseInspectMode Mode { get; set; }
32+
33+
private static Vector3 lastMousePos;
34+
35+
public MouseInspectorBase CurrentInspector
2936
{
30-
switch (index)
37+
get
3138
{
32-
case 0: return;
33-
case 1: Instance.StartInspect(MouseInspectMode.World); break;
34-
case 2: Instance.StartInspect(MouseInspectMode.UI); break;
39+
switch (Mode)
40+
{
41+
case MouseInspectMode.UI:
42+
return uiInspector;
43+
case MouseInspectMode.World:
44+
return worldInspector;
45+
}
46+
return null;
3547
}
36-
UIManager.MouseInspectDropdown.value = 0;
3748
}
3849

3950
// UIPanel
@@ -46,56 +57,54 @@ public static void OnDropdownSelect(int index)
4657
public override bool ShouldSaveActiveState => false;
4758
public override bool ShowByDefault => false;
4859

49-
private static Text objNameLabel;
50-
private static Text objPathLabel;
51-
private static Text mousePosLabel;
60+
internal Text objNameLabel;
61+
internal Text objPathLabel;
62+
internal Text mousePosLabel;
5263

53-
// Mouse Inspector
54-
public static bool Inspecting { get; set; }
55-
public static MouseInspectMode Mode { get; set; }
56-
57-
private static Camera MainCamera;
58-
private static GraphicRaycaster[] graphicRaycasters;
59-
60-
private static GameObject lastHitObject;
61-
private static Vector3 lastMousePos;
62-
63-
private static readonly List<Graphic> wasDisabledGraphics = new List<Graphic>();
64-
private static readonly List<CanvasGroup> wasDisabledCanvasGroups = new List<CanvasGroup>();
65-
private static readonly List<GameObject> objectsAddedCastersTo = new List<GameObject>();
64+
public InspectUnderMouse()
65+
{
66+
Instance = this;
67+
worldInspector = new WorldInspector();
68+
uiInspector = new UiInspector();
69+
}
6670

67-
public void StartInspect(MouseInspectMode mode)
71+
public static void OnDropdownSelect(int index)
6872
{
69-
MainCamera = Camera.main;
70-
71-
if (!MainCamera && mode == MouseInspectMode.World)
73+
switch (index)
7274
{
73-
ExplorerCore.LogWarning("No MainCamera found! Cannot inspect world!");
74-
return;
75+
case 0: return;
76+
case 1: Instance.StartInspect(MouseInspectMode.World); break;
77+
case 2: Instance.StartInspect(MouseInspectMode.UI); break;
7578
}
79+
UIManager.MouseInspectDropdown.value = 0;
80+
}
7681

77-
PanelDragger.ForceEnd();
78-
82+
public void StartInspect(MouseInspectMode mode)
83+
{
7984
Mode = mode;
8085
Inspecting = true;
86+
87+
CurrentInspector.OnBeginMouseInspect();
88+
89+
PanelDragger.ForceEnd();
8190
UIManager.NavBarRect.gameObject.SetActive(false);
8291
UIManager.PanelHolder.SetActive(false);
8392

8493
UIRoot.SetActive(true);
85-
86-
if (mode == MouseInspectMode.UI)
87-
SetupUIRaycast();
8894
}
8995

9096
internal void ClearHitData()
9197
{
92-
lastHitObject = null;
98+
CurrentInspector.ClearHitData();
99+
93100
objNameLabel.text = "No hits...";
94101
objPathLabel.text = "";
95102
}
96103

97104
public void StopInspect()
98105
{
106+
CurrentInspector.OnEndInspect();
107+
ClearHitData();
99108
Inspecting = false;
100109

101110
UIManager.NavBarRect.gameObject.SetActive(true);
@@ -106,11 +115,6 @@ public void StopInspect()
106115
drop.DestroyDropdownList(list.gameObject);
107116

108117
UIRoot.SetActive(false);
109-
110-
if (Mode == MouseInspectMode.UI)
111-
StopUIInspect();
112-
113-
ClearHitData();
114118
}
115119

116120
private static float timeOfLastRaycast;
@@ -123,33 +127,22 @@ public void UpdateInspect()
123127
return;
124128
}
125129

126-
if (lastHitObject && InputManager.GetMouseButtonDown(0))
130+
if (InputManager.GetMouseButtonDown(0))
127131
{
128-
var target = lastHitObject;
132+
CurrentInspector.OnSelectMouseInspect();
129133
StopInspect();
130-
InspectorManager.Inspect(target);
131134
return;
132135
}
133136

134137
var mousePos = InputManager.MousePosition;
135-
136138
if (mousePos != lastMousePos)
137139
UpdatePosition(mousePos);
138140

139141
if (!timeOfLastRaycast.OccuredEarlierThan(0.1f))
140142
return;
141-
142143
timeOfLastRaycast = Time.realtimeSinceStartup;
143144

144-
// actual inspect raycast
145-
146-
switch (Mode)
147-
{
148-
case MouseInspectMode.UI:
149-
RaycastUI(mousePos); break;
150-
case MouseInspectMode.World:
151-
RaycastWorld(mousePos); break;
152-
}
145+
CurrentInspector.UpdateMouseInspect(mousePos);
153146
}
154147

155148
internal void UpdatePosition(Vector2 mousePos)
@@ -171,181 +164,9 @@ internal void UpdatePosition(Vector2 mousePos)
171164

172165
// calculate and set our UI position
173166
var inversePos = UIManager.CanvasRoot.transform.InverseTransformPoint(mousePos);
174-
175167
UIRoot.transform.localPosition = new Vector3(inversePos.x, inversePos.y, 0);
176168
}
177169

178-
internal void OnHitGameObject(GameObject obj)
179-
{
180-
if (obj != lastHitObject)
181-
{
182-
lastHitObject = obj;
183-
objNameLabel.text = $"<b>Click to Inspect:</b> <color=cyan>{obj.name}</color>";
184-
objPathLabel.text = $"Path: {obj.transform.GetTransformPath(true)}";
185-
}
186-
}
187-
188-
// Collider raycasting
189-
190-
internal void RaycastWorld(Vector2 mousePos)
191-
{
192-
var ray = MainCamera.ScreenPointToRay(mousePos);
193-
Physics.Raycast(ray, out RaycastHit hit, 1000f);
194-
195-
if (hit.transform)
196-
{
197-
var obj = hit.transform.gameObject;
198-
OnHitGameObject(obj);
199-
}
200-
else
201-
{
202-
if (lastHitObject)
203-
ClearHitData();
204-
}
205-
}
206-
207-
// UI Graphic raycasting
208-
209-
private static void SetupUIRaycast()
210-
{
211-
foreach (var obj in RuntimeProvider.Instance.FindObjectsOfTypeAll(typeof(Canvas)))
212-
{
213-
var canvas = obj.TryCast<Canvas>();
214-
if (!canvas || !canvas.enabled || !canvas.gameObject.activeInHierarchy)
215-
continue;
216-
if (!canvas.GetComponent<GraphicRaycaster>())
217-
{
218-
canvas.gameObject.AddComponent<GraphicRaycaster>();
219-
//ExplorerCore.Log("Added raycaster to " + canvas.name);
220-
objectsAddedCastersTo.Add(canvas.gameObject);
221-
}
222-
}
223-
224-
// recache Graphic Raycasters each time we start
225-
var casters = RuntimeProvider.Instance.FindObjectsOfTypeAll(typeof(GraphicRaycaster));
226-
graphicRaycasters = new GraphicRaycaster[casters.Length];
227-
for (int i = 0; i < casters.Length; i++)
228-
{
229-
graphicRaycasters[i] = casters[i].TryCast<GraphicRaycaster>();
230-
}
231-
232-
// enable raycastTarget on Graphics
233-
foreach (var obj in RuntimeProvider.Instance.FindObjectsOfTypeAll(typeof(Graphic)))
234-
{
235-
var graphic = obj.TryCast<Graphic>();
236-
if (!graphic || !graphic.enabled || graphic.raycastTarget || !graphic.gameObject.activeInHierarchy)
237-
continue;
238-
graphic.raycastTarget = true;
239-
//ExplorerCore.Log("Enabled raycastTarget on " + graphic.name);
240-
wasDisabledGraphics.Add(graphic);
241-
}
242-
243-
// enable blocksRaycasts on CanvasGroups
244-
foreach (var obj in RuntimeProvider.Instance.FindObjectsOfTypeAll(typeof(CanvasGroup)))
245-
{
246-
var canvas = obj.TryCast<CanvasGroup>();
247-
if (!canvas || !canvas.gameObject.activeInHierarchy || canvas.blocksRaycasts)
248-
continue;
249-
canvas.blocksRaycasts = true;
250-
//ExplorerCore.Log("Enabled raycasts on " + canvas.name);
251-
wasDisabledCanvasGroups.Add(canvas);
252-
}
253-
}
254-
255-
internal void RaycastUI(Vector2 mousePos)
256-
{
257-
var ped = new PointerEventData(null)
258-
{
259-
position = mousePos
260-
};
261-
262-
//ExplorerCore.Log("~~~~~~~~~ begin raycast ~~~~~~~~");
263-
GameObject hitObject = null;
264-
int highestLayer = int.MinValue;
265-
int highestOrder = int.MinValue;
266-
int highestDepth = int.MinValue;
267-
foreach (var gr in graphicRaycasters)
268-
{
269-
if (!gr || !gr.canvas)
270-
continue;
271-
272-
var list = new List<RaycastResult>();
273-
RuntimeProvider.Instance.GraphicRaycast(gr, ped, list);
274-
275-
if (list.Count > 0)
276-
{
277-
foreach (var hit in list)
278-
{
279-
// Manualy trying to determine which object is "on top".
280-
// Could be improved, but seems to work pretty well and isn't as laggy as you would expect.
281-
282-
if (!hit.gameObject)
283-
continue;
284-
285-
if (hit.gameObject.GetComponent<CanvasGroup>() is CanvasGroup group && group.alpha == 0)
286-
continue;
287-
288-
if (hit.gameObject.GetComponent<Graphic>() is Graphic graphic && graphic.color.a == 0f)
289-
continue;
290-
291-
if (hit.sortingLayer < highestLayer)
292-
continue;
293-
294-
if (hit.sortingLayer > highestLayer)
295-
{
296-
highestLayer = hit.sortingLayer;
297-
highestDepth = int.MinValue;
298-
}
299-
300-
if (hit.depth < highestDepth)
301-
continue;
302-
303-
if (hit.depth > highestDepth)
304-
{
305-
highestDepth = hit.depth;
306-
highestOrder = int.MinValue;
307-
}
308-
309-
if (hit.sortingOrder <= highestOrder)
310-
continue;
311-
312-
highestOrder = hit.sortingOrder;
313-
hitObject = hit.gameObject;
314-
}
315-
}
316-
else
317-
{
318-
if (lastHitObject)
319-
ClearHitData();
320-
}
321-
}
322-
323-
if (hitObject)
324-
OnHitGameObject(hitObject);
325-
326-
//ExplorerCore.Log("~~~~~~~~~ end raycast ~~~~~~~~");
327-
}
328-
329-
private static void StopUIInspect()
330-
{
331-
foreach (var obj in objectsAddedCastersTo)
332-
{
333-
if (obj.GetComponent<GraphicRaycaster>() is GraphicRaycaster raycaster)
334-
GameObject.Destroy(raycaster);
335-
}
336-
337-
foreach (var graphic in wasDisabledGraphics)
338-
graphic.raycastTarget = false;
339-
340-
foreach (var canvas in wasDisabledCanvasGroups)
341-
canvas.blocksRaycasts = false;
342-
343-
objectsAddedCastersTo.Clear();
344-
wasDisabledCanvasGroups.Clear();
345-
wasDisabledGraphics.Clear();
346-
}
347-
348-
349170
// UI Construction
350171

351172
protected internal override void DoSetDefaultPosAndAnchors()
@@ -367,7 +188,10 @@ public override void ConstructPanelContent()
367188

368189
// Title text
369190

370-
var title = UIFactory.CreateLabel(inspectContent, "InspectLabel", "<b>Mouse Inspector</b> (press <b>ESC</b> to cancel)", TextAnchor.MiddleCenter);
191+
var title = UIFactory.CreateLabel(inspectContent,
192+
"InspectLabel",
193+
"<b>Mouse Inspector</b> (press <b>ESC</b> to cancel)",
194+
TextAnchor.MiddleCenter);
371195
UIFactory.SetLayoutElement(title.gameObject, flexibleWidth: 9999);
372196

373197
mousePosLabel = UIFactory.CreateLabel(inspectContent, "MousePosLabel", "Mouse Position:", TextAnchor.MiddleCenter);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using UnityEngine;
6+
7+
namespace UnityExplorer.Inspectors.MouseInspectors
8+
{
9+
public abstract class MouseInspectorBase
10+
{
11+
public abstract void OnBeginMouseInspect();
12+
13+
public abstract void UpdateMouseInspect(Vector2 mousePos);
14+
15+
public abstract void OnSelectMouseInspect();
16+
17+
public abstract void ClearHitData();
18+
19+
public abstract void OnEndInspect();
20+
}
21+
}

0 commit comments

Comments
 (0)