From 29d5850b2f44ad520fc89324a110a721ac08841b Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 20:21:23 +0100 Subject: [PATCH] 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();