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-11 19:15:46 +10:00
|
|
|
|
public static AutoCompleteModal Instance => UIManager.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;
|
|
|
|
|
|
|
|
|
|
public ISuggestionProvider CurrentHandler { get; private set; }
|
|
|
|
|
|
|
|
|
|
public ButtonListSource<Suggestion> dataHandler;
|
|
|
|
|
public ScrollPool<ButtonCell> scrollPool;
|
|
|
|
|
|
|
|
|
|
private List<Suggestion> suggestions = new List<Suggestion>();
|
|
|
|
|
|
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-04-24 04:03:33 +10:00
|
|
|
|
private void AutoCompleter_OnClickedOutsidePanels()
|
|
|
|
|
{
|
|
|
|
|
if (!this.UIRoot || !this.UIRoot.activeInHierarchy)
|
|
|
|
|
return;
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
2021-04-24 04:03:33 +10:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Update()
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
|
|
|
|
if (!UIRoot || !UIRoot.activeSelf)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (suggestions.Any() && CurrentHandler != null)
|
|
|
|
|
{
|
2021-05-07 17:06:56 +10:00
|
|
|
|
if (!CurrentHandler.InputField.UIRoot.activeInHierarchy)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
ReleaseOwnership(CurrentHandler);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UpdatePosition();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 04:03:33 +10:00
|
|
|
|
public void TakeOwnership(ISuggestionProvider provider)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
|
|
|
|
CurrentHandler = provider;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 04:03:33 +10:00
|
|
|
|
public void ReleaseOwnership(ISuggestionProvider provider)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
|
|
|
|
if (CurrentHandler == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (CurrentHandler == provider)
|
|
|
|
|
{
|
|
|
|
|
CurrentHandler = null;
|
|
|
|
|
UIRoot.SetActive(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 04:03:33 +10:00
|
|
|
|
private List<Suggestion> GetEntries() => suggestions;
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
2021-04-24 04:03:33 +10:00
|
|
|
|
private bool ShouldDisplay(Suggestion data, string filter) => true;
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
2021-05-11 19:15:46 +10:00
|
|
|
|
public void SetSuggestions(IEnumerable<Suggestion> collection)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
2021-05-11 19:15:46 +10:00
|
|
|
|
suggestions = collection as List<Suggestion> ?? collection.ToList();
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
|
|
|
|
if (!suggestions.Any())
|
|
|
|
|
UIRoot.SetActive(false);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
UIRoot.SetActive(true);
|
2021-04-24 04:03:33 +10:00
|
|
|
|
UIRoot.transform.SetAsLastSibling();
|
2021-04-23 21:50:58 +10:00
|
|
|
|
dataHandler.RefreshData();
|
2021-04-30 23:12:18 +10:00
|
|
|
|
scrollPool.Refresh(true, true);
|
2021-04-23 21:50:58 +10:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-24 04:03:33 +10:00
|
|
|
|
private void OnCellClicked(int dataIndex)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
|
|
|
|
var suggestion = suggestions[dataIndex];
|
|
|
|
|
CurrentHandler.OnSuggestionClicked(suggestion);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 19:56:21 +10:00
|
|
|
|
private void SetCell(ButtonCell cell, int index)
|
2021-04-23 21:50:58 +10:00
|
|
|
|
{
|
|
|
|
|
if (index < 0 || index >= suggestions.Count)
|
|
|
|
|
{
|
|
|
|
|
cell.Disable();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var suggestion = suggestions[index];
|
2021-04-26 19:56:21 +10:00
|
|
|
|
cell.Button.ButtonText.text = suggestion.DisplayText;
|
2021-04-23 21:50:58 +10:00
|
|
|
|
}
|
|
|
|
|
|
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-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
|
|
|
|
|
|
|
|
|
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-05 21:27:09 +10:00
|
|
|
|
public override string GetSaveData() => null;
|
2021-04-23 21:50:58 +10:00
|
|
|
|
}
|
|
|
|
|
}
|