2024-07-09 22:33:38 +02:00
|
|
|
class_name ItemSlot
|
|
|
|
extends PanelContainer
|
|
|
|
|
2024-09-08 10:54:39 +02:00
|
|
|
enum {
|
|
|
|
ALLOW_DRAG = 1,
|
|
|
|
ALLOW_DROP = 2
|
|
|
|
}
|
|
|
|
|
2024-07-09 22:33:38 +02:00
|
|
|
@onready var texture_rect:TextureRect = %TextureRect
|
2024-09-06 12:34:18 +02:00
|
|
|
@onready var count_label = %CountLabel
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-09-08 10:54:39 +02:00
|
|
|
var drag_drop_flags:int = 0
|
|
|
|
var _item_stack:ItemStack = null
|
|
|
|
|
|
|
|
func set_drag_drop_flags(flags:int) -> void:
|
|
|
|
drag_drop_flags = flags
|
2024-09-06 12:34:18 +02:00
|
|
|
|
2024-09-08 10:54:39 +02:00
|
|
|
func display(item_stack):
|
|
|
|
_item_stack = item_stack
|
|
|
|
|
|
|
|
func _process(_delta)->void:
|
|
|
|
if _item_stack == null:
|
|
|
|
return
|
|
|
|
|
|
|
|
if _item_stack.count == 0:
|
|
|
|
texture_rect.texture = null
|
|
|
|
update_quantity_text(0)
|
|
|
|
else:
|
|
|
|
texture_rect.texture = _item_stack.item.icon
|
|
|
|
update_quantity_text(_item_stack.count)
|
|
|
|
|
2024-09-06 12:34:18 +02:00
|
|
|
func update_quantity_text(stack_size) -> void:
|
|
|
|
if stack_size <= 1:
|
|
|
|
count_label.hide()
|
|
|
|
else:
|
|
|
|
count_label.text = str(stack_size)
|
|
|
|
count_label.show()
|
2024-09-08 10:54:39 +02:00
|
|
|
|
|
|
|
func _get_drag_data(_at_position:Vector2)->Variant:
|
|
|
|
if drag_drop_flags & ALLOW_DRAG == 0:
|
|
|
|
return null
|
|
|
|
|
|
|
|
if _item_stack == null:
|
|
|
|
return null
|
|
|
|
|
|
|
|
var drag_data:ItemDrag = ItemDrag.new(self, self)
|
|
|
|
|
|
|
|
var drag_preview = load("res://ui/item_slot.tscn").instantiate()
|
|
|
|
var drag_stack = ItemStack.new()
|
|
|
|
drag_stack.item = _item_stack.item
|
|
|
|
drag_stack.count = _item_stack.count
|
|
|
|
drag_preview.display(drag_stack)
|
|
|
|
set_drag_preview(drag_preview)
|
|
|
|
|
|
|
|
return drag_data
|
|
|
|
|
|
|
|
func _can_drop_data(_at_position:Vector2, data:Variant)->bool:
|
|
|
|
if drag_drop_flags & ALLOW_DROP == 0:
|
|
|
|
return false
|
|
|
|
|
|
|
|
if !data is ItemDrag:
|
|
|
|
return false
|
|
|
|
|
|
|
|
return _item_stack.count == 0
|
|
|
|
|
|
|
|
func _drop_data(_at_position:Vector2, data:Variant)->void:
|
|
|
|
if drag_drop_flags & ALLOW_DROP == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
if !data is ItemDrag:
|
|
|
|
return
|
|
|
|
|
|
|
|
var drag_data := data as ItemDrag
|
|
|
|
|
|
|
|
drag_data.destination = self
|
|
|
|
if drag_data.source is ItemSlot:
|
|
|
|
var source_slot := drag_data.source as ItemSlot
|
|
|
|
|
|
|
|
# add stuff to the destination
|
|
|
|
self._item_stack.item = source_slot._item_stack.item
|
|
|
|
self._item_stack.count = source_slot._item_stack.count
|
|
|
|
|
|
|
|
# remove it from the source
|
|
|
|
source_slot._item_stack.count = 0
|