2021-04-23 21:50:58 +10:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
using UnityExplorer.Core.Input;
|
|
|
|
|
using UnityExplorer.Core.Runtime;
|
|
|
|
|
using UnityExplorer.UI;
|
2021-04-26 19:56:21 +10:00
|
|
|
|
using UnityExplorer.UI.ObjectPool;
|
2021-04-27 21:22:48 +10:00
|
|
|
|
using UnityExplorer.UI.Panels;
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
|
|
|
|
namespace UnityExplorer.UI.Widgets.AutoComplete
|
|
|
|
|
{
|
2021-05-12 20:48:56 +10:00
|
|
|
|
// Shared modal panel for "AutoComplete" suggestions.
|
|
|
|
|
// A data source implements ISuggestionProvider and uses TakeOwnership and ReleaseOwnership
|
|
|
|
|
// for control, and SetSuggestions to set the actual suggestion data.
|
|
|
|
|
|
2021-05-11 19:15:46 +10:00
|
|
|
|
public class AutoCompleteModal : UIPanel
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
2021-05-13 23:03:30 +10:00
|
|
|
|
public static AutoCompleteModal Instance => UIManager.GetPanel<AutoCompleteModal>(UIManager.Panels.AutoCompleter);
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
2021-04-28 23:58:13 +10:00
|
|
|
|
public override string Name => "AutoCompleter";
|
|
|
|
|
public override UIManager.Panels PanelType => UIManager.Panels.AutoCompleter;
|
2021-05-05 21:27:09 +10:00
|
|
|
|
public override int MinWidth => -1;
|
|
|
|
|
public override int MinHeight => -1;
|
2021-04-28 23:58:13 +10:00
|
|
|
|
|
|
|
|
|
public override bool CanDragAndResize => false;
|
|
|
|
|
public override bool ShouldSaveActiveState => false;
|
|
|
|
|
public override bool NavButtonWanted => false;
|
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
public static ISuggestionProvider CurrentHandler { get; private set; }
|
2021-04-28 23:58:13 +10:00
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
public static ButtonListSource<Suggestion> dataHandler;
|
|
|
|
|
public static ScrollPool<ButtonCell> scrollPool;
|
2021-04-28 23:58:13 +10:00
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
private static List<Suggestion> Suggestions = new List<Suggestion>();
|
|
|
|
|
private static int SelectedIndex = 0;
|
|
|
|
|
|
|
|
|
|
public static Suggestion SelectedSuggestion => Suggestions[SelectedIndex];
|
|
|
|
|
|
|
|
|
|
public static bool Suggesting(ISuggestionProvider handler) => CurrentHandler == handler && Instance.UIRoot.activeSelf;
|
2021-04-28 23:58:13 +10:00
|
|
|
|
|
2021-05-11 19:15:46 +10:00
|
|
|
|
public AutoCompleteModal()
|
2021-04-24 04:03:33 +10:00
|
|
|
|
{
|
|
|
|
|
OnPanelsReordered += UIPanel_OnPanelsReordered;
|
|
|
|
|
OnClickedOutsidePanels += AutoCompleter_OnClickedOutsidePanels;
|
|
|
|
|
}
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
public void TakeOwnership(ISuggestionProvider provider)
|
2021-04-24 04:03:33 +10:00
|
|
|
|
{
|
2021-05-15 01:21:07 +10:00
|
|
|
|
CurrentHandler = provider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ReleaseOwnership(ISuggestionProvider provider)
|
|
|
|
|
{
|
|
|
|
|
if (CurrentHandler == null)
|
2021-04-24 04:03:33 +10:00
|
|
|
|
return;
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
if (CurrentHandler == provider)
|
|
|
|
|
{
|
|
|
|
|
CurrentHandler = null;
|
2021-04-24 04:03:33 +10:00
|
|
|
|
UIRoot.SetActive(false);
|
2021-05-15 01:21:07 +10:00
|
|
|
|
}
|
2021-04-24 04:03:33 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
public void SetSuggestions(IEnumerable<Suggestion> suggestions)
|
2021-04-24 04:03:33 +10:00
|
|
|
|
{
|
2021-05-15 01:21:07 +10:00
|
|
|
|
Suggestions = suggestions as List<Suggestion> ?? suggestions.ToList();
|
|
|
|
|
SelectedIndex = 0;
|
2021-04-24 04:03:33 +10:00
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
if (!Suggestions.Any())
|
|
|
|
|
base.UIRoot.SetActive(false);
|
|
|
|
|
else
|
2021-04-24 04:03:33 +10:00
|
|
|
|
{
|
2021-05-15 01:21:07 +10:00
|
|
|
|
base.UIRoot.SetActive(true);
|
|
|
|
|
base.UIRoot.transform.SetAsLastSibling();
|
|
|
|
|
dataHandler.RefreshData();
|
|
|
|
|
scrollPool.Refresh(true, true);
|
2021-04-24 04:03:33 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
private static float timeOfLastNavHold = -1f;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if the AutoCompleteModal used the navigation input, false if not.
|
|
|
|
|
/// The navigation inputs are Control+Up/Down, and Control+Enter.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool CheckNavigation(ISuggestionProvider handler)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
2021-05-15 01:21:07 +10:00
|
|
|
|
if (!Suggesting(handler))
|
|
|
|
|
return false;
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
if (InputManager.GetKey(KeyCode.LeftControl) || InputManager.GetKey(KeyCode.RightControl))
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
2021-05-15 01:21:07 +10:00
|
|
|
|
bool up = InputManager.GetKey(KeyCode.UpArrow);
|
|
|
|
|
bool down = InputManager.GetKey(KeyCode.DownArrow);
|
|
|
|
|
|
|
|
|
|
if (up || down)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
2021-05-15 01:21:07 +10:00
|
|
|
|
if (up)
|
|
|
|
|
{
|
|
|
|
|
if (InputManager.GetKeyDown(KeyCode.UpArrow))
|
|
|
|
|
{
|
|
|
|
|
SetSelectedSuggestion(SelectedIndex - 1);
|
|
|
|
|
timeOfLastNavHold = Time.realtimeSinceStartup + 0.3f;
|
|
|
|
|
}
|
|
|
|
|
else if (timeOfLastNavHold.OccuredEarlierThan(0.05f))
|
|
|
|
|
{
|
|
|
|
|
SetSelectedSuggestion(SelectedIndex - 1);
|
|
|
|
|
timeOfLastNavHold = Time.realtimeSinceStartup;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (InputManager.GetKeyDown(KeyCode.DownArrow))
|
|
|
|
|
{
|
|
|
|
|
SetSelectedSuggestion(SelectedIndex + 1);
|
|
|
|
|
timeOfLastNavHold = Time.realtimeSinceStartup + 0.3f;
|
|
|
|
|
}
|
|
|
|
|
else if (timeOfLastNavHold.OccuredEarlierThan(0.05f))
|
|
|
|
|
{
|
|
|
|
|
SetSelectedSuggestion(SelectedIndex + 1);
|
|
|
|
|
timeOfLastNavHold = Time.realtimeSinceStartup;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2021-04-23 21:50:58 +10:00
|
|
|
|
}
|
2021-05-15 01:21:07 +10:00
|
|
|
|
|
|
|
|
|
return false;
|
2021-04-23 21:50:58 +10:00
|
|
|
|
}
|
2021-05-15 01:21:07 +10:00
|
|
|
|
|
|
|
|
|
return !timeOfLastNavHold.OccuredEarlierThan(0.2f);
|
2021-04-23 21:50:58 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
public static bool CheckEnter(ISuggestionProvider handler)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
2021-05-15 01:21:07 +10:00
|
|
|
|
return Suggesting(handler) && InputManager.GetKeyDown(KeyCode.Return);
|
2021-04-23 21:50:58 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
public static bool CheckEscape(ISuggestionProvider handler)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
2021-05-15 01:21:07 +10:00
|
|
|
|
return Suggesting(handler) && InputManager.GetKeyDown(KeyCode.Escape);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void SetSelectedSuggestion(int index)
|
|
|
|
|
{
|
|
|
|
|
if (index < 0 || index >= Suggestions.Count)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
return;
|
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
SelectedIndex = index;
|
|
|
|
|
scrollPool.Refresh(true, false);
|
2021-04-23 21:50:58 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
// Internal update
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
public override void Update()
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
2021-05-15 01:21:07 +10:00
|
|
|
|
if (!UIRoot || !UIRoot.activeSelf)
|
|
|
|
|
return;
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
if (Suggestions.Any() && CurrentHandler != null)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
2021-05-15 01:21:07 +10:00
|
|
|
|
if (!CurrentHandler.InputField.UIRoot.activeInHierarchy)
|
|
|
|
|
ReleaseOwnership(CurrentHandler);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UpdatePosition();
|
|
|
|
|
}
|
2021-04-23 21:50:58 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
// Setting autocomplete cell buttons
|
|
|
|
|
|
|
|
|
|
private readonly Color selectedSuggestionColor = new Color(46/255f, 54/255f, 53/255f);
|
|
|
|
|
private readonly Color inactiveSuggestionColor = new Color(0.11f, 0.11f, 0.11f);
|
|
|
|
|
|
|
|
|
|
private List<Suggestion> GetEntries() => Suggestions;
|
|
|
|
|
|
|
|
|
|
private bool ShouldDisplay(Suggestion data, string filter) => true;
|
|
|
|
|
|
2021-04-24 04:03:33 +10:00
|
|
|
|
private void OnCellClicked(int dataIndex)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
2021-05-15 01:21:07 +10:00
|
|
|
|
var suggestion = Suggestions[dataIndex];
|
2021-04-23 21:50:58 +10:00
|
|
|
|
CurrentHandler.OnSuggestionClicked(suggestion);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
private bool setFirstCell;
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
private void SetCell(ButtonCell cell, int index)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
2021-05-15 01:21:07 +10:00
|
|
|
|
if (index < 0 || index >= Suggestions.Count)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
|
|
|
|
cell.Disable();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
var suggestion = Suggestions[index];
|
2021-04-26 19:56:21 +10:00
|
|
|
|
cell.Button.ButtonText.text = suggestion.DisplayText;
|
2021-05-15 01:21:07 +10:00
|
|
|
|
|
|
|
|
|
if (index == SelectedIndex && setFirstCell)
|
|
|
|
|
{
|
|
|
|
|
if (cell.Rect.MinY() > scrollPool.Viewport.MinY())
|
|
|
|
|
{
|
|
|
|
|
// cell is too far down
|
|
|
|
|
var diff = cell.Rect.MinY() - scrollPool.Viewport.MinY();
|
|
|
|
|
var pos = scrollPool.Content.anchoredPosition;
|
|
|
|
|
pos.y -= diff;
|
|
|
|
|
scrollPool.Content.anchoredPosition = pos;
|
|
|
|
|
}
|
|
|
|
|
else if (cell.Rect.MaxY() < scrollPool.Viewport.MaxY())
|
|
|
|
|
{
|
|
|
|
|
// cell is too far up
|
|
|
|
|
var diff = cell.Rect.MaxY() - scrollPool.Viewport.MaxY();
|
|
|
|
|
var pos = scrollPool.Content.anchoredPosition;
|
|
|
|
|
pos.y -= diff;
|
|
|
|
|
scrollPool.Content.anchoredPosition = pos;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RuntimeProvider.Instance.SetColorBlock(cell.Button.Component, selectedSuggestionColor);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
RuntimeProvider.Instance.SetColorBlock(cell.Button.Component, inactiveSuggestionColor);
|
|
|
|
|
|
|
|
|
|
setFirstCell = true;
|
2021-04-23 21:50:58 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
// Updating panel position
|
|
|
|
|
|
2021-05-11 19:15:46 +10:00
|
|
|
|
private int lastCaretPosition;
|
|
|
|
|
private Vector3 lastInputPosition;
|
|
|
|
|
|
2021-04-24 04:03:33 +10:00
|
|
|
|
private void UpdatePosition()
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
2021-05-11 19:15:46 +10:00
|
|
|
|
if (CurrentHandler == null || !CurrentHandler.InputField.Component.isFocused)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var input = CurrentHandler.InputField;
|
2021-04-24 04:03:33 +10:00
|
|
|
|
|
2021-05-11 19:15:46 +10:00
|
|
|
|
if (input.Component.caretPosition == lastCaretPosition && input.UIRoot.transform.position == lastInputPosition)
|
|
|
|
|
return;
|
|
|
|
|
lastInputPosition = input.UIRoot.transform.position;
|
|
|
|
|
lastCaretPosition = input.Component.caretPosition;
|
|
|
|
|
|
2021-04-24 04:03:33 +10:00
|
|
|
|
if (CurrentHandler.AnchorToCaretPosition)
|
|
|
|
|
{
|
2021-05-12 20:48:56 +10:00
|
|
|
|
var textGen = input.Component.cachedInputTextGenerator;
|
2021-05-11 19:15:46 +10:00
|
|
|
|
int caretIdx = Math.Max(0, Math.Min(textGen.characterCount - 1, input.Component.caretPosition));
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
2021-05-11 19:15:46 +10:00
|
|
|
|
// normalize the caret horizontal position
|
|
|
|
|
Vector3 caretPos = textGen.characters[caretIdx].cursorPos;
|
|
|
|
|
// transform to world point
|
|
|
|
|
caretPos = input.UIRoot.transform.TransformPoint(caretPos);
|
2021-05-12 20:48:56 +10:00
|
|
|
|
caretPos += new Vector3(input.Rect.rect.width * 0.5f, -(input.Rect.rect.height * 0.5f), 0);
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
2021-05-11 19:15:46 +10:00
|
|
|
|
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);
|
|
|
|
|
}
|
2021-04-24 04:03:33 +10:00
|
|
|
|
|
|
|
|
|
this.Dragger.OnEndResize();
|
2021-04-23 21:50:58 +10:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
// Event listeners for panel
|
|
|
|
|
|
|
|
|
|
private void AutoCompleter_OnClickedOutsidePanels()
|
|
|
|
|
{
|
|
|
|
|
if (!this.UIRoot || !this.UIRoot.activeInHierarchy)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (CurrentHandler != null)
|
|
|
|
|
ReleaseOwnership(CurrentHandler);
|
|
|
|
|
else
|
|
|
|
|
UIRoot.SetActive(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UIPanel_OnPanelsReordered()
|
|
|
|
|
{
|
|
|
|
|
if (!this.UIRoot || !this.UIRoot.activeInHierarchy)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (this.UIRoot.transform.GetSiblingIndex() != UIManager.PanelHolder.transform.childCount - 1)
|
|
|
|
|
{
|
|
|
|
|
if (CurrentHandler != null)
|
|
|
|
|
ReleaseOwnership(CurrentHandler);
|
|
|
|
|
else
|
|
|
|
|
UIRoot.SetActive(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UI Construction
|
|
|
|
|
|
2021-05-05 21:27:09 +10:00
|
|
|
|
protected internal override void DoSetDefaultPosAndAnchors()
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
2021-04-24 04:03:33 +10:00
|
|
|
|
var mainRect = uiRoot.GetComponent<RectTransform>();
|
|
|
|
|
mainRect.pivot = new Vector2(0f, 1f);
|
2021-04-25 00:21:12 +10:00
|
|
|
|
mainRect.anchorMin = new Vector2(0.42f, 0.4f);
|
|
|
|
|
mainRect.anchorMax = new Vector2(0.68f, 0.6f);
|
2021-04-24 04:03:33 +10:00
|
|
|
|
}
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
2021-04-24 04:03:33 +10:00
|
|
|
|
public override void ConstructPanelContent()
|
|
|
|
|
{
|
2021-04-23 21:50:58 +10:00
|
|
|
|
dataHandler = new ButtonListSource<Suggestion>(scrollPool, GetEntries, SetCell, ShouldDisplay, OnCellClicked);
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
scrollPool = UIFactory.CreateScrollPool<ButtonCell>(this.content, "AutoCompleter", out GameObject scrollObj, out GameObject scrollContent);
|
|
|
|
|
scrollPool.Initialize(dataHandler);
|
2021-04-24 04:03:33 +10:00
|
|
|
|
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
|
|
|
|
|
UIFactory.SetLayoutGroup<VerticalLayoutGroup>(scrollContent, true, false, true, false);
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
2021-05-15 01:21:07 +10:00
|
|
|
|
var bottomRow = UIFactory.CreateHorizontalGroup(this.content, "BottomRow", true, true, true, true, 0, new Vector4(2, 2, 2, 2));
|
|
|
|
|
UIFactory.SetLayoutElement(bottomRow, minHeight: 20, flexibleWidth: 9999);
|
|
|
|
|
UIFactory.CreateLabel(bottomRow, "HelpText", "Control+Up/Down to select, Enter to use, Esc to hide",
|
|
|
|
|
TextAnchor.MiddleLeft, Color.grey, false, 13);
|
|
|
|
|
|
2021-04-23 21:50:58 +10:00
|
|
|
|
UIRoot.SetActive(false);
|
|
|
|
|
}
|
2021-04-24 04:03:33 +10:00
|
|
|
|
|
2021-04-29 21:01:08 +10:00
|
|
|
|
public override void DoSaveToConfigElement()
|
2021-04-24 04:03:33 +10:00
|
|
|
|
{
|
|
|
|
|
// not savable
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-13 23:03:30 +10:00
|
|
|
|
public override string GetSaveDataFromConfigManager() => null;
|
2021-04-23 21:50:58 +10:00
|
|
|
|
}
|
|
|
|
|
}
|