public void UpdateInventoryIcon(int index) { InventoryUi inventoryUi = Object.FindObjectOfType <InventoryUi>(); //TODO: Caching if (inventory[index] != null) { inventoryUi.GetComponentsInChildren <Image>()[index * 2 + 1].sprite = Resources.Load <Sprite>($"Icons/{inventory[index].iconName}"); } else { inventoryUi.GetComponentsInChildren <Image>()[index * 2 + 1].sprite = Resources.Load <Sprite>($"Icons/icon_blank"); } }
public override void OnUpdate(Player parent) { //Get button if (Input.GetButtonDown("inventory")) { //Update the inventory open inventoryOpen = !inventoryOpen; //Set lockstate Cursor.lockState = inventoryOpen ? CursorLockMode.None : CursorLockMode.Locked; //Update inventory inventoryUi.SetOpen(inventoryOpen); //Put the cursor in the center of the screen. cursorObject.transform.localPosition = Vector3.zero; } //Update hotbar //TODO: Have this in controls or something if (Input.mouseScrollDelta.y > 0.2f) { parent.inventory.IncreaseHotbarIndex(hotbarParent); } else if (Input.mouseScrollDelta.y < -0.2f) { parent.inventory.DecreaseHotbarIndex(hotbarParent); } //Hotbar buttons HandleHotbarButtons(parent); //Update cursor if (!inventoryOpen) { return; } //Set the position of the cursor object cursorObject.transform.position = Input.mousePosition; //Perform actions! //ACTION PRIORITY: // - Dropdown menu handling // - Handling Drag Actions //===== Dropdown Handling ===== //Open the dropdown menu if (Input.GetMouseButtonDown(1)) { //Get the pointer position Vector3 pointerPosition = Input.mousePosition; //Find the index of what we are over int index = InventoryUi.CursorPositionToIndex(pointerPosition.x, pointerPosition.y); //Check if something is even there if (index != -1 && parent.inventory.inventory[index] != null) { inventoryUi.OpenDropdownMenu(parent.inventory.inventory[index].GetInteractionOptions()); /*inventoryUi.OpenDropdownMenu(new DropdownOption[] { * new DropdownOption("Use", null), * new DropdownOption("Equip", null), * new DropdownOption("Examine", null), * new DropdownOption("Drop", null), * });*/ } } //Ignore any other actions if (inventoryUi.currentlyOpenDropdown) { if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(1)) { //Select the current button option DropdownButton.hoveredButton?.OnClick(); //Close the dropdown inventoryUi.CloseDropdownMenu(); } return; } //===== Drag Handling ===== //Todo: Dragging from the hotbar too if (Input.GetMouseButtonDown(0) && !isDragging) { //Get the pointer position Vector3 pointerPosition = Input.mousePosition; //Find the index of what we are over int index = InventoryUi.CursorPositionToIndex(pointerPosition.x, pointerPosition.y); //Check if something is even there if (index != -1 && parent.inventory.inventory[index] != null) { //Find the image we are dragging draggingImage = inventoryUi.GetComponentsInChildren <Image>()[index * 2 + 1]; startingDragImagePosition = draggingImage.transform.localPosition; dragStartIndex = index; //Begin dragging isDragging = true; } } //If Dragging if (isDragging) { draggingImage.transform.position = Input.mousePosition; } //End Dragging if (Input.GetMouseButtonUp(0) && isDragging) { //Reset the image draggingImage.transform.localPosition = startingDragImagePosition; draggingImage = null; isDragging = false; //Apply effects of the drag //Get the pointer position Vector3 pointerPosition = Input.mousePosition; //Find the index of what we are over int index = InventoryUi.CursorPositionToIndex(pointerPosition.x, pointerPosition.y); if (index != -1 && parent.inventory.inventory[index] == null) { parent.inventory.SwapInventoryIndexes(dragStartIndex, index); } else { //Check for putting into the hotbar int hotbarRequest = InventoryUi.CursorPositionToHotbarIndex(hotbarParent, pointerPosition.x, pointerPosition.y); if (hotbarRequest != -1) { Log.PrintDebug($"{hotbarRequest}"); parent.inventory.InsertIntoHotbar(hotbarRequest, dragStartIndex); } else { //Dragged onto blank space, assume user wants to drop the item. parent.inventory.DropItemAtIndex(dragStartIndex); } } } }