UnityExplorer/src/UI/Widgets/InputFieldScroller.cs

126 lines
3.9 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine.Events;
2021-04-23 21:50:58 +10:00
using UnityExplorer.UI.Models;
2021-05-26 17:40:09 +10:00
namespace UnityExplorer.UI.Widgets
{
// To fix an issue with Input Fields and allow them to go inside a ScrollRect nicely.
2021-04-23 21:50:58 +10:00
public class InputFieldScroller : UIBehaviourModel
{
public override GameObject UIRoot
{
get
{
if (InputField.UIRoot)
return InputField.UIRoot;
return null;
}
}
public Action OnScroll;
2021-04-30 21:34:50 +10:00
internal AutoSliderScrollbar Slider;
internal InputFieldRef InputField;
2021-04-30 21:34:50 +10:00
internal RectTransform ContentRect;
internal RectTransform ViewportRect;
2021-04-30 21:34:50 +10:00
internal static CanvasScaler RootScaler;
public InputFieldScroller(AutoSliderScrollbar sliderScroller, InputFieldRef inputField)
{
2021-04-30 21:34:50 +10:00
this.Slider = sliderScroller;
this.InputField = inputField;
2021-01-22 21:56:00 +11:00
inputField.OnValueChanged += OnTextChanged;
ContentRect = inputField.UIRoot.GetComponent<RectTransform>();
2021-04-30 21:34:50 +10:00
ViewportRect = ContentRect.transform.parent.GetComponent<RectTransform>();
2021-04-30 21:34:50 +10:00
if (!RootScaler)
RootScaler = UIManager.CanvasRoot.GetComponent<CanvasScaler>();
}
internal string m_lastText;
internal bool m_updateWanted;
2021-04-30 21:34:50 +10:00
internal bool m_wantJumpToBottom;
private float m_desiredContentHeight;
private float lastContentPosition;
private float lastViewportHeight;
2021-04-23 21:50:58 +10:00
public override void Update()
{
if (this.ContentRect.localPosition.y != lastContentPosition)
{
lastContentPosition = ContentRect.localPosition.y;
OnScroll?.Invoke();
}
if (ViewportRect.rect.height != lastViewportHeight)
{
lastViewportHeight = ViewportRect.rect.height;
m_updateWanted = true;
2021-04-30 21:34:50 +10:00
}
if (m_updateWanted)
2021-04-30 21:34:50 +10:00
{
m_updateWanted = false;
ProcessInputText();
float desiredHeight = Math.Max(m_desiredContentHeight, ViewportRect.rect.height);
if (ContentRect.rect.height < desiredHeight)
{
ContentRect.sizeDelta = new Vector2(0, desiredHeight);
this.Slider.UpdateSliderHandle();
}
else if (ContentRect.rect.height > desiredHeight)
{
ContentRect.sizeDelta = new Vector2(0, desiredHeight);
this.Slider.UpdateSliderHandle();
}
}
2021-01-22 21:56:00 +11:00
2021-04-30 21:34:50 +10:00
if (m_wantJumpToBottom)
{
Slider.Slider.value = 1f;
m_wantJumpToBottom = false;
}
}
2021-01-22 21:56:00 +11:00
internal void OnTextChanged(string text)
{
m_lastText = text;
m_updateWanted = true;
}
2021-04-30 21:34:50 +10:00
internal void ProcessInputText()
{
var curInputRect = InputField.Component.textComponent.rectTransform.rect;
2021-04-30 21:34:50 +10:00
var scaleFactor = RootScaler.scaleFactor;
// Current text settings
var texGenSettings = InputField.Component.textComponent.GetGenerationSettings(curInputRect.size);
texGenSettings.generateOutOfBounds = false;
texGenSettings.scaleFactor = scaleFactor;
// Preferred text rect height
var textGen = InputField.Component.textComponent.cachedTextGeneratorForLayout;
2021-04-30 21:34:50 +10:00
m_desiredContentHeight = textGen.GetPreferredHeight(m_lastText, texGenSettings) + 10;
}
2021-04-23 21:50:58 +10:00
public override void ConstructUI(GameObject parent)
{
throw new NotImplementedException();
}
}
}