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

Commit ca65aff

Browse files
committed
Finish GameObject Inspector, some UI cleanups
1 parent f4e473f commit ca65aff

File tree

12 files changed

+1093
-165
lines changed

12 files changed

+1093
-165
lines changed

src/UI/Inspectors/GameObjectInspector.cs

Lines changed: 151 additions & 104 deletions
Large diffs are not rendered by default.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using UnityEngine;
6+
using UnityEngine.UI;
7+
using UnityExplorer.UI.Widgets;
8+
9+
namespace UnityExplorer.UI.Inspectors
10+
{
11+
public class ComponentCell : ButtonCell
12+
{
13+
public Toggle BehaviourToggle;
14+
public ButtonRef DestroyButton;
15+
16+
public Action<bool, int> OnBehaviourToggled;
17+
public Action<int> OnDestroyClicked;
18+
19+
private void BehaviourToggled(bool val)
20+
{
21+
OnBehaviourToggled?.Invoke(val, CurrentDataIndex);
22+
}
23+
24+
private void DestroyClicked()
25+
{
26+
OnDestroyClicked?.Invoke(CurrentDataIndex);
27+
}
28+
29+
public override GameObject CreateContent(GameObject parent)
30+
{
31+
var root = base.CreateContent(parent);
32+
33+
// Add mask to button so text doesnt overlap on Close button
34+
this.Button.Component.gameObject.AddComponent<Mask>().showMaskGraphic = true;
35+
36+
// Behaviour toggle
37+
38+
var toggleObj = UIFactory.CreateToggle(UIRoot, "BehaviourToggle", out BehaviourToggle, out var behavText);
39+
UIFactory.SetLayoutElement(toggleObj, minHeight: 25, minWidth: 25);
40+
BehaviourToggle.onValueChanged.AddListener(BehaviourToggled);
41+
// put at first object
42+
toggleObj.transform.SetSiblingIndex(0);
43+
44+
// Destroy button
45+
46+
DestroyButton = UIFactory.CreateButton(UIRoot, "DestroyButton", "X", new Color(0.3f, 0.2f, 0.2f));
47+
UIFactory.SetLayoutElement(DestroyButton.Component.gameObject, minHeight: 21, minWidth: 25);
48+
DestroyButton.OnClick += DestroyClicked;
49+
50+
return root;
51+
}
52+
}
53+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using UnityEngine;
6+
using UnityExplorer.UI.Widgets;
7+
8+
namespace UnityExplorer.UI.Inspectors
9+
{
10+
public class ComponentList : ButtonListHandler<Component, ComponentCell>
11+
{
12+
public GameObjectInspector Parent;
13+
14+
public ComponentList(ScrollPool<ComponentCell> scrollPool, Func<List<Component>> getEntriesMethod)
15+
: base(scrollPool, getEntriesMethod, null, null, null)
16+
{
17+
base.SetICell = SetComponentCell;
18+
base.ShouldDisplay = CheckShouldDisplay;
19+
base.OnCellClicked = OnComponentClicked;
20+
}
21+
22+
private bool CheckShouldDisplay(Component _, string __) => true;
23+
24+
public override void OnCellBorrowed(ComponentCell cell)
25+
{
26+
base.OnCellBorrowed(cell);
27+
28+
cell.OnBehaviourToggled += OnBehaviourToggled;
29+
cell.OnDestroyClicked += OnDestroyClicked;
30+
}
31+
32+
public override void SetCell(ComponentCell cell, int index)
33+
{
34+
base.SetCell(cell, index);
35+
}
36+
37+
private void OnComponentClicked(int index)
38+
{
39+
var entries = GetEntries();
40+
41+
if (index < 0 || index >= entries.Count)
42+
return;
43+
44+
var comp = entries[index];
45+
if (comp)
46+
InspectorManager.Inspect(comp);
47+
}
48+
49+
private void OnBehaviourToggled(bool value, int index)
50+
{
51+
try
52+
{
53+
var entries = GetEntries();
54+
var comp = entries[index];
55+
56+
if (comp.TryCast<Behaviour>() is Behaviour behaviour)
57+
behaviour.enabled = value;
58+
}
59+
catch (Exception ex)
60+
{
61+
ExplorerCore.LogWarning($"Exception toggling Behaviour.enabled: {ex.ReflectionExToString()}");
62+
}
63+
}
64+
65+
private void OnDestroyClicked(int index)
66+
{
67+
try
68+
{
69+
var entries = GetEntries();
70+
var comp = entries[index];
71+
72+
GameObject.DestroyImmediate(comp);
73+
74+
Parent.UpdateComponents();
75+
}
76+
catch (Exception ex)
77+
{
78+
ExplorerCore.LogWarning($"Exception destroying Component: {ex.ReflectionExToString()}");
79+
}
80+
}
81+
82+
private static readonly Dictionary<string, string> compToStringCache = new Dictionary<string, string>();
83+
84+
// Called from ButtonListHandler.SetCell, will be valid
85+
private void SetComponentCell(ComponentCell cell, int index)
86+
{
87+
var entries = GetEntries();
88+
cell.Enable();
89+
90+
var comp = entries[index];
91+
var type = comp.GetActualType();
92+
93+
if (!compToStringCache.ContainsKey(type.AssemblyQualifiedName))
94+
compToStringCache.Add(type.AssemblyQualifiedName, SignatureHighlighter.Parse(type, true));
95+
96+
cell.Button.ButtonText.text = compToStringCache[type.AssemblyQualifiedName];
97+
98+
if (typeof(Behaviour).IsAssignableFrom(type))
99+
{
100+
cell.BehaviourToggle.interactable = true;
101+
cell.BehaviourToggle.Set(comp.TryCast<Behaviour>().enabled, false);
102+
}
103+
else
104+
{
105+
cell.BehaviourToggle.interactable = false;
106+
cell.BehaviourToggle.Set(false, false);
107+
}
108+
109+
// if component is the first index it must be the transform, dont show Destroy button for it.
110+
if (index == 0 && cell.DestroyButton.Component.gameObject.activeSelf)
111+
cell.DestroyButton.Component.gameObject.SetActive(false);
112+
else if (index > 0 && !cell.DestroyButton.Component.gameObject.activeSelf)
113+
cell.DestroyButton.Component.gameObject.SetActive(true);
114+
}
115+
}
116+
}

0 commit comments

Comments
 (0)