using OTG.Lab06.Abilities; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; namespace OTG.Lab06.UI { public sealed class AbilitySlotUI : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler { [SerializeField] private Image icon; [SerializeField] private Image cooldownOverlay; [SerializeField] private Text hotkeyLabel; [SerializeField] private Text nameLabel; [SerializeField] private Text manaLabel; [SerializeField] private Text cooldownLabel; private AbilityRuntime runtime; private TooltipUI tooltip; public void Bind(AbilityRuntime abilityRuntime, int index, TooltipUI tooltipUI) { runtime = abilityRuntime; tooltip = tooltipUI; if (icon != null) { icon.sprite = runtime.Data.icon; icon.color = runtime.Data.icon == null ? new Color(0.35f, 0.35f, 0.35f, 1f) : Color.white; } if (hotkeyLabel != null) { hotkeyLabel.text = (index + 1).ToString(); } if (manaLabel != null) { manaLabel.text = Mathf.RoundToInt(runtime.Data.manaCost).ToString(); } if (nameLabel != null) { nameLabel.text = GetShortName(runtime.Data.abilityName); } Refresh(); } private static string GetShortName(string abilityName) { if (string.IsNullOrWhiteSpace(abilityName)) { return string.Empty; } return abilityName.Length <= 9 ? abilityName : abilityName.Substring(0, 9); } private void Update() { Refresh(); } private void Refresh() { if (runtime == null) { return; } if (cooldownOverlay != null) { cooldownOverlay.fillAmount = runtime.CooldownNormalized; } if (cooldownLabel != null) { bool show = runtime.CooldownRemaining > 0.05f; cooldownLabel.gameObject.SetActive(show); if (show) { cooldownLabel.text = runtime.CooldownRemaining.ToString("0.0"); } } } public void OnPointerEnter(PointerEventData eventData) { if (tooltip != null && runtime != null) { tooltip.Show(runtime.Data, transform as RectTransform); } } public void OnPointerExit(PointerEventData eventData) { if (tooltip != null) { tooltip.Hide(); } } } }