From 679c735596168228ad0df4e42a3768c61c809a40 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 14:12:57 +0100 Subject: [PATCH 01/18] Improve game tab --- src/game_tab/GameTab.cpp | 1 + src/game_tab/left_panel/GameTabLeftPanel.cpp | 16 +++++- src/game_tab/left_panel/board/BoardCanvas.cpp | 9 +--- src/game_tab/left_panel/board/BoardCanvas.hpp | 6 --- .../right_panel/GameTabRightPanel.cpp | 54 +------------------ .../right_panel/GameTabRightPanel.hpp | 7 --- .../right_panel/editor/EditorCanvas.cpp | 40 ++++---------- .../right_panel/editor/EditorCanvas.hpp | 12 ++--- 8 files changed, 31 insertions(+), 114 deletions(-) diff --git a/src/game_tab/GameTab.cpp b/src/game_tab/GameTab.cpp index 20196f3..8205788 100644 --- a/src/game_tab/GameTab.cpp +++ b/src/game_tab/GameTab.cpp @@ -57,6 +57,7 @@ void GameTab::OnGameChange(wxCommandEvent &event) { else { editor_panel->Notify(); board_panel->Notify(); + RefreshTabTitle(); } } diff --git a/src/game_tab/left_panel/GameTabLeftPanel.cpp b/src/game_tab/left_panel/GameTabLeftPanel.cpp index 50919c0..b1bf516 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.cpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.cpp @@ -72,14 +72,28 @@ void GameTabLeftPanel::OnPlay(wxCommandEvent &event) { } void GameTabLeftPanel::Notify(bool animate, bool backward) { + wxLogDebug("Called!"); // Update fen and captures std::string fen = game->GetFen(); std::map captures; HalfMove *m = game->GetCurrentMove(); + //animate=false; + //backward=false; if (m != nullptr) { captures = m->GetLineCaptures(); + /*HalfMove *parent=m->GetParent(); + if(!parent){ + animate=true; + } + if(last_absolute_move == parent->GetAbsoluteMove()){ + wxLogDebug("Next true!"); + animate=true; + } else if(m->GetAbsoluteMove() == last_absolute_move){ + animate=true; + backward=true; + }*/ } - + // Update board canvas: if(!animate){ if(m){ diff --git a/src/game_tab/left_panel/board/BoardCanvas.cpp b/src/game_tab/left_panel/board/BoardCanvas.cpp index 7f2c38d..1a85176 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.cpp +++ b/src/game_tab/left_panel/board/BoardCanvas.cpp @@ -75,10 +75,6 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) { adata.src.y + adata.frame*(adata.transVect.y/adata.frames), false); // end drawing adata.frame++; - if(adata.frame>=adata.frames){ - adata.reuseBuffer=false; - SetupBoard(adata.final_board, adata.final_is_black_turn, adata.final_captures,white_player,black_player); - } } } @@ -116,10 +112,6 @@ void BoardCanvas::SetupBoard(std::string board, bool is_black_turn, } void BoardCanvas::Animate(const std::string &board, bool is_black_turn, std::map captures, std::string src, std::string dst,bool faster){ - adata.final_board=board; - adata.final_is_black_turn=is_black_turn; - adata.final_captures=captures; - std::uint8_t pfile = src[0]-'a'; std::uint8_t prank = src[1]-'1'; adata.piece_moved = this->board[pfile + 8 * (7-prank)]; // Piece to move @@ -172,6 +164,7 @@ void BoardCanvas::Animate(const std::string &board, bool is_black_turn, std::map } adata.duration=faster ? duration_backup : duration_backup; adata.reuseBuffer=false; + SetupBoard(board, is_black_turn, captures,white_player,black_player); } void BoardCanvas::DrawBoard(wxDC &dc) { diff --git a/src/game_tab/left_panel/board/BoardCanvas.hpp b/src/game_tab/left_panel/board/BoardCanvas.hpp index 30f0d06..b26526f 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.hpp +++ b/src/game_tab/left_panel/board/BoardCanvas.hpp @@ -50,12 +50,6 @@ typedef struct AnimState { int duration,duration_fast; /// @brief Animation FPS std::uint8_t fps; - /// @brief Board to draw at the end of the animation - std::string final_board; - /// @brief Which player is to move at the end of the animation - bool final_is_black_turn; - /// @brief Final state of captured pieces at the end of the animation - std::map final_captures; /// @brief Current animated piece char piece_moved; /// @brief Starting point of the animated piece diff --git a/src/game_tab/right_panel/GameTabRightPanel.cpp b/src/game_tab/right_panel/GameTabRightPanel.cpp index c88202b..3a46f59 100644 --- a/src/game_tab/right_panel/GameTabRightPanel.cpp +++ b/src/game_tab/right_panel/GameTabRightPanel.cpp @@ -10,7 +10,7 @@ wxDEFINE_EVENT(NEXT_MOVE_EVENT, wxCommandEvent); GameTabRightPanel::GameTabRightPanel(wxFrame *parent, std::shared_ptr game) : TabGameRightPanel(parent), game(game), selected_item(-1), live_engine(nullptr) { - editor_canvas = new EditorCanvas((wxFrame *)editor_page); + editor_canvas = new EditorCanvas((wxFrame *)editor_page,game); editor_canvas_sizer->Add(editor_canvas, 1, wxEXPAND); tags_list->InsertColumn(0, L"Name", wxLIST_FORMAT_LEFT, 200); tags_list->InsertColumn(1, L"Value", wxLIST_FORMAT_LEFT, 500); @@ -22,16 +22,6 @@ GameTabRightPanel::GameTabRightPanel(wxFrame *parent, std::shared_ptr game // Bind events this->Bind(wxEVT_TEXT, &GameTabRightPanel::OnCommentChange, this, COMMENT_INPUT_BOX); - this->Bind(GOTO_MOVE_EVENT, &GameTabRightPanel::OnGotoMove, this, wxID_ANY); - this->Bind(DELETE_MOVE_EVENT, &GameTabRightPanel::OnMoveDelete, this, - wxID_ANY); - this->Bind(PROMOTE_MOVE_EVENT, &GameTabRightPanel::OnMovePromote, this, - wxID_ANY); - this->Bind(SET_AS_MAINLINE_EVENT, &GameTabRightPanel::OnMoveSetAsMainline, - this, wxID_ANY); - /*this->Bind(NEXT_MOVE_EVENT, &GameTabRightPanel::OnNextMove, this, wxID_ANY); - this->Bind(PREVIOUS_MOVE_EVENT, &GameTabRightPanel::OnPreviousMove, this, - wxID_ANY);*/ this->Bind(wxEVT_LIST_ITEM_SELECTED, &GameTabRightPanel::OnTagSelected, this, wxID_ANY); this->Bind(wxEVT_LIST_ITEM_DESELECTED, &GameTabRightPanel::OnTagDeselected, @@ -90,12 +80,6 @@ void GameTabRightPanel::OnTagDeselected(wxListEvent &event) { delete_button->Enable(false); } -void GameTabRightPanel::NotifyBoard() { - wxCommandEvent previousEvent(GAME_CHANGE, GetId()); - previousEvent.SetEventObject(this); - ProcessEvent(previousEvent); -} - void GameTabRightPanel::OnLiveEngineClose(wxCloseEvent &e) { live_engine = nullptr; e.Skip(); @@ -143,29 +127,6 @@ void GameTabRightPanel::OnDelete(wxCommandEvent &event) { } } -void GameTabRightPanel::OnGotoMove(wxCommandEvent &event) { - wxLogDebug("GameTabRightPanel: received GOTO_MOVE_EVENT"); - game->SetCurrent((HalfMove *)event.GetClientData()); - Notify(); -} - -void GameTabRightPanel::OnMoveDelete(wxCommandEvent &event) { - game->DeleteMove((HalfMove *)event.GetClientData()); - Notify(); -} - -void GameTabRightPanel::OnMovePromote(wxCommandEvent &event) { - wxLogDebug("GameTabRightPanel: promote move called"); - game->PromoteMove((HalfMove *)event.GetClientData()); - Notify(); -} - -void GameTabRightPanel::OnMoveSetAsMainline(wxCommandEvent &event) { - wxLogDebug("GameTabRightPanel: set move as mainline called"); - game->SetMoveAsMainline((HalfMove *)event.GetClientData()); - Notify(); -} - void GameTabRightPanel::Notify() { HalfMove *m = game->GetCurrentMove(); if (m != nullptr) { @@ -177,7 +138,6 @@ void GameTabRightPanel::Notify() { if (live_engine != nullptr) { live_engine->SetFEN(game->GetFen()); } - NotifyBoard(); } void GameTabRightPanel::ApplyPreferences() { @@ -206,18 +166,6 @@ void GameTabRightPanel::RefreshTagsList() { } } -void GameTabRightPanel::OnPreviousMove(wxCommandEvent &event) { - game->Previous(); - Notify(); - NotifyBoard(); -} - -void GameTabRightPanel::OnNextMove(wxCommandEvent &event) { - game->Next(); - Notify(); - NotifyBoard(); -} - std::string GameTabRightPanel::GetNagFromStr(std::string str){ // TODO: Bind more NAG! if(str=="!") diff --git a/src/game_tab/right_panel/GameTabRightPanel.hpp b/src/game_tab/right_panel/GameTabRightPanel.hpp index 0a353b6..aa28f17 100644 --- a/src/game_tab/right_panel/GameTabRightPanel.hpp +++ b/src/game_tab/right_panel/GameTabRightPanel.hpp @@ -23,20 +23,13 @@ class GameTabRightPanel : public TabGameRightPanel { public: GameTabRightPanel(wxFrame *parent, std::shared_ptr game); - void NotifyBoard(); void Notify(); void OnCommentChange(wxCommandEvent &event); - void OnGotoMove(wxCommandEvent &event); - void OnMoveDelete(wxCommandEvent &event); - void OnMovePromote(wxCommandEvent &event); - void OnMoveSetAsMainline(wxCommandEvent &event); void RefreshTagsList(); void OnTagSelected(wxListEvent &event); void OnTagDeselected(wxListEvent &event); void OnApply(wxCommandEvent &event); void OnDelete(wxCommandEvent &event); - void OnPreviousMove(wxCommandEvent &event); - void OnNextMove(wxCommandEvent &event); void OnLiveAnalysis(wxCommandEvent &event); void ApplyPreferences(); void OnLiveEngineClose(wxCloseEvent &e); diff --git a/src/game_tab/right_panel/editor/EditorCanvas.cpp b/src/game_tab/right_panel/editor/EditorCanvas.cpp index ae0c096..8a1c745 100644 --- a/src/game_tab/right_panel/editor/EditorCanvas.cpp +++ b/src/game_tab/right_panel/editor/EditorCanvas.cpp @@ -1,7 +1,7 @@ #include "EditorCanvas.hpp" -EditorCanvas::EditorCanvas(wxFrame *parent) - : wxPanel(parent), NeedRedraw(false) { +EditorCanvas::EditorCanvas(wxFrame *parent, std::shared_ptr game) + : wxPanel(parent), game(game), NeedRedraw(false) { hide_icon = LoadPNG("hide", wxSize(CGEditor::status.MoveIconWidth, CGEditor::status.MoveIconWidth)); t.ResizePieces(CGEditor::status.MoveIconWidth); @@ -173,28 +173,18 @@ void EditorCanvas::DrawElement(const cgeditor::Element &e) { } } void EditorCanvas::HandleEvent(const cgeditor::Event &e) { - wxLogDebug("Editor event!"); if (e.type == cgeditor::Event::Goto) { - wxCommandEvent event(GOTO_MOVE_EVENT, GetId()); - event.SetEventObject(this); - event.SetClientData(e.move); - ProcessEvent(event); + game->SetCurrent((HalfMove *)e.move); } else if (e.type == cgeditor::Event::Delete) { - wxCommandEvent event(DELETE_MOVE_EVENT, GetId()); - event.SetEventObject(this); - event.SetClientData(e.move); - ProcessEvent(event); + game->DeleteMove((HalfMove *)e.move); } else if (e.type == cgeditor::Event::Promote) { - wxCommandEvent event(PROMOTE_MOVE_EVENT, GetId()); - event.SetEventObject(this); - event.SetClientData(e.move); - ProcessEvent(event); + game->PromoteMove((HalfMove *)e.move); } else if (e.type == cgeditor::Event::SetAsMainline) { - wxCommandEvent event(SET_AS_MAINLINE_EVENT, GetId()); - event.SetEventObject(this); - event.SetClientData(e.move); - ProcessEvent(event); + game->SetMoveAsMainline((HalfMove *)e.move); } + wxCommandEvent event(GAME_CHANGE, GetId()); + event.SetEventObject(this); + ProcessEvent(event); } void EditorCanvas::MouseEvent(wxMouseEvent &event) { @@ -234,17 +224,5 @@ void EditorCanvas::SetMoves(HalfMove *moves, HalfMove *current) { Refresh(); } -void EditorCanvas::OnKeyEvent(wxKeyEvent &event) { - /*if (event.GetKeyCode() == WXK_LEFT) { - wxCommandEvent previousEvent(PREVIOUS_MOVE_EVENT, GetId()); - previousEvent.SetEventObject(this); - ProcessEvent(previousEvent); - } else if (event.GetKeyCode() == WXK_RIGHT) { - wxCommandEvent nextEvent(NEXT_MOVE_EVENT, GetId()); - nextEvent.SetEventObject(this); - ProcessEvent(nextEvent); - }*/ -} - wxBEGIN_EVENT_TABLE(EditorCanvas, wxPanel) EVT_PAINT(EditorCanvas::OnPaint) EVT_MOUSE_EVENTS(EditorCanvas::MouseEvent) wxEND_EVENT_TABLE() diff --git a/src/game_tab/right_panel/editor/EditorCanvas.hpp b/src/game_tab/right_panel/editor/EditorCanvas.hpp index 05d6258..7ee56d3 100644 --- a/src/game_tab/right_panel/editor/EditorCanvas.hpp +++ b/src/game_tab/right_panel/editor/EditorCanvas.hpp @@ -4,14 +4,10 @@ #include "CGEditor.hpp" #include "ochess.hpp" #include "../../left_panel/board/Theme.hpp" +#include "game_tab/Game.hpp" // Foreign events -wxDECLARE_EVENT(GOTO_MOVE_EVENT, wxCommandEvent); -wxDECLARE_EVENT(PREVIOUS_MOVE_EVENT, wxCommandEvent); -wxDECLARE_EVENT(NEXT_MOVE_EVENT, wxCommandEvent); -wxDECLARE_EVENT(DELETE_MOVE_EVENT, wxCommandEvent); -wxDECLARE_EVENT(PROMOTE_MOVE_EVENT, wxCommandEvent); -wxDECLARE_EVENT(SET_AS_MAINLINE_EVENT, wxCommandEvent); +wxDECLARE_EVENT(GAME_CHANGE, wxCommandEvent); class EditorCanvas : public wxPanel, public cgeditor::CGEditor { wxPaintDC *dc; @@ -20,6 +16,7 @@ class EditorCanvas : public wxPanel, public cgeditor::CGEditor { wxBitmap hide_icon; Theme t; wxFont default_font; + std::shared_ptr game; wxColour color_scrollbar_bg; wxColour color_scrollbar; @@ -29,13 +26,12 @@ class EditorCanvas : public wxPanel, public cgeditor::CGEditor { wxColour color_menu_item_bg; public: - EditorCanvas(wxFrame *parent); + EditorCanvas(wxFrame *parent, std::shared_ptr game); void OnPaint(wxPaintEvent &event); void MouseEvent(wxMouseEvent &event); void DrawElement(const cgeditor::Element &e); void HandleEvent(const cgeditor::Event &e); void SetMoves(HalfMove *moves, HalfMove *current); - void OnKeyEvent(wxKeyEvent &event); DECLARE_EVENT_TABLE() }; From 0cb695a22bf7371511893963a4896167d8c16ae1 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 14:35:20 +0100 Subject: [PATCH 02/18] Update animation trigger --- src/game_tab/left_panel/GameTabLeftPanel.cpp | 64 +++++++++----------- src/game_tab/left_panel/GameTabLeftPanel.hpp | 1 + 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/game_tab/left_panel/GameTabLeftPanel.cpp b/src/game_tab/left_panel/GameTabLeftPanel.cpp index b1bf516..2553d30 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.cpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.cpp @@ -22,6 +22,8 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr game) // Configure FEN field fen_text_field->SetFont(wxFont(*wxNORMAL_FONT).Bold().Larger()); + last_move=game->GetCurrentMove(); + // Bind events: Bind(PLAY_MOVE_EVENT, &GameTabLeftPanel::OnPlay, this, wxID_ANY); Bind(wxEVT_BUTTON, [bc=board_canvas](wxCommandEvent &event){bc->Zoom(10);}, ZOOM_IN_BTN); @@ -77,58 +79,50 @@ void GameTabLeftPanel::Notify(bool animate, bool backward) { std::string fen = game->GetFen(); std::map captures; HalfMove *m = game->GetCurrentMove(); - //animate=false; - //backward=false; + std::string src,dst; + animate=false; + backward=false; if (m != nullptr) { captures = m->GetLineCaptures(); - /*HalfMove *parent=m->GetParent(); - if(!parent){ + std::string absolute_move= m->GetAbsoluteMove(); + // Check if we should animate + if(m->GetParent()==last_move){ + wxLogDebug("Animate animate next"); animate=true; - } - if(last_absolute_move == parent->GetAbsoluteMove()){ - wxLogDebug("Next true!"); - animate=true; - } else if(m->GetAbsoluteMove() == last_absolute_move){ + src=absolute_move.substr(0,2); + dst=absolute_move.substr(2,2); + } else if (m->GetMainline()==last_move){ + wxLogDebug("Animate Previous"); animate=true; backward=true; - }*/ + src=absolute_move.substr(2,2); + dst=absolute_move.substr(0,2); + } else { + wxLogDebug("Animate Previous"); + for(auto v: m->GetVariations()){ + if(v==last_move){ + animate=true; + backward=true; + src=absolute_move.substr(2,2); + dst=absolute_move.substr(0,2); + } + } + } } // Update board canvas: if(!animate){ - if(m){ - last_absolute_move=m->GetAbsoluteMove(); - } board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board, game->IsBlackToPlay(), captures, game->GetTag("White"),game->GetTag("Black")); } else{ - if(backward && last_absolute_move.size()>0){ - std::string dst=last_absolute_move.substr(0,2); - std::string src=last_absolute_move.substr(2,2); - board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board, - game->IsBlackToPlay(), captures,src,dst,repeat); - if(m){ - last_absolute_move=m->GetAbsoluteMove(); - } - } - else if(!backward && m){ - std::string new_absolute_move=m->GetAbsoluteMove(); - if(last_absolute_move!=new_absolute_move){ - last_absolute_move=new_absolute_move; - std::string src=last_absolute_move.substr(0,2); - std::string dst=last_absolute_move.substr(2,2); - board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board, + board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board, game->IsBlackToPlay(), captures,src,dst,repeat); - } - } - // If m undefined - if(!m){ - last_absolute_move=""; - } } + last_move=m; + // Update fen field: fen_text_field->SetValue(game->GetFen()); } diff --git a/src/game_tab/left_panel/GameTabLeftPanel.hpp b/src/game_tab/left_panel/GameTabLeftPanel.hpp index b22d6fc..e2fd7bc 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.hpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.hpp @@ -13,6 +13,7 @@ class GameTabLeftPanel : public TabGameLeftPanel { BoardCanvas *board_canvas; std::string last_absolute_move; bool repeat; + HalfMove *last_move; public: GameTabLeftPanel(wxFrame *parent, std::shared_ptr game); From 0c64a5f9567b23fde5480975bf393d46f3203786 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 14:46:04 +0100 Subject: [PATCH 03/18] Update animation trigger --- src/game_tab/left_panel/GameTabLeftPanel.cpp | 23 +++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/game_tab/left_panel/GameTabLeftPanel.cpp b/src/game_tab/left_panel/GameTabLeftPanel.cpp index 2553d30..8b44e28 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.cpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.cpp @@ -84,20 +84,22 @@ void GameTabLeftPanel::Notify(bool animate, bool backward) { backward=false; if (m != nullptr) { captures = m->GetLineCaptures(); - std::string absolute_move= m->GetAbsoluteMove(); // Check if we should animate if(m->GetParent()==last_move){ - wxLogDebug("Animate animate next"); - animate=true; - src=absolute_move.substr(0,2); - dst=absolute_move.substr(2,2); + wxLogDebug("Animate animate next"); + std::string absolute_move= m->GetAbsoluteMove(); + animate=true; + src=absolute_move.substr(0,2); + dst=absolute_move.substr(2,2); } else if (m->GetMainline()==last_move){ wxLogDebug("Animate Previous"); + std::string absolute_move= last_move->GetAbsoluteMove(); animate=true; backward=true; src=absolute_move.substr(2,2); dst=absolute_move.substr(0,2); } else { + std::string absolute_move= last_move->GetAbsoluteMove(); wxLogDebug("Animate Previous"); for(auto v: m->GetVariations()){ if(v==last_move){ @@ -108,6 +110,15 @@ void GameTabLeftPanel::Notify(bool animate, bool backward) { } } } + }else if(last_move!=nullptr) { + if(last_move->GetParent()==nullptr){ + wxLogDebug("Animate Previous"); + std::string absolute_move= last_move->GetAbsoluteMove(); + animate=true; + backward=true; + src=absolute_move.substr(2,2); + dst=absolute_move.substr(0,2); + } } // Update board canvas: @@ -115,7 +126,7 @@ void GameTabLeftPanel::Notify(bool animate, bool backward) { board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board, game->IsBlackToPlay(), captures, game->GetTag("White"),game->GetTag("Black")); - } +} else{ board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board, game->IsBlackToPlay(), captures,src,dst,repeat); From ea76a42c6bb6c4432cb6a85e4388421c3ffd96f2 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 14:48:46 +0100 Subject: [PATCH 04/18] Update --- src/game_tab/left_panel/board/BoardCanvas.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/game_tab/left_panel/board/BoardCanvas.cpp b/src/game_tab/left_panel/board/BoardCanvas.cpp index 1a85176..ba6519f 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.cpp +++ b/src/game_tab/left_panel/board/BoardCanvas.cpp @@ -16,7 +16,7 @@ BoardCanvas::BoardCanvas(wxFrame *parent) ApplyPreferences(); // The following should be called when using an EVT_PAINT handler SetBackgroundStyle(wxBG_STYLE_PAINT); - adata.duration=200; + adata.duration=5000; adata.duration_fast=80; adata.fps=30; // Let GameTableLeftPanel process keyboard events: From f0e6f0b665527e027938d421bf5567d669b285c7 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 16:09:23 +0100 Subject: [PATCH 05/18] Update --- src/game_tab/left_panel/board/BoardCanvas.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/game_tab/left_panel/board/BoardCanvas.cpp b/src/game_tab/left_panel/board/BoardCanvas.cpp index ba6519f..29eac3e 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.cpp +++ b/src/game_tab/left_panel/board/BoardCanvas.cpp @@ -22,6 +22,9 @@ BoardCanvas::BoardCanvas(wxFrame *parent) // Let GameTableLeftPanel process keyboard events: Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();}); Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();}); + Bind(wxEVT_PAINT, &BoardCanvas::OnPaint, this); + //Bind(wxEVT_IDLE, [p=this](wxIdleEvent& event){p->Refresh();p->Update();}); + } BoardCanvas::~BoardCanvas() { @@ -44,6 +47,7 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) { wxBufferedPaintDC dc(this); dc.SetBackground(*wxWHITE_BRUSH); dc.Clear(); + wxLogDebug("lll"); if(!adata.reuseBuffer){ // Setting up required attributes @@ -73,6 +77,7 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) { dc.DrawBitmap(*t->Get(adata.piece_moved), adata.src.x + adata.frame*(adata.transVect.x/adata.frames), adata.src.y + adata.frame*(adata.transVect.y/adata.frames), false); + wxLogDebug("Here: %d",(int)adata.src.y + adata.frame*(adata.transVect.y/adata.frames)); // end drawing adata.frame++; } @@ -404,6 +409,6 @@ void BoardCanvas::SetClockTime(short hours, short min, short sec, } } -wxBEGIN_EVENT_TABLE(BoardCanvas, wxPanel) EVT_PAINT(BoardCanvas::OnPaint) +wxBEGIN_EVENT_TABLE(BoardCanvas, wxPanel) EVT_MOUSE_EVENTS(BoardCanvas::MouseEvent) wxEND_EVENT_TABLE() From 88430eec2951290633875b0f036a33bbc6ee60a2 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 17:30:55 +0100 Subject: [PATCH 06/18] Improve editor drawing loop --- libs/cgeditor | 2 +- src/game_tab/right_panel/editor/EditorCanvas.cpp | 15 +++++++++++---- src/game_tab/right_panel/editor/EditorCanvas.hpp | 1 - 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/libs/cgeditor b/libs/cgeditor index 3271972..31c332d 160000 --- a/libs/cgeditor +++ b/libs/cgeditor @@ -1 +1 @@ -Subproject commit 3271972f9eef3069bf80de8be4c057102fff1138 +Subproject commit 31c332da9ab426daa73b68772d0ef9c1b0744a50 diff --git a/src/game_tab/right_panel/editor/EditorCanvas.cpp b/src/game_tab/right_panel/editor/EditorCanvas.cpp index 8a1c745..3856102 100644 --- a/src/game_tab/right_panel/editor/EditorCanvas.cpp +++ b/src/game_tab/right_panel/editor/EditorCanvas.cpp @@ -1,7 +1,7 @@ #include "EditorCanvas.hpp" EditorCanvas::EditorCanvas(wxFrame *parent, std::shared_ptr game) - : wxPanel(parent), game(game), NeedRedraw(false) { + : wxPanel(parent), game(game) { hide_icon = LoadPNG("hide", wxSize(CGEditor::status.MoveIconWidth, CGEditor::status.MoveIconWidth)); t.ResizePieces(CGEditor::status.MoveIconWidth); @@ -210,10 +210,17 @@ void EditorCanvas::MouseEvent(wxMouseEvent &event) { Refresh(); } - // Should another draw of CGEditor be made? - if (NeedRedraw) { + // Handle editor events + Update(); + bool need_redraw=false; + for(auto event: status.Events){ + HandleEvent(event); + need_redraw=true; + } + if(need_redraw){ + status.Events.clear(); Refresh(); - NeedRedraw = false; + Update(); } } diff --git a/src/game_tab/right_panel/editor/EditorCanvas.hpp b/src/game_tab/right_panel/editor/EditorCanvas.hpp index 7ee56d3..740a6a1 100644 --- a/src/game_tab/right_panel/editor/EditorCanvas.hpp +++ b/src/game_tab/right_panel/editor/EditorCanvas.hpp @@ -11,7 +11,6 @@ wxDECLARE_EVENT(GAME_CHANGE, wxCommandEvent); class EditorCanvas : public wxPanel, public cgeditor::CGEditor { wxPaintDC *dc; - bool NeedRedraw; wxPoint Middle(cgeditor::Element e); wxBitmap hide_icon; Theme t; From 4e85af5e08b86fb32d6698836f3a227bc6f086b3 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 18:18:27 +0100 Subject: [PATCH 07/18] Update Game tab --- src/game_tab/GameTab.cpp | 2 +- src/game_tab/HalfMove.cpp | 12 ++++ src/game_tab/HalfMove.hpp | 2 + src/game_tab/left_panel/GameTabLeftPanel.cpp | 62 ++++---------------- src/game_tab/left_panel/GameTabLeftPanel.hpp | 2 +- 5 files changed, 27 insertions(+), 53 deletions(-) diff --git a/src/game_tab/GameTab.cpp b/src/game_tab/GameTab.cpp index 8205788..32534fd 100644 --- a/src/game_tab/GameTab.cpp +++ b/src/game_tab/GameTab.cpp @@ -21,7 +21,7 @@ GameTab::GameTab(wxFrame *parent, std::shared_ptr game) // Refresh panels RefreshTabTitle(); - board_panel->Notify(false, false); + board_panel->Notify(); editor_panel->Notify(); board_panel->Bind(wxEVT_TOOL,&GameTab::OnToolClick,this); diff --git a/src/game_tab/HalfMove.cpp b/src/game_tab/HalfMove.cpp index 7f19480..a78eb0d 100644 --- a/src/game_tab/HalfMove.cpp +++ b/src/game_tab/HalfMove.cpp @@ -177,6 +177,18 @@ void HalfMove::Promote() { } } +bool HalfMove::HasChild(HalfMove *m){ + if(m!=mainline){ + for(auto var: variations){ + if(m== var){ + return(true); + } + } + return false; + } + return true; +} + bool HalfMove::IsVariation() { HalfMove *m = this; HalfMove *p = HalfMove::parent; diff --git a/src/game_tab/HalfMove.hpp b/src/game_tab/HalfMove.hpp index 3d5177e..7bc8b22 100644 --- a/src/game_tab/HalfMove.hpp +++ b/src/game_tab/HalfMove.hpp @@ -45,6 +45,8 @@ public: void Promote(); /// @brief Check if current half move is within a variation bool IsVariation(); + /// @brief Return true if current moves has m as mainline or variation + bool HasChild(HalfMove *m); /// @brief Get the root of a variation HalfMove *GetRoot(); /// @brief Get parent of the current move diff --git a/src/game_tab/left_panel/GameTabLeftPanel.cpp b/src/game_tab/left_panel/GameTabLeftPanel.cpp index 8b44e28..e986653 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.cpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.cpp @@ -21,7 +21,6 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr game) // Configure FEN field fen_text_field->SetFont(wxFont(*wxNORMAL_FONT).Bold().Larger()); - last_move=game->GetCurrentMove(); // Bind events: @@ -33,11 +32,11 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr game) Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){ if(e.GetKeyCode() == WXK_RIGHT){ p->game->Next(); - p->Notify(true,false); + p->Notify(); p->repeat=true; } else if(e.GetKeyCode() == WXK_LEFT){ p->game->Previous(); - p->Notify(true,true); + p->Notify(); p->repeat=true; } // Notify other classes @@ -48,10 +47,10 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr game) Bind(wxEVT_MOUSEWHEEL, [p=this](wxMouseEvent& e){ if(e.GetWheelRotation()<0){ p->game->Next(); - p->Notify(true,false); + p->Notify(); }else { p->game->Previous(); - p->Notify(true,true); + p->Notify(); } // Notify other classes wxCommandEvent event(GAME_CHANGE, p->GetId()); @@ -69,71 +68,32 @@ void GameTabLeftPanel::OnPlay(wxCommandEvent &event) { event.SetEventObject(this); ProcessEvent(event); } - // Refresh board canvas: - Notify(); } -void GameTabLeftPanel::Notify(bool animate, bool backward) { +void GameTabLeftPanel::Notify(bool skip_animation) { wxLogDebug("Called!"); // Update fen and captures std::string fen = game->GetFen(); std::map captures; + bool animate=false; HalfMove *m = game->GetCurrentMove(); std::string src,dst; - animate=false; - backward=false; - if (m != nullptr) { + if (m) captures = m->GetLineCaptures(); - // Check if we should animate - if(m->GetParent()==last_move){ - wxLogDebug("Animate animate next"); - std::string absolute_move= m->GetAbsoluteMove(); - animate=true; - src=absolute_move.substr(0,2); - dst=absolute_move.substr(2,2); - } else if (m->GetMainline()==last_move){ - wxLogDebug("Animate Previous"); - std::string absolute_move= last_move->GetAbsoluteMove(); - animate=true; - backward=true; - src=absolute_move.substr(2,2); - dst=absolute_move.substr(0,2); - } else { - std::string absolute_move= last_move->GetAbsoluteMove(); - wxLogDebug("Animate Previous"); - for(auto v: m->GetVariations()){ - if(v==last_move){ - animate=true; - backward=true; - src=absolute_move.substr(2,2); - dst=absolute_move.substr(0,2); - } - } - } - }else if(last_move!=nullptr) { - if(last_move->GetParent()==nullptr){ - wxLogDebug("Animate Previous"); - std::string absolute_move= last_move->GetAbsoluteMove(); - animate=true; - backward=true; - src=absolute_move.substr(2,2); - dst=absolute_move.substr(0,2); - } - } + // Update board canvas: - if(!animate){ + if(!skip_animation || animate){ board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board, game->IsBlackToPlay(), captures, game->GetTag("White"),game->GetTag("Black")); -} + } else{ board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board, game->IsBlackToPlay(), captures,src,dst,repeat); } - + // Update last move last_move=m; - // Update fen field: fen_text_field->SetValue(game->GetFen()); } diff --git a/src/game_tab/left_panel/GameTabLeftPanel.hpp b/src/game_tab/left_panel/GameTabLeftPanel.hpp index e2fd7bc..5073e9c 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.hpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.hpp @@ -17,7 +17,7 @@ class GameTabLeftPanel : public TabGameLeftPanel { public: GameTabLeftPanel(wxFrame *parent, std::shared_ptr game); - void Notify(bool animate=false,bool backward=false); + void Notify(bool skip_animation=false); void OnPlay(wxCommandEvent &event); void OnGotoMove(wxCommandEvent &event); void OnRefreshBoard(wxCommandEvent &event); From bbf3282839eb9efe57b1451763ef0859198c33af Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 19:05:15 +0100 Subject: [PATCH 08/18] Update Game tab --- src/game_tab/Game.hpp | 2 ++ src/game_tab/HalfMove.cpp | 12 ------------ src/game_tab/HalfMove.hpp | 2 -- src/game_tab/left_panel/GameTabLeftPanel.cpp | 9 ++++----- src/game_tab/left_panel/GameTabLeftPanel.hpp | 5 ++--- 5 files changed, 8 insertions(+), 22 deletions(-) diff --git a/src/game_tab/Game.hpp b/src/game_tab/Game.hpp index 25284ee..9398056 100644 --- a/src/game_tab/Game.hpp +++ b/src/game_tab/Game.hpp @@ -5,6 +5,8 @@ #include "ochess.hpp" #include +#define UNPACK_ABSOLUTE_MOVE(MOVE,SRC,DST) {(SRC)=(MOVE).substr(0,2);(DST)=(MOVE).substr(2,2);} + class Game { std::string board; std::string initial_fen; diff --git a/src/game_tab/HalfMove.cpp b/src/game_tab/HalfMove.cpp index a78eb0d..7f19480 100644 --- a/src/game_tab/HalfMove.cpp +++ b/src/game_tab/HalfMove.cpp @@ -177,18 +177,6 @@ void HalfMove::Promote() { } } -bool HalfMove::HasChild(HalfMove *m){ - if(m!=mainline){ - for(auto var: variations){ - if(m== var){ - return(true); - } - } - return false; - } - return true; -} - bool HalfMove::IsVariation() { HalfMove *m = this; HalfMove *p = HalfMove::parent; diff --git a/src/game_tab/HalfMove.hpp b/src/game_tab/HalfMove.hpp index 7bc8b22..3d5177e 100644 --- a/src/game_tab/HalfMove.hpp +++ b/src/game_tab/HalfMove.hpp @@ -45,8 +45,6 @@ public: void Promote(); /// @brief Check if current half move is within a variation bool IsVariation(); - /// @brief Return true if current moves has m as mainline or variation - bool HasChild(HalfMove *m); /// @brief Get the root of a variation HalfMove *GetRoot(); /// @brief Get parent of the current move diff --git a/src/game_tab/left_panel/GameTabLeftPanel.cpp b/src/game_tab/left_panel/GameTabLeftPanel.cpp index e986653..1142e99 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.cpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.cpp @@ -21,7 +21,6 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr game) // Configure FEN field fen_text_field->SetFont(wxFont(*wxNORMAL_FONT).Bold().Larger()); - last_move=game->GetCurrentMove(); // Bind events: Bind(PLAY_MOVE_EVENT, &GameTabLeftPanel::OnPlay, this, wxID_ANY); @@ -68,6 +67,7 @@ void GameTabLeftPanel::OnPlay(wxCommandEvent &event) { event.SetEventObject(this); ProcessEvent(event); } + Notify(true); } void GameTabLeftPanel::Notify(bool skip_animation) { @@ -78,12 +78,13 @@ void GameTabLeftPanel::Notify(bool skip_animation) { bool animate=false; HalfMove *m = game->GetCurrentMove(); std::string src,dst; - if (m) + if (m){ captures = m->GetLineCaptures(); + } // Update board canvas: - if(!skip_animation || animate){ + if(skip_animation || !animate){ board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board, game->IsBlackToPlay(), captures, game->GetTag("White"),game->GetTag("Black")); @@ -92,8 +93,6 @@ void GameTabLeftPanel::Notify(bool skip_animation) { board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board, game->IsBlackToPlay(), captures,src,dst,repeat); } - // Update last move - last_move=m; // Update fen field: fen_text_field->SetValue(game->GetFen()); } diff --git a/src/game_tab/left_panel/GameTabLeftPanel.hpp b/src/game_tab/left_panel/GameTabLeftPanel.hpp index 5073e9c..359b815 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.hpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.hpp @@ -11,10 +11,9 @@ wxDECLARE_EVENT(GAME_CHANGE, wxCommandEvent); class GameTabLeftPanel : public TabGameLeftPanel { std::shared_ptr game; BoardCanvas *board_canvas; - std::string last_absolute_move; bool repeat; - HalfMove *last_move; - + std::string last_absolute_move; + public: GameTabLeftPanel(wxFrame *parent, std::shared_ptr game); void Notify(bool skip_animation=false); From dcf6c01e54c5306fac464706cff81e75ee8f05bf Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 19:13:34 +0100 Subject: [PATCH 09/18] Update Game tab --- src/game_tab/HalfMove.cpp | 15 +++++++++++++++ src/game_tab/HalfMove.hpp | 2 ++ 2 files changed, 17 insertions(+) diff --git a/src/game_tab/HalfMove.cpp b/src/game_tab/HalfMove.cpp index 7f19480..01621e2 100644 --- a/src/game_tab/HalfMove.cpp +++ b/src/game_tab/HalfMove.cpp @@ -177,6 +177,21 @@ void HalfMove::Promote() { } } +bool HalfMove::HasParent(HalfMove*m){ + return m==parent; +} + +bool HalfMove::HasChild(HalfMove*m){ + if(mainline==m){ + return true; + } + for(auto var: variations){ + if(var == m) + return true; + } + return false; +} + bool HalfMove::IsVariation() { HalfMove *m = this; HalfMove *p = HalfMove::parent; diff --git a/src/game_tab/HalfMove.hpp b/src/game_tab/HalfMove.hpp index 3d5177e..08a190b 100644 --- a/src/game_tab/HalfMove.hpp +++ b/src/game_tab/HalfMove.hpp @@ -46,6 +46,8 @@ public: /// @brief Check if current half move is within a variation bool IsVariation(); /// @brief Get the root of a variation + bool HasParent(HalfMove*m); + bool HasChild(HalfMove*m); HalfMove *GetRoot(); /// @brief Get parent of the current move HalfMove *GetParent(); From 29d5850b2f44ad520fc89324a110a721ac08841b Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 20:21:23 +0100 Subject: [PATCH 10/18] Improve board canvas --- src/game_tab/Game.cpp | 6 +++++ src/game_tab/Game.hpp | 1 + src/game_tab/left_panel/GameTabLeftPanel.cpp | 23 ++++++++++++++-- src/game_tab/left_panel/GameTabLeftPanel.hpp | 4 +-- src/game_tab/left_panel/board/BoardCanvas.cpp | 26 +++++++++++-------- src/game_tab/left_panel/board/BoardCanvas.hpp | 1 + src/game_tab/left_panel/board/Theme.cpp | 5 +++- src/game_tab/left_panel/board/Theme.hpp | 2 +- 8 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/game_tab/Game.cpp b/src/game_tab/Game.cpp index fdc754a..8ab67cb 100644 --- a/src/game_tab/Game.cpp +++ b/src/game_tab/Game.cpp @@ -142,6 +142,12 @@ void Game::Next() { } } +HalfMove *Game::GetNextMove(){ + if(current!=nullptr) + return current->GetMainline(); + return moves; +} + void Game::SetCurrent(HalfMove *m) { current = m; } std::string Game::GetFen() { diff --git a/src/game_tab/Game.hpp b/src/game_tab/Game.hpp index 9398056..e0337ce 100644 --- a/src/game_tab/Game.hpp +++ b/src/game_tab/Game.hpp @@ -27,6 +27,7 @@ public: void SetTag(std::string tagname, std::string value); void DeleteTag(std::string tagname); HalfMove *GetCurrentMove(); + HalfMove *GetNextMove(); HalfMove *GetMoves(); std::string GetFen(); std::string GetResult(); diff --git a/src/game_tab/left_panel/GameTabLeftPanel.cpp b/src/game_tab/left_panel/GameTabLeftPanel.cpp index 1142e99..fd89791 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.cpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.cpp @@ -21,6 +21,7 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr game) // Configure FEN field fen_text_field->SetFont(wxFont(*wxNORMAL_FONT).Bold().Larger()); + last_move=game->GetCurrentMove(); // Bind events: Bind(PLAY_MOVE_EVENT, &GameTabLeftPanel::OnPlay, this, wxID_ANY); @@ -62,12 +63,12 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr game) void GameTabLeftPanel::OnPlay(wxCommandEvent &event) { wxLogDebug("Game tab received PLAY_MOVE_EVENT"); if (game->Play(event.GetString().ToStdString())) { + Notify(true); // Notify other classes wxCommandEvent event(GAME_CHANGE, GetId()); event.SetEventObject(this); ProcessEvent(event); } - Notify(true); } void GameTabLeftPanel::Notify(bool skip_animation) { @@ -78,10 +79,26 @@ void GameTabLeftPanel::Notify(bool skip_animation) { bool animate=false; HalfMove *m = game->GetCurrentMove(); std::string src,dst; + + // Update capture and check if we should to animations during moves change: if (m){ captures = m->GetLineCaptures(); + if(m->HasParent(last_move)){ + UNPACK_ABSOLUTE_MOVE(m->GetAbsoluteMove(),src,dst); + animate=true; + }else if(m->HasChild(last_move)){ + // Accessing last_move here is safe since it is still + // in the tree of moves (since HasChild found it so not deleted) + UNPACK_ABSOLUTE_MOVE(last_move->GetAbsoluteMove(),dst,src); + animate=true; + } + } else if(game->GetNextMove()){ // First move animation + HalfMove *next=game->GetNextMove(); + if(next==last_move){ + UNPACK_ABSOLUTE_MOVE(game->GetNextMove()->GetAbsoluteMove(),dst,src); + animate=true; + } } - // Update board canvas: if(skip_animation || !animate){ @@ -93,6 +110,8 @@ void GameTabLeftPanel::Notify(bool skip_animation) { board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board, game->IsBlackToPlay(), captures,src,dst,repeat); } + // Update last move + last_move=m; // Update fen field: fen_text_field->SetValue(game->GetFen()); } diff --git a/src/game_tab/left_panel/GameTabLeftPanel.hpp b/src/game_tab/left_panel/GameTabLeftPanel.hpp index 359b815..56297d8 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.hpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.hpp @@ -12,8 +12,8 @@ class GameTabLeftPanel : public TabGameLeftPanel { std::shared_ptr game; BoardCanvas *board_canvas; bool repeat; - std::string last_absolute_move; - + HalfMove *last_move; + public: GameTabLeftPanel(wxFrame *parent, std::shared_ptr game); void Notify(bool skip_animation=false); diff --git a/src/game_tab/left_panel/board/BoardCanvas.cpp b/src/game_tab/left_panel/board/BoardCanvas.cpp index 29eac3e..5297250 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.cpp +++ b/src/game_tab/left_panel/board/BoardCanvas.cpp @@ -19,12 +19,22 @@ BoardCanvas::BoardCanvas(wxFrame *parent) adata.duration=5000; adata.duration_fast=80; adata.fps=30; + adata.buffer=new wxBitmap(500,500,32); // Let GameTableLeftPanel process keyboard events: Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();}); Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();}); Bind(wxEVT_PAINT, &BoardCanvas::OnPaint, this); - //Bind(wxEVT_IDLE, [p=this](wxIdleEvent& event){p->Refresh();p->Update();}); + Bind(wxEVT_SIZE, &BoardCanvas::OnResize, this); +} +void BoardCanvas::OnResize(wxSizeEvent &e){ + wxSize size=e.GetSize(); + if(size.x>100 && size.y>100){ + // Setup buffer (use for animations) + if(adata.buffer!=nullptr) + free(adata.buffer); + adata.buffer=new wxBitmap(size.x,size.y,32); + } } BoardCanvas::~BoardCanvas() { @@ -46,8 +56,6 @@ BoardCanvas::BoardCanvas(wxFrame *parent, std::uint32_t square_width, void BoardCanvas::OnPaint(wxPaintEvent &event) { wxBufferedPaintDC dc(this); dc.SetBackground(*wxWHITE_BRUSH); - dc.Clear(); - wxLogDebug("lll"); if(!adata.reuseBuffer){ // Setting up required attributes @@ -60,12 +68,8 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) { boardX = 0; if (boardY > canvas_size.y) boardY = 0; - - // Setup buffer (later use for animations) - if(adata.buffer!=nullptr) - free(adata.buffer); - adata.buffer=new wxBitmap(canvas_size.x,canvas_size.y,32); wxMemoryDC memDC(*adata.buffer); + memDC.Clear(); DrawBoard(memDC); dc.Blit(0,0,canvas_size.x,canvas_size.y,(wxDC*)&memDC,0,0); } @@ -77,8 +81,7 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) { dc.DrawBitmap(*t->Get(adata.piece_moved), adata.src.x + adata.frame*(adata.transVect.x/adata.frames), adata.src.y + adata.frame*(adata.transVect.y/adata.frames), false); - wxLogDebug("Here: %d",(int)adata.src.y + adata.frame*(adata.transVect.y/adata.frames)); - // end drawing + // End drawing adata.frame++; } } @@ -390,7 +393,8 @@ void BoardCanvas::MouseEvent(wxMouseEvent &event) { } void BoardCanvas::Zoom(std::int32_t zoom) { - t->Zoom(zoom); + if(!t->Zoom(zoom)) + return; t_captures->ResizePieces(t->GetPiecesSizes() * CAPTURE_FACTOR); Refresh(); } diff --git a/src/game_tab/left_panel/board/BoardCanvas.hpp b/src/game_tab/left_panel/board/BoardCanvas.hpp index b26526f..76aa60f 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.hpp +++ b/src/game_tab/left_panel/board/BoardCanvas.hpp @@ -89,6 +89,7 @@ public: void MouseEvent(wxMouseEvent &event); void Zoom(std::int32_t zoom); void Swap(); + void OnResize(wxSizeEvent &e); void SetupBoard(std::string board, bool is_black_turn, std::map captures, std::string white_player, std::string black_player); diff --git a/src/game_tab/left_panel/board/Theme.cpp b/src/game_tab/left_panel/board/Theme.cpp index 6ecbafc..0c05fa1 100644 --- a/src/game_tab/left_panel/board/Theme.cpp +++ b/src/game_tab/left_panel/board/Theme.cpp @@ -130,10 +130,13 @@ void Theme::ResizeSquares(std::uint32_t width) { skin_scaled['3']->SetMask(RoundedMask(width, 3)); } -void Theme::Zoom(int amount) { +bool Theme::Zoom(int amount) { double width = skin_scaled['s']->GetWidth() + amount; + if(width<=20) + return false; ResizeSquares(std::max(width, 1.0)); ResizePieces(std::max(width * PIECE_SIZE_FACTOR, 1.0)); + return true; } void Theme::SetSquareRadius(std::uint8_t radius) { diff --git a/src/game_tab/left_panel/board/Theme.hpp b/src/game_tab/left_panel/board/Theme.hpp index 88036d8..b896d09 100644 --- a/src/game_tab/left_panel/board/Theme.hpp +++ b/src/game_tab/left_panel/board/Theme.hpp @@ -29,7 +29,7 @@ public: void ResizeSquaresAndPieces(std::uint32_t width); void SetSquareRadius(std::uint8_t radius); std::uint8_t GetSquareRadius(); - void Zoom(int amount); + bool Zoom(int amount); double GetPiecesSizes(); double GetSquaresSizes(); From dd24427d81d04765cbe3426b511b1ad05952d087 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 20:28:39 +0100 Subject: [PATCH 11/18] Debug drawing canvas --- src/game_tab/left_panel/board/BoardCanvas.cpp | 2 +- src/game_tab/left_panel/board/Theme.cpp | 2 ++ src/game_tab/right_panel/editor/EditorCanvas.cpp | 6 +++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/game_tab/left_panel/board/BoardCanvas.cpp b/src/game_tab/left_panel/board/BoardCanvas.cpp index 5297250..0e44979 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.cpp +++ b/src/game_tab/left_panel/board/BoardCanvas.cpp @@ -16,7 +16,7 @@ BoardCanvas::BoardCanvas(wxFrame *parent) ApplyPreferences(); // The following should be called when using an EVT_PAINT handler SetBackgroundStyle(wxBG_STYLE_PAINT); - adata.duration=5000; + adata.duration=200; adata.duration_fast=80; adata.fps=30; adata.buffer=new wxBitmap(500,500,32); diff --git a/src/game_tab/left_panel/board/Theme.cpp b/src/game_tab/left_panel/board/Theme.cpp index 0c05fa1..6c662dc 100644 --- a/src/game_tab/left_panel/board/Theme.cpp +++ b/src/game_tab/left_panel/board/Theme.cpp @@ -134,6 +134,8 @@ bool Theme::Zoom(int amount) { double width = skin_scaled['s']->GetWidth() + amount; if(width<=20) return false; + if(width>=180) + return false; ResizeSquares(std::max(width, 1.0)); ResizePieces(std::max(width * PIECE_SIZE_FACTOR, 1.0)); return true; diff --git a/src/game_tab/right_panel/editor/EditorCanvas.cpp b/src/game_tab/right_panel/editor/EditorCanvas.cpp index 3856102..5a0afd0 100644 --- a/src/game_tab/right_panel/editor/EditorCanvas.cpp +++ b/src/game_tab/right_panel/editor/EditorCanvas.cpp @@ -14,13 +14,17 @@ EditorCanvas::EditorCanvas(wxFrame *parent, std::shared_ptr game) color_comments_bg=wxColour(255, 255, 204); color_current_move_bg=wxColour(216, 216, 216); color_menu_item_bg=wxColour(216, 216, 216); - + // The following should be called when using an EVT_PAINT handler + SetBackgroundStyle(wxBG_STYLE_PAINT); + Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();}); Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();}); } void EditorCanvas::OnPaint(wxPaintEvent &event) { wxPaintDC current_dc(this); + current_dc.SetBackground(*wxWHITE_BRUSH); + current_dc.Clear(); dc = ¤t_dc; // Refresh canvas size From 4f2c68de6013bab307b299cfd6158225475a3fc6 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 2 Jan 2023 08:15:51 +0100 Subject: [PATCH 12/18] Update CGEditor+debug --- libs/cgeditor | 2 +- src/game_tab/left_panel/GameTabLeftPanel.cpp | 2 +- src/game_tab/right_panel/editor/EditorCanvas.cpp | 8 +------- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/libs/cgeditor b/libs/cgeditor index 31c332d..5e18d43 160000 --- a/libs/cgeditor +++ b/libs/cgeditor @@ -1 +1 @@ -Subproject commit 31c332da9ab426daa73b68772d0ef9c1b0744a50 +Subproject commit 5e18d43a6ba4fd378178418de6b2ca7a67d64c21 diff --git a/src/game_tab/left_panel/GameTabLeftPanel.cpp b/src/game_tab/left_panel/GameTabLeftPanel.cpp index fd89791..fa97f6f 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.cpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.cpp @@ -63,12 +63,12 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr game) void GameTabLeftPanel::OnPlay(wxCommandEvent &event) { wxLogDebug("Game tab received PLAY_MOVE_EVENT"); if (game->Play(event.GetString().ToStdString())) { - Notify(true); // Notify other classes wxCommandEvent event(GAME_CHANGE, GetId()); event.SetEventObject(this); ProcessEvent(event); } + Notify(true); // Redraw event is move failed! Otherwise piece not resets to it initial position after dragging } void GameTabLeftPanel::Notify(bool skip_animation) { diff --git a/src/game_tab/right_panel/editor/EditorCanvas.cpp b/src/game_tab/right_panel/editor/EditorCanvas.cpp index 5a0afd0..19d81ec 100644 --- a/src/game_tab/right_panel/editor/EditorCanvas.cpp +++ b/src/game_tab/right_panel/editor/EditorCanvas.cpp @@ -216,13 +216,7 @@ void EditorCanvas::MouseEvent(wxMouseEvent &event) { // Handle editor events Update(); - bool need_redraw=false; - for(auto event: status.Events){ - HandleEvent(event); - need_redraw=true; - } - if(need_redraw){ - status.Events.clear(); + if(ProcessEvents()){ Refresh(); Update(); } From 98edb4253c0d131b855bee882530fa2b28906fc2 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 2 Jan 2023 08:28:59 +0100 Subject: [PATCH 13/18] Debug BoardCanvas --- src/game_tab/left_panel/board/BoardCanvas.cpp | 12 ++++++++---- src/game_tab/left_panel/board/BoardCanvas.hpp | 1 - 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/game_tab/left_panel/board/BoardCanvas.cpp b/src/game_tab/left_panel/board/BoardCanvas.cpp index 0e44979..6581934 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.cpp +++ b/src/game_tab/left_panel/board/BoardCanvas.cpp @@ -25,6 +25,11 @@ BoardCanvas::BoardCanvas(wxFrame *parent) Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();}); Bind(wxEVT_PAINT, &BoardCanvas::OnPaint, this); Bind(wxEVT_SIZE, &BoardCanvas::OnResize, this); + + // Mouse events: + Bind(wxEVT_MOTION, &BoardCanvas::MouseEvent, this); + Bind(wxEVT_LEFT_DOWN, &BoardCanvas::MouseEvent, this); + Bind(wxEVT_LEFT_UP, &BoardCanvas::MouseEvent, this); } void BoardCanvas::OnResize(wxSizeEvent &e){ @@ -367,6 +372,9 @@ void BoardCanvas::MouseEvent(wxMouseEvent &event) { ((char)('a' + file)) + std::to_string(rank + 1); event.SetString(move); ProcessEvent(event); + } else { + // If square not valid just redraw (place piece back to its square) + Refresh(); } } if (event.LeftDown()) { @@ -412,7 +420,3 @@ void BoardCanvas::SetClockTime(short hours, short min, short sec, white_time = std::make_tuple(hours, min, sec); } } - -wxBEGIN_EVENT_TABLE(BoardCanvas, wxPanel) - EVT_MOUSE_EVENTS(BoardCanvas::MouseEvent) - wxEND_EVENT_TABLE() diff --git a/src/game_tab/left_panel/board/BoardCanvas.hpp b/src/game_tab/left_panel/board/BoardCanvas.hpp index 76aa60f..9a00262 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.hpp +++ b/src/game_tab/left_panel/board/BoardCanvas.hpp @@ -95,5 +95,4 @@ public: std::string white_player, std::string black_player); void Animate(const std::string &board, bool is_black_turn, std::map captures, std::string src, std::string dst,bool faster); void SetClockTime(short hours, short min, short sec, bool IsBlack); - DECLARE_EVENT_TABLE() }; From 3e40032109886786e469796dced8523d60b90ee4 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 2 Jan 2023 10:12:20 +0100 Subject: [PATCH 14/18] Simplify absolute moves --- src/game_tab/Game.hpp | 2 -- src/game_tab/HalfMove.cpp | 22 +++++++++++++++----- src/game_tab/HalfMove.hpp | 7 +++---- src/game_tab/left_panel/GameTabLeftPanel.cpp | 10 ++++----- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/game_tab/Game.hpp b/src/game_tab/Game.hpp index e0337ce..bc00c5a 100644 --- a/src/game_tab/Game.hpp +++ b/src/game_tab/Game.hpp @@ -5,8 +5,6 @@ #include "ochess.hpp" #include -#define UNPACK_ABSOLUTE_MOVE(MOVE,SRC,DST) {(SRC)=(MOVE).substr(0,2);(DST)=(MOVE).substr(2,2);} - class Game { std::string board; std::string initial_fen; diff --git a/src/game_tab/HalfMove.cpp b/src/game_tab/HalfMove.cpp index 01621e2..ae63527 100644 --- a/src/game_tab/HalfMove.cpp +++ b/src/game_tab/HalfMove.cpp @@ -1,13 +1,13 @@ #include "HalfMove.hpp" HalfMove::HalfMove(std::string move_absolute,std::string move_san) : capture(' ') { - this->move_absolute=move_absolute; + SetAbsoluteMove(move_absolute); this->move = move_san; fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; } HalfMove::HalfMove(std::string move_absolute, std::string move_san, std::string fen) : fen(fen), capture(' ') { - this->move_absolute=move_absolute; + SetAbsoluteMove(move_absolute); this->move = move_san; } @@ -21,7 +21,8 @@ HalfMove::~HalfMove() { } HalfMove::HalfMove(HalfMove *m){ - move_absolute=m->move_absolute; + src=m->src; + dst=m->dst; move=m->move; fen=m->fen; capture=m->capture; @@ -137,6 +138,11 @@ void HalfMove::SetAsMainline() { HalfMove *HalfMove::GetMainline() { return (mainline); } +void HalfMove::SetAbsoluteMove(const std::string &move_absolute){ + this->src=move_absolute.substr(0,2); + this->dst=move_absolute.substr(2,2); +} + HalfMove::HalfMove(pgnp::HalfMove *m) : capture(' ') { this->move = m->move; this->nag = m->NAG; @@ -151,6 +157,11 @@ HalfMove::HalfMove(pgnp::HalfMove *m) : capture(' ') { } } +void HalfMove::GetAbsoluteMove(std::string &src,std::string &dst){ + src=this->src; + dst=this->dst; +} + void HalfMove::SetFen(std::string fen) { this->fen = fen; } void HalfMove::Promote() { @@ -213,8 +224,9 @@ bool HalfMove::IsABlackMove() { return (IsBlack); } void HalfMove::BuildAndVerify(HalfMove *m, std::string fen) { arbiter.Setup(fen); - m->move_absolute=arbiter.ParseSAN(m->move); - bool work = arbiter.Play(m->move_absolute,arbiter.ParseSANPromotion(m->move)); + std::string move_absolute=arbiter.ParseSAN(m->move); + m->SetAbsoluteMove(move_absolute); + bool work = arbiter.Play(move_absolute,arbiter.ParseSANPromotion(m->move)); if (!work) { wxLogDebug("Bug! %s", m->move); } diff --git a/src/game_tab/HalfMove.hpp b/src/game_tab/HalfMove.hpp index 08a190b..9775c3e 100644 --- a/src/game_tab/HalfMove.hpp +++ b/src/game_tab/HalfMove.hpp @@ -21,11 +21,9 @@ class HalfMove : public cgeditor::CGEHalfMove { std::string fen; char capture; void BuildAndVerify(HalfMove *m, std::string fen); - std::string move_absolute; + std::string src,dst; public: - - HalfMove(HalfMove *m); HalfMove(std::string move_absolute,std::string move_san); HalfMove(std::string move_absolute,std::string move_san, std::string fen); @@ -62,7 +60,8 @@ public: void SetFen(std::string fen); void SetCapture(char c); bool IsABlackMove(); - std::string GetAbsoluteMove(){return move_absolute;}; + void GetAbsoluteMove(std::string &src,std::string &dst); + void SetAbsoluteMove(const std::string &move_absolute); /** * @brief Build current move diff --git a/src/game_tab/left_panel/GameTabLeftPanel.cpp b/src/game_tab/left_panel/GameTabLeftPanel.cpp index fa97f6f..4aaddb4 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.cpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.cpp @@ -72,34 +72,32 @@ void GameTabLeftPanel::OnPlay(wxCommandEvent &event) { } void GameTabLeftPanel::Notify(bool skip_animation) { - wxLogDebug("Called!"); // Update fen and captures std::string fen = game->GetFen(); std::map captures; bool animate=false; HalfMove *m = game->GetCurrentMove(); std::string src,dst; - // Update capture and check if we should to animations during moves change: if (m){ captures = m->GetLineCaptures(); if(m->HasParent(last_move)){ - UNPACK_ABSOLUTE_MOVE(m->GetAbsoluteMove(),src,dst); + m->GetAbsoluteMove(src,dst); animate=true; }else if(m->HasChild(last_move)){ // Accessing last_move here is safe since it is still // in the tree of moves (since HasChild found it so not deleted) - UNPACK_ABSOLUTE_MOVE(last_move->GetAbsoluteMove(),dst,src); + last_move->GetAbsoluteMove(dst,src); animate=true; } } else if(game->GetNextMove()){ // First move animation HalfMove *next=game->GetNextMove(); if(next==last_move){ - UNPACK_ABSOLUTE_MOVE(game->GetNextMove()->GetAbsoluteMove(),dst,src); + game->GetNextMove()->GetAbsoluteMove(dst,src); animate=true; } } - + // Update board canvas: if(skip_animation || !animate){ board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board, From 73f7be6c0366fd4334e70d68f0376c64357edd80 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 2 Jan 2023 10:56:27 +0100 Subject: [PATCH 15/18] Add mat icon --- src/binres/binres.cpp | 2 + src/binres/binres.hpp | 1 + src/binres/mat.png | Bin 0 -> 1833 bytes src/binres/mat_png.hpp | 155 ++++++++++++++++++ src/game_tab/left_panel/board/BoardCanvas.cpp | 8 + src/game_tab/left_panel/board/BoardCanvas.hpp | 10 ++ src/game_tab/left_panel/board/Theme.cpp | 10 +- src/game_tab/left_panel/board/Theme.hpp | 1 + 8 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 src/binres/mat.png create mode 100644 src/binres/mat_png.hpp diff --git a/src/binres/binres.cpp b/src/binres/binres.cpp index 963a714..14a2077 100644 --- a/src/binres/binres.cpp +++ b/src/binres/binres.cpp @@ -19,6 +19,8 @@ wxBitmap LoadPNG(std::string icon) { return (wxBITMAP_PNG(chesscom_8bits)); } else if (icon == "hide") { return (wxBITMAP_PNG(hide)); + } else if (icon == "mat") { + return (wxBITMAP_PNG(mat)); } return (wxNullBitmap); } \ No newline at end of file diff --git a/src/binres/binres.hpp b/src/binres/binres.hpp index 6db39fd..8551932 100644 --- a/src/binres/binres.hpp +++ b/src/binres/binres.hpp @@ -7,6 +7,7 @@ #include "cburnett_png.hpp" #include "chesscom_8bits_png.hpp" #include "hide_png.hpp" +#include "mat_png.hpp" wxBitmap LoadPNG(std::string icon, wxSize size); wxBitmap LoadPNG(std::string icon); \ No newline at end of file diff --git a/src/binres/mat.png b/src/binres/mat.png new file mode 100644 index 0000000000000000000000000000000000000000..54b4b9c1eb59149800a966478dfd821013706c50 GIT binary patch literal 1833 zcmV+^2iEwBP)pD@WndA-Kib4cthUBSEgD}j8jZhv zTVsD1V|_%8iP$EFXp9=8&|Q|TP1VYytzxM3MU7M_wzUOax--Wgt;jHUc4u}L8^3@1 z&DnF$cjw%>_nv!+nUM`dqtOx}PzUg(1XKWA3eX$?d8bT{13U`QUIumvV4ITUj+%ys z!ECfoUWCNsZSz=HRzl!bFv>G@(+|Q1W~D{bw0)Vzl97KO~r9ll{GS81AdT=q~Zq_4Hr zf2jDZPcwZJhek&+F=5Vxvx|$Ns;Y@&W(*xXXySaTisIs8xnn|fe0<{CrlzLE^hc`9 zexzv5YXE1QPR(oX!=cg9$=`mzAD@5riN$X3-hH_9F7wd4^2!=K_w3Vh$Bg>G8NOEl z+&le|m`=nyHr?c9U=I!+Fo^(wg$ozJ@Aq45dwP2nkv~4i3e)Hz9&YjAcgs2ME!Q(v9yM2d+s|;CtnVGR; z$4(PBJ3k-uO6NIgra;%#7pD@FLN-UEivd_(nOp3bCdB9SAsD>SVz+0{9vnS-)WlU* zh9HEq1p@)AIyyQgduUP!3w{zFmmLQV$j3)cPL9R4r$;tE(x=rFkT@ALfKx)^@wR!) zSnkG)3_X&Zm!*}Rt&{O#g26XLqtO!JpEqKtt3cK-m|JX@jStCJ$S@c?cm81FX3w6D zl9Cd44HASZa2s%HLhS5}^gG#43=HhX2OBopKHs+OdyI{ZnYblOuCRQ16Nv=2ZvDo@ z6&4mqQ|p{_=AwDcJ*mR_sa6Xwps(B8&rhnfKb;5w`1afH@c0u?rZ>zY%F5>BowrlZ z2x^i`MA9e1|4~dc23>(BnE8^^9f()3pi9KbLc#wV#HgTHY}0NqF7V>$=bs0#`krPJ zcX@R+)~QTWN%Z~LXW|MA3s73>a{jO)-}5A>>$+uqm6w-?va&LZZQqZ5_$y)F6NN${ z5P2`i2msQpqf_kKwF_h8<0dW?S_C1ieI!q-)cX&II3@rb@#3Mo+gVxx&Qs7m_z1;1 zHnjt2NM&~S>NOY`*qwz2o#UmoFQB^mGDo%qd?gs@pW&Eh98BmJMAUV<1MvznxJ#%f zjL7K@#48L^qiRh8FM3klZL)V%9 z6{2a{J`g^379Us=TvMZswz+oXKo|sCQge+-n90t*vbY5iR!!E641KsQTikRBUvxo)pw`Yf&+m7%I42^7_t=%tY22wV5vz-4|=+~x_rlv z9r+Z%H8N*T4{{8Haqu^D3i^FM6crV@761SQiWH5EUMo9Kv^{!@#bS8`y&b^i8MT^y zY-K2oc1LfvpAJpajwdGc1^{2qSZEGGpor?5b+w%cAcYVBXl!g8D<1j%x^!b^Cc(h^ zKz>0}O-;?QRCbOp48&uRn;Ccmz?svbSeVK@KQ}YzUgAt@z_jUsxeqRRaaN%db8*0n$yWUTE captures, std::string white_player, std::string black_player) { + gs.board = board; + gs.is_black_turn = is_black_turn; + gs.captures = captures; + gs.white=white_player; + gs.black=black_player; + this->board = board; this->is_black_turn = is_black_turn; this->captures = captures; @@ -241,6 +247,8 @@ void BoardCanvas::DrawBoard(wxDC &dc) { } if (piece != ' ') { dc.DrawBitmap(*t->Get(piece), px, py, false); + if(piece == 'k' || piece == 'K') + dc.DrawBitmap(*t->Get('#'), x+square_width/2+centrer_offset, y+centrer_offset, false); } } } diff --git a/src/game_tab/left_panel/board/BoardCanvas.hpp b/src/game_tab/left_panel/board/BoardCanvas.hpp index 9a00262..5d7c76d 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.hpp +++ b/src/game_tab/left_panel/board/BoardCanvas.hpp @@ -58,6 +58,15 @@ typedef struct AnimState { wxPoint transVect; } AnimState; +typedef struct GameState { + std::string white, black; + std::string board; + std::map captures; + bool is_black_turn; + bool mat_black; + bool mat_white; +} GameState; + class BoardCanvas : public wxPanel { // *t is theme for board+pieces and // *t_captures is theme for captured pieces (scale down version of t) @@ -78,6 +87,7 @@ class BoardCanvas : public wxPanel { // Current animation state AnimState adata; + GameState gs; public: BoardCanvas(wxFrame *parent); diff --git a/src/game_tab/left_panel/board/Theme.cpp b/src/game_tab/left_panel/board/Theme.cpp index 6c662dc..f72dfab 100644 --- a/src/game_tab/left_panel/board/Theme.cpp +++ b/src/game_tab/left_panel/board/Theme.cpp @@ -11,6 +11,8 @@ Theme::Theme() : square_radius(10) { config->Read("board/theme/squares/path", "default").ToStdString(); wxFileName square_file(square); CONFIG_CLOSE(config); + // Mat + skin['#']=LoadPNG("mat").ConvertToImage(); // Piece if (piece == "default" || !piece_file.FileExists()) { wxLogDebug("Loading piece skin from binres"); @@ -95,12 +97,18 @@ wxBitmap *Theme::Get(char c) { return (skin_scaled[c]); } void Theme::ResizePieces(std::uint32_t width) { for (std::pair c : skin) { - if (c.first != 's' && c.first != 'S') { + if (c.first != 's' && c.first != 'S' && c.first != '#') { if (skin_scaled.count(c.first)) delete skin_scaled[c.first]; skin_scaled[c.first] = new wxBitmap(c.second.Scale(width, width, wxIMAGE_QUALITY_HIGH)); } + else if(c.first == '#'){ + if (skin_scaled.count(c.first)) + delete skin_scaled[c.first]; + skin_scaled[c.first] = + new wxBitmap(c.second.Scale(width*MAT_SIZE_FACTOR, width*MAT_SIZE_FACTOR, wxIMAGE_QUALITY_HIGH)); + } } } diff --git a/src/game_tab/left_panel/board/Theme.hpp b/src/game_tab/left_panel/board/Theme.hpp index b896d09..c3acf10 100644 --- a/src/game_tab/left_panel/board/Theme.hpp +++ b/src/game_tab/left_panel/board/Theme.hpp @@ -8,6 +8,7 @@ #define ELT_DIM 200 #define DEFAULT_SIZE 80 #define PIECE_SIZE_FACTOR 0.8 // Should be between 0 and 1 +#define MAT_SIZE_FACTOR 0.4 #define DEFAULT_PIECE_THEME "assets/pieces/cburnett.png" #define DEFAULT_SQUARE_THEME "assets/boards/chesscom_8bits.png" From 56252f2b6c42fe369a4b0c77287469c49c381943 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 2 Jan 2023 11:36:13 +0100 Subject: [PATCH 16/18] Improve UI --- src/game_tab/Game.cpp | 8 ++ src/game_tab/Game.hpp | 1 + src/game_tab/left_panel/GameTabLeftPanel.cpp | 15 ++- src/game_tab/left_panel/board/BoardCanvas.cpp | 91 ++++++++++--------- src/game_tab/left_panel/board/BoardCanvas.hpp | 8 +- 5 files changed, 69 insertions(+), 54 deletions(-) diff --git a/src/game_tab/Game.cpp b/src/game_tab/Game.cpp index 8ab67cb..53be08f 100644 --- a/src/game_tab/Game.cpp +++ b/src/game_tab/Game.cpp @@ -92,6 +92,14 @@ void Game::SetMoveAsMainline(HalfMove *m) { } } +bool Game::IsCheckmate(bool forBlack){ + arbiter.Setup(GetFen()); + if(forBlack){ + return arbiter.IsBlackTurn() && arbiter.IsCheckMate(); + } + return !arbiter.IsBlackTurn() && arbiter.IsCheckMate(); +} + bool Game::Play(std::string move) { wxLogDebug("Playing move %s", move); std::string fen = GetFen(); diff --git a/src/game_tab/Game.hpp b/src/game_tab/Game.hpp index bc00c5a..0590027 100644 --- a/src/game_tab/Game.hpp +++ b/src/game_tab/Game.hpp @@ -31,6 +31,7 @@ public: std::string GetResult(); bool Play(std::string move); bool IsBlackToPlay(); + bool IsCheckmate(bool forBlack); void Previous(); void Next(); void DeleteMove(HalfMove *m); diff --git a/src/game_tab/left_panel/GameTabLeftPanel.cpp b/src/game_tab/left_panel/GameTabLeftPanel.cpp index 4aaddb4..25ded2c 100644 --- a/src/game_tab/left_panel/GameTabLeftPanel.cpp +++ b/src/game_tab/left_panel/GameTabLeftPanel.cpp @@ -99,14 +99,19 @@ void GameTabLeftPanel::Notify(bool skip_animation) { } // Update board canvas: + GameState gs; + gs.board=chessarbiter::FENParser::Parse(fen).board; + gs.is_black_turn=game->IsBlackToPlay(); + gs.captures=captures; + gs.white=game->GetTag("White"); + gs.black=game->GetTag("Black"); + gs.mat_black=game->IsCheckmate(true); + gs.mat_white=game->IsCheckmate(false); if(skip_animation || !animate){ - board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board, - game->IsBlackToPlay(), captures, - game->GetTag("White"),game->GetTag("Black")); + board_canvas->SetupBoard(gs); } else{ - board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board, - game->IsBlackToPlay(), captures,src,dst,repeat); + board_canvas->Animate(gs, src,dst,repeat); } // Update last move last_move=m; diff --git a/src/game_tab/left_panel/board/BoardCanvas.cpp b/src/game_tab/left_panel/board/BoardCanvas.cpp index 4617f3d..f6846f1 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.cpp +++ b/src/game_tab/left_panel/board/BoardCanvas.cpp @@ -3,29 +3,36 @@ wxDEFINE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent); BoardCanvas::BoardCanvas(wxFrame *parent) - : wxPanel(parent), black_side(false), is_black_turn(true), frozen(false), + : wxPanel(parent), black_side(false), frozen(false), lock_square_size(false), t(new Theme()), t_captures(new Theme()) { - board = "rnbqkbnrpppppppp PPPPPPPPRNBQKBNR"; is_dragging = false; valid_drag = false; - adata.reuseBuffer=false; - adata.buffer=nullptr; - t_captures->ResizePieces(t->GetPiecesSizes() * CAPTURE_FACTOR); - SetClockTime(-1, -1, -1, false); - SetClockTime(-1, -1, -1, true); - ApplyPreferences(); - // The following should be called when using an EVT_PAINT handler - SetBackgroundStyle(wxBG_STYLE_PAINT); + // Init animation data adata.duration=200; adata.duration_fast=80; adata.fps=30; adata.buffer=new wxBitmap(500,500,32); + adata.reuseBuffer=false; + adata.buffer=nullptr; + // Init game state + gs.is_black_turn=false; + gs.board = "rnbqkbnrpppppppp PPPPPPPPRNBQKBNR"; + gs.mat_black=false; + gs.mat_white=false; + // Init clocks + SetClockTime(-1, -1, -1, false); + SetClockTime(-1, -1, -1, true); + // Init capture pieces + t_captures->ResizePieces(t->GetPiecesSizes() * CAPTURE_FACTOR); + // Load preferences + ApplyPreferences(); + // The following should be called when using an EVT_PAINT handler + SetBackgroundStyle(wxBG_STYLE_PAINT); // Let GameTableLeftPanel process keyboard events: Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();}); Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();}); Bind(wxEVT_PAINT, &BoardCanvas::OnPaint, this); Bind(wxEVT_SIZE, &BoardCanvas::OnResize, this); - // Mouse events: Bind(wxEVT_MOTION, &BoardCanvas::MouseEvent, this); Bind(wxEVT_LEFT_DOWN, &BoardCanvas::MouseEvent, this); @@ -113,30 +120,26 @@ void BoardCanvas::ApplyPreferences() { Refresh(); } -void BoardCanvas::SetupBoard(std::string board, bool is_black_turn, - std::map captures, - std::string white_player, std::string black_player) { - gs.board = board; - gs.is_black_turn = is_black_turn; - gs.captures = captures; - gs.white=white_player; - gs.black=black_player; - - this->board = board; - this->is_black_turn = is_black_turn; - this->captures = captures; - this->white_player=white_player; - this->black_player=black_player; +void BoardCanvas::SetupBoard(const GameState &new_gs) { + gs.board = new_gs.board; + gs.is_black_turn = new_gs.is_black_turn; + gs.captures = new_gs.captures; + gs.white=new_gs.white; + gs.black=new_gs.black; + gs.mat_white=new_gs.mat_white; + gs.mat_black=new_gs.mat_black; + gs.black_time=new_gs.black_time; + gs.white_time=new_gs.white_time; Refresh(); } -void BoardCanvas::Animate(const std::string &board, bool is_black_turn, std::map captures, std::string src, std::string dst,bool faster){ +void BoardCanvas::Animate(const GameState &new_gs, std::string src, std::string dst, bool faster){ std::uint8_t pfile = src[0]-'a'; std::uint8_t prank = src[1]-'1'; - adata.piece_moved = this->board[pfile + 8 * (7-prank)]; // Piece to move + adata.piece_moved = gs.board[pfile + 8 * (7-prank)]; // Piece to move // Now remove the piece that will be moved - this->board[pfile + 8 * (7-prank)]=' '; + gs.board[pfile + 8 * (7-prank)]=' '; wxMemoryDC memDC(*adata.buffer); DrawBoard(memDC); @@ -183,7 +186,7 @@ void BoardCanvas::Animate(const std::string &board, bool is_black_turn, std::map } adata.duration=faster ? duration_backup : duration_backup; adata.reuseBuffer=false; - SetupBoard(board, is_black_turn, captures,white_player,black_player); + SetupBoard(new_gs); } void BoardCanvas::DrawBoard(wxDC &dc) { @@ -236,7 +239,7 @@ void BoardCanvas::DrawBoard(wxDC &dc) { } std::uint32_t px = x + centrer_offset; std::uint32_t py = y + centrer_offset; - char piece = board[(7 - pfile) + 8 * prank]; + char piece = gs.board[(7 - pfile) + 8 * prank]; if (is_dragging && (7 - pfile) == active_square.x && (7 - prank) == active_square.y) { dp = piece; @@ -247,7 +250,7 @@ void BoardCanvas::DrawBoard(wxDC &dc) { } if (piece != ' ') { dc.DrawBitmap(*t->Get(piece), px, py, false); - if(piece == 'k' || piece == 'K') + if((piece == 'k' && gs.mat_black) || (piece == 'K' && gs.mat_white)) dc.DrawBitmap(*t->Get('#'), x+square_width/2+centrer_offset, y+centrer_offset, false); } } @@ -257,7 +260,7 @@ void BoardCanvas::DrawBoard(wxDC &dc) { dc.SetPen(wxPen(*wxBLACK, 3)); std::uint32_t badgeY = boardY; std::uint32_t badgeWidth = square_width / 2; - if (is_black_turn) { + if (gs.is_black_turn) { dc.SetBrush(*wxBLACK_BRUSH); if (black_side) { badgeY = boardY + (8 * square_width) - badgeWidth; @@ -286,15 +289,15 @@ void BoardCanvas::DrawBoard(wxDC &dc) { offsetYPlayerName = offsetY+captures_size; } for (char p : {'P', 'N', 'B', 'R', 'Q'}) { - if (captures.find(p) != captures.end()) { - for (std::uint8_t i = 0; i < captures[p]; i++) { + if (gs.captures.find(p) != gs.captures.end()) { + for (std::uint8_t i = 0; i < gs.captures[p]; i++) { dc.DrawBitmap(*t_captures->Get(p), boardX + offsetX, boardY + offsetY); offsetX += captures_size / 2; } offsetX += captures_size / 2; } } - dc.DrawText(wxString(black_player),boardX,boardY + offsetYPlayerName); + dc.DrawText(wxString(gs.black),boardX,boardY + offsetYPlayerName); // Black (white's captures): offsetX = 0; if (black_side) { @@ -305,15 +308,15 @@ void BoardCanvas::DrawBoard(wxDC &dc) { offsetYPlayerName = offsetY+captures_size; } for (char p : {'p', 'n', 'b', 'r', 'q'}) { - if (captures.find(p) != captures.end()) { - for (std::uint8_t i = 0; i < captures[p]; i++) { + if (gs.captures.find(p) != gs.captures.end()) { + for (std::uint8_t i = 0; i < gs.captures[p]; i++) { dc.DrawBitmap(*t_captures->Get(p), boardX + offsetX, boardY + offsetY); offsetX += captures_size / 2; } offsetX += captures_size / 2; } } - dc.DrawText(wxString(white_player),boardX,boardY + offsetYPlayerName); + dc.DrawText(wxString(gs.white),boardX,boardY + offsetYPlayerName); // Draw dragging piece if (DrawDraggingPiece) { @@ -327,9 +330,9 @@ void BoardCanvas::DrawBoard(wxDC &dc) { } // Draw Clocks - if (std::get<0>(black_time) >= 0) { + if (std::get<0>(gs.black_time) >= 0) { wxFont font = dc.GetFont(); - ClockTime clock = black_side ? white_time : black_time; + ClockTime clock = black_side ? gs.white_time : gs.black_time; wxString time = wxString::Format("%ds", std::get<1>(clock), std::get<2>(clock)); if (std::get<0>(clock) > 0) { @@ -342,7 +345,7 @@ void BoardCanvas::DrawBoard(wxDC &dc) { dc.GetTextExtent(time, &width, &height); dc.DrawText(time, wxPoint(boardX + square_width * 8 - width, boardY - height - numbers_size.y*2)); - clock = black_side ? black_time : white_time; + clock = black_side ? gs.black_time : gs.white_time; time = wxString::Format("%ds", std::get<1>(clock), std::get<2>(clock)); if (std::get<0>(clock) > 0) { time = wxString::Format("%d:%d", std::get<0>(clock), std::get<1>(clock)); @@ -394,7 +397,7 @@ void BoardCanvas::MouseEvent(wxMouseEvent &event) { if (IsCurrentSquareValid) { active_square.x = file; active_square.y = rank; - if (board[(7 - rank) * 8 + file] != ' ') { + if (gs.board[(7 - rank) * 8 + file] != ' ') { wxLogDebug("Drag start on square (%d,%d)", file, rank); valid_drag = true; } @@ -423,8 +426,8 @@ void BoardCanvas::Swap() { void BoardCanvas::SetClockTime(short hours, short min, short sec, bool IsBlack) { if (IsBlack) { - black_time = std::make_tuple(hours, min, sec); + gs.black_time = std::make_tuple(hours, min, sec); } else { - white_time = std::make_tuple(hours, min, sec); + gs.white_time = std::make_tuple(hours, min, sec); } } diff --git a/src/game_tab/left_panel/board/BoardCanvas.hpp b/src/game_tab/left_panel/board/BoardCanvas.hpp index 5d7c76d..7b6ccf7 100644 --- a/src/game_tab/left_panel/board/BoardCanvas.hpp +++ b/src/game_tab/left_panel/board/BoardCanvas.hpp @@ -65,6 +65,7 @@ typedef struct GameState { bool is_black_turn; bool mat_black; bool mat_white; + ClockTime black_time, white_time; } GameState; class BoardCanvas : public wxPanel { @@ -82,7 +83,6 @@ class BoardCanvas : public wxPanel { wxSize canvas_size; wxPoint active_square; std::map captures; - ClockTime black_time, white_time; bool frozen,lock_square_size; // Current animation state @@ -100,9 +100,7 @@ public: void Zoom(std::int32_t zoom); void Swap(); void OnResize(wxSizeEvent &e); - void SetupBoard(std::string board, bool is_black_turn, - std::map captures, - std::string white_player, std::string black_player); - void Animate(const std::string &board, bool is_black_turn, std::map captures, std::string src, std::string dst,bool faster); + void SetupBoard(const GameState &new_gs); + void Animate(const GameState &new_gs, std::string src, std::string dst,bool faster); void SetClockTime(short hours, short min, short sec, bool IsBlack); }; From d1c1031332f47cf076d5da0a8a161dbb8210a0b6 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 2 Jan 2023 11:44:11 +0100 Subject: [PATCH 17/18] Debug --- src/game_tab/HalfMove.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/game_tab/HalfMove.cpp b/src/game_tab/HalfMove.cpp index ae63527..32924e1 100644 --- a/src/game_tab/HalfMove.cpp +++ b/src/game_tab/HalfMove.cpp @@ -193,6 +193,8 @@ bool HalfMove::HasParent(HalfMove*m){ } bool HalfMove::HasChild(HalfMove*m){ + if(m==nullptr) + return false; if(mainline==m){ return true; } From 042f6ca17012414d13235d3e04ea9012ff49f79c Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 2 Jan 2023 11:58:40 +0100 Subject: [PATCH 18/18] Debug comments --- src/game_tab/right_panel/GameTabRightPanel.cpp | 7 ++++++- src/game_tab/right_panel/GameTabRightPanel.hpp | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/game_tab/right_panel/GameTabRightPanel.cpp b/src/game_tab/right_panel/GameTabRightPanel.cpp index 3a46f59..dd0ebca 100644 --- a/src/game_tab/right_panel/GameTabRightPanel.cpp +++ b/src/game_tab/right_panel/GameTabRightPanel.cpp @@ -89,7 +89,12 @@ void GameTabRightPanel::OnCommentChange(wxCommandEvent &event) { wxLogDebug("GameTabRightPanel: comment input change"); HalfMove *m = game->GetCurrentMove(); if (m != nullptr) { - m->comment=event.GetString().ToStdString(); + m->comment=event.GetString().Trim().ToStdString(); + // Remove newlines: + for(char &c:m->comment){ + if(c=='\n') + c=' '; + } } editor_canvas->Refresh(); } diff --git a/src/game_tab/right_panel/GameTabRightPanel.hpp b/src/game_tab/right_panel/GameTabRightPanel.hpp index aa28f17..6f3aaaa 100644 --- a/src/game_tab/right_panel/GameTabRightPanel.hpp +++ b/src/game_tab/right_panel/GameTabRightPanel.hpp @@ -1,3 +1,5 @@ +#include + #include "../Game.hpp" #include "LiveEngineDialog.hpp" #include "editor/EditorCanvas.hpp"