aboutsummaryrefslogtreecommitdiff
path: root/src/game_tab/left_panel/GameTabLeftPanel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/game_tab/left_panel/GameTabLeftPanel.cpp')
-rw-r--r--src/game_tab/left_panel/GameTabLeftPanel.cpp78
1 files changed, 39 insertions, 39 deletions
diff --git a/src/game_tab/left_panel/GameTabLeftPanel.cpp b/src/game_tab/left_panel/GameTabLeftPanel.cpp
index 50919c0..25ded2c 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> 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);
@@ -31,11 +32,11 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game)
Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){
if(e.GetKeyCode() == WXK_RIGHT){
p->game->Next();
- p->Notify(true,false);
+ p->Notify();
p->repeat=true;
} else if(e.GetKeyCode() == WXK_LEFT){
p->game->Previous();
- p->Notify(true,true);
+ p->Notify();
p->repeat=true;
}
// Notify other classes
@@ -46,10 +47,10 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game)
Bind(wxEVT_MOUSEWHEEL, [p=this](wxMouseEvent& e){
if(e.GetWheelRotation()<0){
p->game->Next();
- p->Notify(true,false);
+ p->Notify();
}else {
p->game->Previous();
- p->Notify(true,true);
+ p->Notify();
}
// Notify other classes
wxCommandEvent event(GAME_CHANGE, p->GetId());
@@ -67,54 +68,53 @@ void GameTabLeftPanel::OnPlay(wxCommandEvent &event) {
event.SetEventObject(this);
ProcessEvent(event);
}
- // Refresh board canvas:
- Notify();
+ Notify(true); // Redraw event is move failed! Otherwise piece not resets to it initial position after dragging
}
-void GameTabLeftPanel::Notify(bool animate, bool backward) {
+void GameTabLeftPanel::Notify(bool skip_animation) {
// Update fen and captures
std::string fen = game->GetFen();
std::map<char, std::uint8_t> captures;
+ bool animate=false;
HalfMove *m = game->GetCurrentMove();
- if (m != nullptr) {
+ 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)){
+ 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)
+ last_move->GetAbsoluteMove(dst,src);
+ animate=true;
+ }
+ } else if(game->GetNextMove()){ // First move animation
+ HalfMove *next=game->GetNextMove();
+ if(next==last_move){
+ game->GetNextMove()->GetAbsoluteMove(dst,src);
+ animate=true;
+ }
}
// Update board canvas:
- if(!animate){
- if(m){
- last_absolute_move=m->GetAbsoluteMove();
- }
- board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board,
- game->IsBlackToPlay(), captures,
- game->GetTag("White"),game->GetTag("Black"));
+ 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(gs);
}
else{
- if(backward && last_absolute_move.size()>0){
- std::string dst=last_absolute_move.substr(0,2);
- std::string src=last_absolute_move.substr(2,2);
- board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board,
- game->IsBlackToPlay(), captures,src,dst,repeat);
- if(m){
- last_absolute_move=m->GetAbsoluteMove();
- }
- }
- else if(!backward && m){
- std::string new_absolute_move=m->GetAbsoluteMove();
- if(last_absolute_move!=new_absolute_move){
- last_absolute_move=new_absolute_move;
- std::string src=last_absolute_move.substr(0,2);
- std::string dst=last_absolute_move.substr(2,2);
- board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board,
- game->IsBlackToPlay(), captures,src,dst,repeat);
- }
- }
- // If m undefined
- if(!m){
- last_absolute_move="";
- }
+ board_canvas->Animate(gs, src,dst,repeat);
}
-
+ // Update last move
+ last_move=m;
// Update fen field:
fen_text_field->SetValue(game->GetFen());
}