Skip to content

Implement drag and drop of pieces#62

Merged
fsmosca merged 2 commits intomasterfrom
copilot/implement-drag-and-drop
Mar 29, 2026
Merged

Implement drag and drop of pieces#62
fsmosca merged 2 commits intomasterfrom
copilot/implement-drag-and-drop

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 29, 2026

Adds drag-and-drop piece movement alongside the existing two-click system. Users can now press on a piece, drag to a destination square, and release to make a move.

Approach

Uses tkinter's ButtonPress-1/ButtonRelease-1 bindings on the existing RButton board squares. On release, winfo_containing() resolves the target square under the cursor. If source ≠ target, a __drag_move__ event is injected into FreeSimpleGUI's event queue via write_event_value().

The drag handler in play_game() sets up move_from, piece, moved_piece, move_state=1, and reassigns button = drag_to — letting the existing move_state==1 code path handle validation, execution, promotion, castling, en passant, and all UI updates with zero duplication.

Changes

  • __init__ — drag state variables (_drag_source, _widget_to_square, _drag_window)
  • setup_board_drag_drop(window) — binds press/release events to all 64 square widgets, builds widget→square reverse mapping
  • _on_drag_press / _on_drag_release — tracks source on press; on release, resolves target via winfo_containing() with widget hierarchy walk, injects __drag_move__ event
  • play_game() event loop — converts __drag_move__ into the existing move-state machine (handles mid-click override gracefully)
  • create_new_window() — re-binds drag events when the board is flipped/recreated
  • main_loop() — initial binding after window finalization

Copilot AI and others added 2 commits March 29, 2026 02:07
Add drag-and-drop support alongside the existing two-click piece
movement. Uses tkinter ButtonPress/ButtonRelease bindings on each
board square button to detect when a piece is dragged from one
square to another. The target square is determined via
winfo_containing() and the move is injected into the FreeSimpleGUI
event loop via write_event_value(), where the existing move
validation and execution code handles it.

Agent-Logs-Url: https://github.com/fsmosca/Python-Easy-Chess-GUI/sessions/8a412d60-5a44-4b76-91b9-d11bd4e558c2

Co-authored-by: fsmosca <22366935+fsmosca@users.noreply.github.com>
@fsmosca fsmosca marked this pull request as ready for review March 29, 2026 02:54
@fsmosca fsmosca merged commit 8541edb into master Mar 29, 2026
@fsmosca fsmosca deleted the copilot/implement-drag-and-drop branch March 29, 2026 02:55
@fsmosca fsmosca restored the copilot/implement-drag-and-drop branch March 29, 2026 03:04
@fsmosca fsmosca deleted the copilot/implement-drag-and-drop branch March 29, 2026 04:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants