fix: multi-cell tile drag snaps by module position, not mouse

_end_drag now calculates the target grid cell from the module's visual
top-left position (mouse_pos - grab_offset) instead of the raw mouse
position. This prevents multi-cell tiles from jumping an extra cell when
grabbed near an edge.
This commit is contained in:
Eric Smith 2026-05-21 09:09:43 -04:00
parent db0c6780df
commit 6aba0e23d0
2 changed files with 10 additions and 5 deletions

View file

@ -1,7 +1,7 @@
[layout] [layout]
name="Main" name="Main"
saved_at="2026-05-21T09:03:37" saved_at="2026-05-21T09:09:37"
[tiles] [tiles]
@ -27,6 +27,6 @@ h=1
id="system_monitor/testing" id="system_monitor/testing"
col=2 col=2
row=1 row=0
w=1 w=1
h=2 h=2

View file

@ -548,15 +548,20 @@ func _end_drag(mouse_pos: Vector2) -> void:
_drag_module.modulate = Color(1, 1, 1, 1) _drag_module.modulate = Color(1, 1, 1, 1)
var d := _get_module_grid_data(_drag_module) var d := _get_module_grid_data(_drag_module)
var cell_pos := _cell_at_position(mouse_pos) # Calculate target cell from the module's visual position (which already
# accounts for the grab offset), not from the raw mouse position.
# This ensures multi-cell tiles snap to the grid based on where the
# module's top-left would logically land, not where the pointer is.
var snap_pos := _drag_module.position + Vector2(_cell_w * 0.5, _cell_h * 0.5)
var cell_pos := _cell_at_position(snap_pos)
var target_col := cell_pos.x var target_col := cell_pos.x
var target_row := cell_pos.y var target_row := cell_pos.y
# Check if mouse is actually inside the cell bounds # Check if the module's center is actually inside the cell bounds
var on_cell := false var on_cell := false
if _is_valid_cell(target_col, target_row): if _is_valid_cell(target_col, target_row):
var cell_ref: PanelContainer = _cells[target_row][target_col] var cell_ref: PanelContainer = _cells[target_row][target_col]
on_cell = Rect2(Vector2.ZERO, cell_ref.size).has_point(mouse_pos - cell_ref.position) on_cell = Rect2(Vector2.ZERO, cell_ref.size).has_point(snap_pos - cell_ref.position)
if not on_cell or not _is_valid_cell(target_col, target_row): if not on_cell or not _is_valid_cell(target_col, target_row):
_drop_to_source() _drop_to_source()