Files
test/Assets/PerfectWorld/Scripts/Chat/UI/EmojiButtonCell.cs
2026-04-08 15:43:36 +07:00

71 lines
2.2 KiB
C#

using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BrewMonster.Scripts.ChatUI
{
/// <summary>
/// Một ô emoji trong bảng chọn emoji — One emoji cell in the emoji picker grid.
/// Gán lên prefab Button có Image + (tuỳ chọn) TMP_Text cho tooltip.
/// </summary>
[RequireComponent(typeof(Button))]
public class EmojiButtonCell : MonoBehaviour
{
[SerializeField] Image _icon;
[Tooltip("Tuỳ chọn: hiện hint text (tên emoji). Optional: show hint/tooltip text.")]
public event Action<int, int> OnClicked; // (emotionSet, emotionIndex)
private int _emotionSet;
private int _emotionIndex;
private Button _button;
private void Awake()
{
_button = GetComponent<Button>();
if (_icon == null)
_icon = GetComponentInChildren<Image>(true);
}
private void OnEnable()
{
if (_button != null)
_button.onClick.AddListener(HandleClick);
}
private void OnDisable()
{
if (_button != null)
_button.onClick.RemoveListener(HandleClick);
}
/// <summary>
/// Gán dữ liệu cho ô — Bind emotion data to this cell.
/// </summary>
/// <param name="emotionSet">Chỉ số bộ (N trong Emotions{N}.txt).</param>
/// <param name="emotionIndex">Chỉ số emotion trong bộ (dòng trong .txt).</param>
/// <param name="icon">Sprite frame đầu tiên để hiển thị (có thể null). First frame sprite.</param>
/// <param name="hint">Tên / tooltip. Hint / tooltip text.</param>
public void Bind(int emotionSet, int emotionIndex, Sprite icon, string hint)
{
_emotionSet = emotionSet;
_emotionIndex = emotionIndex;
if (_icon != null)
{
_icon.enabled = icon != null;
_icon.sprite = icon;
}
if (_button != null)
_button.interactable = icon != null;
}
private void HandleClick()
{
OnClicked?.Invoke(_emotionSet, _emotionIndex);
}
}
}