Skip to content

Commit

Permalink
feat: Rewrote inventory handling basing on ESCItems (#417)
Browse files Browse the repository at this point in the history
Co-authored-by: Dennis Ploeger <[email protected]>
  • Loading branch information
dploeger and dploeger authored Oct 21, 2021
1 parent 0271046 commit af26521
Show file tree
Hide file tree
Showing 35 changed files with 244 additions and 335 deletions.
27 changes: 0 additions & 27 deletions addons/escoria-core/design/esc_inventory_item.svg

This file was deleted.

Binary file modified addons/escoria-core/design/icons.sketch
Binary file not shown.
96 changes: 8 additions & 88 deletions addons/escoria-core/game/core-scripts/esc_inventory_item.gd
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")
36 changes: 18 additions & 18 deletions addons/escoria-core/game/core-scripts/esc_item.gd
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ export(bool) var combine_is_one_way = false
# A false value is useful for items in the background, such as buttons.
export(bool) var use_from_inventory_only = false

# Scene based on ESCInventoryItem used in inventory for the object if it is
# picked up, that displays and handles the item
export(PackedScene) var inventory_item_scene_file: PackedScene
# The visual representation for this item when its in the inventory
export(Texture) var inventory_texture: Texture = null \
setget ,_get_inventory_texture

# Color used for dialogs
export(Color) var dialog_color = ColorN("white")
Expand All @@ -124,25 +124,22 @@ export(float) var v_speed_damp: float = 1.0
export(NodePath) var animation_player_node: NodePath = "" \
setget _set_animation_player_node


# ESCAnimationsResource (for walking, idling...)
var animations: ESCAnimationResource

# Reference to the animation node (null if none was found)
var animation_sprite = null

# Reference to the sprite node
var _sprite_node: Node = null

# Reference to the current terrain
var terrain: ESCTerrain

# Reference to this items collision shape node
var collision: Node

# The representation of this item in the scene. Will
# be loaded, if inventory_item_scene_file is set.
var inventory_item: ESCInventoryItem = null setget ,_get_inventory_item

# Reference to the sprite node
var _sprite_node: Node = null

# The movable subnode
var _movable: ESCMovable = null
Expand Down Expand Up @@ -464,15 +461,6 @@ func _update_terrain(rc: int, event_name: String) -> void:
_movable.update_terrain(event_name)


# Get inventory item from the inventory item scene
# **Returns** The inventory item of this ESCitem
func _get_inventory_item() -> ESCInventoryItem:
if not inventory_item and inventory_item_scene_file:
inventory_item = inventory_item_scene_file.instance()
inventory_item.global_id = self.global_id
return inventory_item


func _get_property_list():
var properties = []
properties.append({
Expand Down Expand Up @@ -505,3 +493,15 @@ func _set_animation_player_node(node_path: NodePath):
)

animation_player_node = node_path


# Returns either the set inventory texture or the texture of a TextureRect
# found as a child if it is null
func _get_inventory_texture() -> Texture:
if inventory_texture == null:
for c in get_children():
if c is TextureRect or c is Sprite:
return c.texture
return null
else:
return inventory_texture
22 changes: 11 additions & 11 deletions addons/escoria-core/game/scenes/inventory/inventory_ui.gd
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,26 @@ func add_new_item_by_id(item_id: String) -> void:
"Check item's id in ESCORIA_ALL_ITEMS scene."
]
)
var item_inventory_button = (
escoria.object_manager.get_object(item_id).node as ESCItem
).inventory_item.duplicate()
item_inventory_button.global_id = item_id
items_ids_in_inventory[item_id] = item_inventory_button
get_node(inventory_ui_container).add_item(item_inventory_button)

var inventory_item = ESCInventoryItem.new(
escoria.object_manager.get_object(item_id).node
)
var inventory_item_button = get_node(
inventory_ui_container
).add_item(inventory_item)

items_ids_in_inventory[item_id] = inventory_item

if not escoria.object_manager.has(item_id):
escoria.object_manager.register_object(
ESCObject.new(
item_id,
item_inventory_button
inventory_item_button
),
true
)

item_inventory_button.visible = true

escoria.inputs_manager.register_inventory_item(item_inventory_button)
escoria.inputs_manager.register_inventory_item(inventory_item_button)


# remove item fromInventory UI using its id set in its scene
Expand Down Expand Up @@ -117,7 +118,6 @@ func remove_item_by_id(item_id: String) -> void:
)

get_node(inventory_ui_container).remove_item(item_inventory_button)
item_inventory_button.queue_free()
items_ids_in_inventory.erase(item_id)


Expand Down
107 changes: 107 additions & 0 deletions addons/escoria-core/library/inventory/esc_inventory_button.gd
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 addons/escoria-core/library/inventory/esc_inventory_container.gd
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()
Loading

0 comments on commit af26521

Please sign in to comment.