using System; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using TMPro; namespace Ashwild.Building { /// /// One clickable card in the construction menu — a pure view. It renders the icon/label it is /// handed and reports a left-click back by its index, never resolving what it represents. The /// manager that populated the menu maps that index to a buildable and decides what the pick does. /// [DisallowMultipleComponent] public class BuildMenuCardUI : MonoBehaviour, IPointerClickHandler { #region Serialized Fields [Header("References")] [SerializeField] private Image iconImage; [SerializeField] private TextMeshProUGUI nameText; #endregion #region State /// /// This card's position in the menu, reported back on click. /// private int index; /// /// Invoked with this card's index when the player left-clicks it. /// private Action onSelected; #endregion #region Public API /// /// Binds the card to its index and the menu's selection callback, then draws the payload. /// public void Initialize(BuildCardView view, int cardIndex, Action selectedCallback) { index = cardIndex; onSelected = selectedCallback; if (iconImage != null) { iconImage.sprite = view.Icon; iconImage.enabled = view.Icon != null; } if (nameText != null) nameText.text = view.Label; } #endregion #region Event Handlers /// /// Reports a left-click to the menu by index; other buttons are ignored. /// public void OnPointerClick(PointerEventData eventData) { Debug.Log($"[BuildMenuCardUI] Click reçu — button={eventData.button}, index={index}", this); if (eventData.button != PointerEventData.InputButton.Left) return; if (onSelected == null) { Debug.LogWarning("[BuildMenuCardUI] onSelected est null — la carte n'a pas été Initialize().", this); return; } Debug.Log($"[BuildMenuCardUI] Invoke onSelected({index})", this); onSelected.Invoke(index); } #endregion } }