Files
A-Fairytale-Gone-Bad-Briar-…/Assets/Scripts/UI/HUD/InteractTextUI.cs

166 lines
4.8 KiB
C#

using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BriarQueen.UI.HUD
{
[RequireComponent(typeof(RectTransform))]
[RequireComponent(typeof(LayoutElement))]
public class InteractTextUI : MonoBehaviour
{
[Header("UI Elements")]
[SerializeField]
private TextMeshProUGUI _text;
[SerializeField]
private Image _background;
[Header("Sizing")]
[SerializeField]
private Vector2 _backgroundPadding = new(20f, 10f);
[SerializeField]
private Vector2 _minBackgroundSize = new(80f, 36f);
[SerializeField]
private Vector2 _maxBackgroundSize = new(1400f, 240f);
private RectTransform _backgroundRect;
private string _lastText = string.Empty;
private LayoutElement _rootLayoutElement;
private RectTransform _rootRect;
private RectTransform _textRect;
public string Text
{
get => _text != null ? _text.text : string.Empty;
set
{
if (_text == null)
return;
var safeValue = value ?? string.Empty;
if (_text.text == safeValue)
return;
_text.text = safeValue;
RefreshLayout();
}
}
private void Awake()
{
CacheRefs();
RefreshLayout();
}
private void LateUpdate()
{
if (_text == null)
return;
if (_lastText != _text.text)
RefreshLayout();
}
#if UNITY_EDITOR
private void OnValidate()
{
CacheRefs();
if (!Application.isPlaying)
RefreshLayout();
}
#endif
private void CacheRefs()
{
_rootRect = transform as RectTransform;
_rootLayoutElement = GetComponent<LayoutElement>();
if (_text != null)
_textRect = _text.rectTransform;
if (_background != null)
_backgroundRect = _background.rectTransform;
}
public void SetText(string value)
{
Text = value;
}
public void ClearText()
{
Text = string.Empty;
}
public void RefreshLayout()
{
if (_rootRect == null || _rootLayoutElement == null || _textRect == null || _backgroundRect == null ||
_text == null || _background == null)
return;
_lastText = _text.text ?? string.Empty;
var hasText = !string.IsNullOrWhiteSpace(_lastText);
_text.enabled = hasText;
_background.enabled = hasText;
if (!hasText)
return;
_backgroundRect.anchorMin = Vector2.zero;
_backgroundRect.anchorMax = Vector2.one;
_backgroundRect.pivot = new Vector2(0.5f, 0.5f);
_backgroundRect.offsetMin = Vector2.zero;
_backgroundRect.offsetMax = Vector2.zero;
_backgroundRect.anchoredPosition = Vector2.zero;
_background.transform.SetAsFirstSibling();
_textRect.anchorMin = new Vector2(0.5f, 0.5f);
_textRect.anchorMax = new Vector2(0.5f, 0.5f);
_textRect.pivot = new Vector2(0.5f, 0.5f);
_textRect.anchoredPosition = Vector2.zero;
_text.textWrappingMode = TextWrappingModes.NoWrap;
_text.overflowMode = TextOverflowModes.Overflow;
_text.enableAutoSizing = false;
Canvas.ForceUpdateCanvases();
_text.ForceMeshUpdate();
var preferred = _text.GetPreferredValues(_lastText);
var textWidth = preferred.x;
var textHeight = preferred.y;
_textRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, textWidth);
_textRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, textHeight);
var rootWidth = Mathf.Clamp(
textWidth + _backgroundPadding.x * 2f,
_minBackgroundSize.x,
_maxBackgroundSize.x);
var rootHeight = Mathf.Clamp(
textHeight + _backgroundPadding.y * 2f,
_minBackgroundSize.y,
_maxBackgroundSize.y);
_rootRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, rootWidth);
_rootRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, rootHeight);
_rootLayoutElement.minWidth = rootWidth;
_rootLayoutElement.preferredWidth = rootWidth;
_rootLayoutElement.flexibleWidth = 0f;
_rootLayoutElement.minHeight = rootHeight;
_rootLayoutElement.preferredHeight = rootHeight;
_rootLayoutElement.flexibleHeight = 0f;
LayoutRebuilder.ForceRebuildLayoutImmediate(_rootRect);
}
}
}