2020-11-11 20:16:43 +11:00
|
|
|
|
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;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
2021-05-26 17:40:09 +10:00
|
|
|
|
namespace UnityExplorer.UI.Widgets
|
2020-11-11 20:16:43 +11:00
|
|
|
|
{
|
|
|
|
|
// 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
|
2020-11-11 20:16:43 +11:00
|
|
|
|
{
|
2021-04-25 00:21:12 +10:00
|
|
|
|
public override GameObject UIRoot
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2021-05-07 17:06:56 +10:00
|
|
|
|
if (InputField.UIRoot)
|
|
|
|
|
return InputField.UIRoot;
|
2021-04-25 00:21:12 +10:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-30 19:50:04 +11:00
|
|
|
|
|
2021-05-10 21:07:27 +10:00
|
|
|
|
public Action OnScroll;
|
|
|
|
|
|
2021-04-30 21:34:50 +10:00
|
|
|
|
internal AutoSliderScrollbar Slider;
|
2021-05-07 17:06:56 +10:00
|
|
|
|
internal InputFieldRef InputField;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
2021-04-30 21:34:50 +10:00
|
|
|
|
internal RectTransform ContentRect;
|
|
|
|
|
internal RectTransform ViewportRect;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
2021-04-30 21:34:50 +10:00
|
|
|
|
internal static CanvasScaler RootScaler;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
2021-05-07 17:06:56 +10:00
|
|
|
|
public InputFieldScroller(AutoSliderScrollbar sliderScroller, InputFieldRef inputField)
|
2020-11-11 20:16:43 +11:00
|
|
|
|
{
|
2021-04-30 21:34:50 +10:00
|
|
|
|
this.Slider = sliderScroller;
|
|
|
|
|
this.InputField = inputField;
|
2021-01-22 21:56:00 +11:00
|
|
|
|
|
2021-05-07 17:06:56 +10:00
|
|
|
|
inputField.OnValueChanged += OnTextChanged;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
2021-05-07 17:06:56 +10:00
|
|
|
|
ContentRect = inputField.UIRoot.GetComponent<RectTransform>();
|
2021-04-30 21:34:50 +10:00
|
|
|
|
ViewportRect = ContentRect.transform.parent.GetComponent<RectTransform>();
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
2021-04-30 21:34:50 +10:00
|
|
|
|
if (!RootScaler)
|
|
|
|
|
RootScaler = UIManager.CanvasRoot.GetComponent<CanvasScaler>();
|
2020-11-11 20:16:43 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal string m_lastText;
|
|
|
|
|
internal bool m_updateWanted;
|
2021-04-30 21:34:50 +10:00
|
|
|
|
internal bool m_wantJumpToBottom;
|
|
|
|
|
private float m_desiredContentHeight;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
2021-05-10 21:07:27 +10:00
|
|
|
|
private float lastContentPosition;
|
|
|
|
|
private float lastViewportHeight;
|
|
|
|
|
|
2021-04-23 21:50:58 +10:00
|
|
|
|
public override void Update()
|
2020-11-11 20:16:43 +11:00
|
|
|
|
{
|
2021-05-10 21:07:27 +10:00
|
|
|
|
if (this.ContentRect.localPosition.y != lastContentPosition)
|
2020-11-11 20:16:43 +11:00
|
|
|
|
{
|
2021-05-10 21:07:27 +10:00
|
|
|
|
lastContentPosition = ContentRect.localPosition.y;
|
|
|
|
|
OnScroll?.Invoke();
|
2020-11-11 20:16:43 +11:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 21:07:27 +10:00
|
|
|
|
if (ViewportRect.rect.height != lastViewportHeight)
|
2020-11-11 20:16:43 +11:00
|
|
|
|
{
|
2021-05-10 21:07:27 +10:00
|
|
|
|
lastViewportHeight = ViewportRect.rect.height;
|
|
|
|
|
m_updateWanted = true;
|
2021-04-30 21:34:50 +10:00
|
|
|
|
}
|
2021-05-10 21:07:27 +10:00
|
|
|
|
|
|
|
|
|
if (m_updateWanted)
|
2021-04-30 21:34:50 +10:00
|
|
|
|
{
|
2021-05-10 21:07:27 +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();
|
|
|
|
|
}
|
2020-11-11 20:16:43 +11:00
|
|
|
|
}
|
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
|
|
|
|
|
2020-11-11 20:16:43 +11:00
|
|
|
|
internal void OnTextChanged(string text)
|
|
|
|
|
{
|
|
|
|
|
m_lastText = text;
|
|
|
|
|
m_updateWanted = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-30 21:34:50 +10:00
|
|
|
|
internal void ProcessInputText()
|
2020-11-11 20:16:43 +11:00
|
|
|
|
{
|
2021-05-11 19:15:46 +10:00
|
|
|
|
var curInputRect = InputField.Component.textComponent.rectTransform.rect;
|
2021-04-30 21:34:50 +10:00
|
|
|
|
var scaleFactor = RootScaler.scaleFactor;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
|
|
|
|
|
// Current text settings
|
2021-05-11 19:15:46 +10:00
|
|
|
|
var texGenSettings = InputField.Component.textComponent.GetGenerationSettings(curInputRect.size);
|
2020-11-11 20:16:43 +11:00
|
|
|
|
texGenSettings.generateOutOfBounds = false;
|
|
|
|
|
texGenSettings.scaleFactor = scaleFactor;
|
|
|
|
|
|
|
|
|
|
// Preferred text rect height
|
2021-05-11 19:15:46 +10:00
|
|
|
|
var textGen = InputField.Component.textComponent.cachedTextGeneratorForLayout;
|
2021-04-30 21:34:50 +10:00
|
|
|
|
m_desiredContentHeight = textGen.GetPreferredHeight(m_lastText, texGenSettings) + 10;
|
2020-11-11 20:16:43 +11:00
|
|
|
|
}
|
2021-04-23 21:50:58 +10:00
|
|
|
|
|
|
|
|
|
public override void ConstructUI(GameObject parent)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2020-11-11 20:16:43 +11:00
|
|
|
|
}
|
|
|
|
|
}
|