ochess/src/game_tab/left_panel/GameTabLeftPanel.cpp

131 lines
4 KiB
C++
Raw Normal View History

2022-02-28 13:44:27 +01:00
#include "GameTabLeftPanel.hpp"
2022-02-23 18:11:55 +01:00
#include <wx/clipbrd.h>
2022-02-28 20:16:57 +01:00
GameTabLeftPanel::GameTabLeftPanel(wxFrame *parent, std::shared_ptr<Game> game)
2022-12-29 12:28:51 +01:00
: TabGameLeftPanel(parent), game(game), repeat(false) {
2022-02-23 18:11:55 +01:00
2023-01-01 12:15:08 +01:00
// Configure toolbar (note that toolbar events are processed into the GameTab class)
2022-12-30 15:58:13 +01:00
game_toolbar->AddTool(0, wxT("Save As"),
2022-12-30 13:17:06 +01:00
wxArtProvider::GetBitmap(wxART_FILE_SAVE, wxART_TOOLBAR));
2022-12-30 16:24:44 +01:00
game_toolbar->AddTool(1, wxT("Duplicate Game"),
wxArtProvider::GetBitmap(wxART_COPY, wxART_TOOLBAR));
2022-12-30 13:17:06 +01:00
2022-02-28 14:02:02 +01:00
// Add board
2022-02-23 18:11:55 +01:00
board_canvas = new BoardCanvas((wxFrame *)this);
2022-12-30 13:17:06 +01:00
main_sizer->Insert(1, board_canvas, 1, wxEXPAND);
2022-02-23 18:11:55 +01:00
2022-02-28 14:02:02 +01:00
// Configure buttons
swap_button->SetBitmapLabel(LoadPNG("swap"));
zoomin_button->SetBitmapLabel(LoadPNG("zoomin"));
zoomout_button->SetBitmapLabel(LoadPNG("zoomout"));
2022-02-23 18:11:55 +01:00
2022-02-28 15:27:51 +01:00
// Configure FEN field
2022-12-27 17:42:30 +01:00
fen_text_field->SetFont(wxFont(*wxNORMAL_FONT).Bold().Larger());
2022-02-28 15:27:51 +01:00
2023-01-01 14:35:20 +01:00
last_move=game->GetCurrentMove();
2023-01-01 12:15:08 +01:00
// Bind events:
2022-02-28 13:44:27 +01:00
Bind(PLAY_MOVE_EVENT, &GameTabLeftPanel::OnPlay, this, wxID_ANY);
2023-01-01 12:15:08 +01:00
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);
});
2022-02-23 18:11:55 +01:00
}
2022-12-29 10:08:22 +01:00
2022-02-28 13:44:27 +01:00
void GameTabLeftPanel::OnPlay(wxCommandEvent &event) {
2022-02-23 18:11:55 +01:00
wxLogDebug("Game tab received PLAY_MOVE_EVENT");
if (game->Play(event.GetString().ToStdString())) {
2023-01-01 12:15:08 +01:00
// Notify other classes
wxCommandEvent event(GAME_CHANGE, GetId());
event.SetEventObject(this);
ProcessEvent(event);
2022-02-23 18:11:55 +01:00
}
2023-01-01 12:15:08 +01:00
// Refresh board canvas:
2022-02-23 18:11:55 +01:00
Notify();
}
2022-12-29 10:08:22 +01:00
void GameTabLeftPanel::Notify(bool animate, bool backward) {
2023-01-01 14:12:57 +01:00
wxLogDebug("Called!");
2023-01-01 12:15:08 +01:00
// Update fen and captures
2022-02-23 18:11:55 +01:00
std::string fen = game->GetFen();
std::map<char, std::uint8_t> captures;
HalfMove *m = game->GetCurrentMove();
2023-01-01 14:35:20 +01:00
std::string src,dst;
animate=false;
backward=false;
2022-12-31 20:45:03 +01:00
if (m != nullptr) {
2022-02-23 18:11:55 +01:00
captures = m->GetLineCaptures();
2023-01-01 14:35:20 +01:00
std::string absolute_move= m->GetAbsoluteMove();
// Check if we should animate
if(m->GetParent()==last_move){
wxLogDebug("Animate animate next");
2023-01-01 14:12:57 +01:00
animate=true;
2023-01-01 14:35:20 +01:00
src=absolute_move.substr(0,2);
dst=absolute_move.substr(2,2);
} else if (m->GetMainline()==last_move){
wxLogDebug("Animate Previous");
2023-01-01 14:12:57 +01:00
animate=true;
backward=true;
2023-01-01 14:35:20 +01:00
src=absolute_move.substr(2,2);
dst=absolute_move.substr(0,2);
} else {
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);
}
}
}
2022-12-28 12:40:16 +01:00
}
2023-01-01 14:12:57 +01:00
2023-01-01 12:15:08 +01:00
// Update board canvas:
2022-12-28 12:40:16 +01:00
if(!animate){
board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board,
2022-12-30 13:17:06 +01:00
game->IsBlackToPlay(), captures,
game->GetTag("White"),game->GetTag("Black"));
2022-12-28 12:40:16 +01:00
}
else{
2023-01-01 14:35:20 +01:00
board_canvas->Animate(chessarbiter::FENParser::Parse(fen).board,
2022-12-29 10:08:22 +01:00
game->IsBlackToPlay(), captures,src,dst,repeat);
2022-12-28 12:40:16 +01:00
}
2022-12-28 12:10:08 +01:00
2023-01-01 14:35:20 +01:00
last_move=m;
2023-01-01 12:15:08 +01:00
// Update fen field:
2022-02-28 15:27:51 +01:00
fen_text_field->SetValue(game->GetFen());
2022-02-23 18:11:55 +01:00
}
2022-02-28 14:02:02 +01:00
void GameTabLeftPanel::ApplyPreferences() { board_canvas->ApplyPreferences(); }