ochess/src/game_tab/left_panel/GameTabLeftPanel.cpp

82 lines
2.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-02-28 14:02:02 +01:00
: TabGameLeftPanel(parent), game(game) {
2022-02-23 18:11:55 +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-02-28 14:02:02 +01:00
main_sizer->Insert(0, 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
fen_text_field->SetFont(wxFont(*wxSMALL_FONT).Bold());
2022-02-28 13:44:27 +01:00
Bind(PLAY_MOVE_EVENT, &GameTabLeftPanel::OnPlay, this, wxID_ANY);
Bind(PREVIOUS_MOVE_EVENT, &GameTabLeftPanel::OnPreviousMove, this, wxID_ANY);
Bind(NEXT_MOVE_EVENT, &GameTabLeftPanel::OnNextMove, 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-02-23 18:11:55 +01:00
}
2022-02-28 13:44:27 +01:00
void GameTabLeftPanel::OnPreviousMove(wxCommandEvent &event) {
2022-02-23 18:11:55 +01:00
game->Previous();
Notify();
NotifyEditor();
}
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-02-28 13:44:27 +01:00
void GameTabLeftPanel::OnNextMove(wxCommandEvent &event) {
2022-02-23 18:11:55 +01:00
wxLogDebug("Game tab received NEXT_MOVE_EVENT");
game->Next();
Notify();
NotifyEditor();
}
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-02-28 13:44:27 +01:00
void GameTabLeftPanel::Notify() {
2022-02-23 18:11:55 +01:00
std::string fen = game->GetFen();
std::map<char, std::uint8_t> captures;
HalfMove *m = game->GetCurrentMove();
if (m != NULL) {
captures = m->GetLineCaptures();
}
board_canvas->SetupBoard(chessarbiter::FENParser::Parse(fen).board,
game->IsBlackToPlay(), captures);
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(); }