Skip to content

Commit

Permalink
Context menu shows up
Browse files Browse the repository at this point in the history
  • Loading branch information
snipercup committed Jan 28, 2024
1 parent 166c020 commit 79d1ade
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
19 changes: 18 additions & 1 deletion Scenes/UI/CtrlInventoryStackedCustom.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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"]
40 changes: 11 additions & 29 deletions Scripts/CtrlInventoryStackedCustom.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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 = {}
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions Scripts/CtrlInventoryStackedListItem.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
5 changes: 0 additions & 5 deletions Scripts/InventoryWindow.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""))
Expand Down Expand Up @@ -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)

0 comments on commit 79d1ade

Please sign in to comment.