ochess/src/game_tab/left_panel/GameTabLeftPanel.cpp

140 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
2022-12-30 13:17:06 +01:00
// Configure toolbal
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
2022-02-28 13:44:27 +01:00
Bind(PLAY_MOVE_EVENT, &GameTabLeftPanel::OnPlay, this, wxID_ANY);
Bind(wxEVT_BUTTON, &GameTabLeftPanel::OnSwap, this, SWAP_BTN);
Bind(wxEVT_BUTTON, &GameTabLeftPanel::OnZoomIn, this, ZOOM_IN_BTN);
Bind(wxEVT_BUTTON, &GameTabLeftPanel::OnZoomOut, this, ZOOM_OUT_BTN);
2022-12-29 10:08:22 +01:00
Bind(wxEVT_KEY_DOWN, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
2022-12-30 13:17:06 +01:00
}
2022-12-30 15:58:13 +01:00
2022-02-23 18:11:55 +01:00
2022-12-29 10:08:22 +01:00
void GameTabLeftPanel::PreviousMove(bool isKeyDown) {
if(isKeyDown){
game->Previous();
Notify(true,true);
repeat=true;
} else {
repeat=false;
}
}
void GameTabLeftPanel::NextMove(bool isKeyDown) {
if(isKeyDown){
game->Next();
Notify(true,false);
repeat=true;
}
else{
repeat=false;
}
2022-02-23 18:11:55 +01:00
}
2022-02-28 13:44:27 +01:00
void GameTabLeftPanel::OnZoomIn(wxCommandEvent &event) {
2022-02-23 18:11:55 +01:00
wxLogDebug("Clicked on zoom in");
board_canvas->Zoom(10);
}
2022-02-28 13:44:27 +01:00
void GameTabLeftPanel::OnZoomOut(wxCommandEvent &event) {
2022-02-23 18:11:55 +01:00
wxLogDebug("Clicked on zoom out");
board_canvas->Zoom(-10);
}
2022-02-28 13:44:27 +01:00
void GameTabLeftPanel::OnSwap(wxCommandEvent &event) {
2022-02-23 18:11:55 +01:00
wxLogDebug("Clicked on swap");
board_canvas->Swap();
}
2022-12-29 10:08:22 +01:00
2022-02-23 18:11:55 +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())) {
NotifyEditor();
}
Notify();
2022-12-28 12:10:08 +01:00
std::string fen = game->GetFen();
std::map<char, std::uint8_t> captures;
HalfMove *m = game->GetCurrentMove();
2022-12-31 20:45:03 +01:00
if (m != nullptr) {
2022-12-28 12:10:08 +01:00
captures = m->GetLineCaptures();
2022-12-28 12:40:16 +01:00
}
2022-02-23 18:11:55 +01:00
}
2022-12-29 10:08:22 +01:00
void GameTabLeftPanel::Notify(bool animate, bool backward) {
2022-02-23 18:11:55 +01:00
std::string fen = game->GetFen();
std::map<char, std::uint8_t> captures;
HalfMove *m = game->GetCurrentMove();
2022-12-31 20:45:03 +01:00
if (m != nullptr) {
2022-02-23 18:11:55 +01:00
captures = m->GetLineCaptures();
2022-12-28 12:40:16 +01:00
}
if(!animate){
2022-12-29 10:08:22 +01:00
if(m){
last_absolute_move=m->GetAbsoluteMove();
}
2022-12-28 12:40:16 +01:00
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{
2022-12-29 10:08:22 +01:00
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="";
}
2022-12-28 12:40:16 +01:00
}
2022-12-28 12:10:08 +01:00
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 13:44:27 +01:00
void GameTabLeftPanel::NotifyEditor() {
2022-02-23 18:11:55 +01:00
wxCommandEvent previousEvent(GAME_CHANGE, GetId());
previousEvent.SetEventObject(this);
ProcessEvent(previousEvent);
}
2022-02-28 14:02:02 +01:00
void GameTabLeftPanel::ApplyPreferences() { board_canvas->ApplyPreferences(); }