126 lines
4.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityExplorer.UI.Widgets;
namespace UnityExplorer.Inspectors
{
public class ComponentList : ButtonListHandler<Component, ComponentCell>
{
public GameObjectInspector Parent;
2021-06-05 19:36:09 +10:00
public ComponentList(ScrollPool<ComponentCell> scrollPool, Func<List<Component>> getEntriesMethod)
: base(scrollPool, getEntriesMethod, null, null, null)
{
base.SetICell = SetComponentCell;
base.ShouldDisplay = CheckShouldDisplay;
base.OnCellClicked = OnComponentClicked;
}
2021-05-17 21:48:39 +10:00
public void Clear()
{
RefreshData();
ScrollPool.Refresh(true, true);
2021-05-17 21:48:39 +10:00
}
private bool CheckShouldDisplay(Component _, string __) => true;
public override void OnCellBorrowed(ComponentCell cell)
{
base.OnCellBorrowed(cell);
cell.OnBehaviourToggled += OnBehaviourToggled;
cell.OnDestroyClicked += OnDestroyClicked;
}
public override void SetCell(ComponentCell cell, int index)
{
base.SetCell(cell, index);
}
private void OnComponentClicked(int index)
{
var entries = GetEntries();
if (index < 0 || index >= entries.Count)
return;
var comp = entries[index];
if (comp)
InspectorManager.Inspect(comp);
}
private void OnBehaviourToggled(bool value, int index)
{
try
{
var entries = GetEntries();
var comp = entries[index];
if (comp.TryCast<Behaviour>() is Behaviour behaviour)
behaviour.enabled = value;
}
catch (Exception ex)
{
ExplorerCore.LogWarning($"Exception toggling Behaviour.enabled: {ex.ReflectionExToString()}");
}
}
private void OnDestroyClicked(int index)
{
try
{
var entries = GetEntries();
var comp = entries[index];
GameObject.DestroyImmediate(comp);
Parent.UpdateComponents();
}
catch (Exception ex)
{
ExplorerCore.LogWarning($"Exception destroying Component: {ex.ReflectionExToString()}");
}
}
private static readonly Dictionary<string, string> compToStringCache = new Dictionary<string, string>();
// Called from ButtonListHandler.SetCell, will be valid
private void SetComponentCell(ComponentCell cell, int index)
{
var entries = GetEntries();
cell.Enable();
var comp = entries[index];
var type = comp.GetActualType();
if (!compToStringCache.ContainsKey(type.AssemblyQualifiedName))
compToStringCache.Add(type.AssemblyQualifiedName, SignatureHighlighter.Parse(type, true));
cell.Button.ButtonText.text = compToStringCache[type.AssemblyQualifiedName];
if (typeof(Behaviour).IsAssignableFrom(type))
{
cell.BehaviourToggle.interactable = true;
cell.BehaviourToggle.Set(comp.TryCast<Behaviour>().enabled, false);
2021-05-17 18:47:37 +10:00
cell.BehaviourToggle.graphic.color = new Color(0.8f, 1, 0.8f, 0.3f);
}
else
{
cell.BehaviourToggle.interactable = false;
2021-05-17 18:47:37 +10:00
cell.BehaviourToggle.Set(true, false);
//RuntimeProvider.Instance.SetColorBlock(cell.BehaviourToggle,)
2021-06-05 19:36:09 +10:00
cell.BehaviourToggle.graphic.color = new Color(0.2f, 0.2f, 0.2f);
}
// if component is the first index it must be the transform, dont show Destroy button for it.
if (index == 0 && cell.DestroyButton.Component.gameObject.activeSelf)
cell.DestroyButton.Component.gameObject.SetActive(false);
else if (index > 0 && !cell.DestroyButton.Component.gameObject.activeSelf)
cell.DestroyButton.Component.gameObject.SetActive(true);
}
}
}