Encounter / Assets / Scripts / GUIHandlers / ItemGUI.cs
ItemGUI.cs
Raw
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// GUI handler for the items in shops, loot, and inventory
/// </summary>
public class ItemGUI : MonoBehaviour
{
    [Header("UI Components")]
    [SerializeField] private TextMeshProUGUI item_NameF;               // TMP component for the name of the item in the GUI
    [SerializeField] private TextMeshProUGUI item_CoVaF;               // TMP component for the value/cost location for the item in the GUI
    [SerializeField] private TextMeshProUGUI item_NumberF;             // TMP component for the amount of items present in this GUI information field
    [SerializeField] private Image item_SpriteF;                       // Sprite component for the sprite of the item that is being referenced

    private Item itemInfo;                                             // Item information reference that can be pulled from the GUI in GetAspect()
    private bool valueActivated;                                       // Conditional Parameter in MultiplyCostOrValue() to indicate if value or cost should be shown
    private int amountOfItem;                                          // The amount of item present in the item's/GUI's information field

    private readonly Vector2 normAnchorMax = new Vector2(.52f, .72f);
    private readonly Vector2 spriteAnchorMin = new Vector2(0f, .31f);

    /// <summary>
    /// Retrieval system for the Item information that is hidden in the GUI
    /// </summary>
    /// <returns>The item whose information is being shown or connected to the GUI</returns>
    public Item GetAspect()
        => itemInfo;
    /// <summary>
    /// Deactivates the item cost field for more generalized inventory management
    /// </summary>
    public void DeactivateItemCostValueText() 
        => item_CoVaF.enabled = false;
    /// <summary>
    /// Injects necessary information into the GUI system from the item information available
    /// </summary>
    /// <param name="item">Item parent linked class, i.e. Weapon, Relic, or Apparel can be used</param>
    /// <param name="amount">The amount of the item to be indicated in the GUI System</param>
    /// <exception cref="NullReferenceException">Exception is thrown either when the sprite component isn't linked or the text components are not linked</exception>
    public void InjectItemInformation(Item item, int amount = 1)
    {
        this.itemInfo = item;
        // If the item itself is null
        try
        {
            item_SpriteF.sprite = item.ItemSprite;
        }
        catch (NullReferenceException)
        {
            Destroy(this.gameObject);
            return;
        }
        // (1) If certain components are not linked...
        try
        {
            if (item_NameF == null || item_NumberF == null)
                throw new NullReferenceException();

            amountOfItem = amount;
            item_NameF.text = item.ItemName;
            item_NumberF.text = amount.ToString();
            MultiplyCostOrValue();
        }
        catch (NullReferenceException) when (item_NameF == null || item_NumberF == null)
        {
            // (2) Simply skip processing the information indicated
        }
    }
    /// <summary>
    /// Activates the conditional parameter if the value should be shown and not the cost
    /// </summary>
    public void SwitchCostToValueText() 
        => valueActivated = true;
    public void SwitchValueToCostText()
        => valueActivated = false;
    /// <summary>
    /// Multiples the amount of items available by the value of the item, or by the cost depending on the situation
    /// </summary>
    /// <param name="value">The number of items the GUI holds</param>
    public void MultiplyCostOrValue()
    {
        if (valueActivated)
            item_CoVaF.text = (itemInfo.ItemValue * amountOfItem).ToString();
        else
            item_CoVaF.text = (itemInfo.ItemCost * amountOfItem).ToString();
    }
    /// <summary>
    /// Retrieves the amount of items present in the GUI system
    /// </summary>
    /// <returns>Total item amount for the Item Info</returns>
    public int GetItemAmount() 
        => this.amountOfItem;
    /// <summary>
    /// Retrieves the name of the item
    /// </summary>
    /// <returns>Item Name</returns>
    public string GetItemName() 
        => this.itemInfo.ItemName;
    public void DeactivateAllElements()
    {
        item_NumberF.enabled = false;
        item_CoVaF.enabled = false;
        item_NameF.enabled = false;
    }
    public void IndicateNoNameLargeSpriteWithAmount()
    {
        Vector2 adjustmentVector = new Vector2(.05f, .17f);
        Vector2 adjustedAnchorField = new Vector2(.52f, .62f);
        DeactivateAllElements();
        item_NumberF.enabled = true;

        this.GetComponent<RectTransform>().anchorMax = adjustedAnchorField;
        item_SpriteF.rectTransform.anchorMax = Vector2.one - adjustmentVector;
        item_SpriteF.rectTransform.anchorMin = Vector2.zero + adjustmentVector;
    }
    public void IndicateNoNameLargeSpriteWithValue()
    {
        Vector2 adjustmentVector = new Vector2(.05f, .17f);
        Vector2 adjustedAnchorField = new Vector2(.52f, .62f);
        DeactivateAllElements();
        item_NumberF.enabled = true;
        item_CoVaF.enabled = true;
        SwitchCostToValueText();
        MultiplyCostOrValue();

        this.GetComponent<RectTransform>().anchorMax = adjustedAnchorField;
        item_SpriteF.rectTransform.anchorMax = Vector2.one - adjustmentVector;
        item_SpriteF.rectTransform.anchorMin = Vector2.zero + adjustmentVector;
    }
    public void IndicateNoNameLargeSpriteWithCost()
    {
        Vector2 adjustmentVector = new Vector2(.05f, .17f);
        Vector2 adjustedAnchorField = new Vector2(.52f, .62f);
        DeactivateAllElements();
        item_NumberF.enabled = true;
        item_CoVaF.enabled = true;
        SwitchValueToCostText();
        MultiplyCostOrValue();

        this.GetComponent<RectTransform>().anchorMax = adjustedAnchorField;
        item_SpriteF.rectTransform.anchorMax = Vector2.one - adjustmentVector;
        item_SpriteF.rectTransform.anchorMin = Vector2.zero + adjustmentVector;
    }
    public void IndicateLargeSpriteNoOtherElements()
    {
        Vector2 adjustmentVector = new Vector2(.05f, .05f);
        Vector2 adjustedAnchorField = new Vector2(.52f, .62f);
        DeactivateAllElements();

        this.GetComponent<RectTransform>().anchorMax = adjustedAnchorField;
        item_SpriteF.rectTransform.anchorMax = Vector2.one - adjustmentVector;
        item_SpriteF.rectTransform.anchorMin = Vector2.zero + adjustmentVector;
    }
}