252 lines
11 KiB
C#
Raw Normal View History

2021-04-23 21:50:58 +10:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityExplorer.UI.Models;
using UnityExplorer.UI.Panels;
using UnityExplorer.UI.Widgets;
2021-04-23 21:50:58 +10:00
using UnityExplorer.UI.Widgets.AutoComplete;
namespace UnityExplorer.UI.ObjectExplorer
2021-04-23 21:50:58 +10:00
{
public class ObjectSearch : UIModel
{
public ObjectExplorerPanel Parent { get; }
2021-04-23 21:50:58 +10:00
public ObjectSearch(ObjectExplorerPanel parent)
2021-04-23 21:50:58 +10:00
{
Parent = parent;
}
private SearchContext m_context = SearchContext.UnityObject;
private SceneFilter m_sceneFilter = SceneFilter.Any;
private ChildFilter m_childFilter = ChildFilter.Any;
2021-05-05 21:27:09 +10:00
private string desiredTypeInput;
private string lastCheckedTypeInput;
private bool lastTypeCanHaveGO;
2021-04-23 21:50:58 +10:00
2021-05-16 21:46:38 +10:00
public ButtonListHandler<object, ButtonCell> dataHandler;
2021-04-23 21:50:58 +10:00
private ScrollPool<ButtonCell> resultsScrollPool;
2021-04-23 21:50:58 +10:00
private List<object> currentResults = new List<object>();
2021-05-05 21:27:09 +10:00
public TypeCompleter typeAutocompleter;
2021-04-23 21:50:58 +10:00
public override GameObject UIRoot => uiRoot;
private GameObject uiRoot;
private GameObject sceneFilterRow;
private GameObject childFilterRow;
private GameObject unityObjectClassRow;
private InputFieldRef nameInputField;
2021-04-23 21:50:58 +10:00
private Text resultsLabel;
public List<object> GetEntries() => currentResults;
public void DoSearch()
{
cachedCellTexts.Clear();
2021-04-23 21:50:58 +10:00
if (m_context == SearchContext.Singleton)
currentResults = SearchProvider.SingletonSearch(nameInputField.Text);
2021-04-23 21:50:58 +10:00
else if (m_context == SearchContext.StaticClass)
currentResults = SearchProvider.StaticClassSearch(nameInputField.Text);
2021-04-23 21:50:58 +10:00
else
{
string compType = "";
2021-05-05 21:27:09 +10:00
if (m_context == SearchContext.UnityObject)
compType = this.desiredTypeInput;
2021-04-23 21:50:58 +10:00
currentResults = SearchProvider.UnityObjectSearch(nameInputField.Text, compType, m_context, m_childFilter, m_sceneFilter);
2021-04-23 21:50:58 +10:00
}
dataHandler.RefreshData();
2021-04-30 23:12:18 +10:00
resultsScrollPool.Refresh(true);
2021-04-23 21:50:58 +10:00
resultsLabel.text = $"{currentResults.Count} results";
}
2021-05-05 21:27:09 +10:00
public void Update()
{
2021-05-16 21:46:38 +10:00
if (m_context == SearchContext.UnityObject && lastCheckedTypeInput != desiredTypeInput)
2021-05-05 21:27:09 +10:00
{
lastCheckedTypeInput = desiredTypeInput;
//var type = ReflectionUtility.GetTypeByName(desiredTypeInput);
if (ReflectionUtility.AllTypes.TryGetValue(desiredTypeInput, out var cachedType))
2021-05-05 21:27:09 +10:00
{
var type = cachedType;
2021-05-05 21:27:09 +10:00
lastTypeCanHaveGO = typeof(Component).IsAssignableFrom(type) || type == typeof(GameObject);
sceneFilterRow.SetActive(lastTypeCanHaveGO);
childFilterRow.SetActive(lastTypeCanHaveGO);
}
else
{
sceneFilterRow.SetActive(false);
childFilterRow.SetActive(false);
lastTypeCanHaveGO = false;
}
}
}
// UI Callbacks
private void OnContextDropdownChanged(int value)
{
m_context = (SearchContext)value;
2021-05-16 21:46:38 +10:00
lastCheckedTypeInput = null;
sceneFilterRow.SetActive(false);
childFilterRow.SetActive(false);
2021-05-05 21:27:09 +10:00
unityObjectClassRow.SetActive(m_context == SearchContext.UnityObject);
}
private void OnSceneFilterDropChanged(int value) => m_sceneFilter = (SceneFilter)value;
private void OnChildFilterDropChanged(int value) => m_childFilter = (ChildFilter)value;
private void OnTypeInputChanged(string val)
{
desiredTypeInput = val;
if (string.IsNullOrEmpty(val))
{
sceneFilterRow.SetActive(false);
childFilterRow.SetActive(false);
lastCheckedTypeInput = val;
}
}
// Cache the syntax-highlighted text for each search result to reduce allocs.
private static readonly Dictionary<int, string> cachedCellTexts = new Dictionary<int, string>();
public void SetCell(ButtonCell cell, int index)
2021-04-23 21:50:58 +10:00
{
if (!cachedCellTexts.ContainsKey(index))
{
string text;
if (m_context == SearchContext.StaticClass)
text = SignatureHighlighter.Parse(currentResults[index] as Type, true);
else
text = ToStringUtility.ToStringWithType(currentResults[index], currentResults[index]?.GetActualType());
cachedCellTexts.Add(index, text);
}
cell.Button.ButtonText.text = cachedCellTexts[index];
2021-04-23 21:50:58 +10:00
}
private void OnCellClicked(int dataIndex)
{
if (m_context == SearchContext.StaticClass)
InspectorManager.Inspect(currentResults[dataIndex] as Type);
else
InspectorManager.Inspect(currentResults[dataIndex]);
2021-04-23 21:50:58 +10:00
}
private bool ShouldDisplayCell(object arg1, string arg2) => true;
public override void ConstructUI(GameObject parent)
{
uiRoot = UIFactory.CreateVerticalGroup(parent, "ObjectSearch", true, true, true, true, 2, new Vector4(2, 2, 2, 2));
UIFactory.SetLayoutElement(uiRoot, flexibleHeight: 9999);
// Search context row
var contextGroup = UIFactory.CreateHorizontalGroup(uiRoot, "SearchContextRow", false, true, true, true, 2, new Vector4(2, 2, 2, 2));
UIFactory.SetLayoutElement(contextGroup, minHeight: 25, flexibleHeight: 0);
var contextLbl = UIFactory.CreateLabel(contextGroup, "SearchContextLabel", "Searching for:", TextAnchor.MiddleLeft);
UIFactory.SetLayoutElement(contextLbl.gameObject, minWidth: 110, flexibleWidth: 0);
var contextDropObj = UIFactory.CreateDropdown(contextGroup, out Dropdown contextDrop, null, 14, OnContextDropdownChanged);
foreach (var name in Enum.GetNames(typeof(SearchContext)))
contextDrop.options.Add(new Dropdown.OptionData(name));
UIFactory.SetLayoutElement(contextDropObj, minHeight: 25, flexibleHeight: 0, flexibleWidth: 9999);
// Unity class input
unityObjectClassRow = UIFactory.CreateHorizontalGroup(uiRoot, "UnityClassRow", false, true, true, true, 2, new Vector4(2, 2, 2, 2));
UIFactory.SetLayoutElement(unityObjectClassRow, minHeight: 25, flexibleHeight: 0);
var unityClassLbl = UIFactory.CreateLabel(unityObjectClassRow, "UnityClassLabel", "Class filter:", TextAnchor.MiddleLeft);
2021-04-23 21:50:58 +10:00
UIFactory.SetLayoutElement(unityClassLbl.gameObject, minWidth: 110, flexibleWidth: 0);
var classInputField = UIFactory.CreateInputField(unityObjectClassRow, "ClassInput", "...");
UIFactory.SetLayoutElement(classInputField.UIRoot, minHeight: 25, flexibleHeight: 0, flexibleWidth: 9999);
2021-04-23 21:50:58 +10:00
2021-05-05 21:27:09 +10:00
typeAutocompleter = new TypeCompleter(typeof(UnityEngine.Object), classInputField);
classInputField.OnValueChanged += OnTypeInputChanged;
2021-04-23 21:50:58 +10:00
2021-05-05 21:27:09 +10:00
//unityObjectClassRow.SetActive(false);
2021-04-23 21:50:58 +10:00
// Child filter row
childFilterRow = UIFactory.CreateHorizontalGroup(uiRoot, "ChildFilterRow", false, true, true, true, 2, new Vector4(2, 2, 2, 2));
UIFactory.SetLayoutElement(childFilterRow, minHeight: 25, flexibleHeight: 0);
var childLbl = UIFactory.CreateLabel(childFilterRow, "ChildLabel", "Child filter:", TextAnchor.MiddleLeft);
UIFactory.SetLayoutElement(childLbl.gameObject, minWidth: 110, flexibleWidth: 0);
var childDropObj = UIFactory.CreateDropdown(childFilterRow, out Dropdown childDrop, null, 14, OnChildFilterDropChanged);
foreach (var name in Enum.GetNames(typeof(ChildFilter)))
childDrop.options.Add(new Dropdown.OptionData(name));
UIFactory.SetLayoutElement(childDropObj, minHeight: 25, flexibleHeight: 0, flexibleWidth: 9999);
childFilterRow.SetActive(false);
// Scene filter row
sceneFilterRow = UIFactory.CreateHorizontalGroup(uiRoot, "SceneFilterRow", false, true, true, true, 2, new Vector4(2, 2, 2, 2));
UIFactory.SetLayoutElement(sceneFilterRow, minHeight: 25, flexibleHeight: 0);
var sceneLbl = UIFactory.CreateLabel(sceneFilterRow, "SceneLabel", "Scene filter:", TextAnchor.MiddleLeft);
UIFactory.SetLayoutElement(sceneLbl.gameObject, minWidth: 110, flexibleWidth: 0);
var sceneDropObj = UIFactory.CreateDropdown(sceneFilterRow, out Dropdown sceneDrop, null, 14, OnSceneFilterDropChanged);
foreach (var name in Enum.GetNames(typeof(SceneFilter)))
sceneDrop.options.Add(new Dropdown.OptionData(name));
UIFactory.SetLayoutElement(sceneDropObj, minHeight: 25, flexibleHeight: 0, flexibleWidth: 9999);
sceneFilterRow.SetActive(false);
// Name filter input
var nameRow = UIFactory.CreateHorizontalGroup(uiRoot, "NameRow", true, true, true, true, 2, new Vector4(2, 2, 2, 2));
UIFactory.SetLayoutElement(nameRow, minHeight: 25, flexibleHeight: 0);
var nameLbl = UIFactory.CreateLabel(nameRow, "NameFilterLabel", "Name contains:", TextAnchor.MiddleLeft);
UIFactory.SetLayoutElement(nameLbl.gameObject, minWidth: 110, flexibleWidth: 0);
nameInputField = UIFactory.CreateInputField(nameRow, "NameFilterInput", "...");
UIFactory.SetLayoutElement(nameInputField.UIRoot, minHeight: 25, flexibleHeight: 0, flexibleWidth: 9999);
2021-04-23 21:50:58 +10:00
// Search button
var searchButton = UIFactory.CreateButton(uiRoot, "SearchButton", "Search");
2021-05-11 19:18:27 +10:00
UIFactory.SetLayoutElement(searchButton.Component.gameObject, minHeight: 25, flexibleHeight: 0);
searchButton.OnClick += DoSearch;
2021-04-23 21:50:58 +10:00
// Results count label
var resultsCountRow = UIFactory.CreateHorizontalGroup(uiRoot, "ResultsCountRow", true, true, true, true);
UIFactory.SetLayoutElement(resultsCountRow, minHeight: 25, flexibleHeight: 0);
resultsLabel = UIFactory.CreateLabel(resultsCountRow, "ResultsLabel", "0 results", TextAnchor.MiddleCenter);
// RESULTS SCROLL POOL
2021-05-16 21:46:38 +10:00
dataHandler = new ButtonListHandler<object, ButtonCell>(resultsScrollPool, GetEntries, SetCell, ShouldDisplayCell, OnCellClicked);
resultsScrollPool = UIFactory.CreateScrollPool<ButtonCell>(uiRoot, "ResultsList", out GameObject scrollObj,
out GameObject scrollContent);
2021-04-30 23:12:18 +10:00
resultsScrollPool.Initialize(dataHandler);
2021-04-23 21:50:58 +10:00
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
}
}
}