using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; namespace DuloGames.UI { [AddComponentMenu("UI/Icon Slots/Talent Slot", 12)] public class UITalentSlot : UISlotBase { [SerializeField] private Text m_PointsText; [SerializeField] private Color m_pointsMinColor = Color.white; [SerializeField] private Color m_pointsMaxColor = Color.white; [SerializeField] private Color m_pointsActiveColor = Color.white; private UITalentInfo m_TalentInfo; private UISpellInfo m_SpellInfo; private int m_CurrentPoints = 0; protected override void Start() { base.Start(); // Disable Drag and Drop this.dragAndDropEnabled = false; } /// /// Gets the spell info of the spell assigned to this slot. /// /// The spell info. public UISpellInfo GetSpellInfo() { return this.m_SpellInfo; } /// /// Gets the talent info of the talent assigned to this slot. /// /// The talent info. public UITalentInfo GetTalentInfo() { return this.m_TalentInfo; } /// /// Determines whether this slot is assigned. /// /// true if this instance is assigned; otherwise, false. public override bool IsAssigned() { return (this.m_TalentInfo != null); } /// /// Assign the specified slot by talentInfo and spellInfo. /// /// Talent info. /// Spell info. public bool Assign(UITalentInfo talentInfo, UISpellInfo spellInfo) { if (talentInfo == null || spellInfo == null) return false; // Use the base class to assign the icon this.Assign(spellInfo.Icon); // Set the talent info this.m_TalentInfo = talentInfo; // Set the spell info this.m_SpellInfo = spellInfo; // Update the points label this.UpdatePointsLabel(); // Return success return true; } /// /// Updates the points label. /// public void UpdatePointsLabel() { if (this.m_PointsText == null) return; // Set the points string on the label this.m_PointsText.text = ""; // No points assigned if (this.m_CurrentPoints == 0) { this.m_PointsText.text += "" + this.m_CurrentPoints.ToString() + ""; this.m_PointsText.text += "/" + this.m_TalentInfo.maxPoints.ToString() + ""; } // Assigned but not maxed else if (this.m_CurrentPoints > 0 && this.m_CurrentPoints < this.m_TalentInfo.maxPoints) { this.m_PointsText.text += "" + this.m_CurrentPoints.ToString() + ""; this.m_PointsText.text += "/" + this.m_TalentInfo.maxPoints.ToString() + ""; } // Maxed else { this.m_PointsText.text += "" + this.m_CurrentPoints.ToString() + "/" + this.m_TalentInfo.maxPoints.ToString() + ""; } } /// /// Unassign this slot. /// public override void Unassign() { // Remove the icon base.Unassign(); // Clear the talent info this.m_TalentInfo = null; // Clear the spell info this.m_SpellInfo = null; } /// /// Raises the pointer click event. /// /// Event data. public override void OnPointerClick(PointerEventData eventData) { if (!this.IsAssigned()) return; // Redirect right click if (eventData.button == PointerEventData.InputButton.Right) { this.OnRightPointerClick(eventData); return; } // Check if the talent is maxed if (this.m_CurrentPoints >= this.m_TalentInfo.maxPoints) return; // Increase the points this.m_CurrentPoints = this.m_CurrentPoints + 1; // Update the label string this.UpdatePointsLabel(); } /// /// Raises the right click event. /// public virtual void OnRightPointerClick(PointerEventData eventData) { // Check if the talent is at it's base if (this.m_CurrentPoints == 0) return; // Increase the points this.m_CurrentPoints = this.m_CurrentPoints - 1; // Update the label string this.UpdatePointsLabel(); } /// /// Adds points. /// /// Points. public void AddPoints(int points) { if (!this.IsAssigned() || points == 0) return; // Add the points this.m_CurrentPoints = this.m_CurrentPoints + points; // Make sure we dont exceed the limites if (this.m_CurrentPoints < 0) this.m_CurrentPoints = 0; if (this.m_CurrentPoints > this.m_TalentInfo.maxPoints) this.m_CurrentPoints = this.m_TalentInfo.maxPoints; // Update the label string this.UpdatePointsLabel(); } /// /// Raises the tooltip event. /// /// If set to true show. public override void OnTooltip(bool show) { // Make sure we have spell info, otherwise game might crash if (this.m_SpellInfo == null) return; // If we are showing the tooltip if (show) { UITooltip.InstantiateIfNecessary(this.gameObject); // Prepare the tooltip lines UISpellSlot.PrepareTooltip(this.m_SpellInfo); // Anchor to this slot UITooltip.AnchorToRect(this.transform as RectTransform); // Show the tooltip UITooltip.Show(); } else { // Hide the tooltip UITooltip.Hide(); } } } }