breadwinner/scenes/ui/inventory/inventory_slot_ui.gd

71 lines
2.5 KiB
GDScript3
Raw Normal View History

class_name InventorySlotUI extends TextureRect
var slot_index: int = -1
var slot_type: String = "backpack" # Can be "backpack" or "hotbar"
var inventory_menu: InventoryMenu
var item_data: ItemData
# _init() runs the exact millisecond this node is created in memory
func _init() -> void:
# 1. Create a background panel
var bg = Panel.new()
bg.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT) # Make it fill the slot
bg.show_behind_parent = true # CRITICAL: Forces it to render BEHIND your item icon
bg.mouse_filter = Control.MOUSE_FILTER_IGNORE # Prevents it from eating drag-and-drop clicks
# 2. Give it a nice, dark, rounded style
var style = StyleBoxFlat.new()
style.bg_color = Color(0.15, 0.15, 0.15, 0.8) # Dark gray, slightly transparent
style.border_width_bottom = 2
style.border_width_top = 2
style.border_width_left = 2
style.border_width_right = 2
style.border_color = Color(0.3, 0.3, 0.3, 1.0) # Lighter gray border
style.corner_radius_top_left = 8
style.corner_radius_top_right = 8
style.corner_radius_bottom_left = 8
style.corner_radius_bottom_right = 8
bg.add_theme_stylebox_override("panel", style)
add_child(bg)
func _get_drag_data(_at_position: Vector2) -> Variant:
if item_data == null:
return null
var preview = TextureRect.new()
preview.texture = texture
preview.expand_mode = TextureRect.EXPAND_IGNORE_SIZE
preview.custom_minimum_size = Vector2(64, 64)
var preview_container = Control.new()
preview_container.add_child(preview)
preview.position = -preview.custom_minimum_size / 2
set_drag_preview(preview_container)
return {"source_index": slot_index, "source_type": slot_type}
func _can_drop_data(_at_position: Vector2, data: Variant) -> bool:
return typeof(data) == TYPE_DICTIONARY and data.has("source_index") and data.has("source_type")
func _drop_data(_at_position: Vector2, data: Variant) -> void:
var source_index = data["source_index"]
var source_type = data["source_type"]
inventory_menu.swap_items(source_type, source_index, slot_type, slot_index)
func _gui_input(event: InputEvent) -> void:
if event is InputEventMouseButton and event.pressed:
# RIGHT CLICK: Show Context Menu
if event.button_index == MOUSE_BUTTON_RIGHT:
if item_data != null:
inventory_menu.show_context_menu(self, get_global_mouse_position())
accept_event() # Tells Godot we handled this input
# CTRL + LEFT CLICK: Quick Move
elif event.button_index == MOUSE_BUTTON_LEFT and event.is_command_or_control_pressed():
if item_data != null:
inventory_menu.quick_move_item(self)
accept_event()