201 lines
6.0 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.EventSystems;
using UnityEngine.UI;
using UnityExplorer.Core.Input;
using UnityExplorer.Core.Runtime;
using UnityExplorer.UI;
using UnityExplorer.UI.Models;
namespace UnityExplorer.UI.Widgets.AutoComplete
{
public class AutoCompleter : UIPanel
2021-04-23 21:50:58 +10:00
{
// Static
2021-04-23 21:50:58 +10:00
public static AutoCompleter Instance => UIManager.AutoCompleter;
2021-04-23 21:50:58 +10:00
// Instance
2021-04-23 21:50:58 +10:00
public AutoCompleter()
{
OnPanelsReordered += UIPanel_OnPanelsReordered;
OnClickedOutsidePanels += AutoCompleter_OnClickedOutsidePanels;
}
2021-04-23 21:50:58 +10:00
private void AutoCompleter_OnClickedOutsidePanels()
{
if (!this.UIRoot || !this.UIRoot.activeInHierarchy)
return;
2021-04-23 21:50:58 +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 string Name => "AutoCompleter";
public override UIManager.Panels PanelType => UIManager.Panels.AutoCompleter;
public override bool CanDrag => false;
public ISuggestionProvider CurrentHandler { get; private set; }
public ButtonListSource<Suggestion> dataHandler;
public ScrollPool scrollPool;
private List<Suggestion> suggestions = new List<Suggestion>();
private int lastCaretPos;
public override void Update()
2021-04-23 21:50:58 +10:00
{
if (!UIRoot || !UIRoot.activeSelf)
return;
if (suggestions.Any() && CurrentHandler != null)
{
if (!CurrentHandler.InputField.gameObject.activeInHierarchy)
ReleaseOwnership(CurrentHandler);
else
{
lastCaretPos = CurrentHandler.InputField.caretPosition;
UpdatePosition();
}
}
}
public void TakeOwnership(ISuggestionProvider provider)
2021-04-23 21:50:58 +10:00
{
CurrentHandler = provider;
}
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);
}
}
private List<Suggestion> GetEntries() => suggestions;
2021-04-23 21:50:58 +10:00
private bool ShouldDisplay(Suggestion data, string filter) => true;
2021-04-23 21:50:58 +10:00
public void SetSuggestions(List<Suggestion> collection)
2021-04-23 21:50:58 +10:00
{
suggestions = collection;
if (!suggestions.Any())
UIRoot.SetActive(false);
else
{
UIRoot.SetActive(true);
UIRoot.transform.SetAsLastSibling();
2021-04-23 21:50:58 +10:00
dataHandler.RefreshData();
scrollPool.RefreshAndJumpToTop();
2021-04-23 21:50:58 +10:00
}
}
private void OnCellClicked(int dataIndex)
2021-04-23 21:50:58 +10:00
{
var suggestion = suggestions[dataIndex];
CurrentHandler.OnSuggestionClicked(suggestion);
}
private void SetCell(ButtonCell<Suggestion> cell, int index)
2021-04-23 21:50:58 +10:00
{
if (index < 0 || index >= suggestions.Count)
{
cell.Disable();
return;
}
var suggestion = suggestions[index];
cell.buttonText.text = suggestion.DisplayText;
}
private void UpdatePosition()
2021-04-23 21:50:58 +10:00
{
if (CurrentHandler == null || !CurrentHandler.InputField.isFocused)
return;
Vector3 pos;
2021-04-23 21:50:58 +10:00
var input = CurrentHandler.InputField;
2021-04-23 21:50:58 +10:00
var textGen = input.textComponent.cachedTextGenerator;
int caretPos = 0;
if (CurrentHandler.AnchorToCaretPosition)
{
caretPos = lastCaretPos--;
2021-04-23 21:50:58 +10:00
caretPos = Math.Max(0, caretPos);
caretPos = Math.Min(textGen.characterCount - 1, caretPos);
}
2021-04-23 21:50:58 +10:00
pos = textGen.characters[caretPos].cursorPos;
2021-04-23 21:50:58 +10:00
pos = input.transform.TransformPoint(pos);
uiRoot.transform.position = new Vector3(pos.x + 10, pos.y - 20, 0);
this.Dragger.OnEndResize();
2021-04-23 21:50:58 +10:00
}
public override void SetDefaultPosAndAnchors()
2021-04-23 21:50:58 +10:00
{
var mainRect = uiRoot.GetComponent<RectTransform>();
mainRect.pivot = new Vector2(0f, 1f);
mainRect.anchorMin = new Vector2(0, 1);
mainRect.anchorMax = new Vector2(0, 1);
mainRect.offsetMin = new Vector2(25, 0);
mainRect.offsetMax = new Vector2(525, 169);
}
2021-04-23 21:50:58 +10:00
public override void ConstructPanelContent()
{
2021-04-23 21:50:58 +10:00
dataHandler = new ButtonListSource<Suggestion>(scrollPool, GetEntries, SetCell, ShouldDisplay, OnCellClicked);
var prototypeCell = ButtonCell<Suggestion>.CreatePrototypeCell(this.content);
prototypeCell.GetComponentInChildren<Text>().supportRichText = true;
2021-04-23 21:50:58 +10:00
scrollPool = UIFactory.CreateScrollPool(this.content, "AutoCompleter", out GameObject scrollObj, out GameObject scrollContent);
scrollPool.Initialize(dataHandler, prototypeCell);
UIFactory.SetLayoutElement(scrollObj, flexibleHeight: 9999);
UIFactory.SetLayoutGroup<VerticalLayoutGroup>(scrollContent, true, false, true, false);
2021-04-23 21:50:58 +10:00
UIRoot.SetActive(false);
}
public override void SaveToConfigManager()
{
// not savable
}
public override void LoadSaveData()
{
// not savable
}
2021-04-23 21:50:58 +10:00
}
}