|
| 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