mirror of
https://github.com/GrahamKracker/UnityExplorer.git
synced 2025-07-03 03:52:28 +08:00
Rewrite EvaluateWidget, add BaseArgumentHandler, use autocomplete for InteractiveEnum
This commit is contained in:
@ -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();
|
||||
|
160
src/UI/Widgets/AutoComplete/EnumCompleter.cs
Normal file
160
src/UI/Widgets/AutoComplete/EnumCompleter.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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;
|
||||
|
51
src/UI/Widgets/EvaluateWidget/BaseArgumentHandler.cs
Normal file
51
src/UI/Widgets/EvaluateWidget/BaseArgumentHandler.cs
Normal file
@ -0,0 +1,51 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityExplorer.UI.Widgets.AutoComplete;
|
||||
using UniverseLib.UI;
|
||||
using UniverseLib.UI.Models;
|
||||
|
||||
namespace UnityExplorer.UI.Widgets
|
||||
{
|
||||
public abstract class BaseArgumentHandler : IPooledObject
|
||||
{
|
||||
protected EvaluateWidget evaluator;
|
||||
|
||||
internal Text argNameLabel;
|
||||
internal InputFieldRef inputField;
|
||||
internal TypeCompleter typeCompleter;
|
||||
|
||||
// IPooledObject
|
||||
public float DefaultHeight => 25f;
|
||||
public GameObject UIRoot { get; set; }
|
||||
|
||||
public abstract void CreateSpecialContent();
|
||||
|
||||
public GameObject CreateContent(GameObject parent)
|
||||
{
|
||||
UIRoot = UIFactory.CreateUIObject("ArgRow", parent);
|
||||
UIFactory.SetLayoutElement(UIRoot, minHeight: 25, flexibleHeight: 50, minWidth: 50, flexibleWidth: 9999);
|
||||
UIFactory.SetLayoutGroup<HorizontalLayoutGroup>(UIRoot, false, false, true, true, 5);
|
||||
UIRoot.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
|
||||
argNameLabel = UIFactory.CreateLabel(UIRoot, "ArgLabel", "not set", TextAnchor.MiddleLeft);
|
||||
UIFactory.SetLayoutElement(argNameLabel.gameObject, minWidth: 40, flexibleWidth: 90, minHeight: 25, flexibleHeight: 50);
|
||||
argNameLabel.horizontalOverflow = HorizontalWrapMode.Wrap;
|
||||
|
||||
inputField = UIFactory.CreateInputField(UIRoot, "InputField", "...");
|
||||
UIFactory.SetLayoutElement(inputField.UIRoot, minHeight: 25, flexibleHeight: 50, minWidth: 100, flexibleWidth: 1000);
|
||||
inputField.Component.lineType = InputField.LineType.MultiLineNewline;
|
||||
inputField.UIRoot.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
|
||||
typeCompleter = new TypeCompleter(typeof(object), this.inputField);
|
||||
typeCompleter.Enabled = false;
|
||||
|
||||
CreateSpecialContent();
|
||||
|
||||
return UIRoot;
|
||||
}
|
||||
}
|
||||
}
|
176
src/UI/Widgets/EvaluateWidget/EvaluateWidget.cs
Normal file
176
src/UI/Widgets/EvaluateWidget/EvaluateWidget.cs
Normal file
@ -0,0 +1,176 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityExplorer.UI;
|
||||
using UniverseLib.UI.Models;
|
||||
using UnityExplorer.UI.Widgets.AutoComplete;
|
||||
using UniverseLib.UI;
|
||||
using UniverseLib;
|
||||
using UnityExplorer.CacheObject;
|
||||
|
||||
namespace UnityExplorer.UI.Widgets
|
||||
{
|
||||
public class EvaluateWidget : IPooledObject
|
||||
{
|
||||
public CacheMember Owner { get; set; }
|
||||
|
||||
public GameObject UIRoot { get; set; }
|
||||
public float DefaultHeight => -1f;
|
||||
|
||||
private ParameterInfo[] parameters;
|
||||
internal GameObject parametersHolder;
|
||||
private ParameterHandler[] paramHandlers;
|
||||
|
||||
private Type[] genericArguments;
|
||||
internal GameObject genericArgumentsHolder;
|
||||
private GenericArgumentHandler[] genericHandlers;
|
||||
|
||||
public void OnBorrowedFromPool(CacheMember owner)
|
||||
{
|
||||
this.Owner = owner;
|
||||
|
||||
parameters = owner.Arguments;
|
||||
paramHandlers = new ParameterHandler[parameters.Length];
|
||||
|
||||
genericArguments = owner.GenericArguments;
|
||||
genericHandlers = new GenericArgumentHandler[genericArguments.Length];
|
||||
|
||||
SetArgRows();
|
||||
|
||||
this.UIRoot.SetActive(true);
|
||||
|
||||
InspectorManager.OnInspectedTabsChanged += InspectorManager_OnInspectedTabsChanged;
|
||||
}
|
||||
|
||||
public void OnReturnToPool()
|
||||
{
|
||||
foreach (var widget in paramHandlers)
|
||||
{
|
||||
widget.OnReturned();
|
||||
Pool<ParameterHandler>.Return(widget);
|
||||
}
|
||||
paramHandlers = null;
|
||||
|
||||
foreach (var widget in genericHandlers)
|
||||
{
|
||||
widget.OnReturned();
|
||||
Pool<GenericArgumentHandler>.Return(widget);
|
||||
}
|
||||
genericHandlers = null;
|
||||
|
||||
this.Owner = null;
|
||||
|
||||
InspectorManager.OnInspectedTabsChanged -= InspectorManager_OnInspectedTabsChanged;
|
||||
}
|
||||
|
||||
private void InspectorManager_OnInspectedTabsChanged()
|
||||
{
|
||||
foreach (var handler in this.paramHandlers)
|
||||
handler.PopulateDropdown();
|
||||
}
|
||||
|
||||
public Type[] TryParseGenericArguments()
|
||||
{
|
||||
Type[] outArgs = new Type[genericArguments.Length];
|
||||
|
||||
for (int i = 0; i < genericArguments.Length; i++)
|
||||
outArgs[i] = genericHandlers[i].Evaluate();
|
||||
|
||||
return outArgs;
|
||||
}
|
||||
|
||||
public object[] TryParseArguments()
|
||||
{
|
||||
object[] outArgs = new object[parameters.Length];
|
||||
|
||||
for (int i = 0; i < parameters.Length; i++)
|
||||
outArgs[i] = paramHandlers[i].Evaluate();
|
||||
|
||||
return outArgs;
|
||||
}
|
||||
|
||||
private void SetArgRows()
|
||||
{
|
||||
if (genericArguments.Any())
|
||||
{
|
||||
genericArgumentsHolder.SetActive(true);
|
||||
SetGenericRows();
|
||||
}
|
||||
else
|
||||
genericArgumentsHolder.SetActive(false);
|
||||
|
||||
if (parameters.Any())
|
||||
{
|
||||
parametersHolder.SetActive(true);
|
||||
SetNormalArgRows();
|
||||
}
|
||||
else
|
||||
parametersHolder.SetActive(false);
|
||||
}
|
||||
|
||||
private void SetGenericRows()
|
||||
{
|
||||
for (int i = 0; i < genericArguments.Length; i++)
|
||||
{
|
||||
var type = genericArguments[i];
|
||||
|
||||
var holder = genericHandlers[i] = Pool<GenericArgumentHandler>.Borrow();
|
||||
holder.UIRoot.transform.SetParent(this.genericArgumentsHolder.transform, false);
|
||||
holder.OnBorrowed(this, type);
|
||||
}
|
||||
}
|
||||
|
||||
private void SetNormalArgRows()
|
||||
{
|
||||
for (int i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
var param = parameters[i];
|
||||
|
||||
var holder = paramHandlers[i] = Pool<ParameterHandler>.Borrow();
|
||||
holder.UIRoot.transform.SetParent(this.parametersHolder.transform, false);
|
||||
holder.OnBorrowed(this, param);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public GameObject CreateContent(GameObject parent)
|
||||
{
|
||||
UIRoot = UIFactory.CreateVerticalGroup(parent, "EvaluateWidget", false, false, true, true, 3, new Vector4(2, 2, 2, 2),
|
||||
new Color(0.15f, 0.15f, 0.15f));
|
||||
UIFactory.SetLayoutElement(UIRoot, minWidth: 50, flexibleWidth: 9999, minHeight: 50, flexibleHeight: 800);
|
||||
//UIRoot.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
|
||||
// generic args
|
||||
this.genericArgumentsHolder = UIFactory.CreateUIObject("GenericHolder", UIRoot);
|
||||
UIFactory.SetLayoutElement(genericArgumentsHolder, flexibleWidth: 1000);
|
||||
var genericsTitle = UIFactory.CreateLabel(genericArgumentsHolder, "GenericsTitle", "Generic Arguments", TextAnchor.MiddleLeft);
|
||||
UIFactory.SetLayoutElement(genericsTitle.gameObject, minHeight: 25, flexibleWidth: 1000);
|
||||
UIFactory.SetLayoutGroup<VerticalLayoutGroup>(genericArgumentsHolder, false, false, true, true, 3);
|
||||
UIFactory.SetLayoutElement(genericArgumentsHolder, minHeight: 25, flexibleHeight: 750, minWidth: 50, flexibleWidth: 9999);
|
||||
//genericArgHolder.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
|
||||
// args
|
||||
this.parametersHolder = UIFactory.CreateUIObject("ArgHolder", UIRoot);
|
||||
UIFactory.SetLayoutElement(parametersHolder, flexibleWidth: 1000);
|
||||
var argsTitle = UIFactory.CreateLabel(parametersHolder, "ArgsTitle", "Arguments", TextAnchor.MiddleLeft);
|
||||
UIFactory.SetLayoutElement(argsTitle.gameObject, minHeight: 25, flexibleWidth: 1000);
|
||||
UIFactory.SetLayoutGroup<VerticalLayoutGroup>(parametersHolder, false, false, true, true, 3);
|
||||
UIFactory.SetLayoutElement(parametersHolder, minHeight: 25, flexibleHeight: 750, minWidth: 50, flexibleWidth: 9999);
|
||||
//argHolder.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
|
||||
// evaluate button
|
||||
var evalButton = UIFactory.CreateButton(UIRoot, "EvaluateButton", "Evaluate", new Color(0.2f, 0.2f, 0.2f));
|
||||
UIFactory.SetLayoutElement(evalButton.Component.gameObject, minHeight: 25, minWidth: 150, flexibleWidth: 0);
|
||||
evalButton.OnClick += () =>
|
||||
{
|
||||
Owner.EvaluateAndSetCell();
|
||||
};
|
||||
|
||||
return UIRoot;
|
||||
}
|
||||
}
|
||||
}
|
61
src/UI/Widgets/EvaluateWidget/GenericArgumentHandler.cs
Normal file
61
src/UI/Widgets/EvaluateWidget/GenericArgumentHandler.cs
Normal file
@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using UniverseLib;
|
||||
|
||||
namespace UnityExplorer.UI.Widgets
|
||||
{
|
||||
public class GenericArgumentHandler : BaseArgumentHandler
|
||||
{
|
||||
private Type genericType;
|
||||
|
||||
public void OnBorrowed(EvaluateWidget evaluator, Type genericConstraint)
|
||||
{
|
||||
this.evaluator = evaluator;
|
||||
this.genericType = genericConstraint;
|
||||
|
||||
typeCompleter.Enabled = true;
|
||||
typeCompleter.BaseType = genericType;
|
||||
typeCompleter.CacheTypes();
|
||||
|
||||
var constraints = genericType.GetGenericParameterConstraints();
|
||||
typeCompleter.GenericConstraints = constraints;
|
||||
|
||||
var sb = new StringBuilder($"<color={SignatureHighlighter.CONST}>{genericType.Name}</color>");
|
||||
|
||||
for (int j = 0; j < constraints.Length; j++)
|
||||
{
|
||||
if (j == 0) sb.Append(' ').Append('(');
|
||||
else sb.Append(',').Append(' ');
|
||||
|
||||
sb.Append(SignatureHighlighter.Parse(constraints[j], false));
|
||||
|
||||
if (j + 1 == constraints.Length)
|
||||
sb.Append(')');
|
||||
}
|
||||
|
||||
argNameLabel.text = sb.ToString();
|
||||
}
|
||||
|
||||
public void OnReturned()
|
||||
{
|
||||
this.evaluator = null;
|
||||
this.genericType = null;
|
||||
|
||||
this.typeCompleter.Enabled = false;
|
||||
|
||||
this.inputField.Text = "";
|
||||
}
|
||||
|
||||
public Type Evaluate()
|
||||
{
|
||||
return ReflectionUtility.GetTypeByName(this.inputField.Text)
|
||||
?? throw new Exception($"Could not find any type by name '{this.inputField.Text}'!");
|
||||
}
|
||||
|
||||
public override void CreateSpecialContent()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
186
src/UI/Widgets/EvaluateWidget/ParameterHandler.cs
Normal file
186
src/UI/Widgets/EvaluateWidget/ParameterHandler.cs
Normal file
@ -0,0 +1,186 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Reflection;
|
||||
using UnityEngine.UI;
|
||||
using UnityExplorer.CacheObject.IValues;
|
||||
using UnityExplorer.UI.Widgets.AutoComplete;
|
||||
using UniverseLib;
|
||||
using UniverseLib.UI;
|
||||
|
||||
namespace UnityExplorer.UI.Widgets
|
||||
{
|
||||
public class ParameterHandler : BaseArgumentHandler
|
||||
{
|
||||
private ParameterInfo paramInfo;
|
||||
private Type paramType;
|
||||
|
||||
internal Dropdown dropdown;
|
||||
private bool usingDropdown;
|
||||
internal EnumCompleter enumCompleter;
|
||||
private ButtonRef enumHelperButton;
|
||||
|
||||
public void OnBorrowed(EvaluateWidget evaluator, ParameterInfo paramInfo)
|
||||
{
|
||||
this.evaluator = evaluator;
|
||||
this.paramInfo = paramInfo;
|
||||
|
||||
this.paramType = paramInfo.ParameterType;
|
||||
if (paramType.IsByRef)
|
||||
paramType = paramType.GetElementType();
|
||||
|
||||
this.argNameLabel.text =
|
||||
$"{SignatureHighlighter.Parse(paramType, false)} <color={SignatureHighlighter.LOCAL_ARG}>{paramInfo.Name}</color>";
|
||||
|
||||
if (ParseUtility.CanParse(paramType) || typeof(Type).IsAssignableFrom(paramType))
|
||||
{
|
||||
this.inputField.Component.gameObject.SetActive(true);
|
||||
this.dropdown.gameObject.SetActive(false);
|
||||
this.typeCompleter.Enabled = typeof(Type).IsAssignableFrom(paramType);
|
||||
this.enumCompleter.Enabled = paramType.IsEnum;
|
||||
this.enumHelperButton.Component.gameObject.SetActive(paramType.IsEnum);
|
||||
|
||||
if (!typeCompleter.Enabled)
|
||||
{
|
||||
if (paramType == typeof(string))
|
||||
inputField.PlaceholderText.text = "...";
|
||||
else
|
||||
inputField.PlaceholderText.text = $"eg. {ParseUtility.GetExampleInput(paramType)}";
|
||||
}
|
||||
else
|
||||
{
|
||||
inputField.PlaceholderText.text = "Enter a Type name...";
|
||||
this.typeCompleter.BaseType = typeof(object);
|
||||
this.typeCompleter.CacheTypes();
|
||||
}
|
||||
|
||||
if (enumCompleter.Enabled)
|
||||
{
|
||||
enumCompleter.EnumType = paramType;
|
||||
enumCompleter.CacheEnumValues();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// non-parsable, and not a Type
|
||||
this.inputField.Component.gameObject.SetActive(false);
|
||||
this.dropdown.gameObject.SetActive(true);
|
||||
this.typeCompleter.Enabled = false;
|
||||
this.enumCompleter.Enabled = false;
|
||||
this.enumHelperButton.Component.gameObject.SetActive(false);
|
||||
|
||||
usingDropdown = true;
|
||||
PopulateDropdown();
|
||||
|
||||
InspectorManager.OnInspectedTabsChanged += PopulateDropdown;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnReturned()
|
||||
{
|
||||
this.evaluator = null;
|
||||
this.paramInfo = null;
|
||||
|
||||
usingDropdown = false;
|
||||
|
||||
this.enumCompleter.Enabled = false;
|
||||
this.typeCompleter.Enabled = false;
|
||||
|
||||
this.inputField.Text = "";
|
||||
|
||||
InspectorManager.OnInspectedTabsChanged -= PopulateDropdown;
|
||||
}
|
||||
|
||||
public object Evaluate()
|
||||
{
|
||||
if (!usingDropdown)
|
||||
{
|
||||
var input = this.inputField.Text;
|
||||
|
||||
if (paramType == typeof(string))
|
||||
return input;
|
||||
|
||||
if (string.IsNullOrEmpty(input))
|
||||
{
|
||||
if (paramInfo.IsOptional)
|
||||
return paramInfo.DefaultValue;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!ParseUtility.TryParse(input, paramType, out object parsed, out Exception ex))
|
||||
{
|
||||
ExplorerCore.LogWarning($"Cannot parse argument '{paramInfo.Name}' ({paramInfo.ParameterType.Name})" +
|
||||
$"{(ex == null ? "" : $", {ex.GetType().Name}: {ex.Message}")}");
|
||||
return null;
|
||||
}
|
||||
else
|
||||
return parsed;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (dropdown.value == 0)
|
||||
return null;
|
||||
else
|
||||
return dropdownUnderlyingValues[dropdown.value];
|
||||
}
|
||||
}
|
||||
|
||||
private object[] dropdownUnderlyingValues;
|
||||
|
||||
internal void PopulateDropdown()
|
||||
{
|
||||
if (!usingDropdown)
|
||||
return;
|
||||
|
||||
dropdown.options.Clear();
|
||||
var underlyingValues = new List<object>();
|
||||
|
||||
dropdown.options.Add(new Dropdown.OptionData("null"));
|
||||
underlyingValues.Add(null);
|
||||
|
||||
var argType = paramType;
|
||||
|
||||
int tabIndex = 0;
|
||||
foreach (var tab in InspectorManager.Inspectors)
|
||||
{
|
||||
tabIndex++;
|
||||
|
||||
if (argType.IsAssignableFrom(tab.Target.GetActualType()))
|
||||
{
|
||||
dropdown.options.Add(new Dropdown.OptionData($"Tab {tabIndex}: {tab.Tab.TabText.text}"));
|
||||
underlyingValues.Add(tab.Target);
|
||||
}
|
||||
}
|
||||
|
||||
dropdownUnderlyingValues = underlyingValues.ToArray();
|
||||
}
|
||||
|
||||
private void EnumHelper_OnClick()
|
||||
{
|
||||
enumCompleter.HelperButtonClicked();
|
||||
}
|
||||
|
||||
public override void CreateSpecialContent()
|
||||
{
|
||||
enumHelperButton = UIFactory.CreateButton(UIRoot, "EnumHelper", "▼");
|
||||
UIFactory.SetLayoutElement(enumHelperButton.Component.gameObject, minWidth: 25, minHeight: 25, flexibleWidth: 0, flexibleHeight: 0);
|
||||
enumHelperButton.OnClick += EnumHelper_OnClick;
|
||||
|
||||
var dropdownObj = UIFactory.CreateDropdown(UIRoot, out dropdown, "Select argument...", 14, (int val) =>
|
||||
{
|
||||
//ArgDropdownChanged(val);
|
||||
});
|
||||
UIFactory.SetLayoutElement(dropdownObj, minHeight: 25, flexibleHeight: 50, minWidth: 100, flexibleWidth: 1000);
|
||||
dropdownObj.AddComponent<ContentSizeFitter>().verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
|
||||
enumCompleter = new EnumCompleter(paramType, this.inputField);
|
||||
enumCompleter.Enabled = false;
|
||||
}
|
||||
|
||||
//private void ArgDropdownChanged(int value)
|
||||
//{
|
||||
// // not needed
|
||||
//}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user