Prepare for pieces animation step2

This commit is contained in:
Loic Guegan 2022-12-28 12:40:16 +01:00
parent c451c63aaa
commit 7187e6d6ee
4 changed files with 22 additions and 19 deletions

View file

@ -26,7 +26,7 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game)
void GameTabLeftPanel::OnPreviousMove(wxCommandEvent &event) {
game->Previous();
Notify();
Notify(true);
NotifyEditor();
}
@ -48,7 +48,7 @@ void GameTabLeftPanel::OnSwap(wxCommandEvent &event) {
void GameTabLeftPanel::OnNextMove(wxCommandEvent &event) {
wxLogDebug("Game tab received NEXT_MOVE_EVENT");
game->Next();
Notify();
Notify(true);
NotifyEditor();
}
@ -59,26 +59,30 @@ void GameTabLeftPanel::OnPlay(wxCommandEvent &event) {
}
Notify();
std::string fen = game->GetFen();
std::map<char, std::uint8_t> captures;
HalfMove *m = game->GetCurrentMove();
if (m != NULL) {
captures = m->GetLineCaptures();
}
/*board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board,
game->IsBlackToPlay(), captures,"a1","a2"); */
}
}
void GameTabLeftPanel::Notify() {
void GameTabLeftPanel::Notify(bool animate) {
std::string fen = game->GetFen();
std::map<char, std::uint8_t> captures;
HalfMove *m = game->GetCurrentMove();
if (m != NULL) {
captures = m->GetLineCaptures();
}
board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board,
game->IsBlackToPlay(), captures);
}
if(!animate){
board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board,
game->IsBlackToPlay(), captures);
}
else{
board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board,
game->IsBlackToPlay(), captures,"a1","a2");
}
fen_text_field->SetValue(game->GetFen());
}

View file

@ -15,7 +15,7 @@ class GameTabLeftPanel : public TabGameLeftPanel {
public:
GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game);
void Notify();
void Notify(bool animate=false);
void OnPlay(wxCommandEvent &event);
void OnGotoMove(wxCommandEvent &event);
void OnPreviousMove(wxCommandEvent &event);

View file

@ -15,15 +15,11 @@ BoardCanvas::BoardCanvas(wxFrame *parent)
ApplyPreferences();
// The following should be called when using an EVT_PAINT handler
SetBackgroundStyle(wxBG_STYLE_PAINT);
timer.Bind(wxEVT_TIMER, &BoardCanvas::OnTimerTick, this);
timer.Bind(wxEVT_TIMER, [p=this](wxTimerEvent &e){p->Refresh();});
duration=500;
fps=30;
}
void BoardCanvas::OnTimerTick(wxTimerEvent &event) {
Refresh();
}
BoardCanvas::~BoardCanvas() {
delete t;
delete t_captures;
@ -64,7 +60,9 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) {
else {
// Otherwise reuse buffer and animate. TEST CODE FOR NOW:
dc.DrawBitmap(*buffer, 0, 0, true);
dc.DrawRectangle(wxRect(0+frame,0+frame,50,50));
// Draw piece
dc.DrawBitmap(*t->Get(piece_moved), 0+frame, 0+frame, false);
// end drawing
frame++;
if(frame*fps>=duration){
timer.Stop();
@ -105,6 +103,7 @@ void BoardCanvas::SetupBoard(std::string board, bool is_black_turn,
}
void BoardCanvas::Animate(std::string board, bool is_black_turn, std::map<char, std::uint8_t> captures, std::string src, std::string dst){
return; // Shortcut this method for now
this->final_board=board;
this->final_is_black_turn=is_black_turn;
this->final_captures=captures;
@ -113,7 +112,7 @@ void BoardCanvas::Animate(std::string board, bool is_black_turn, std::map<char,
std::uint8_t pfile = src[0]-'a';
std::uint8_t prank = src[1]-'1';
char piece_moved = this->board[(7 - pfile) + 8 * (7-prank)]; // Piece to move
this->piece_moved = this->board[(7 - pfile) + 8 * (7-prank)]; // Piece to move
// Animate piece here
reuseBuffer=true;
frame=0;

View file

@ -67,6 +67,7 @@ class BoardCanvas : public wxPanel {
std::map<char, std::uint8_t> final_captures;
std::string src;
std::string dst;
char piece_moved;
public:
BoardCanvas(wxFrame *parent);
@ -79,7 +80,6 @@ public:
void MouseEvent(wxMouseEvent &event);
void Zoom(std::int32_t zoom);
void Swap();
void OnTimerTick(wxTimerEvent &event);
void SetupBoard(std::string board, bool is_black_turn,
std::map<char, std::uint8_t> captures);
void Animate(std::string board, bool is_black_turn, std::map<char, std::uint8_t> captures, std::string src, std::string dst);