More progress on C# Console - implement AutoCompletes, some cleanups

This commit is contained in:
Sinai
2021-05-11 19:15:46 +10:00
parent 712bf7b669
commit 6e9bb83099
10 changed files with 269 additions and 160 deletions

View File

@ -12,11 +12,11 @@ using UnityExplorer.UI.Panels;
namespace UnityExplorer.UI.Widgets.AutoComplete
{
public class AutoCompleter : UIPanel
public class AutoCompleteModal : UIPanel
{
// Static
public static AutoCompleter Instance => UIManager.AutoCompleter;
public static AutoCompleteModal Instance => UIManager.AutoCompleter;
// Instance
@ -36,9 +36,7 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
private List<Suggestion> suggestions = new List<Suggestion>();
private int lastCaretPos;
public AutoCompleter()
public AutoCompleteModal()
{
OnPanelsReordered += UIPanel_OnPanelsReordered;
OnClickedOutsidePanels += AutoCompleter_OnClickedOutsidePanels;
@ -80,7 +78,6 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
ReleaseOwnership(CurrentHandler);
else
{
lastCaretPos = CurrentHandler.InputField.InputField.caretPosition;
UpdatePosition();
}
}
@ -107,9 +104,9 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
private bool ShouldDisplay(Suggestion data, string filter) => true;
public void SetSuggestions(List<Suggestion> collection)
public void SetSuggestions(IEnumerable<Suggestion> collection)
{
suggestions = collection;
suggestions = collection as List<Suggestion> ?? collection.ToList();
if (!suggestions.Any())
UIRoot.SetActive(false);
@ -140,28 +137,40 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
cell.Button.ButtonText.text = suggestion.DisplayText;
}
private int lastCaretPosition;
private Vector3 lastInputPosition;
private void UpdatePosition()
{
if (CurrentHandler == null || !CurrentHandler.InputField.InputField.isFocused)
if (CurrentHandler == null || !CurrentHandler.InputField.Component.isFocused)
return;
Vector3 pos;
var input = CurrentHandler.InputField;
var textGen = input.InputField.textComponent.cachedTextGenerator;
int caretPos = 0;
if (input.Component.caretPosition == lastCaretPosition && input.UIRoot.transform.position == lastInputPosition)
return;
lastInputPosition = input.UIRoot.transform.position;
lastCaretPosition = input.Component.caretPosition;
if (CurrentHandler.AnchorToCaretPosition)
{
caretPos = lastCaretPos--;
var textGen = input.Component.textComponent.cachedTextGeneratorForLayout;
int caretIdx = Math.Max(0, Math.Min(textGen.characterCount - 1, input.Component.caretPosition));
caretPos = Math.Max(0, caretPos);
caretPos = Math.Min(textGen.characterCount - 1, caretPos);
// normalize the caret horizontal position
Vector3 caretPos = textGen.characters[caretIdx].cursorPos;
caretPos += new Vector3(input.Rect.rect.width * 0.5f, 0, 0);
// transform to world point
caretPos = input.UIRoot.transform.TransformPoint(caretPos);
uiRoot.transform.position = new Vector3(caretPos.x + 10, caretPos.y - 30, 0);
}
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);
}
pos = textGen.characters[caretPos].cursorPos;
pos = input.UIRoot.transform.TransformPoint(pos);
uiRoot.transform.position = new Vector3(pos.x + 10, pos.y - 20, 0);
this.Dragger.OnEndResize();
}

View File

@ -53,7 +53,7 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
SuggestionClicked?.Invoke(suggestion);
suggestions.Clear();
AutoCompleter.Instance.SetSuggestions(suggestions);
AutoCompleteModal.Instance.SetSuggestions(suggestions);
chosenSuggestion = suggestion.UnderlyingValue;
}
@ -62,14 +62,14 @@ namespace UnityExplorer.UI.Widgets.AutoComplete
if (string.IsNullOrEmpty(value) || value == chosenSuggestion)
{
chosenSuggestion = null;
AutoCompleter.Instance.ReleaseOwnership(this);
AutoCompleteModal.Instance.ReleaseOwnership(this);
}
else
{
GetSuggestions(value);
AutoCompleter.Instance.TakeOwnership(this);
AutoCompleter.Instance.SetSuggestions(suggestions);
AutoCompleteModal.Instance.TakeOwnership(this);
AutoCompleteModal.Instance.SetSuggestions(suggestions);
}
}

View File

@ -105,23 +105,23 @@ namespace UnityExplorer.UI.Utility
internal void ProcessInputText()
{
var curInputRect = InputField.InputField.textComponent.rectTransform.rect;
var curInputRect = InputField.Component.textComponent.rectTransform.rect;
var scaleFactor = RootScaler.scaleFactor;
// Current text settings
var texGenSettings = InputField.InputField.textComponent.GetGenerationSettings(curInputRect.size);
var texGenSettings = InputField.Component.textComponent.GetGenerationSettings(curInputRect.size);
texGenSettings.generateOutOfBounds = false;
texGenSettings.scaleFactor = scaleFactor;
// Preferred text rect height
var textGen = InputField.InputField.textComponent.cachedTextGeneratorForLayout;
var textGen = InputField.Component.textComponent.cachedTextGeneratorForLayout;
m_desiredContentHeight = textGen.GetPreferredHeight(m_lastText, texGenSettings) + 10;
// TODO more intelligent jump.
// We can detect if the caret is outside the viewport area.
// jump to bottom
if (InputField.InputField.caretPosition == InputField.Text.Length
if (InputField.Component.caretPosition == InputField.Text.Length
&& InputField.Text.Length > 0
&& InputField.Text[InputField.Text.Length - 1] == '\n')
{