diff --git a/MrBigsock/Assets/Code/UI/InventoryPanel.cs b/MrBigsock/Assets/Code/UI/InventoryPanel.cs index d8d3512e56e03be99c443f4e9c827bed5920b741..c17694bfd5aee6e1901f72023c4305c57e113a9c 100644 --- a/MrBigsock/Assets/Code/UI/InventoryPanel.cs +++ b/MrBigsock/Assets/Code/UI/InventoryPanel.cs @@ -30,84 +30,73 @@ namespace BigSock.UI // Get the tooltip child-component if it's not set in the prefab. toolTip ??= transform.Find("ToolTip")?.GetComponent<TextMeshProUGUI>(); - } - public void GenerateInv() - { + /* + Generates the GUI elements for the player's inventory. + */ + public void GenerateInv() { + if(player == null) return; var inventory = player.Inventory; - for (int i = 0; i < inventory.Backpack.Count; ++i) - { - var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridBackPack.transform); - var invScript = invSlot.GetComponent<ItemSlot>(); - invScript.inventory = inventory; - invScript.inventoryType = InventoryType.Backpack; - invScript.position = i; - var item = inventory.Backpack[i]; - if (item != null) - { - var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform); - var itemScript = itemPref.GetComponent<ItemPref>(); - itemScript.item = item; - itemScript.itemSlot = invScript; - invScript.item = itemScript; - itemScript.transform.position = invScript.transform.position; - } + for (int i = 0; i < inventory.Backpack.Count; ++i) { + var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridBackPack.transform); + var invScript = invSlot.GetComponent<ItemSlot>(); + invScript.inventory = inventory; + invScript.inventoryType = InventoryType.Backpack; + invScript.position = i; + var item = inventory.Backpack[i]; + if (item != null) { + var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform); + var itemScript = itemPref.GetComponent<ItemPref>(); + itemScript.item = item; + itemScript.Place(invScript); + } } - for (int i = 0; i < inventory.Tools.Count; ++i) - { - var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridTools.transform); - var invScript = invSlot.GetComponent<ItemSlot>(); - invScript.inventory = inventory; - invScript.inventoryType = InventoryType.Tool; - invScript.position = i; - var item = inventory.Tools[i]; - if (item != null) - { - var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform); - var itemScript = itemPref.GetComponent<ItemPref>(); - itemScript.item = item; - itemScript.itemSlot = invScript; - invScript.item = itemScript; - itemScript.transform.position = invScript.transform.position; - } + + for (int i = 0; i < inventory.Tools.Count; ++i) { + var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridTools.transform); + var invScript = invSlot.GetComponent<ItemSlot>(); + invScript.inventory = inventory; + invScript.inventoryType = InventoryType.Tool; + invScript.position = i; + var item = inventory.Tools[i]; + if (item != null) { + var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform); + var itemScript = itemPref.GetComponent<ItemPref>(); + itemScript.item = item; + itemScript.Place(invScript); + } } - for (int i = 0; i < inventory.Equipment.Count; ++i) - { - var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridEquipment.transform); - var invScript = invSlot.GetComponent<ItemSlot>(); - invScript.inventory = inventory; - invScript.inventoryType = InventoryType.Equipment; - invScript.position = i; - var item = inventory.Equipment[i]; - if (item != null) - { - var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform); - var itemScript = itemPref.GetComponent<ItemPref>(); - itemScript.item = item; - itemScript.itemSlot = invScript; - invScript.item = itemScript; - itemScript.transform.position = invScript.transform.position; - } + + for (int i = 0; i < inventory.Equipment.Count; ++i) { + var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridEquipment.transform); + var invScript = invSlot.GetComponent<ItemSlot>(); + invScript.inventory = inventory; + invScript.inventoryType = InventoryType.Equipment; + invScript.position = i; + var item = inventory.Equipment[i]; + if (item != null) { + var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform); + var itemScript = itemPref.GetComponent<ItemPref>(); + itemScript.item = item; + itemScript.Place(invScript); + } } - for (int i = 0; i < inventory.Accessories.Count; ++i) - { - var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridAccessory.transform); - var invScript = invSlot.GetComponent<ItemSlot>(); - invScript.inventory = inventory; - invScript.inventoryType = InventoryType.Accessory; - invScript.position = i; - var item = inventory.Accessories[i]; - if (item != null) - { - var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform); - var itemScript = itemPref.GetComponent<ItemPref>(); - itemScript.item = item; - itemScript.itemSlot = invScript; - invScript.item = itemScript; - itemScript.transform.position = invScript.transform.position; - } + + for (int i = 0; i < inventory.Accessories.Count; ++i) { + var invSlot = PrefabService.SINGLETON.Instance(INVSLOT, gridAccessory.transform); + var invScript = invSlot.GetComponent<ItemSlot>(); + invScript.inventory = inventory; + invScript.inventoryType = InventoryType.Accessory; + invScript.position = i; + var item = inventory.Accessories[i]; + if (item != null) { + var itemPref = PrefabService.SINGLETON.Instance(ITEM, transform); + var itemScript = itemPref.GetComponent<ItemPref>(); + itemScript.item = item; + itemScript.Place(invScript); + } } } @@ -117,8 +106,6 @@ namespace BigSock.UI (Current version is just a basic test of the concept) */ public void SetToolTip(string text) { - //Debug.Log($"[InventoryPanel.SetToolTip()]"); - toolTip?.SetText(text); /* diff --git a/MrBigsock/Assets/Code/UI/ItemPref.cs b/MrBigsock/Assets/Code/UI/ItemPref.cs index 8295d5d4a2fa909c72104cdbc650da7050e6e947..4b15eef06ae833edd852a13c345e592e6dece523 100644 --- a/MrBigsock/Assets/Code/UI/ItemPref.cs +++ b/MrBigsock/Assets/Code/UI/ItemPref.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -15,43 +16,65 @@ namespace BigSock.UI public ItemSlot itemSlot; private RectTransform rectTransform; private CanvasGroup canvasGroup; - public Vector2? position; - void Start() - { + + void Start() { + rectTransform = GetComponent<RectTransform>(); + canvasGroup = GetComponent<CanvasGroup>(); + // Change the sprite if the item has one. var sprite = item?.Icon; if(sprite != null) GetComponent<UnityEngine.UI.Image>().overrideSprite = sprite; } - private void Awake() - { - rectTransform = GetComponent<RectTransform>(); - canvasGroup = GetComponent<CanvasGroup>(); + private void Awake() { + } + + + /* + Moves the item to be centered on it's ItemSlot + */ + public void ResetPosition() { + if(itemSlot != null) transform.position = itemSlot.transform.position; } - public void OnBeginDrag(PointerEventData eventData) - { + + /* + Place this item in the given slot. + Only does GUI stuff here, inventory code is handled elsewhere. + */ + public void Place(ItemSlot slot) { + // Cannot pass it null. + if(slot == null) throw new ArgumentNullException(nameof(slot)); + + // Inform our current slot that we've moved. + if(itemSlot != null) itemSlot.item = null; - position = transform.position; + itemSlot = slot; + slot.item = this; + ResetPosition(); + } + + /* + When the item is getting dragged, make it semi-transparent. + */ + public void OnBeginDrag(PointerEventData eventData) { canvasGroup.alpha = .6f; canvasGroup.blocksRaycasts = false; } - public void OnDrag(PointerEventData eventData) - { - + /* + Update its position while it's getting dragged. + */ + public void OnDrag(PointerEventData eventData) { rectTransform.anchoredPosition += eventData.delta; - } - public void OnEndDrag(PointerEventData eventData) - { - - if (position != null) - { - transform.position = position.Value; - } + /* + When the item has been let go, make it opaque and center it on its slot. + */ + public void OnEndDrag(PointerEventData eventData) { + ResetPosition(); canvasGroup.alpha = 1f; canvasGroup.blocksRaycasts = true; @@ -61,18 +84,15 @@ namespace BigSock.UI } - /* When the user clicks on this box, display the tooltip. (OnPointerClick only triggers if you press and release on the same object) */ public void OnPointerClick(PointerEventData eventData) { - //Debug.Log($"[ItemPref.OnPointerClick()] button: {eventData.button}, dragging: {eventData.dragging}"); if(itemSlot != null && item != null) itemSlot.DisplayToolTip(); } - } } diff --git a/MrBigsock/Assets/Code/UI/ItemSlot.cs b/MrBigsock/Assets/Code/UI/ItemSlot.cs index 28f68084537029fedabdebbef0a27b2eac407c38..c611d0597827b01fcf372ab8ffcdd30c181e18e3 100644 --- a/MrBigsock/Assets/Code/UI/ItemSlot.cs +++ b/MrBigsock/Assets/Code/UI/ItemSlot.cs @@ -9,7 +9,7 @@ using BigSock.Item; namespace BigSock.UI { - public class ItemSlot : MonoBehaviour, IDropHandler, IPointerClickHandler + public class ItemSlot : MonoBehaviour, IDropHandler { public InventoryType inventoryType; public int position; @@ -34,9 +34,7 @@ namespace BigSock.UI transform.hasChanged = false; firstRan = true; - if(item != null) - item.transform.position = transform.position; - + item?.ResetPosition(); } } @@ -58,14 +56,7 @@ namespace BigSock.UI ); // If we successfully moved the item in the inventory: update the gui. - if (didMove) { - itemPref.itemSlot.item = null; // Remove this from the other slot. - itemPref.itemSlot = this; // Make this its new slot. - item = itemPref; // Make it the item in this slot. - // Place the item centered over this slot. - eventData.pointerDrag.transform.position = transform.position; - itemPref.position = null; - } + if (didMove) itemPref.Place(this); /* Notes: @@ -75,17 +66,6 @@ namespace BigSock.UI */ } - - /* - When the user clicks on this box, display the tooltip. - (OnPointerClick only triggers if you press and release on the same objectS) - */ - public void OnPointerClick(PointerEventData eventData) { - Debug.Log($"[ItemSlot.OnPointerClick()] button: {eventData.button}, dragging: {eventData.dragging}"); - //DisplayToolTip(); - } - - /* Display the tooltip for our item onto the screen. */