81 lines
2.5 KiB
C#
81 lines
2.5 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using UnityEngine.EventSystems;
|
|
using TMPro;
|
|
|
|
namespace Ashwild.Building
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
[DisallowMultipleComponent]
|
|
public class BuildMenuCardUI : MonoBehaviour, IPointerClickHandler
|
|
{
|
|
#region Serialized Fields
|
|
|
|
[Header("References")]
|
|
[SerializeField] private Image iconImage;
|
|
[SerializeField] private TextMeshProUGUI nameText;
|
|
|
|
#endregion
|
|
|
|
#region State
|
|
|
|
/// <summary>
|
|
/// This card's position in the menu, reported back on click.
|
|
/// </summary>
|
|
private int index;
|
|
|
|
/// <summary>
|
|
/// Invoked with this card's index when the player left-clicks it.
|
|
/// </summary>
|
|
private Action<int> onSelected;
|
|
|
|
#endregion
|
|
|
|
#region Public API
|
|
|
|
/// <summary>
|
|
/// Binds the card to its index and the menu's selection callback, then draws the payload.
|
|
/// </summary>
|
|
public void Initialize(BuildCardView view, int cardIndex, Action<int> 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
|
|
|
|
/// <summary>
|
|
/// Reports a left-click to the menu by index; other buttons are ignored.
|
|
/// </summary>
|
|
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
|
|
}
|
|
}
|