From 79d1ade0c105fed7937935aee9c69d1f2938540e Mon Sep 17 00:00:00 2001 From: snipercup <50166150+snipercup@users.noreply.github.com> Date: Sun, 28 Jan 2024 11:37:58 +0100 Subject: [PATCH] Context menu shows up --- Scenes/UI/CtrlInventoryStackedCustom.tscn | 19 ++++++++++- Scripts/CtrlInventoryStackedCustom.gd | 40 +++++++---------------- Scripts/CtrlInventoryStackedListItem.gd | 12 +++++-- Scripts/InventoryWindow.gd | 5 --- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/Scenes/UI/CtrlInventoryStackedCustom.tscn b/Scenes/UI/CtrlInventoryStackedCustom.tscn index d6d0433b..ed979b09 100644 --- a/Scenes/UI/CtrlInventoryStackedCustom.tscn +++ b/Scenes/UI/CtrlInventoryStackedCustom.tscn @@ -4,7 +4,7 @@ [ext_resource type="PackedScene" uid="uid://bgnxsnv6ltej8" path="res://Scenes/UI/CtrlInventoryStackedListItem.tscn" id="2_woew4"] [ext_resource type="PackedScene" uid="uid://dxgl4vkc313we" path="res://Scenes/UI/CtrlInventoryStackedlistHeaderItem.tscn" id="3_svicc"] -[node name="CtrlInventoryStackedCustom" type="Control" node_paths=PackedStringArray("inventoryGrid", "WeightBar", "VolumeBar")] +[node name="CtrlInventoryStackedCustom" type="Control" node_paths=PackedStringArray("inventoryGrid", "WeightBar", "VolumeBar", "context_menu")] layout_mode = 3 anchors_preset = 15 anchor_right = 1.0 @@ -19,6 +19,7 @@ WeightBar = NodePath("VBoxContainer/HBoxContainer/WeightBar") VolumeBar = NodePath("VBoxContainer/HBoxContainer/VolumeBar") listItemContainer = ExtResource("2_woew4") listHeaderContainer = ExtResource("3_svicc") +context_menu = NodePath("ContextMenu") [node name="ColorRect" type="ColorRect" parent="."] layout_mode = 1 @@ -65,3 +66,19 @@ text = "Volume:" [node name="VolumeBar" type="ProgressBar" parent="VBoxContainer/HBoxContainer"] layout_mode = 2 size_flags_horizontal = 3 + +[node name="ContextMenu" type="PopupMenu" parent="."] +size = Vector2i(112, 100) +item_count = 5 +item_0/text = "Equip (left)" +item_0/id = 0 +item_1/text = "Equip (right)" +item_1/id = 1 +item_2/text = "Drop" +item_2/id = 2 +item_3/text = "Wear" +item_3/id = 3 +item_4/text = "Disassemble" +item_4/id = 4 + +[connection signal="id_pressed" from="ContextMenu" to="." method="_on_context_menu_item_selected"] diff --git a/Scripts/CtrlInventoryStackedCustom.gd b/Scripts/CtrlInventoryStackedCustom.gd index 597ea673..b0907dce 100644 --- a/Scripts/CtrlInventoryStackedCustom.gd +++ b/Scripts/CtrlInventoryStackedCustom.gd @@ -31,6 +31,8 @@ extends Control @export var max_volume: int = 1000 @export var listItemContainer: PackedScene @export var listHeaderContainer: PackedScene +# Context menu that will show actions for selected items +@export var context_menu: PopupMenu var selectedItem: InventoryItem = null var selectedItems: Array = [] @@ -39,8 +41,6 @@ var group_to_item_mapping: Dictionary = {} var group_controls: Dictionary = {} var last_hovered_item: Node = null -# Context menu that will show actions for selected items -var context_menu: PopupMenu # Dictionary to store header controls var header_controls: Dictionary = {} @@ -65,19 +65,7 @@ signal disassemble_item(items) func _ready(): populate_inventory_list() update_bars() - setup_context_menu() connect_inventory_signals() - - -func _input(event): - if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_RIGHT and event.pressed: - # Check if the right mouse button was pressed - var mouse_pos = get_global_mouse_position() - var hovered_item = get_hovered_item(mouse_pos) - if hovered_item: - # If the mouse is over an inventory item, show the context menu - show_context_menu() - # Take care of the hovering over items in the grid func _process(_delta): @@ -109,21 +97,11 @@ func _remove_highlight(item: Control): for control in group_controls[group_name]: control.unhighlight() -func setup_context_menu(): - # Initialize and configure the context menu - context_menu = PopupMenu.new() - add_child(context_menu) - context_menu.add_item("Equip (left)", 0) - context_menu.add_item("Equip (right)", 1) - context_menu.add_item("Drop", 2) - context_menu.add_item("Wear", 3) - context_menu.add_item("Disassemble", 4) - context_menu.connect("id_pressed", _on_context_menu_item_selected) - -# Function to show context menu -func show_context_menu(): - # Show the context menu at the center of the screen - context_menu.popup_centered() +# Function to show context menu at specified position +func show_context_menu(myposition: Vector2): + # Create a small Rect2i around the position + var popup_rect = Rect2i(myposition.x, myposition.y, 1, 1) + context_menu.popup(popup_rect) # Handle context menu item selection @@ -202,6 +180,9 @@ func add_header_row_to_grid(): create_header("W") create_header("V") create_header("F") + +func _on_item__right_clicked(clickedItem: Control): + show_context_menu(clickedItem.global_position) # When an item in the inventory is clicked # There are 5 items per row in the grid, but they are treated as a group of 5 @@ -280,6 +261,7 @@ func create_ui_element(property: String, item: InventoryItem, group_name: String # Now we can use the name to get information about the property element.name = property + "_" + str(item.get_name()) element.connect("item_clicked", _on_item_clicked) + element.connect("item_right_clicked", _on_item__right_clicked) # We use groups to keep track of the items element.add_to_group(group_name) return element diff --git a/Scripts/CtrlInventoryStackedListItem.gd b/Scripts/CtrlInventoryStackedListItem.gd index 819a8161..ba421e36 100644 --- a/Scripts/CtrlInventoryStackedListItem.gd +++ b/Scripts/CtrlInventoryStackedListItem.gd @@ -22,6 +22,8 @@ var selected_color: Color = Color(0.5, 0.5, 0.8, 1) # Selected color var is_selected: bool = false signal item_clicked(item: Control) +signal item_right_clicked(item: Control) + # Called when the node enters the scene tree for the first time. func _ready(): @@ -78,11 +80,15 @@ func get_icon() -> Texture: func _on_gui_input(event): if event is InputEventMouseButton: - match event.button_index: - MOUSE_BUTTON_LEFT: - if event.pressed: + if event.pressed: # Check if the mouse button was pressed down + match event.button_index: + MOUSE_BUTTON_LEFT: + # Handle left mouse button click if is_selected: unselect_item() else: select_item() item_clicked.emit(self) + MOUSE_BUTTON_RIGHT: + # Handle right mouse button click + item_right_clicked.emit(self) # Emit a new signal for right-click diff --git a/Scripts/InventoryWindow.gd b/Scripts/InventoryWindow.gd index c3d5da1b..07710942 100644 --- a/Scripts/InventoryWindow.gd +++ b/Scripts/InventoryWindow.gd @@ -44,7 +44,6 @@ func _process(_delta): else: tooltip.visible = false - func _on_inventory_item_mouse_entered(item): is_showing_tooltip = true tooltip_item_name.text = str(item.get_property("name", "")) @@ -244,19 +243,15 @@ func _on_left_hand_equipment_slot_cleared(): func _on_right_hand_equipment_slot_cleared(): item_was_cleared.emit("RightHand") - func _on_equip_right_button_button_up(): RightHandEquipmentSlot.equip(inventory_control.get_selected_inventory_item()) - func _on_equip_left_button_button_up(): LeftHandEquipmentSlot.equip(inventory_control.get_selected_inventory_item()) - func _on_transfer_left_button_button_up(): inventory.transfer(inventory_control.get_selected_inventory_item(), proximity_inventory_control.inventory) - func _on_transfer_right_button_button_up(): var selected_inventory_item: InventoryItem = proximity_inventory_control.get_selected_inventory_item() proximity_inventory_control.inventory.transfer(selected_inventory_item, inventory)