Prepare for pieces animation

This commit is contained in:
Loic Guegan 2022-12-28 12:10:08 +01:00
parent 2151ccbe65
commit c451c63aaa
3 changed files with 75 additions and 17 deletions

View file

@ -58,6 +58,16 @@ void GameTabLeftPanel::OnPlay(wxCommandEvent &event) {
NotifyEditor();
}
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() {
@ -66,9 +76,10 @@ void GameTabLeftPanel::Notify() {
HalfMove *m = game->GetCurrentMove();
if (m != NULL) {
captures = m->GetLineCaptures();
}
}
board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board,
game->IsBlackToPlay(), captures);
fen_text_field->SetValue(game->GetFen());
}

View file

@ -8,12 +8,20 @@ BoardCanvas::BoardCanvas(wxFrame *parent)
board = "rnbqkbnrpppppppp PPPPPPPPRNBQKBNR";
is_dragging = false;
valid_drag = false;
reuseBuffer=false;
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);
timer.Bind(wxEVT_TIMER, &BoardCanvas::OnTimerTick, this);
duration=500;
fps=30;
}
void BoardCanvas::OnTimerTick(wxTimerEvent &event) {
Refresh();
}
BoardCanvas::~BoardCanvas() {
@ -35,22 +43,35 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) {
dc.SetBackground(*wxWHITE_BRUSH);
dc.Clear();
// Setting up required attributes
REFRESH_MOUSE_LOCATION();
square_width = t->GetSquaresSizes();
canvas_size = dc.GetSize();
boardX = (canvas_size.x - (8 * square_width)) / 2;
boardY = (canvas_size.y - (8 * square_width)) / 2;
if (boardX > canvas_size.x)
boardX = 0;
if (boardY > canvas_size.y)
boardY = 0;
if(!reuseBuffer){
// Setting up required attributes
REFRESH_MOUSE_LOCATION();
square_width = t->GetSquaresSizes();
canvas_size = dc.GetSize();
boardX = (canvas_size.x - (8 * square_width)) / 2;
boardY = (canvas_size.y - (8 * square_width)) / 2;
if (boardX > canvas_size.x)
boardX = 0;
if (boardY > canvas_size.y)
boardY = 0;
// Setup buffer (later use for animations)
buffer=new wxBitmap(canvas_size.x,canvas_size.y,32);
wxMemoryDC memDC(*buffer);
DrawBoard(memDC);
dc.Blit(0,0,canvas_size.x,canvas_size.y,(wxDC*)&memDC,0,0);
// Setup buffer (later use for animations)
buffer=new wxBitmap(canvas_size.x,canvas_size.y,32);
wxMemoryDC memDC(*buffer);
DrawBoard(memDC);
dc.Blit(0,0,canvas_size.x,canvas_size.y,(wxDC*)&memDC,0,0);
}
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));
frame++;
if(frame*fps>=duration){
timer.Stop();
reuseBuffer=false;
SetupBoard(final_board, final_is_black_turn, final_captures);
}
}
}
void BoardCanvas::ApplyPreferences() {
@ -83,6 +104,22 @@ void BoardCanvas::SetupBoard(std::string board, bool is_black_turn,
Refresh();
}
void BoardCanvas::Animate(std::string board, bool is_black_turn, std::map<char, std::uint8_t> captures, std::string src, std::string dst){
this->final_board=board;
this->final_is_black_turn=is_black_turn;
this->final_captures=captures;
this->src=src;
this->dst=dst;
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
// Animate piece here
reuseBuffer=true;
frame=0;
timer.Start(1000/fps); // in ms;
}
void BoardCanvas::DrawBoard(wxDC &dc) {
std::uint32_t piece_width = t->GetPiecesSizes();
std::uint32_t centrer_offset = (square_width - piece_width) / 2;

View file

@ -57,8 +57,16 @@ class BoardCanvas : public wxPanel {
ClockTime black_time, white_time;
bool frozen,lock_square_size;
// Drawing buffer
// Drawing buffer (ANIMATIONS)
wxBitmap *buffer;
bool reuseBuffer;
wxTimer timer;
int frame,duration,fps;
std::string final_board;
bool final_is_black_turn;
std::map<char, std::uint8_t> final_captures;
std::string src;
std::string dst;
public:
BoardCanvas(wxFrame *parent);
@ -71,8 +79,10 @@ 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);
void SetClockTime(short hours, short min, short sec, bool IsBlack);
DECLARE_EVENT_TABLE()
};