2022-01-02 19:43:55 +11:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using UnityExplorer.CacheObject.IValues;
|
2022-01-31 21:24:01 +11:00
|
|
|
|
using UniverseLib.UI.Models;
|
|
|
|
|
using UniverseLib.Utility;
|
2022-01-02 19:43:55 +11:00
|
|
|
|
|
|
|
|
|
namespace UnityExplorer.UI.Widgets.AutoComplete
|
|
|
|
|
{
|
|
|
|
|
public class EnumCompleter : ISuggestionProvider
|
|
|
|
|
{
|
|
|
|
|
public bool Enabled
|
|
|
|
|
{
|
|
|
|
|
get => _enabled;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_enabled = value;
|
|
|
|
|
if (!_enabled)
|
|
|
|
|
AutoCompleteModal.Instance.ReleaseOwnership(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private bool _enabled = true;
|
|
|
|
|
|
|
|
|
|
public event Action<Suggestion> SuggestionClicked;
|
|
|
|
|
|
|
|
|
|
public Type EnumType { get; set; }
|
|
|
|
|
|
|
|
|
|
public InputFieldRef InputField { get; }
|
|
|
|
|
public bool AnchorToCaretPosition => false;
|
|
|
|
|
|
2022-04-12 05:20:35 +10:00
|
|
|
|
private readonly List<Suggestion> suggestions = new();
|
|
|
|
|
private readonly HashSet<string> suggestedValues = new();
|
2022-01-02 19:43:55 +11:00
|
|
|
|
|
|
|
|
|
private OrderedDictionary enumValues;
|
|
|
|
|
|
|
|
|
|
internal string chosenSuggestion;
|
|
|
|
|
|
|
|
|
|
bool ISuggestionProvider.AllowNavigation => false;
|
|
|
|
|
|
|
|
|
|
public EnumCompleter(Type enumType, InputFieldRef inputField)
|
|
|
|
|
{
|
|
|
|
|
EnumType = enumType;
|
|
|
|
|
InputField = inputField;
|
|
|
|
|
|
|
|
|
|
inputField.OnValueChanged += OnInputFieldChanged;
|
|
|
|
|
|
|
|
|
|
if (EnumType != null)
|
|
|
|
|
CacheEnumValues();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CacheEnumValues()
|
|
|
|
|
{
|
|
|
|
|
enumValues = InteractiveEnum.GetEnumValues(EnumType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetLastSplitInput(string fullInput)
|
|
|
|
|
{
|
|
|
|
|
string ret = fullInput;
|
2022-04-12 05:20:35 +10:00
|
|
|
|
|
2022-01-02 19:43:55 +11:00
|
|
|
|
int lastSplit = fullInput.LastIndexOf(',');
|
|
|
|
|
if (lastSplit >= 0)
|
|
|
|
|
{
|
|
|
|
|
lastSplit++;
|
|
|
|
|
if (lastSplit == fullInput.Length)
|
|
|
|
|
ret = "";
|
|
|
|
|
else
|
|
|
|
|
ret = fullInput.Substring(lastSplit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnSuggestionClicked(Suggestion suggestion)
|
|
|
|
|
{
|
|
|
|
|
chosenSuggestion = suggestion.UnderlyingValue;
|
|
|
|
|
|
|
|
|
|
string lastInput = GetLastSplitInput(InputField.Text);
|
|
|
|
|
|
|
|
|
|
if (lastInput != suggestion.UnderlyingValue)
|
|
|
|
|
{
|
|
|
|
|
string valueToSet = InputField.Text;
|
|
|
|
|
|
|
|
|
|
if (valueToSet.Length > 0)
|
|
|
|
|
valueToSet = valueToSet.Substring(0, InputField.Text.Length - lastInput.Length);
|
|
|
|
|
|
|
|
|
|
valueToSet += suggestion.UnderlyingValue;
|
|
|
|
|
|
|
|
|
|
InputField.Text = valueToSet;
|
|
|
|
|
|
|
|
|
|
//InputField.Text += suggestion.UnderlyingValue.Substring(lastInput.Length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SuggestionClicked?.Invoke(suggestion);
|
|
|
|
|
|
|
|
|
|
suggestions.Clear();
|
|
|
|
|
AutoCompleteModal.Instance.SetSuggestions(suggestions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void HelperButtonClicked()
|
|
|
|
|
{
|
|
|
|
|
GetSuggestions("");
|
2022-04-12 00:17:06 +10:00
|
|
|
|
AutoCompleteModal.TakeOwnership(this);
|
2022-01-02 19:43:55 +11:00
|
|
|
|
AutoCompleteModal.Instance.SetSuggestions(suggestions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnInputFieldChanged(string value)
|
|
|
|
|
{
|
|
|
|
|
if (!Enabled)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(value) || GetLastSplitInput(value) == chosenSuggestion)
|
|
|
|
|
{
|
|
|
|
|
chosenSuggestion = null;
|
|
|
|
|
AutoCompleteModal.Instance.ReleaseOwnership(this);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
GetSuggestions(value);
|
|
|
|
|
|
2022-04-12 00:17:06 +10:00
|
|
|
|
AutoCompleteModal.TakeOwnership(this);
|
2022-01-02 19:43:55 +11:00
|
|
|
|
AutoCompleteModal.Instance.SetSuggestions(suggestions);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void GetSuggestions(string value)
|
|
|
|
|
{
|
|
|
|
|
suggestions.Clear();
|
|
|
|
|
suggestedValues.Clear();
|
|
|
|
|
|
|
|
|
|
if (EnumType == null)
|
|
|
|
|
{
|
|
|
|
|
ExplorerCore.LogWarning("Autocompleter Base enum type is null!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
value = GetLastSplitInput(value);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < this.enumValues.Count; i++)
|
|
|
|
|
{
|
2022-04-12 05:20:35 +10:00
|
|
|
|
CachedEnumValue enumValue = (CachedEnumValue)enumValues[i];
|
2022-01-02 19:43:55 +11:00
|
|
|
|
if (enumValue.Name.ContainsIgnoreCase(value))
|
|
|
|
|
AddSuggestion(enumValue.Name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-12 05:20:35 +10:00
|
|
|
|
internal static readonly Dictionary<string, string> sharedValueToLabel = new(4096);
|
2022-01-02 19:43:55 +11:00
|
|
|
|
|
|
|
|
|
void AddSuggestion(string value)
|
|
|
|
|
{
|
|
|
|
|
if (suggestedValues.Contains(value))
|
|
|
|
|
return;
|
|
|
|
|
suggestedValues.Add(value);
|
|
|
|
|
|
|
|
|
|
if (!sharedValueToLabel.ContainsKey(value))
|
|
|
|
|
sharedValueToLabel.Add(value, $"<color={SignatureHighlighter.CONST}>{value}</color>");
|
|
|
|
|
|
|
|
|
|
suggestions.Add(new Suggestion(sharedValueToLabel[value], value));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|