From bbf3282839eb9efe57b1451763ef0859198c33af Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 19:05:15 +0100 Subject: Update Game tab --- src/game_tab/Game.hpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/game_tab/Game.hpp') 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; -- cgit v1.2.3 From 29d5850b2f44ad520fc89324a110a721ac08841b Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sun, 1 Jan 2023 20:21:23 +0100 Subject: 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(-) (limited to 'src/game_tab/Game.hpp') 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(); -- cgit v1.2.3 From 3e40032109886786e469796dced8523d60b90ee4 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 2 Jan 2023 10:12:20 +0100 Subject: 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(-) (limited to 'src/game_tab/Game.hpp') 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, -- cgit v1.2.3 From 56252f2b6c42fe369a4b0c77287469c49c381943 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 2 Jan 2023 11:36:13 +0100 Subject: 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 | 85 ++++++++++++++------------- src/game_tab/left_panel/board/BoardCanvas.hpp | 8 +-- 5 files changed, 66 insertions(+), 51 deletions(-) (limited to 'src/game_tab/Game.hpp') 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; + // 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; - t_captures->ResizePieces(t->GetPiecesSizes() * CAPTURE_FACTOR); + // 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); - adata.duration=200; - 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_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); }; -- cgit v1.2.3