using System; using System.Collections.Generic; using UnityEditor; using UnityEditor.IMGUI.Controls; using UnityEngine; using UnityObject = UnityEngine.Object; namespace litefeel.Finder.Editor { class SimpleTreeView : TreeView { private TreeViewItem m_Root = new TreeViewItem(-1, -1, "Root"); private List m_TreeItems = new List(); public Action onItemSelect; public Action onItemClick; public Action onItemDoubleClick; private List m_Items = new List(); private string m_FilterStr; public List Items { get { return m_Items; } set { m_Items = value ?? new List(); } } public SimpleTreeView(TreeViewState treeViewState) : base(treeViewState) { Reload(); } public void SetFilter(string filterStr) { m_FilterStr = filterStr; if (string.IsNullOrWhiteSpace(m_FilterStr)) m_FilterStr = null; else m_FilterStr = m_FilterStr.ToLower(); Reload(); } protected override TreeViewItem BuildRoot() { // BuildRoot is called every time Reload is called to ensure that TreeViewItems // are created from data. Here we create a fixed set of items. In a real world example, // a data model should be passed into the TreeView and the items created from the model. // This section illustrates that IDs should be unique. The root item is required to // have a depth of -1, and the rest of the items increment from that. m_TreeItems.Clear(); for (var i = 0; i < m_Items.Count; i++) { if (m_FilterStr == null || m_Items[i].ToLower().Contains(m_FilterStr)) m_TreeItems.Add(new TreeViewItem(i, 0, m_Items[i])); } // Utility method that initializes the TreeViewItem.children and .parent for all items. SetupParentsAndChildrenFromDepths(m_Root, m_TreeItems); // Return root of the tree return m_Root; } protected override bool CanMultiSelect(TreeViewItem item) { return false; } protected override void DoubleClickedItem(int id) { onItemDoubleClick?.Invoke(id); } protected override void SingleClickedItem(int id) { onItemClick?.Invoke(id); } protected override void SelectionChanged(IList selectedIds) { onItemSelect?.Invoke(selectedIds[0]); } } }