2024-07-09 22:33:38 +02:00
|
|
|
class_name ItemSlot
|
2024-09-14 12:45:42 +02:00
|
|
|
extends Panel
|
2024-07-09 22:33:38 +02:00
|
|
|
|
2024-09-27 11:46:31 +02:00
|
|
|
signal mouse_entered_slot
|
|
|
|
signal mouse_left_pressed
|
|
|
|
|
2024-09-08 10:54:39 +02:00
|
|
|
enum {
|
|
|
|
ALLOW_DRAG = 1,
|
|
|
|
ALLOW_DROP = 2
|
|
|
|
}
|
|
|
|
|
2024-09-27 11:46:31 +02:00
|
|
|
@onready var select_panel = %SelectPanel
|
2024-09-14 12:45:42 +02:00
|
|
|
@onready var highlight_panel = %HighlightPanel
|
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-27 11:46:31 +02:00
|
|
|
|
2024-09-08 10:54:39 +02:00
|
|
|
var drag_drop_flags:int = 0
|
|
|
|
var _item_stack:ItemStack = null
|
|
|
|
|
2024-09-14 12:45:42 +02:00
|
|
|
var highlighted:bool = false : set = _set_highlighted
|
2024-09-27 11:46:31 +02:00
|
|
|
var selected:bool = false : set = _set_selected
|
2024-09-14 12:45:42 +02:00
|
|
|
|
|
|
|
func _set_highlighted(new_state):
|
|
|
|
highlight_panel.visible = new_state
|
|
|
|
|
2024-09-27 11:46:31 +02:00
|
|
|
func _set_selected(new_state):
|
|
|
|
select_panel.visible = new_state
|
|
|
|
|
2024-09-08 10:54:39 +02:00
|
|
|
func set_drag_drop_flags(flags:int) -> void:
|
|
|
|
drag_drop_flags = flags
|
2024-09-06 12:34:18 +02:00
|
|
|
|
2024-10-18 12:01:15 +02:00
|
|
|
func get_item() -> ItemResource:
|
2024-09-27 11:46:31 +02:00
|
|
|
if _item_stack == null:
|
|
|
|
return null
|
|
|
|
|
|
|
|
return _item_stack.item
|
|
|
|
|
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
|
|
|
|
|
2024-09-14 12:45:42 +02:00
|
|
|
theme_type_variation = "PanelHighlighted"
|
|
|
|
|
2024-09-08 10:54:39 +02:00
|
|
|
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
|
2024-09-27 11:46:31 +02:00
|
|
|
|
|
|
|
func _on_mouse_entered():
|
2024-10-11 18:40:40 +02:00
|
|
|
mouse_entered_slot.emit(self)
|
2024-09-27 11:46:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
func _on_gui_input(event):
|
|
|
|
var mouse_button_event = event as InputEventMouseButton
|
|
|
|
|
|
|
|
if mouse_button_event and mouse_button_event.button_index == MOUSE_BUTTON_LEFT and mouse_button_event.pressed:
|
2024-09-27 18:09:30 +02:00
|
|
|
get_viewport().set_input_as_handled()
|
2024-10-11 18:40:40 +02:00
|
|
|
mouse_left_pressed.emit(self)
|