Update Game tab

This commit is contained in:
Loic Guegan 2023-01-01 18:18:27 +01:00
parent 88430eec29
commit 4e85af5e08
5 changed files with 27 additions and 53 deletions

View file

@ -21,7 +21,7 @@ GameTab::GameTab(wxFrame *parent, std::shared_ptr<Game> game)
// Refresh panels // Refresh panels
RefreshTabTitle(); RefreshTabTitle();
board_panel->Notify(false, false); board_panel->Notify();
editor_panel->Notify(); editor_panel->Notify();
board_panel->Bind(wxEVT_TOOL,&GameTab::OnToolClick,this); board_panel->Bind(wxEVT_TOOL,&GameTab::OnToolClick,this);

View file

@ -177,6 +177,18 @@ void HalfMove::Promote() {
} }
} }
bool HalfMove::HasChild(HalfMove *m){
if(m!=mainline){
for(auto var: variations){
if(m== var){
return(true);
}
}
return false;
}
return true;
}
bool HalfMove::IsVariation() { bool HalfMove::IsVariation() {
HalfMove *m = this; HalfMove *m = this;
HalfMove *p = HalfMove::parent; HalfMove *p = HalfMove::parent;

View file

@ -45,6 +45,8 @@ public:
void Promote(); void Promote();
/// @brief Check if current half move is within a variation /// @brief Check if current half move is within a variation
bool IsVariation(); bool IsVariation();
/// @brief Return true if current moves has m as mainline or variation
bool HasChild(HalfMove *m);
/// @brief Get the root of a variation /// @brief Get the root of a variation
HalfMove *GetRoot(); HalfMove *GetRoot();
/// @brief Get parent of the current move /// @brief Get parent of the current move

View file

@ -21,7 +21,6 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game)
// Configure FEN field // Configure FEN field
fen_text_field->SetFont(wxFont(*wxNORMAL_FONT).Bold().Larger()); fen_text_field->SetFont(wxFont(*wxNORMAL_FONT).Bold().Larger());
last_move=game->GetCurrentMove(); last_move=game->GetCurrentMove();
// Bind events: // Bind events:
@ -33,11 +32,11 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game)
Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){ Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){
if(e.GetKeyCode() == WXK_RIGHT){ if(e.GetKeyCode() == WXK_RIGHT){
p->game->Next(); p->game->Next();
p->Notify(true,false); p->Notify();
p->repeat=true; p->repeat=true;
} else if(e.GetKeyCode() == WXK_LEFT){ } else if(e.GetKeyCode() == WXK_LEFT){
p->game->Previous(); p->game->Previous();
p->Notify(true,true); p->Notify();
p->repeat=true; p->repeat=true;
} }
// Notify other classes // Notify other classes
@ -48,10 +47,10 @@ GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game)
Bind(wxEVT_MOUSEWHEEL, [p=this](wxMouseEvent& e){ Bind(wxEVT_MOUSEWHEEL, [p=this](wxMouseEvent& e){
if(e.GetWheelRotation()<0){ if(e.GetWheelRotation()<0){
p->game->Next(); p->game->Next();
p->Notify(true,false); p->Notify();
}else { }else {
p->game->Previous(); p->game->Previous();
p->Notify(true,true); p->Notify();
} }
// Notify other classes // Notify other classes
wxCommandEvent event(GAME_CHANGE, p->GetId()); wxCommandEvent event(GAME_CHANGE, p->GetId());
@ -69,71 +68,32 @@ void GameTabLeftPanel::OnPlay(wxCommandEvent &event) {
event.SetEventObject(this); event.SetEventObject(this);
ProcessEvent(event); ProcessEvent(event);
} }
// Refresh board canvas:
Notify();
} }
void GameTabLeftPanel::Notify(bool animate, bool backward) { void GameTabLeftPanel::Notify(bool skip_animation) {
wxLogDebug("Called!"); wxLogDebug("Called!");
// Update fen and captures // Update fen and captures
std::string fen = game->GetFen(); std::string fen = game->GetFen();
std::map<char, std::uint8_t> captures; std::map<char, std::uint8_t> captures;
bool animate=false;
HalfMove *m = game->GetCurrentMove(); HalfMove *m = game->GetCurrentMove();
std::string src,dst; std::string src,dst;
animate=false; if (m)
backward=false;
if (m != nullptr) {
captures = m->GetLineCaptures(); captures = m->GetLineCaptures();
// Check if we should animate
if(m->GetParent()==last_move){
wxLogDebug("Animate animate next");
std::string absolute_move= m->GetAbsoluteMove();
animate=true;
src=absolute_move.substr(0,2);
dst=absolute_move.substr(2,2);
} else if (m->GetMainline()==last_move){
wxLogDebug("Animate Previous");
std::string absolute_move= last_move->GetAbsoluteMove();
animate=true;
backward=true;
src=absolute_move.substr(2,2);
dst=absolute_move.substr(0,2);
} else {
std::string absolute_move= last_move->GetAbsoluteMove();
wxLogDebug("Animate Previous");
for(auto v: m->GetVariations()){
if(v==last_move){
animate=true;
backward=true;
src=absolute_move.substr(2,2);
dst=absolute_move.substr(0,2);
}
}
}
}else if(last_move!=nullptr) {
if(last_move->GetParent()==nullptr){
wxLogDebug("Animate Previous");
std::string absolute_move= last_move->GetAbsoluteMove();
animate=true;
backward=true;
src=absolute_move.substr(2,2);
dst=absolute_move.substr(0,2);
}
}
// Update board canvas: // Update board canvas:
if(!animate){ if(!skip_animation || animate){
board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board, board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board,
game->IsBlackToPlay(), captures, game->IsBlackToPlay(), captures,
game->GetTag("White"),game->GetTag("Black")); game->GetTag("White"),game->GetTag("Black"));
} }
else{ else{
board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board, board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board,
game->IsBlackToPlay(), captures,src,dst,repeat); game->IsBlackToPlay(), captures,src,dst,repeat);
} }
// Update last move
last_move=m; last_move=m;
// Update fen field: // Update fen field:
fen_text_field->SetValue(game->GetFen()); fen_text_field->SetValue(game->GetFen());
} }

View file

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