feat: Rewrote inventory handling basing on ESCItems (#417)

Co-authored-by: Dennis Ploeger <develop@dieploegers.de>
This commit is contained in:
Dennis Ploeger
2021-10-21 21:56:25 +02:00
committed by GitHub
parent 0271046421
commit af26521d3d
35 changed files with 245 additions and 336 deletions

View File

@@ -1,27 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="16px" height="16px" viewBox="0 0 16 16" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>esc_inventory_item</title>
<defs>
<linearGradient x1="0%" y1="32.9861111%" x2="100%" y2="67.0138889%" id="linearGradient-1">
<stop stop-color="#D4FF2A" offset="0%"></stop>
<stop stop-color="#81D135" offset="100%"></stop>
</linearGradient>
<linearGradient x1="0%" y1="0%" x2="100%" y2="100%" id="linearGradient-2">
<stop stop-color="#D4FF2A" offset="0%"></stop>
<stop stop-color="#81D135" offset="100%"></stop>
</linearGradient>
</defs>
<g id="esc_inventory_item" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Group-Copy" transform="translate(2.000000, 4.000000)">
<rect id="Rectangle" fill="url(#linearGradient-1)" x="0" y="1" width="12" height="7" rx="1"></rect>
<path d="M10.2857143,0 C11.2324881,0 12,0.8954305 12,2 L12,7 C12,8.1045695 11.2324881,9 10.2857143,9 L1.71428571,9 C0.767511857,9 0,8.1045695 0,7 L0,2 C0,0.8954305 0.767511857,0 1.71428571,0 L10.2857143,0 Z M10.2857143,1 L1.71428571,1 C1.27471214,1 0.912422434,1.38604019 0.862909484,1.88337887 L0.857142857,2 L0.857142857,7 C0.857142857,7.51283584 1.18803445,7.93550716 1.61432475,7.99327227 L1.71428571,8 L10.2857143,8 C10.7252879,8 11.0875776,7.61395981 11.1370905,7.11662113 L11.1428571,7 L11.1428571,2 C11.1428571,1.48716416 10.8119656,1.06449284 10.3856753,1.00672773 L10.2857143,1 Z" id="Rectangle" fill="#1B2F0D" fill-rule="nonzero"></path>
</g>
<path d="M13,2 L13,12 L3,12 L3,2 L13,2 Z" id="Rectangle-Copy" fill="url(#linearGradient-2)" fill-rule="nonzero"></path>
<path d="M13,2 L13,12 L3,12 L3,2 L13,2 Z M12,3 L4,3 L4,11 L12,11 L12,3 Z" id="Rectangle" fill="#1B2F0D" fill-rule="nonzero"></path>
<g id="Group" transform="translate(1.000000, 6.000000)">
<rect id="Rectangle" fill="url(#linearGradient-1)" x="1" y="1" width="12" height="7"></rect>
<path d="M9,0 L9,5 L5,5 L5,0 L9,0 Z M8,1 L6,1 L6,4 L8,4 L8,1 Z" id="Rectangle" fill="#1B2F0D" fill-rule="nonzero"></path>
<path d="M12,0 C13.1045695,0 14,0.8954305 14,2 L14,7 C14,8.1045695 13.1045695,9 12,9 L2,9 C0.8954305,9 0,8.1045695 0,7 L0,2 C0,0.8954305 0.8954305,0 2,0 L12,0 Z M12,1 L2,1 C1.48716416,1 1.06449284,1.38604019 1.00672773,1.88337887 L1,2 L1,7 C1,7.51283584 1.38604019,7.93550716 1.88337887,7.99327227 L2,8 L12,8 C12.5128358,8 12.9355072,7.61395981 12.9932723,7.11662113 L13,7 L13,2 C13,1.48716416 12.6139598,1.06449284 12.1166211,1.00672773 L12,1 Z" id="Rectangle" fill="#1B2F0D" fill-rule="nonzero"></path>
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 2.7 KiB

View File

@@ -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
var global_id: String = ""
# The texture for the item
var texture: Texture = null
# 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")
func _init(p_item: ESCItem) -> void:
global_id = p_item.global_id
texture = p_item._get_inventory_texture()
# 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")

View File

@@ -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")
@@ -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
@@ -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({
@@ -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

View File

@@ -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
@@ -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)

View File

@@ -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")

View File

@@ -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()

View File

@@ -98,6 +98,16 @@ func set_escoria_ui_settings():
"type": TYPE_STRING_ARRAY,
"hint": PROPERTY_HINT_DIR
})
if !ProjectSettings.has_setting("escoria/ui/inventory_item_size"):
ProjectSettings.set_setting(
"escoria/ui/inventory_item_size",
Vector2(72, 72)
)
ProjectSettings.add_property_info({
"name": "escoria/ui/inventory_item_size",
"type": TYPE_VECTOR2
})
# Prepare the settings in the Escoria main category

View File

@@ -1,7 +1,7 @@
[gd_scene load_steps=3 format=2]
[ext_resource path="res://addons/escoria-core/game/scenes/inventory/inventory_ui.gd" type="Script" id=1]
[ext_resource path="res://addons/escoria-ui-9verbs/inventory/inventory_ui_container.gd" type="Script" id=3]
[ext_resource path="res://addons/escoria-core/library/inventory/esc_inventory_container.gd" type="Script" id=3]
[node name="inventory_ui" type="PanelContainer"]
margin_right = 600.0
@@ -17,10 +17,14 @@ margin_left = 7.0
margin_top = 7.0
margin_right = 593.0
margin_bottom = 168.0
size_flags_horizontal = 3
size_flags_vertical = 3
[node name="GridContainer" type="GridContainer" parent="ScrollContainer"]
margin_right = 586.0
margin_bottom = 161.0
size_flags_horizontal = 3
size_flags_vertical = 3
custom_constants/vseparation = 16
custom_constants/hseparation = 16
columns = 4

View File

@@ -1,39 +0,0 @@
extends Control
"""
This script is totally user-defined. It does exactly what the user wants the
inventory to look like. It only requires 4 functions to be defined:
- is_empty() -> bool
- get_items() -> Array
- add_item(inventory_item: ESCInventoryItem)
- remove_item(inventory_item: ESCInventoryItem)
The user is free to implement these methods the way s-he likes.
"""
var current_nodes_in_container = {}
func is_empty() -> bool:
return get_child_count() > 0
func get_items() -> Array:
return current_nodes_in_container.keys()
func add_item(inventory_item: ESCInventoryItem):
var center_container = CenterContainer.new()
center_container.size_flags_horizontal = SIZE_EXPAND_FILL
center_container.connect("mouse_entered", inventory_item, "_on_inventory_item_mouse_enter")
center_container.connect("mouse_exited", inventory_item, "_on_inventory_item_mouse_exit")
# center_container.connect("gui_input", self, "_on_gui_input", [inventory_item])
center_container.add_child(inventory_item)
add_child(center_container)
current_nodes_in_container[inventory_item] = center_container
func remove_item(inventory_item: ESCInventoryItem):
var node_to_remove = current_nodes_in_container[inventory_item]
current_nodes_in_container.erase(node_to_remove)
node_to_remove.disconnect("mouse_entered", inventory_item, "_on_inventory_item_mouse_enter")
node_to_remove.disconnect("mouse_exited", inventory_item, "_on_inventory_item_mouse_exit")
# node_to_remove.disconnect("pressed", self, "_on_gui_input")
remove_child(node_to_remove)
node_to_remove.queue_free()

View File

@@ -2,7 +2,7 @@
[ext_resource path="res://addons/escoria-ui-simplemouse/inventory/inventory_ui.gd" type="Script" id=1]
[ext_resource path="res://addons/escoria-ui-simplemouse/images/inventory_bg.png" type="Texture" id=2]
[ext_resource path="res://addons/escoria-ui-simplemouse/inventory/inventory_ui_container.gd" type="Script" id=3]
[ext_resource path="res://addons/escoria-core/library/inventory/esc_inventory_container.gd" type="Script" id=3]
[ext_resource path="res://addons/escoria-ui-simplemouse/images/frame.png" type="Texture" id=5]
[ext_resource path="res://addons/escoria-ui-simplemouse/images/inventory_icon.png" type="Texture" id=6]

View File

@@ -1,37 +0,0 @@
extends Control
"""
This script is totally user-defined. It does exactly what the user wants the
inventory to look like. It only requires 4 functions to be defined:
- is_empty() -> bool
- get_items() -> Array
- add_item(inventory_item: ESCInventoryItem)
- remove_item(inventory_item: ESCInventoryItem)
The user is free to implement these methods the way s-he likes.
"""
var current_nodes_in_container = {}
func is_empty() -> bool:
return get_child_count() > 0
func get_items() -> Array:
return current_nodes_in_container.keys()
func add_item(inventory_item: ESCInventoryItem):
var center_container = CenterContainer.new()
center_container.size_flags_horizontal = SIZE_EXPAND_FILL
center_container.connect("mouse_entered", inventory_item, "_on_inventory_item_mouse_enter")
center_container.connect("mouse_exited", inventory_item, "_on_inventory_item_mouse_exit")
center_container.add_child(inventory_item)
add_child(center_container)
current_nodes_in_container[inventory_item] = center_container
func remove_item(inventory_item: ESCInventoryItem):
var node_to_remove = current_nodes_in_container[inventory_item]
current_nodes_in_container.erase(node_to_remove)
node_to_remove.disconnect("mouse_entered", inventory_item, "_on_inventory_item_mouse_enter")
node_to_remove.disconnect("mouse_exited", inventory_item, "_on_inventory_item_mouse_exit")
remove_child(node_to_remove)
node_to_remove.queue_free()