Files
A-Fairytale-Gone-Bad-Briar-…/Assets/Scripts/UI/Codex/CodexEntryButton.cs

71 lines
1.8 KiB
C#

using System;
using BriarQueen.Framework.Managers.Player.Data;
using BriarQueen.UI.Menus.Components;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace BriarQueen.UI.Codex
{
public class CodexEntryButton : MonoBehaviour
{
[SerializeField] private Button _button;
[SerializeField] private UnderlineButton _underlineButton;
[SerializeField] private TextMeshProUGUI _label;
public CodexEntrySo Entry { get; private set; }
public UnderlineButton UnderlineButton => _underlineButton;
public event Action<CodexEntrySo> OnEntryClicked;
private void Awake()
{
AddButtonListener();
}
private void OnEnable()
{
AddButtonListener();
}
private void OnDestroy()
{
RemoveButtonListener();
}
public void Initialize(CodexEntrySo entry)
{
Entry = entry;
if (_label != null)
_label.text = entry != null ? entry.Title : string.Empty;
}
public void SetSelected(bool selected)
{
// Selection state is now driven entirely by UnderlineButtonGroup
// via UnderlineButton — nothing to do here directly
}
private void HandleClicked()
{
if (Entry == null) return;
OnEntryClicked?.Invoke(Entry);
}
private void AddButtonListener()
{
if (_button == null) return;
_button.onClick.RemoveListener(HandleClicked);
_button.onClick.AddListener(HandleClicked);
}
private void RemoveButtonListener()
{
if (_button == null) return;
_button.onClick.RemoveListener(HandleClicked);
}
}
}