Rewrite EvaluateWidget, add BaseArgumentHandler, use autocomplete for InteractiveEnum

This commit is contained in:
Sinai
2022-01-02 19:43:55 +11:00
parent e585fc6da0
commit 7b477a8b0e
13 changed files with 768 additions and 490 deletions

View File

@ -164,9 +164,7 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
if (!CurrentHandler.InputField.UIRoot.activeInHierarchy)
ReleaseOwnership(CurrentHandler);
else
{
UpdatePosition();
}
}
}
@ -228,9 +226,9 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
private int lastCaretPosition;
private Vector3 lastInputPosition;
private void UpdatePosition()
internal void UpdatePosition()
{
if (CurrentHandler == null || !CurrentHandler.InputField.Component.isFocused)
if (CurrentHandler == null)
return;
var input = CurrentHandler.InputField;
@ -255,9 +253,10 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
}
else
{
var textGen = input.Component.textComponent.cachedTextGenerator;
var pos = input.UIRoot.transform.TransformPoint(textGen.characters[0].cursorPos);
uiRoot.transform.position = new Vector3(pos.x + 10, pos.y - 20, 0);
uiRoot.transform.position = input.Rect.position + new Vector3(-(input.Rect.rect.width / 2) + 10, -20, 0);
//var textGen = input.Component.textComponent.cachedTextGenerator;
//var pos = input.UIRoot.transform.TransformPoint(textGen.characters[0].cursorPos);
//uiRoot.transform.position = new Vector3(pos.x + 10, pos.y - 20, 0);
}
this.Dragger.OnEndResize();

View File

@ -0,0 +1,160 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using UnityExplorer.CacheObject.IValues;
using UniverseLib;
using UniverseLib.UI;
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;
private readonly List<Suggestion> suggestions = new List<Suggestion>();
private readonly HashSet<string> suggestedValues = new HashSet<string>();
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;
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("");
AutoCompleteModal.Instance.TakeOwnership(this);
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);
AutoCompleteModal.Instance.TakeOwnership(this);
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++)
{
var enumValue = (CachedEnumValue)enumValues[i];
if (enumValue.Name.ContainsIgnoreCase(value))
AddSuggestion(enumValue.Name);
}
}
internal static readonly Dictionary<string, string> sharedValueToLabel = new Dictionary<string, string>(4096);
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));
}
}
}

View File

@ -12,8 +12,6 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
public readonly string DisplayText;
public readonly string UnderlyingValue;
public string Combined => DisplayText + UnderlyingValue;
public Suggestion(string displayText, string underlyingValue)
{
DisplayText = displayText;

View File

@ -9,12 +9,24 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
{
internal static readonly Dictionary<string, string> sharedTypeToLabel = new Dictionary<string, string>(4096);
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 BaseType { get; set; }
public Type[] GenericConstraints { get; set; }
private bool allowAbstract;
private bool allowEnum;
private readonly bool allowAbstract;
private readonly bool allowEnum;
public InputFieldRef InputField { get; }
public bool AnchorToCaretPosition => false;
@ -61,6 +73,9 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
private void OnInputFieldChanged(string value)
{
if (!Enabled)
return;
if (string.IsNullOrEmpty(value) || value == chosenSuggestion)
{
chosenSuggestion = null;