From 802e0eb46d2c872e814d48d2ec776c0f9d995c9c Mon Sep 17 00:00:00 2001 From: Rafael Correa Date: Sat, 11 Nov 2023 22:13:51 -0300 Subject: [PATCH] Add mouse events buttons for ui slots --- core/inventory_handler.gd | 9 ++++++--- multiplayer/networked_handler.gd | 10 +++++----- ui/interactor/action_message_ui.tscn | 2 +- ui/inventory_system_ui.gd | 22 +++++++++++++++++----- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/core/inventory_handler.gd b/core/inventory_handler.gd index 469c2fe7..68af7e49 100644 --- a/core/inventory_handler.gd +++ b/core/inventory_handler.gd @@ -244,7 +244,7 @@ func to_transaction(slot_index : int, inventory : Inventory, amount : int): ## Moves transfer slot information to the [code]slot_index[/code] slot of [Inventory]. -func transaction_to_at(slot_index : int, inventory : Inventory): +func transaction_to_at(slot_index : int, inventory : Inventory, amount_to_move : int = -1): if not is_transaction_active(): return var slot = inventory.slots[slot_index] @@ -252,8 +252,11 @@ func transaction_to_at(slot_index : int, inventory : Inventory): if item == null: return if inventory.is_empty_slot(slot_index) or slot.item == item: - var amount_no_add = inventory.add_at(slot_index, item, transaction_slot.amount) - _set_transaction_slot(item, amount_no_add) + var amount = transaction_slot.amount + if amount_to_move >= 0: + amount = amount_to_move + var amount_no_add = inventory.add_at(slot_index, item, amount) + _set_transaction_slot(item, (transaction_slot.amount - amount) + amount_no_add) else: # Different items in slot and other_slot # Check if transaction_slot amount is equal of origin_slot amount diff --git a/multiplayer/networked_handler.gd b/multiplayer/networked_handler.gd index 6f8878b1..9c52d1e0 100644 --- a/multiplayer/networked_handler.gd +++ b/multiplayer/networked_handler.gd @@ -86,11 +86,11 @@ func to_transaction(slot_index : int , inventory : Inventory, amount : int): to_transaction_rpc(slot_index, inventory.get_path(), amount) -func transaction_to_at(slot_index : int, inventory : Inventory): +func transaction_to_at(slot_index : int, inventory : Inventory, amount_to_move : int = -1): if not multiplayer.is_server(): - transaction_to_at_rpc.rpc_id(1, slot_index, inventory.get_path()) + transaction_to_at_rpc.rpc_id(1, slot_index, inventory.get_path(), amount_to_move) else: - transaction_to_at_rpc(slot_index, inventory.get_path()) + transaction_to_at_rpc(slot_index, inventory.get_path(), amount_to_move) func transaction_to(inventory : Inventory): @@ -211,7 +211,7 @@ func to_transaction_rpc(slot_index : int, object_path : NodePath, amount : int): @rpc("any_peer") -func transaction_to_at_rpc(slot_index : int, object_path : NodePath): +func transaction_to_at_rpc(slot_index : int, object_path : NodePath, amount_to_move : int): if not multiplayer.is_server(): return var object = get_node(object_path) @@ -220,7 +220,7 @@ func transaction_to_at_rpc(slot_index : int, object_path : NodePath): var inventory = object as Inventory if inventory == null: return - super.transaction_to_at(slot_index, inventory) + super.transaction_to_at(slot_index, inventory, amount_to_move) @rpc("any_peer") diff --git a/ui/interactor/action_message_ui.tscn b/ui/interactor/action_message_ui.tscn index 6229db0d..719275ec 100644 --- a/ui/interactor/action_message_ui.tscn +++ b/ui/interactor/action_message_ui.tscn @@ -5,7 +5,7 @@ [ext_resource type="Resource" uid="uid://c2cuwwcpbs81y" path="res://addons/inventory-system/ui/interactor/default_input_icons.tres" id="3_rgd78"] [sub_resource type="LabelSettings" id="LabelSettings_8sxuh"] -outline_size = 4 +outline_size = 8 outline_color = Color(0, 0, 0, 1) [node name="ActionMessageUI" type="HBoxContainer" node_paths=PackedStringArray("texture_rect", "interact_message")] diff --git a/ui/inventory_system_ui.gd b/ui/inventory_system_ui.gd index 5ac0d373..13bb4ec3 100644 --- a/ui/inventory_system_ui.gd +++ b/ui/inventory_system_ui.gd @@ -169,19 +169,31 @@ func _close_player_inventory(): func _slot_point_down(event : InputEvent, slot_index : int, inventory : Inventory): + if not event is InputEventMouseButton: + return + var mouse_event : InputEventMouseButton = event as InputEventMouseButton if inventory_handler.is_transaction_active(): - inventory_handler.transaction_to_at(slot_index, inventory) + var amount = _get_amount_per_mouse_event(mouse_event, inventory_handler.transaction_slot.amount) + inventory_handler.transaction_to_at(slot_index, inventory, amount) $SlotDrop.play() else: if inventory.is_empty_slot(slot_index): return var slot = inventory.slots[slot_index] - var amount = slot.amount - if event is InputEventMouseButton and event.button_index == 2: - amount = ceili(slot.amount/2.0) + var amount = _get_amount_per_mouse_event(mouse_event, slot.amount) inventory_handler.to_transaction(slot_index, inventory, amount) $SlotClick.play() - + + +func _get_amount_per_mouse_event(mouse_event : InputEventMouseButton, amount : int) -> int: + if mouse_event.button_index == MOUSE_BUTTON_LEFT: + return amount + if mouse_event.button_index == MOUSE_BUTTON_RIGHT: + return ceili(amount / 2.0) + if mouse_event.button_index == MOUSE_BUTTON_MIDDLE: + return 1 + return 0 + func _inventory_point_down(event : InputEvent, inventory : Inventory): if event.button_index == 3: