extends Node ## DragManager - handles drag and drop between UI elements signal drag_started(data: Dictionary) signal drag_ended signal item_dropped(data: Dictionary, target_type: String, target_slot: int) var is_dragging: bool = false var drag_data: Dictionary = {} var drag_preview: Control = null func _ready(): _create_drag_preview() func _create_drag_preview(): drag_preview = PanelContainer.new() drag_preview.name = "DragPreview" drag_preview.visible = false drag_preview.z_index = 1000 drag_preview.mouse_filter = Control.MOUSE_FILTER_IGNORE var style = StyleBoxFlat.new() style.bg_color = Color(0.2, 0.2, 0.25, 0.9) style.set_corner_radius_all(4) drag_preview.add_theme_stylebox_override("panel", style) var label = Label.new() label.name = "Label" label.add_theme_font_size_override("font_size", 12) drag_preview.add_child(label) # Add to scene tree when ready call_deferred("_add_preview_to_tree") func _add_preview_to_tree(): var root = get_tree().root if root: root.add_child(drag_preview) func start_drag(item: Dictionary, source_type: String, source_slot: int): is_dragging = true drag_data = { "item": item.duplicate(), "source_type": source_type, "source_slot": source_slot } if drag_preview: var label = drag_preview.get_node_or_null("Label") if label: label.text = item.get("name", "Item") drag_preview.visible = true drag_started.emit(drag_data) func end_drag(): is_dragging = false drag_data = {} if drag_preview: drag_preview.visible = false drag_ended.emit() func drop_on(target_type: String, target_slot: int): if not is_dragging: return item_dropped.emit(drag_data, target_type, target_slot) end_drag() func get_drag_item() -> Dictionary: return drag_data.get("item", {}) func _process(_delta): if is_dragging and drag_preview: drag_preview.global_position = get_viewport().get_mouse_position() + Vector2(15, 10) func _input(event): # Cancel drag on right click or escape if is_dragging: if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_RIGHT: end_drag() elif event.is_action_pressed("ui_cancel"): end_drag()