fix drag: defer initial grid build, correct coordinate space for mouse offset, use gui_input signal

This commit is contained in:
Eric Smith 2026-05-20 14:04:14 -04:00
parent b44312fe6b
commit dd467d8fbb

View file

@ -30,11 +30,14 @@ var _cell_styles: Dictionary = {} # cell -> StyleBoxFlat
func _ready() -> void: func _ready() -> void:
mouse_filter = Control.MOUSE_FILTER_STOP
resized.connect(_rebuild_grid) resized.connect(_rebuild_grid)
_rebuild_grid() gui_input.connect(_on_gui_input)
# Defer initial build so layout is resolved and size is valid
call_deferred("_rebuild_grid")
func _gui_input(event: InputEvent) -> void: func _on_gui_input(event: InputEvent) -> void:
if event is InputEventMouseButton: if event is InputEventMouseButton:
var mb: InputEventMouseButton = event var mb: InputEventMouseButton = event
if mb.button_index == MOUSE_BUTTON_LEFT and not mb.double_click: if mb.button_index == MOUSE_BUTTON_LEFT and not mb.double_click:
@ -124,8 +127,14 @@ func _begin_drag(mouse_pos: Vector2) -> void:
_drag_source_col = col _drag_source_col = col
_drag_source_row = row _drag_source_row = row
# Calculate where the module sits in grid coordinates before reparenting.
# module.position is relative to the cell; cell.position is relative to us (the grid).
var cell: PanelContainer = _cells[row][col]
var module_origin_in_grid := cell.position + module.position
_drag_mouse_offset = mouse_pos - module_origin_in_grid
_drag_module = _take_module_from_cell(col, row) _drag_module = _take_module_from_cell(col, row)
_drag_mouse_offset = mouse_pos - _drag_module.position
_is_dragging = true _is_dragging = true
# Reparent to grid so it renders above all cells # Reparent to grid so it renders above all cells