-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Rewrote inventory handling basing on ESCItems (#417)
Co-authored-by: Dennis Ploeger <[email protected]>
- Loading branch information
Showing
35 changed files
with
244 additions
and
335 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Binary file not shown.
96 changes: 8 additions & 88 deletions
96
addons/escoria-core/game/core-scripts/esc_inventory_item.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,15 @@ | ||
# The inventory representation of an ESC item if pickable | ||
extends TextureButton | ||
class_name ESCInventoryItem, \ | ||
"res://addons/escoria-core/design/esc_inventory_item.svg" | ||
|
||
|
||
# Signal emitted when the item was left clicked | ||
# | ||
# #### Parameters | ||
# | ||
# - item_id: Global ID of the clicked item | ||
signal mouse_left_inventory_item(item_id) | ||
|
||
# Signal emitted when the item was right clicked | ||
# | ||
# #### Parameters | ||
# | ||
# - item_id: Global ID of the clicked item | ||
signal mouse_right_inventory_item(item_id) | ||
|
||
# Signal emitted when the item was double clicked | ||
# | ||
# #### Parameters | ||
# | ||
# - item_id: Global ID of the clicked item | ||
signal mouse_double_left_inventory_item(item_id) | ||
|
||
# Signal emitted when the item was focused | ||
# | ||
# #### Parameters | ||
# | ||
# - item_id: Global ID of the clicked item | ||
signal inventory_item_focused(item_id) | ||
|
||
# Signal emitted when the item is not focused anymore | ||
signal inventory_item_unfocused() | ||
# Basic information about an inventory item | ||
class_name ESCInventoryItem | ||
|
||
|
||
# Global ID of the ESCItem that uses this ESCInventoryItem | ||
# Will be set by ESCItem automatically | ||
var global_id | ||
|
||
|
||
# Connect input handlers | ||
func _ready(): | ||
connect("gui_input", self, "_on_inventory_item_gui_input") | ||
connect("mouse_entered", self, "_on_inventory_item_mouse_enter") | ||
connect("mouse_exited", self, "_on_inventory_item_mouse_exit") | ||
|
||
|
||
# Handle the gui input and emit the respective signals | ||
# | ||
# #### Parameters | ||
# | ||
# - event: The event received | ||
func _on_inventory_item_gui_input(event: InputEvent): | ||
if InputMap.has_action("switch_action_verb") \ | ||
and event.is_action_pressed("switch_action_verb"): | ||
if event.button_index == BUTTON_WHEEL_UP: | ||
escoria.inputs_manager._on_mousewheel_action(-1) | ||
elif event.button_index == BUTTON_WHEEL_DOWN: | ||
escoria.inputs_manager._on_mousewheel_action(1) | ||
if event is InputEventMouseButton: | ||
# var p = get_global_mouse_position() | ||
if event.doubleclick: | ||
if event.button_index == BUTTON_LEFT: | ||
emit_signal( | ||
"mouse_double_left_inventory_item", | ||
global_id, | ||
event | ||
) | ||
else: | ||
if event.is_pressed(): | ||
if event.button_index == BUTTON_LEFT: | ||
emit_signal( | ||
"mouse_left_inventory_item", | ||
global_id, | ||
event | ||
) | ||
if event.button_index == BUTTON_RIGHT: | ||
emit_signal( | ||
"mouse_right_inventory_item", | ||
global_id, | ||
event | ||
) | ||
var global_id: String = "" | ||
|
||
# The texture for the item | ||
var texture: Texture = null | ||
|
||
# Handle mouse entering the item and send the respecitve signal | ||
func _on_inventory_item_mouse_enter(): | ||
emit_signal("inventory_item_focused", global_id) | ||
|
||
func _init(p_item: ESCItem) -> void: | ||
global_id = p_item.global_id | ||
texture = p_item._get_inventory_texture() | ||
|
||
# Handle mouse leaving the item and send the respecitve signal | ||
func _on_inventory_item_mouse_exit(): | ||
emit_signal("inventory_item_unfocused") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
addons/escoria-core/library/inventory/esc_inventory_button.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# The inventory representation of an ESC item if pickable (only used by | ||
# the inventory components) | ||
extends TextureButton | ||
class_name ESCInventoryButton | ||
|
||
|
||
# Signal emitted when the item was left clicked | ||
# | ||
# #### Parameters | ||
# | ||
# - item_id: Global ID of the clicked item | ||
signal mouse_left_inventory_item(item_id) | ||
|
||
# Signal emitted when the item was right clicked | ||
# | ||
# #### Parameters | ||
# | ||
# - item_id: Global ID of the clicked item | ||
signal mouse_right_inventory_item(item_id) | ||
|
||
# Signal emitted when the item was double clicked | ||
# | ||
# #### Parameters | ||
# | ||
# - item_id: Global ID of the clicked item | ||
signal mouse_double_left_inventory_item(item_id) | ||
|
||
# Signal emitted when the item was focused | ||
# | ||
# #### Parameters | ||
# | ||
# - item_id: Global ID of the clicked item | ||
signal inventory_item_focused(item_id) | ||
|
||
# Signal emitted when the item is not focused anymore | ||
signal inventory_item_unfocused() | ||
|
||
|
||
# Global ID of the ESCItem that uses this ESCInventoryItem | ||
var global_id: String = "" | ||
|
||
|
||
func _init(p_item: ESCInventoryItem) -> void: | ||
global_id = p_item.global_id | ||
texture_normal = p_item.texture | ||
expand = true | ||
stretch_mode = TextureButton.STRETCH_KEEP_ASPECT | ||
|
||
|
||
func _process(_delta: float) -> void: | ||
rect_size = ProjectSettings.get_setting("escoria/ui/inventory_item_size") | ||
rect_min_size = ProjectSettings.get_setting( | ||
"escoria/ui/inventory_item_size" | ||
) | ||
|
||
# Connect input handlers | ||
func _ready(): | ||
connect("gui_input", self, "_on_inventory_item_gui_input") | ||
connect("mouse_entered", self, "_on_inventory_item_mouse_enter") | ||
connect("mouse_exited", self, "_on_inventory_item_mouse_exit") | ||
|
||
|
||
# Handle the gui input and emit the respective signals | ||
# | ||
# #### Parameters | ||
# | ||
# - event: The event received | ||
func _on_inventory_item_gui_input(event: InputEvent): | ||
if InputMap.has_action("switch_action_verb") \ | ||
and event.is_action_pressed("switch_action_verb"): | ||
if event.button_index == BUTTON_WHEEL_UP: | ||
escoria.inputs_manager._on_mousewheel_action(-1) | ||
elif event.button_index == BUTTON_WHEEL_DOWN: | ||
escoria.inputs_manager._on_mousewheel_action(1) | ||
if event is InputEventMouseButton: | ||
# var p = get_global_mouse_position() | ||
if event.doubleclick: | ||
if event.button_index == BUTTON_LEFT: | ||
emit_signal( | ||
"mouse_double_left_inventory_item", | ||
global_id, | ||
event | ||
) | ||
else: | ||
if event.is_pressed(): | ||
if event.button_index == BUTTON_LEFT: | ||
emit_signal( | ||
"mouse_left_inventory_item", | ||
global_id, | ||
event | ||
) | ||
if event.button_index == BUTTON_RIGHT: | ||
emit_signal( | ||
"mouse_right_inventory_item", | ||
global_id, | ||
event | ||
) | ||
|
||
|
||
# Handle mouse entering the item and send the respecitve signal | ||
func _on_inventory_item_mouse_enter(): | ||
emit_signal("inventory_item_focused", global_id) | ||
|
||
|
||
# Handle mouse leaving the item and send the respecitve signal | ||
func _on_inventory_item_mouse_exit(): | ||
emit_signal("inventory_item_unfocused") |
31 changes: 31 additions & 0 deletions
31
addons/escoria-core/library/inventory/esc_inventory_container.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Inventory container handler that acts as a base for UIs inventory containers | ||
extends Control | ||
class_name ESCInventoryContainer | ||
|
||
|
||
# Get wether the inventory container currently is empty | ||
# **Returns** Wether the container is empty or not | ||
func is_empty() -> bool: | ||
return get_child_count() > 0 | ||
|
||
# Add a new item into the container and return the control generated for it | ||
# so its events can be handled by the inputs manager | ||
# | ||
# #### Parameters | ||
# - inventory_item: Item to add | ||
# **Returns** The button generated for the item | ||
func add_item(inventory_item: ESCInventoryItem) -> ESCInventoryButton: | ||
var button = ESCInventoryButton.new(inventory_item) | ||
add_child(button) | ||
return button | ||
|
||
|
||
# Remove an item from the container | ||
# | ||
# #### Parameters | ||
# - inventory_item: Item to remove | ||
func remove_item(inventory_item: ESCInventoryItem): | ||
for c in get_children(): | ||
if c is ESCInventoryButton and c.global_id == inventory_item.global_id: | ||
remove_child(c) | ||
c.queue_free() |
Oops, something went wrong.