#include "GameTabLeftPanel.hpp" #include GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr game) : TabGameLeftPanel(parent), game(game), repeat(false) { // Configure toolbar (note that toolbar events are processed into the GameTab class) game_toolbar->AddTool(0, wxT("Save As"), wxArtProvider::GetBitmap(wxART_FILE_SAVE, wxART_TOOLBAR)); game_toolbar->AddTool(1, wxT("Duplicate Game"), wxArtProvider::GetBitmap(wxART_COPY, wxART_TOOLBAR)); // Add board board_canvas = new BoardCanvas((wxFrame *)this); main_sizer->Insert(1, board_canvas, 1, wxEXPAND); // Configure buttons swap_button->SetBitmapLabel(LoadPNG("swap")); zoomin_button->SetBitmapLabel(LoadPNG("zoomin")); zoomout_button->SetBitmapLabel(LoadPNG("zoomout")); // Configure FEN field fen_text_field->SetFont(wxFont(*wxNORMAL_FONT).Bold().Larger()); // Bind events: Bind(PLAY_MOVE_EVENT, &GameTabLeftPanel::OnPlay, this, wxID_ANY); Bind(wxEVT_BUTTON, [bc=board_canvas](wxCommandEvent &event){bc->Zoom(10);}, ZOOM_IN_BTN); Bind(wxEVT_BUTTON, [bc=board_canvas](wxCommandEvent &event){bc->Zoom(-10);}, ZOOM_OUT_BTN); Bind(wxEVT_BUTTON, [bc=board_canvas](wxCommandEvent &event){bc->Swap();}, SWAP_BTN); Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){p->repeat=false;}); Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){ if(e.GetKeyCode() == WXK_RIGHT){ p->game->Next(); p->Notify(true,false); p->repeat=true; } else if(e.GetKeyCode() == WXK_LEFT){ p->game->Previous(); p->Notify(true,true); p->repeat=true; } // Notify other classes wxCommandEvent event(GAME_CHANGE, p->GetId()); event.SetEventObject(p); p->ProcessEvent(event); }); Bind(wxEVT_MOUSEWHEEL, [p=this](wxMouseEvent& e){ if(e.GetWheelRotation()<0){ p->game->Next(); p->Notify(true,false); }else { p->game->Previous(); p->Notify(true,true); } // Notify other classes wxCommandEvent event(GAME_CHANGE, p->GetId()); event.SetEventObject(p); p->ProcessEvent(event); }); } void GameTabLeftPanel::OnPlay(wxCommandEvent &event) { wxLogDebug("Game tab received PLAY_MOVE_EVENT"); if (game->Play(event.GetString().ToStdString())) { // Notify other classes wxCommandEvent event(GAME_CHANGE, GetId()); event.SetEventObject(this); ProcessEvent(event); } // Refresh board canvas: Notify(); } void GameTabLeftPanel::Notify(bool animate, bool backward) { wxLogDebug("Called!"); // Update fen and captures std::string fen = game->GetFen(); std::map captures; HalfMove *m = game->GetCurrentMove(); //animate=false; //backward=false; if (m != nullptr) { captures = m->GetLineCaptures(); /*HalfMove *parent=m->GetParent(); if(!parent){ animate=true; } if(last_absolute_move == parent->GetAbsoluteMove()){ wxLogDebug("Next true!"); animate=true; } else if(m->GetAbsoluteMove() == last_absolute_move){ animate=true; backward=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")); } 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=""; } } // Update fen field: fen_text_field->SetValue(game->GetFen()); } void GameTabLeftPanel::ApplyPreferences() { board_canvas->ApplyPreferences(); }