93 lines
2.7 KiB
C#
93 lines
2.7 KiB
C#
using System;
|
|
using UnityEditor;
|
|
using UnityEditor.IMGUI.Controls;
|
|
using UnityEngine;
|
|
using UnityEngine.TestTools;
|
|
|
|
namespace BracerLib.Editor.Utility
|
|
{
|
|
/// <summary>
|
|
/// Referenced via https://github.com/marijnz/unity-autocomplete-search-field
|
|
/// </summary>
|
|
[Serializable]
|
|
[Obsolete("Use Odin or some attribute-based item instead.")]
|
|
[ExcludeFromCoverage]
|
|
public class EditorSearchField
|
|
{
|
|
private static class Styles
|
|
{
|
|
public const float RESULT_HEIGHT = 20f;
|
|
public const float RESULT_BORDER_WIDTH = 2f;
|
|
public const float RESULT_MARGIN = 15f;
|
|
public const float RESULT_LABEL_OFFSET = 2f;
|
|
|
|
public static readonly GUIStyle ENTRY_EVEN;
|
|
public static readonly GUIStyle ENTRY_ODD;
|
|
public static readonly GUIStyle LABEL_STYLE;
|
|
public static readonly GUIStyle RESULTS_BORDER_STYLE;
|
|
|
|
static Styles()
|
|
{
|
|
ENTRY_EVEN = new GUIStyle("CN EntryBackEven");
|
|
ENTRY_ODD = new GUIStyle("CN EntryBackOdd");
|
|
RESULTS_BORDER_STYLE = new GUIStyle("hostview");
|
|
|
|
LABEL_STYLE = new GUIStyle
|
|
{
|
|
alignment = TextAnchor.MiddleLeft,
|
|
richText = true
|
|
};
|
|
}
|
|
}
|
|
|
|
private static void RepaintFocusedWindow()
|
|
{
|
|
if (EditorWindow.focusedWindow != null)
|
|
EditorWindow.focusedWindow.Repaint();
|
|
}
|
|
|
|
private SearchField searchField;
|
|
private string searchString;
|
|
|
|
public event Action<string> OnInputChanged;
|
|
|
|
public bool HasSearchString => !string.IsNullOrEmpty(SearchString);
|
|
public string SearchString
|
|
{
|
|
get => searchString;
|
|
set
|
|
{
|
|
searchString = value;
|
|
OnGUI();
|
|
}
|
|
}
|
|
|
|
public void OnGUI()
|
|
{
|
|
var rect = GUILayoutUtility.GetRect(1, 1, 18, 18, GUILayout.ExpandWidth(true));
|
|
GUILayout.BeginHorizontal();
|
|
|
|
DoSearchField(rect);
|
|
|
|
GUILayout.EndHorizontal();
|
|
}
|
|
|
|
private void DoSearchField(Rect rect)
|
|
{
|
|
if (searchField == null)
|
|
searchField = new SearchField();
|
|
|
|
var result = searchField.OnGUI(rect, searchString);
|
|
if (result != searchString)
|
|
OnInputChanged?.Invoke(result);
|
|
|
|
searchString = result;
|
|
|
|
if (HasSearchbarFocused())
|
|
RepaintFocusedWindow();
|
|
}
|
|
|
|
private bool HasSearchbarFocused() => GUIUtility.keyboardControl == searchField.searchFieldControlID;
|
|
}
|
|
}
|