UnityExplorer/src/UI/Panels/AutoCompleteModal.cs

323 lines
11 KiB
C#
Raw Normal View History

2021-04-23 21:50:58 +10:00
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using UnityExplorer.UI.Panels;
2021-12-02 18:35:46 +11:00
using UniverseLib;
using UniverseLib.Input;
2021-12-02 18:35:46 +11:00
using UniverseLib.UI;
using UniverseLib.UI.Models;
2022-01-31 21:24:01 +11:00
using UniverseLib.UI.Widgets.ButtonList;
using UniverseLib.UI.Widgets.ScrollView;
using UniverseLib.Utility;
2021-04-23 21:50:58 +10:00
namespace UnityExplorer.UI.Widgets.AutoComplete
{
// 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.
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 => true;
2021-04-28 23:58:13 +10:00
public override bool ShouldSaveActiveState => false;
public override bool NavButtonWanted => false;
public static ISuggestionProvider CurrentHandler { get; private set; }
2021-04-28 23:58:13 +10:00
public static ButtonListHandler<Suggestion, ButtonCell> buttonListDataHandler;
public static ScrollPool<ButtonCell> scrollPool;
private static GameObject navigationTipRow;
2021-04-28 23:58:13 +10:00
2022-01-31 21:24:01 +11:00
private static List<Suggestion> Suggestions = new();
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
public AutoCompleteModal()
{
OnPanelsReordered += UIPanel_OnPanelsReordered;
OnClickedOutsidePanels += AutoCompleter_OnClickedOutsidePanels;
}
2021-04-23 21:50:58 +10:00
2022-04-12 00:17:06 +10:00
public static void TakeOwnership(ISuggestionProvider provider)
{
CurrentHandler = provider;
navigationTipRow.SetActive(provider.AllowNavigation);
}
public void ReleaseOwnership(ISuggestionProvider provider)
{
if (CurrentHandler == null)
return;
2021-04-23 21:50:58 +10:00
if (CurrentHandler == provider)
{
CurrentHandler = null;
UIRoot.SetActive(false);
}
}
public void SetSuggestions(IEnumerable<Suggestion> suggestions)
{
Suggestions = suggestions as List<Suggestion> ?? suggestions.ToList();
SelectedIndex = 0;
if (!Suggestions.Any())
base.UIRoot.SetActive(false);
else
{
base.UIRoot.SetActive(true);
base.UIRoot.transform.SetAsLastSibling();
buttonListDataHandler.RefreshData();
scrollPool.Refresh(true, true);
}
}
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
{
if (!Suggesting(handler))
return false;
2021-04-23 21:50:58 +10:00
bool up = InputManager.GetKey(KeyCode.UpArrow);
bool down = InputManager.GetKey(KeyCode.DownArrow);
if (up || down)
{
if (up)
2021-04-23 21:50:58 +10:00
{
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;
}
2021-04-23 21:50:58 +10:00
}
return true;
2021-04-23 21:50:58 +10:00
}
return !timeOfLastNavHold.OccuredEarlierThan(0.2f);
2021-04-23 21:50:58 +10:00
}
public static bool CheckEnter(ISuggestionProvider handler)
2021-04-23 21:50:58 +10:00
{
return Suggesting(handler) && InputManager.GetKeyDown(KeyCode.Return);
2021-04-23 21:50:58 +10:00
}
public static bool CheckEscape(ISuggestionProvider handler)
2021-04-23 21:50:58 +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;
SelectedIndex = index;
2022-04-12 00:17:06 +10:00
scrollPool.JumpToIndex(index, null);
2021-04-23 21:50:58 +10:00
}
// Internal update
2021-04-23 21:50:58 +10:00
public override void Update()
2021-04-23 21:50:58 +10:00
{
if (!UIRoot || !UIRoot.activeSelf)
return;
2021-04-23 21:50:58 +10:00
if (Suggestions.Any() && CurrentHandler != null)
2021-04-23 21:50:58 +10:00
{
if (!CurrentHandler.InputField.UIRoot.activeInHierarchy)
ReleaseOwnership(CurrentHandler);
else
UpdatePosition();
2021-04-23 21:50:58 +10:00
}
}
// Setting autocomplete cell buttons
2022-04-12 00:17:06 +10:00
private readonly Color selectedSuggestionColor = new(45 / 255f, 75 / 255f, 80 / 255f);
private readonly Color inactiveSuggestionColor = new(0.11f, 0.11f, 0.11f);
private List<Suggestion> GetEntries() => Suggestions;
private bool ShouldDisplay(Suggestion data, string filter) => true;
private void OnCellClicked(int dataIndex)
2021-04-23 21:50:58 +10:00
{
Suggestion suggestion = Suggestions[dataIndex];
2021-04-23 21:50:58 +10:00
CurrentHandler.OnSuggestionClicked(suggestion);
}
private bool setFirstCell;
private void SetCell(ButtonCell cell, int index)
2021-04-23 21:50:58 +10:00
{
if (index < 0 || index >= Suggestions.Count)
2021-04-23 21:50:58 +10:00
{
cell.Disable();
return;
}
Suggestion suggestion = Suggestions[index];
cell.Button.ButtonText.text = suggestion.DisplayText;
if (CurrentHandler.AllowNavigation && index == SelectedIndex && setFirstCell)
{
2022-01-31 21:24:01 +11:00
RuntimeHelper.SetColorBlock(cell.Button.Component, selectedSuggestionColor);
}
else
2022-01-31 21:24:01 +11:00
RuntimeHelper.SetColorBlock(cell.Button.Component, inactiveSuggestionColor);
setFirstCell = true;
2021-04-23 21:50:58 +10:00
}
// Updating panel position
private int lastCaretPosition;
private Vector3 lastInputPosition;
internal void UpdatePosition()
2021-04-23 21:50:58 +10:00
{
if (CurrentHandler == null)
2021-04-23 21:50:58 +10:00
return;
2022-04-12 00:17:06 +10:00
InputFieldRef input = CurrentHandler.InputField;
2022-04-12 00:17:06 +10:00
if (!input.Component.isFocused || input.Component.caretPosition == lastCaretPosition && input.UIRoot.transform.position == lastInputPosition)
return;
lastInputPosition = input.UIRoot.transform.position;
lastCaretPosition = input.Component.caretPosition;
if (CurrentHandler.AnchorToCaretPosition)
{
TextGenerator textGen = input.Component.cachedInputTextGenerator;
int caretIdx = Math.Max(0, Math.Min(textGen.characterCount - 1, input.Component.caretPosition));
2021-04-23 21:50:58 +10:00
// normalize the caret horizontal position
Vector3 caretPos = textGen.characters[caretIdx].cursorPos;
// transform to world point
caretPos = input.UIRoot.transform.TransformPoint(caretPos);
2022-01-31 21:24:01 +11:00
caretPos += new Vector3(input.Transform.rect.width * 0.5f, -(input.Transform.rect.height * 0.5f), 0);
2021-04-23 21:50:58 +10:00
uiRoot.transform.position = new Vector3(caretPos.x + 10, caretPos.y - 30, 0);
}
else
{
2022-01-31 21:24:01 +11:00
uiRoot.transform.position = input.Transform.position + new Vector3(-(input.Transform.rect.width / 2) + 10, -20, 0);
}
this.Dragger.OnEndResize();
2021-04-23 21:50:58 +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
2022-03-15 01:50:50 +11:00
const float MIN_X = 0.42f;
const float MAX_Y = 0.6f;
2021-05-05 21:27:09 +10:00
protected internal override void DoSetDefaultPosAndAnchors()
2021-04-23 21:50:58 +10:00
{
Rect.pivot = new Vector2(0f, 1f);
2022-03-15 01:50:50 +11:00
Rect.anchorMin = new Vector2(MIN_X, 0.4f);
Rect.anchorMax = new Vector2(0.68f, MAX_Y);
}
public override void OnFinishResize(RectTransform panel)
{
float xDiff = panel.anchorMin.x - MIN_X;
float yDiff = panel.anchorMax.y - MAX_Y;
if (xDiff != 0 || yDiff != 0)
{
panel.anchorMin = new(MIN_X, panel.anchorMin.y - yDiff);
panel.anchorMax = new(panel.anchorMax.x - xDiff, MAX_Y);
}
base.OnFinishResize(panel);
}
2021-04-23 21:50:58 +10:00
public override void ConstructPanelContent()
{
// hide the titlebar
this.TitleBar.gameObject.SetActive(false);
buttonListDataHandler = new ButtonListHandler<Suggestion, ButtonCell>(scrollPool, GetEntries, SetCell, ShouldDisplay, OnCellClicked);
2021-04-23 21:50:58 +10:00
scrollPool = UIFactory.CreateScrollPool<ButtonCell>(this.uiContent, "AutoCompleter", out GameObject scrollObj,
2021-05-16 21:46:38 +10:00
out GameObject scrollContent);
scrollPool.Initialize(buttonListDataHandler);
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
UIFactory.SetLayoutGroup<VerticalLayoutGroup>(scrollContent, true, false, true, false);
2021-04-23 21:50:58 +10:00
navigationTipRow = UIFactory.CreateHorizontalGroup(this.uiContent, "BottomRow", true, true, true, true, 0, new Vector4(2, 2, 2, 2));
UIFactory.SetLayoutElement(navigationTipRow, minHeight: 20, flexibleWidth: 9999);
2021-06-05 19:36:09 +10:00
UIFactory.CreateLabel(navigationTipRow, "HelpText", "Up/Down to select, Enter to use, Esc to close",
TextAnchor.MiddleLeft, Color.grey, false, 13);
2021-04-23 21:50:58 +10:00
UIRoot.SetActive(false);
}
}
}