mirror of
https://gitlab.com/manzerbredes/ochess.git
synced 2025-07-03 02:37:39 +00:00
Implement pieces move animations
This commit is contained in:
parent
7187e6d6ee
commit
3efabf1c33
10 changed files with 160 additions and 53 deletions
|
@ -4,7 +4,7 @@ wxDEFINE_EVENT(PLAY_MOVE_EVENT, wxCommandEvent);
|
|||
|
||||
BoardCanvas::BoardCanvas(wxFrame *parent)
|
||||
: wxPanel(parent), black_side(false), is_black_turn(true), frozen(false),
|
||||
lock_square_size(false), t(new Theme()), t_captures(new Theme()) {
|
||||
lock_square_size(false), t(new Theme()), t_captures(new Theme()), buffer(nullptr) {
|
||||
board = "rnbqkbnrpppppppp PPPPPPPPRNBQKBNR";
|
||||
is_dragging = false;
|
||||
valid_drag = false;
|
||||
|
@ -15,9 +15,12 @@ BoardCanvas::BoardCanvas(wxFrame *parent)
|
|||
ApplyPreferences();
|
||||
// The following should be called when using an EVT_PAINT handler
|
||||
SetBackgroundStyle(wxBG_STYLE_PAINT);
|
||||
timer.Bind(wxEVT_TIMER, [p=this](wxTimerEvent &e){p->Refresh();});
|
||||
duration=500;
|
||||
fps=30;
|
||||
duration=200;
|
||||
duration_fast=100;
|
||||
fps=180;
|
||||
|
||||
Bind(wxEVT_KEY_DOWN, &BoardCanvas::OnKeyEvent, this);
|
||||
Bind(wxEVT_KEY_UP, &BoardCanvas::OnKeyEvent, this);
|
||||
}
|
||||
|
||||
BoardCanvas::~BoardCanvas() {
|
||||
|
@ -52,6 +55,8 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) {
|
|||
boardY = 0;
|
||||
|
||||
// Setup buffer (later use for animations)
|
||||
if(buffer!=nullptr)
|
||||
free(buffer);
|
||||
buffer=new wxBitmap(canvas_size.x,canvas_size.y,32);
|
||||
wxMemoryDC memDC(*buffer);
|
||||
DrawBoard(memDC);
|
||||
|
@ -60,12 +65,15 @@ void BoardCanvas::OnPaint(wxPaintEvent &event) {
|
|||
else {
|
||||
// Otherwise reuse buffer and animate. TEST CODE FOR NOW:
|
||||
dc.DrawBitmap(*buffer, 0, 0, true);
|
||||
// Draw piece
|
||||
dc.DrawBitmap(*t->Get(piece_moved), 0+frame, 0+frame, false);
|
||||
double frames=duration/(1000/fps);
|
||||
double percent=frame/frames;
|
||||
// Draw moving piece
|
||||
dc.DrawBitmap(*t->Get(piece_moved),
|
||||
src.x + frame*(transVect.x/frames),
|
||||
src.y + frame*(transVect.y/frames), false);
|
||||
// end drawing
|
||||
frame++;
|
||||
if(frame*fps>=duration){
|
||||
timer.Stop();
|
||||
if(frame>=frames){
|
||||
reuseBuffer=false;
|
||||
SetupBoard(final_board, final_is_black_turn, final_captures);
|
||||
}
|
||||
|
@ -102,21 +110,56 @@ 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){
|
||||
return; // Shortcut this method for now
|
||||
void BoardCanvas::Animate(std::string board, bool is_black_turn, std::map<char, std::uint8_t> captures, std::string src, std::string dst,bool faster){
|
||||
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';
|
||||
this->piece_moved = this->board[(7 - pfile) + 8 * (7-prank)]; // Piece to move
|
||||
// Animate piece here
|
||||
this->piece_moved = this->board[pfile + 8 * (7-prank)]; // Piece to move
|
||||
|
||||
// Now remove the piece that will be moved
|
||||
this->board[pfile + 8 * (7-prank)]=' ';
|
||||
SetupBoard(this->board,this->is_black_turn,this->captures);
|
||||
Update(); // Since refresh in SetupBoard is not synchronous, this call wait the end of Refresh()
|
||||
|
||||
// Now compute piece start position and translation vector (Copy paste from DrawBoard())
|
||||
std::uint32_t piece_width = t->GetPiecesSizes();
|
||||
std::uint32_t centrer_offset = (square_width - piece_width) / 2;
|
||||
if (!black_side) {
|
||||
prank = 7 - prank;
|
||||
pfile = 7 - pfile;
|
||||
}
|
||||
std::uint32_t x = boardX + (7 - pfile) * square_width;
|
||||
std::uint32_t y = boardY + prank * square_width;
|
||||
this->src.x = x + centrer_offset;
|
||||
this->src.y = y + centrer_offset;
|
||||
// Now dst:
|
||||
pfile = dst[0]-'a';
|
||||
prank = dst[1]-'1';
|
||||
if (!black_side) {
|
||||
prank = 7 - prank;
|
||||
pfile = 7 - pfile;
|
||||
}
|
||||
x = boardX + (7 - pfile) * square_width;
|
||||
y = boardY + prank * square_width;
|
||||
transVect.x=x-this->src.x+centrer_offset;
|
||||
transVect.y=y-this->src.y+centrer_offset;
|
||||
|
||||
// Start animation:
|
||||
reuseBuffer=true;
|
||||
int duration_backup=duration;
|
||||
duration=faster ? duration_fast : duration;
|
||||
frame=0;
|
||||
timer.Start(1000/fps); // in ms;
|
||||
int frames=duration/(1000/fps); // total number of frames
|
||||
for(int i=frames;i>0;i--){
|
||||
Refresh();
|
||||
Update();
|
||||
wxMilliSleep((1000/(fps*2)));
|
||||
}
|
||||
duration=faster ? duration_backup : duration_backup;
|
||||
reuseBuffer=false;
|
||||
}
|
||||
|
||||
void BoardCanvas::DrawBoard(wxDC &dc) {
|
||||
|
@ -321,17 +364,22 @@ void BoardCanvas::Swap() {
|
|||
}
|
||||
|
||||
void BoardCanvas::OnKeyEvent(wxKeyEvent &event) {
|
||||
event.ResumePropagation(1);
|
||||
event.Skip();
|
||||
return;
|
||||
wxLogDebug("OnKeyEvent %d",event.GetUnicodeKey());
|
||||
if (event.GetKeyCode() == WXK_LEFT) {
|
||||
wxCommandEvent previousEvent(PREVIOUS_MOVE_EVENT, GetId());
|
||||
previousEvent.SetEventObject(this);
|
||||
//previousEvent.SetEventObject(this);
|
||||
ProcessEvent(previousEvent);
|
||||
} else if (event.GetKeyCode() == WXK_RIGHT) {
|
||||
wxCommandEvent nextEvent(NEXT_MOVE_EVENT, GetId());
|
||||
nextEvent.SetEventObject(this);
|
||||
//nextEvent.SetEventObject(this);
|
||||
ProcessEvent(nextEvent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BoardCanvas::SetClockTime(short hours, short min, short sec,
|
||||
bool IsBlack) {
|
||||
if (IsBlack) {
|
||||
|
@ -343,4 +391,4 @@ void BoardCanvas::SetClockTime(short hours, short min, short sec,
|
|||
|
||||
wxBEGIN_EVENT_TABLE(BoardCanvas, wxPanel) EVT_PAINT(BoardCanvas::OnPaint)
|
||||
EVT_MOUSE_EVENTS(BoardCanvas::MouseEvent)
|
||||
EVT_CHAR_HOOK(BoardCanvas::OnKeyEvent) wxEND_EVENT_TABLE()
|
||||
wxEND_EVENT_TABLE()
|
||||
|
|
|
@ -60,14 +60,13 @@ class BoardCanvas : public wxPanel {
|
|||
// Drawing buffer (ANIMATIONS)
|
||||
wxBitmap *buffer;
|
||||
bool reuseBuffer;
|
||||
wxTimer timer;
|
||||
int frame,duration,fps;
|
||||
int frame,duration,fps,duration_fast;
|
||||
std::string final_board;
|
||||
bool final_is_black_turn;
|
||||
std::map<char, std::uint8_t> final_captures;
|
||||
std::string src;
|
||||
std::string dst;
|
||||
char piece_moved;
|
||||
wxPoint src;
|
||||
wxPoint transVect;
|
||||
|
||||
public:
|
||||
BoardCanvas(wxFrame *parent);
|
||||
|
@ -82,7 +81,7 @@ public:
|
|||
void Swap();
|
||||
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 Animate(std::string board, bool is_black_turn, std::map<char, std::uint8_t> captures, std::string src, std::string dst,bool faster);
|
||||
void SetClockTime(short hours, short min, short sec, bool IsBlack);
|
||||
DECLARE_EVENT_TABLE()
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue