using Invector.vCharacterController; using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.UI; namespace Invector.vItemManager { public delegate void ItemSlotEvent(vItemSlot item); [vClassHeader("Item Slot", openClose = false)] public class vItemSlot : vMonoBehaviour, IPointerClickHandler, ISelectHandler, IDeselectHandler, ISubmitHandler, IPointerEnterHandler, IPointerExitHandler { [vEditorToolbar("Default")] public vItem item; public bool isValid = true; [HideInInspector] public bool isChecked; [vEditorToolbar("Optional")] public Image icon; public Image blockIcon; public Image checkIcon; public Text displayNameText; public Text displayTypeText; public Text displayAmountText; public Text displayDescriptionText; public Text displayAttributesText; [vHelpBox("You can ignore display Attributes using this property")] public List ignoreAttributes; [vEditorToolbar("Events")] public InputField.OnChangeEvent onChangeName; public InputField.OnChangeEvent onChangeType; public InputField.OnChangeEvent onChangeAmount; public InputField.OnChangeEvent onChangeDescription; public InputField.OnChangeEvent onChangeAttributes; public List customAttributeDisplay; [System.Serializable] public class AttributeDisplay { public vItemAttributes name; [Tooltip("Special Tags\n(NAME) = Display name of the Attribute\n(VALUE) = Display the value of the Attribute\n ***Keep Empty to use default attribute display***")] public string displayFormat = "(VALUE)"; public Text text; public InputField.OnChangeEvent onChangeDisplay; } [vEditorToolbar("Events")] public ItemSlotEvent onSubmitSlotCallBack, onSelectSlotCallBack, onDeselectSlotCallBack; public OnHandleItemEvent onAddItem, onRemoveItem; public UnityEvent onEnable; public UnityEvent onDisable; public UnityEvent onClick; protected Selectable selectable; protected Color color = Color.white; private void OnEnable() { onEnable.Invoke(); UpdateDisplays(item); } private void OnDisable() { onDisable.Invoke(); } protected virtual void Start() { var inventory = GetComponentInParent(); if (inventory) inventory.OnUpdateInventory += UpdateDisplays; selectable = GetComponent(); SetValid(isValid); } /// /// Update all slot display texts /// public virtual void UpdateDisplays() { UpdateDisplays(item); } private void OnDestroy() { var inventory = GetComponentInParent(); if (inventory) inventory.OnUpdateInventory -= UpdateDisplays; } /// /// Enable or disable checkIcon /// /// Enable or disable value public virtual void CheckItem(bool value) { isChecked = value; if (checkIcon) { checkIcon.gameObject.SetActive(isChecked); } } /// /// Set if the slot is Selectable or not /// /// Enable or disable value public virtual void SetValid(bool value) { isValid = value; if (selectable) selectable.interactable = value; if (blockIcon == null) return; blockIcon.color = value ? Color.clear : Color.white; blockIcon.SetAllDirty(); isValid = value; } /// /// Add item to slot /// /// target item public virtual void AddItem(vItem item) { if (item != null) { onAddItem.Invoke(item); this.item = item; UpdateDisplays(item); } else RemoveItem(); } private void UpdateDisplays(vItem item) { ChangeDisplayIcon(item); ChangeDisplayName(item); ChangeDisplayType(item); ChangeDisplayAmount(item); ChangeDisplayDescription(item); ChangeDisplayAttributes(item); CheckItem(item != null && item.isInEquipArea); } /// /// Update the Display type text /// /// target item protected virtual void ChangeDisplayType(vItem item) { if (item) { onChangeType.Invoke(item.ItemTypeText()); if (displayTypeText) displayTypeText.text = item.ItemTypeText(); } else { onChangeType.Invoke(""); if (displayTypeText) displayTypeText.text = ""; } } /// /// Update the Display attribute text /// /// protected virtual void ChangeDisplayAttributes(vItem item) { if (item) { if (displayAttributesText) displayAttributesText.text = item.GetItemAttributesText(ignoreAttributes); onChangeAttributes.Invoke(item.GetItemAttributesText(ignoreAttributes)); for (int i = 0; i < item.attributes.Count; i++) { AttributeDisplay attributeDisplay = customAttributeDisplay.Find(att => att.name.Equals(item.attributes[i].name)); if (attributeDisplay != null) { string displayText = item.attributes[i].GetDisplayText(); if (attributeDisplay.text) attributeDisplay.text.text = displayText; attributeDisplay.onChangeDisplay.Invoke(displayText); } } } else { if (displayAttributesText) displayAttributesText.text = ""; onChangeAttributes.Invoke(""); for (int i = 0; i < customAttributeDisplay.Count; i++) { if (customAttributeDisplay[i].text) customAttributeDisplay[i].text.text = ""; customAttributeDisplay[i].onChangeDisplay.Invoke(""); } } } /// /// Update the Display item Icon image /// /// protected virtual void ChangeDisplayIcon(vItem item) { if (icon && item) { icon.sprite = item.icon; color.a = 1; icon.color = color; } } /// /// Update the Display Description text /// /// protected virtual void ChangeDisplayDescription(vItem item) { if (item) { onChangeDescription.Invoke(item.description); if (displayDescriptionText) displayDescriptionText.text = item.description; } else { onChangeDescription.Invoke(""); if (displayDescriptionText) displayDescriptionText.text = ""; } } /// /// Update the Display Amount text /// /// protected virtual void ChangeDisplayAmount(vItem item) { string amountText = ""; if (item != null && this.gameObject.activeSelf) { if (item.stackable) amountText = "x" + item.amount.ToString(); else amountText = ""; } else if (item == null) amountText = ""; if (displayAmountText) displayAmountText.text = amountText; onChangeAmount.Invoke(amountText); } /// /// Update the Display item Name text /// /// protected virtual void ChangeDisplayName(vItem item) { if (item) { onChangeName.Invoke(item.name); if (displayNameText) displayNameText.text = item.name; } else { onChangeName.Invoke(""); if (displayNameText) displayNameText.text = ""; } } /// /// Remove current item from the slot /// public virtual void RemoveItem() { onRemoveItem.Invoke(item); this.item = null; if (icon) { color.a = 0; icon.color = color; icon.sprite = null; icon.SetAllDirty(); } UpdateDisplays(null); } /// /// Check if slot has an item /// /// public virtual bool isOcupad() { return item != null; } #region UnityEngine.EventSystems Implementation public virtual void OnSelect(BaseEventData eventData) { if (onSelectSlotCallBack != null) onSelectSlotCallBack(this); } public virtual void OnDeselect(BaseEventData eventData) { if (onDeselectSlotCallBack != null) onDeselectSlotCallBack(this); } public virtual void OnSubmit(BaseEventData eventData) { if (isValid) { onClick.Invoke(); if (onSubmitSlotCallBack != null) onSubmitSlotCallBack(this); } } public virtual void OnPointerEnter(PointerEventData eventData) { //if(vInput.instance.inputDevice == InputDevice.MouseKeyboard) { EventSystem.current.SetSelectedGameObject(this.gameObject); if (onSelectSlotCallBack != null) onSelectSlotCallBack(this); } } public virtual void OnPointerExit(PointerEventData eventData) { //if (vInput.instance.inputDevice == InputDevice.MouseKeyboard) { if (onDeselectSlotCallBack != null) onDeselectSlotCallBack(this); } } public virtual void OnPointerClick(PointerEventData eventData) { #if UNITY_ANDROID || UNITY_IOS if (vInput.instance.inputDevice == InputDevice.Mobile) #else //if (vInput.instance.inputDevice == InputDevice.MouseKeyboard) #endif { if (eventData.button == PointerEventData.InputButton.Left) { if (isValid) { onClick.Invoke(); if (onSubmitSlotCallBack != null) onSubmitSlotCallBack(this); } } } } #endregion } }