60 lines
2.0 KiB
C#
60 lines
2.0 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
|
|
namespace BrewMonster.Scripts.ChatUI
|
|
{
|
|
/// <summary>
|
|
/// Controls typing preview visibility/content and keyboard-anchored position.
|
|
/// Kept standalone so other chat/input UIs can reuse the same behavior.
|
|
/// </summary>
|
|
[System.Serializable]
|
|
public class TypingPreviewController
|
|
{
|
|
[Tooltip("Root của box xem trước nội dung đang gõ.")]
|
|
[SerializeField] private GameObject typingPreviewRoot;
|
|
[Tooltip("Text hiển thị nội dung đang gõ realtime.")]
|
|
[SerializeField] private TextMeshProUGUI typingPreviewText;
|
|
[Tooltip("Rect của preview box để neo theo keyboard.")]
|
|
[SerializeField] private RectTransform typingPreviewRect;
|
|
[Tooltip("Khoảng cách dọc cộng thêm cho preview box.")]
|
|
[SerializeField] private float previewVerticalOffset = 8f;
|
|
|
|
private Vector2 _baseAnchoredPos;
|
|
private bool _cachedAnchor;
|
|
|
|
public void CacheInitialAnchor()
|
|
{
|
|
if (_cachedAnchor)
|
|
return;
|
|
|
|
if (typingPreviewRect != null)
|
|
_baseAnchoredPos = typingPreviewRect.anchoredPosition;
|
|
|
|
_cachedAnchor = true;
|
|
}
|
|
|
|
public void ApplyKeyboardOffset(float yOffset)
|
|
{
|
|
if (typingPreviewRect == null)
|
|
return;
|
|
|
|
CacheInitialAnchor();
|
|
var previewPos = _baseAnchoredPos;
|
|
previewPos.y += yOffset + previewVerticalOffset;
|
|
typingPreviewRect.anchoredPosition = previewPos;
|
|
}
|
|
|
|
public void UpdatePreview(bool isInputFocused, string body, bool canShowPreview)
|
|
{
|
|
if (typingPreviewRoot == null || typingPreviewText == null)
|
|
return;
|
|
|
|
string trimmedBody = (body ?? string.Empty).Trim();
|
|
bool shouldShow = canShowPreview && isInputFocused && !string.IsNullOrEmpty(trimmedBody);
|
|
typingPreviewRoot.SetActive(shouldShow);
|
|
if (shouldShow)
|
|
typingPreviewText.text = trimmedBody;
|
|
}
|
|
}
|
|
}
|