From 3cc84d5ec8913e9d86b9065ff4e82767048e9cc5 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sat, 7 Jan 2023 16:04:06 +0100 Subject: [PATCH] Now arrows can be drawn using right clicks and drag --- TODO.md | 2 +- src/game_tab/left_panel/board/BoardCanvas.cpp | 39 ++++++++++++++++--- src/game_tab/left_panel/board/BoardCanvas.hpp | 2 +- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/TODO.md b/TODO.md index a1feb09..ef013b8 100644 --- a/TODO.md +++ b/TODO.md @@ -9,7 +9,7 @@ ## Additional Features - [ ] Add a live evaluation bar to the BoardCanvas - - [ ] Be able to draw arrows on the Board + - [x] Be able to draw arrows on the Board - [ ] Highlight the last played move - [ ] Be able to play against an engine - [ ] Implement full chess engine game analyzer/annotator diff --git a/src/game_tab/left_panel/board/BoardCanvas.cpp b/src/game_tab/left_panel/board/BoardCanvas.cpp index eabc615..8687639 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.cpp +++ b/src/game_tab/left_panel/board/BoardCanvas.cpp @@ -15,6 +15,7 @@ BoardCanvas::BoardCanvas(wxFrame *parent) lock_square_size(false), t(new Theme()), t_captures(new Theme()) { is_dragging = false; valid_drag = false; + valid_arrow = false; // Init animation data adata.duration=200; adata.duration_fast=80; @@ -45,6 +46,8 @@ BoardCanvas::BoardCanvas(wxFrame *parent) Bind(wxEVT_MOTION, &BoardCanvas::MouseEvent, this); Bind(wxEVT_LEFT_DOWN, &BoardCanvas::MouseEvent, this); Bind(wxEVT_LEFT_UP, &BoardCanvas::MouseEvent, this); + Bind(wxEVT_RIGHT_DOWN, &BoardCanvas::MouseEvent, this); + Bind(wxEVT_RIGHT_UP, &BoardCanvas::MouseEvent, this); } void BoardCanvas::OnResize(wxSizeEvent &e){ @@ -434,6 +437,19 @@ void BoardCanvas::MouseEvent(wxMouseEvent &event) { is_dragging = true; Refresh(); } + } else if (valid_arrow && event.RightUp()) { + valid_arrow=false; + // Handle drop + REFRESH_MOUSE_LOCATION(); + INIT_CURRENT_SQUARE(); + if (IsCurrentSquareValid) { + std::string arrow = ((char)('a' + active_square.x)) + + std::to_string(+active_square.y + 1) + + ((char)('a' + file)) + std::to_string(rank + 1); + gs.arrows.push_back(arrow); + wxLogDebug("Draw arrow %s",arrow); + Refresh(); + } } else { if (is_dragging) { is_dragging = false; @@ -457,7 +473,7 @@ void BoardCanvas::MouseEvent(wxMouseEvent &event) { Refresh(); } } - if (event.LeftDown()) { + if (event.LeftDown() || event.RightDown()) { SetFocus(); REFRESH_MOUSE_LOCATION(); lastClickX = mouseX; @@ -466,12 +482,22 @@ void BoardCanvas::MouseEvent(wxMouseEvent &event) { if (IsCurrentSquareValid) { active_square.x = file; active_square.y = rank; - if (gs.board[(7 - rank) * 8 + file] != ' ') { - wxLogDebug("Drag start on square (%d,%d)", file, rank); - valid_drag = true; + if(event.LeftDown()){ + if (gs.board[(7 - rank) * 8 + file] != ' ') { + wxLogDebug("Drag start on square (%d,%d)", file, rank); + valid_drag = true; + } + } + else { + wxLogDebug("Arrow start on square (%d,%d)", file, rank); + valid_arrow = true; } } } + else if(event.LeftUp()){ + gs.arrows.clear(); + Refresh(); + } } } // Let GameTableLeftPanel process mouse wheel events: @@ -506,6 +532,7 @@ void BoardCanvas::DrawArrow(wxDC &dc, int xsrc, int ysrc, int xdst, int ydst){ wxPoint vect(xdst-xsrc,ydst-ysrc); double length=ceil(sqrt(pow(vect.x,2)+pow(vect.y,2))); double angle=acos(vect.x/length); + angle= (vect.y>0) ? angle=angle : -angle; // Compute metrics std::uint8_t thickness=50; @@ -514,8 +541,8 @@ void BoardCanvas::DrawArrow(wxDC &dc, int xsrc, int ysrc, int xdst, int ydst){ // Setup transform matrix wxAffineMatrix2D rot_m; - rot_m.Rotate(-angle); - + rot_m.Rotate(angle); + wxLogDebug("Angle is %d,%d",(int)vect.x,(int)vect.y); // Init polygons wxPoint tip[]={wxPoint(tail_length,-thickness/2),wxPoint(tail_length,thickness/2),wxPoint(tail_length+tip_height,0)}; wxPoint tail[]={wxPoint(0,-thickness/4),wxPoint(tail_length,-thickness/4),wxPoint(tail_length,thickness/4),wxPoint(0,thickness/4)}; diff --git a/src/game_tab/left_panel/board/BoardCanvas.hpp b/src/game_tab/left_panel/board/BoardCanvas.hpp index 946711f..bb50098 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.hpp +++ b/src/game_tab/left_panel/board/BoardCanvas.hpp @@ -80,7 +80,7 @@ class BoardCanvas : public wxPanel { std::string white_player,black_player; // Various canvas state variables - bool black_side, is_dragging, valid_drag, is_black_turn; + bool black_side, is_dragging, valid_drag, valid_arrow, is_black_turn; std::uint32_t boardX, boardY, square_width, piece_width, mouseX, mouseY, lastClickX, lastClickY; wxSize canvas_size;