mirror of
https://github.com/D4M13N-D3V/godot_grid_inventory.git
synced 2025-03-14 08:14:55 +00:00
feat context menu for right clicking items to take action against them
This commit is contained in:
parent
fdf73ac404
commit
f892d0b022
@ -10,6 +10,9 @@ item_name = "Shirt"
|
||||
item_description = "This is a shirt"
|
||||
item_size = Vector2(256, 256)
|
||||
item_usable = false
|
||||
item_openable = false
|
||||
item_droppable = false
|
||||
item_destroyable = false
|
||||
item_equipment = true
|
||||
item_equipment_slot = "BODY"
|
||||
item_texture = ExtResource("1_gmlai")
|
||||
|
@ -10,6 +10,9 @@ item_name = "Hat"
|
||||
item_description = "This is a hat"
|
||||
item_size = Vector2(224, 160)
|
||||
item_usable = false
|
||||
item_openable = false
|
||||
item_droppable = false
|
||||
item_destroyable = false
|
||||
item_equipment = true
|
||||
item_equipment_slot = "HEAD"
|
||||
item_texture = ExtResource("1_vm0jo")
|
||||
|
@ -9,7 +9,10 @@ item_id = "large_debug_item"
|
||||
item_name = "Debug Item Large"
|
||||
item_description = "A large item for debugging."
|
||||
item_size = Vector2(256, 256)
|
||||
item_usable = false
|
||||
item_usable = true
|
||||
item_openable = false
|
||||
item_droppable = false
|
||||
item_destroyable = true
|
||||
item_equipment = false
|
||||
item_equipment_slot = "NONE"
|
||||
item_texture = ExtResource("1_l6twi")
|
||||
|
@ -10,6 +10,9 @@ item_name = "Debug Item Medium"
|
||||
item_description = "A medium item for debugging."
|
||||
item_size = Vector2(128, 128)
|
||||
item_usable = false
|
||||
item_openable = true
|
||||
item_droppable = true
|
||||
item_destroyable = false
|
||||
item_equipment = false
|
||||
item_equipment_slot = "NONE"
|
||||
item_texture = ExtResource("1_jwlst")
|
||||
|
@ -10,6 +10,9 @@ item_name = "Debug Item Small"
|
||||
item_description = "A small item for debugging."
|
||||
item_size = Vector2(64, 64)
|
||||
item_usable = false
|
||||
item_openable = false
|
||||
item_droppable = false
|
||||
item_destroyable = true
|
||||
item_equipment = false
|
||||
item_equipment_slot = "NONE"
|
||||
item_texture = ExtResource("1_g0vsl")
|
||||
|
50
grid_inventory_system/scenes/ContextMenu.tscn
Normal file
50
grid_inventory_system/scenes/ContextMenu.tscn
Normal file
@ -0,0 +1,50 @@
|
||||
[gd_scene load_steps=2 format=3 uid="uid://bkmpq37oqwww8"]
|
||||
|
||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/ContextMenu.gd" id="1_dwa8t"]
|
||||
|
||||
[node name="ContextMenu" type="Panel" node_paths=PackedStringArray("use_button", "open_button", "drop_button", "destroy_button")]
|
||||
custom_minimum_size = Vector2(160, 0)
|
||||
offset_left = 240.0
|
||||
offset_top = 102.0
|
||||
offset_right = 400.0
|
||||
offset_bottom = 210.0
|
||||
script = ExtResource("1_dwa8t")
|
||||
use_button = NodePath("ContextOptions/Use")
|
||||
open_button = NodePath("ContextOptions/Open")
|
||||
drop_button = NodePath("ContextOptions/Drop")
|
||||
destroy_button = NodePath("ContextOptions/Destroy")
|
||||
|
||||
[node name="ContextOptions" type="GridContainer" parent="."]
|
||||
layout_mode = 0
|
||||
offset_left = 5.0
|
||||
offset_top = 4.0
|
||||
offset_right = 95.0
|
||||
offset_bottom = 42.0
|
||||
|
||||
[node name="Use" type="Button" parent="ContextOptions"]
|
||||
custom_minimum_size = Vector2(150, 0)
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
theme_override_font_sizes/font_size = 10
|
||||
text = "Use"
|
||||
|
||||
[node name="Open" type="Button" parent="ContextOptions"]
|
||||
custom_minimum_size = Vector2(150, 0)
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
theme_override_font_sizes/font_size = 10
|
||||
text = "Open"
|
||||
|
||||
[node name="Drop" type="Button" parent="ContextOptions"]
|
||||
custom_minimum_size = Vector2(150, 0)
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
theme_override_font_sizes/font_size = 10
|
||||
text = "Drop"
|
||||
|
||||
[node name="Destroy" type="Button" parent="ContextOptions"]
|
||||
custom_minimum_size = Vector2(150, 0)
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
theme_override_font_sizes/font_size = 10
|
||||
text = "Destroy"
|
@ -1,9 +1,9 @@
|
||||
[gd_scene load_steps=5 format=3 uid="uid://brs6u78mwggbp"]
|
||||
|
||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/InventoryController.gd" id="1_hmtt6"]
|
||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/InventoryGrid.gd" id="2_8imhp"]
|
||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemSlot.gd" id="3_sri3o"]
|
||||
[ext_resource type="PackedScene" uid="uid://c2c7v5idagdqr" path="res://grid_inventory_system/debug/debug_ui.tscn" id="4_abx0e"]
|
||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/InventoryController.gd" id="1_pdgp8"]
|
||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/ItemSlot.gd" id="2_7s2pc"]
|
||||
[ext_resource type="Script" path="res://grid_inventory_system/scripts/InventoryGrid.gd" id="3_05dq4"]
|
||||
[ext_resource type="PackedScene" uid="uid://c2c7v5idagdqr" path="res://grid_inventory_system/debug/debug_ui.tscn" id="4_w8gq7"]
|
||||
|
||||
[node name="Inventory" type="Control" node_paths=PackedStringArray("inventory_grid", "inventory_loot_grid", "inventory_background", "inventory_equipment_slots")]
|
||||
layout_mode = 3
|
||||
@ -12,7 +12,7 @@ anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_hmtt6")
|
||||
script = ExtResource("1_pdgp8")
|
||||
inventory_grid = NodePath("UI/Background/Items")
|
||||
inventory_loot_grid = NodePath("UI/Background/ColorRect/LootItems")
|
||||
inventory_background = NodePath("UI/Background")
|
||||
@ -47,7 +47,7 @@ offset_bottom = -4.0
|
||||
grow_horizontal = 0
|
||||
grow_vertical = 2
|
||||
color = Color(0.184314, 0.184314, 0.184314, 1)
|
||||
script = ExtResource("3_sri3o")
|
||||
script = ExtResource("2_7s2pc")
|
||||
|
||||
[node name="HEAD" type="ColorRect" parent="UI/Background/Equipment"]
|
||||
layout_mode = 1
|
||||
@ -86,6 +86,7 @@ grow_vertical = 2
|
||||
color = Color(0.278431, 0.278431, 0.278431, 1)
|
||||
|
||||
[node name="ColorRect" type="ColorRect" parent="UI/Background"]
|
||||
layout_mode = 0
|
||||
offset_left = -3.0
|
||||
offset_top = 470.0
|
||||
offset_right = 711.0
|
||||
@ -102,7 +103,7 @@ offset_right = 704.0
|
||||
offset_bottom = -14.0
|
||||
grow_vertical = 2
|
||||
color = Color(0.164706, 0.164706, 0.164706, 1)
|
||||
script = ExtResource("2_8imhp")
|
||||
script = ExtResource("3_05dq4")
|
||||
inventory_item_grid_width = 21
|
||||
inventory_item_grid_height = 7
|
||||
inventory_grid_default_color = Color(1, 1, 1, 1)
|
||||
@ -118,11 +119,11 @@ offset_right = 701.0
|
||||
offset_bottom = -8.0
|
||||
grow_vertical = 2
|
||||
color = Color(0.164706, 0.164706, 0.164706, 1)
|
||||
script = ExtResource("2_8imhp")
|
||||
script = ExtResource("3_05dq4")
|
||||
inventory_item_grid_width = 21
|
||||
inventory_item_grid_height = 14
|
||||
inventory_grid_default_color = Color(1, 1, 1, 1)
|
||||
inventory_grid_used_color = Color(0, 1, 0, 1)
|
||||
|
||||
[node name="DEBUG" parent="." node_paths=PackedStringArray("inventory") instance=ExtResource("4_abx0e")]
|
||||
[node name="DEBUG" parent="." node_paths=PackedStringArray("inventory") instance=ExtResource("4_w8gq7")]
|
||||
inventory = NodePath("..")
|
19
grid_inventory_system/scripts/ContextMenu.gd
Normal file
19
grid_inventory_system/scripts/ContextMenu.gd
Normal file
@ -0,0 +1,19 @@
|
||||
extends Panel
|
||||
class_name ContextMenu
|
||||
|
||||
@export var use_button:Button
|
||||
@export var open_button:Button
|
||||
@export var drop_button:Button
|
||||
@export var destroy_button:Button
|
||||
|
||||
func set_use(disabled):
|
||||
use_button.disabled=disabled
|
||||
|
||||
func set_open(disabled):
|
||||
open_button.disabled=disabled
|
||||
|
||||
func set_drop(disabled):
|
||||
drop_button.disabled=disabled
|
||||
|
||||
func set_destroy(disabled):
|
||||
destroy_button.disabled=disabled
|
@ -1,7 +1,7 @@
|
||||
extends Control
|
||||
class_name InventoryController
|
||||
const ITEM_BASE = preload("res://grid_inventory_system/scenes/Item.tscn")
|
||||
|
||||
const CONTEXT_MENU = preload("res://grid_inventory_system/scenes/ContextMenu.tscn")
|
||||
@export var inventory_open_input:String = "inventory_open"
|
||||
@export var inventory_close_input:String = "inventory_close"
|
||||
@export var inventory_use_input:String = "inventory_use"
|
||||
@ -12,17 +12,24 @@ const ITEM_BASE = preload("res://grid_inventory_system/scenes/Item.tscn")
|
||||
@export var inventory_background:Control
|
||||
@export var inventory_equipment_slots:Control
|
||||
|
||||
|
||||
var inventory_open = true
|
||||
var inventory_item_dragged = null
|
||||
var inventory_item_cursor_offset = Vector2()
|
||||
var inventory_item_dragged_last_container = null
|
||||
var inventory_item_dragged_last_pos = Vector2()
|
||||
var inventory_context_menu:Control = null
|
||||
var invenotry_context_menu_item:ItemConfiguration = null
|
||||
|
||||
|
||||
signal inventory_item_equipped(item_config,slot)
|
||||
signal inventory_item_unequipped(item_config,slot)
|
||||
signal inventory_item_dropped(item_config)
|
||||
signal inventory_item_grabbed(item_config, x, y, container)
|
||||
signal inventory_item_released(item_config, x, y, container)
|
||||
signal inventory_item_used(item_config, x, y)
|
||||
signal inventory_item_destroyed(item_config, x, y)
|
||||
signal inventory_item_opened(item_config, x, y)
|
||||
|
||||
func _ready():
|
||||
if(inventory_grid==null):
|
||||
@ -53,10 +60,49 @@ func _process(delta):
|
||||
release(cursor_pos)
|
||||
|
||||
if Input.is_action_just_pressed("inventory_use"):
|
||||
use(cursor_pos)
|
||||
try_to_open_context_menu()
|
||||
|
||||
|
||||
if inventory_item_dragged != null:
|
||||
inventory_item_dragged.global_position = cursor_pos + inventory_item_cursor_offset
|
||||
|
||||
func try_to_open_context_menu():
|
||||
var mouse_position = get_global_mouse_position()
|
||||
var c = _get_container_mouse_over()
|
||||
if c != null and c.has_method("grab_item"):
|
||||
var item = c.check_item(mouse_position)
|
||||
if item != null:
|
||||
if(inventory_context_menu!=null):
|
||||
inventory_context_menu.queue_free()
|
||||
show_context_menu(mouse_position,item.item_config)
|
||||
|
||||
|
||||
# Function to show the context menu at a specific position
|
||||
func show_context_menu(mouse_pos: Vector2, item: ItemConfiguration):
|
||||
inventory_context_menu = CONTEXT_MENU.instantiate()
|
||||
inventory_background.add_child(inventory_context_menu)
|
||||
inventory_context_menu.global_position = mouse_pos + Vector2(-20,-15)
|
||||
inventory_context_menu.set_use(not item.item_usable)
|
||||
inventory_context_menu.set_drop(not item.item_droppable)
|
||||
inventory_context_menu.set_destroy(not item.item_destroyable)
|
||||
inventory_context_menu.set_open(not item.item_openable)
|
||||
|
||||
# Function to hide the context menu
|
||||
func hide_context_menu():
|
||||
inventory_context_menu.queue_free()
|
||||
|
||||
func context_menu_open():
|
||||
print("OPEN!")
|
||||
|
||||
func context_menu_use():
|
||||
print("USED!")
|
||||
|
||||
func context_menu_destroy():
|
||||
print("DESTROYED!")
|
||||
|
||||
func context_menu_drop():
|
||||
print("DROPPED!")
|
||||
|
||||
func grab(cursor_pos):
|
||||
var c = _get_container_mouse_over()
|
||||
if c != null and c.has_method("grab_item"):
|
||||
|
@ -68,6 +68,12 @@ func grab_item(pos):
|
||||
inventory_item_grid_items.remove_at(inventory_item_grid_items.find(item))
|
||||
return item
|
||||
|
||||
func check_item(pos):
|
||||
var item = get_item_under_pos(pos)
|
||||
if item == null:
|
||||
return null
|
||||
return item
|
||||
|
||||
# Function to use an item from the inventory grid based on a position
|
||||
func use_item(pos):
|
||||
var item = get_item_under_pos(pos)
|
||||
@ -80,7 +86,7 @@ func use_item(pos):
|
||||
item.queue_free()
|
||||
|
||||
var item_size = get_grid_size(item)
|
||||
var item_pos = position-item.position
|
||||
var item_pos = item.global_position-global_position
|
||||
var g_pos = pos_to_grid_coord(item_pos)
|
||||
inventory_item_grid_items.remove_at(inventory_item_grid_items.find(item))
|
||||
set_grid_space(g_pos.x, g_pos.y, item_size.x, item_size.y, false)
|
||||
@ -88,10 +94,8 @@ func use_item(pos):
|
||||
# Function to convert global position to grid coordinates
|
||||
func pos_to_grid_coord(pos):
|
||||
var results = {}
|
||||
var testa = int(position.x / inventory_item_grid_cell_size)
|
||||
var testb = int(position.y / inventory_item_grid_cell_size)
|
||||
results.x = int(pos.x / inventory_item_grid_cell_size)-testa
|
||||
results.y = int(pos.y / inventory_item_grid_cell_size)-testb
|
||||
results.x = int(pos.x / inventory_item_grid_cell_size)
|
||||
results.y = int(pos.y / inventory_item_grid_cell_size)
|
||||
return results
|
||||
|
||||
# Function to get the grid size of an item in terms of grid cells
|
||||
|
@ -12,12 +12,6 @@ func init_item(item_id):
|
||||
graphic.pivot_offset = Vector2(graphic.size.x/2.0, graphic.size.y/2.0)
|
||||
pivot_offset = Vector2(graphic.size.x/2.0, graphic.size.y/2.0)
|
||||
graphic.position = Vector2()
|
||||
graphic.position = Vector2()
|
||||
graphic.position = Vector2()
|
||||
graphic.position = Vector2()
|
||||
graphic.position = Vector2()
|
||||
graphic.position = Vector2()
|
||||
graphic.position = Vector2()
|
||||
print("TEST")
|
||||
|
||||
func rotate_item(amount):
|
||||
|
@ -4,7 +4,12 @@ class_name ItemConfiguration
|
||||
@export var item_name:String = "Item Name"
|
||||
@export var item_description:String = "This is the default item description."
|
||||
@export var item_size:Vector2 = Vector2(3,3)
|
||||
|
||||
@export var item_usable:bool = false
|
||||
@export var item_openable:bool = false
|
||||
@export var item_droppable:bool = false
|
||||
@export var item_destroyable:bool = false
|
||||
|
||||
@export var item_equipment:bool = false
|
||||
@export var item_equipment_slot:String = "NONE"
|
||||
@export var item_texture:Texture2D
|
||||
|
@ -11,7 +11,7 @@ config_version=5
|
||||
[application]
|
||||
|
||||
config/name="Grid Inventory System"
|
||||
run/main_scene="res://grid_inventory_system/debug/main.tscn"
|
||||
run/main_scene="res://grid_inventory_system/scenes/Inventory.tscn"
|
||||
config/features=PackedStringArray("4.2", "Forward Plus")
|
||||
config/icon="res://icon.svg"
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user