mirror of
https://github.com/omegaleo/Tutorials.git
synced 2026-11-08 17:43:27 +00:00
65 lines
1.9 KiB
GDScript
65 lines
1.9 KiB
GDScript
extends Panel
|
|
class_name InventorySlotGD
|
|
|
|
@onready var icon_rect: TextureRect = $Icon
|
|
@onready var count_label: Label = $CountLabel
|
|
|
|
@export var slot_data : InventorySlotDataGD # Slot data as defined in the InventoryGD class
|
|
@export var slot_index: int
|
|
@export var main_inventory: InventoryGD
|
|
|
|
# Refresh the current slot with the data of it's inventory slot
|
|
func refresh():
|
|
if (slot_index < main_inventory.slots.size()):
|
|
slot_data = main_inventory.slots[slot_index]
|
|
|
|
if slot_data == null:
|
|
icon_rect.texture = null
|
|
count_label.text = ""
|
|
else:
|
|
icon_rect.texture = slot_data.item.icon
|
|
count_label.text = str(slot_data.count) if slot_data.count > 1 else ""
|
|
|
|
# Handle Item being dragged elsewhere
|
|
func _get_drag_data(at_position: Vector2) -> Variant:
|
|
if slot_data == null:
|
|
return null
|
|
|
|
# Create a preview of the item being dragged
|
|
var preview = TextureRect.new()
|
|
preview.texture = slot_data.item.icon
|
|
preview.custom_minimum_size = Vector2(32,32)
|
|
|
|
set_drag_preview(preview)
|
|
|
|
var data = { "from_slot": slot_index, "payload": slot_data }
|
|
|
|
return data
|
|
|
|
func _can_drop_data(at_position: Vector2, data: Variant) -> bool:
|
|
return typeof(data) == TYPE_DICTIONARY and data.has("from_slot")
|
|
|
|
# Can we drop the item where we are?
|
|
func _drop_data(_pos, data):
|
|
var from_index = data.from_slot
|
|
|
|
# Swap the 2 indexes
|
|
var temp = main_inventory.slots[slot_index]
|
|
main_inventory.slots[slot_index] = main_inventory.slots[from_index]
|
|
main_inventory.slots[from_index] = temp
|
|
|
|
# Call the refresh method for all objects that belong in the inventory_slots group
|
|
get_tree().call_group("inventory_slots", "refresh")
|
|
|
|
func initialize_slot(index: int, inventory: InventoryGD):
|
|
slot_index = index
|
|
main_inventory = inventory
|
|
|
|
if index < main_inventory.slots.size():
|
|
var inventory_slot = main_inventory.slots[slot_index]
|
|
|
|
if inventory_slot != null :
|
|
slot_data = inventory_slot
|
|
|
|
refresh()
|