aboutsummaryrefslogtreecommitdiff
path: root/src/game_tab/board/BoardPanel.cpp
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2022-02-23 18:11:55 +0100
committerLoic Guegan <manzerbredes@mailbox.org>2022-02-23 18:11:55 +0100
commitce941c146aea7925bded6b9d2a0d0559d3156ad3 (patch)
tree4c52e02600e3fd127bfb28b3e974d45541ec9e4e /src/game_tab/board/BoardPanel.cpp
Create repository
Diffstat (limited to 'src/game_tab/board/BoardPanel.cpp')
-rw-r--r--src/game_tab/board/BoardPanel.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/game_tab/board/BoardPanel.cpp b/src/game_tab/board/BoardPanel.cpp
new file mode 100644
index 0000000..ff4fb62
--- /dev/null
+++ b/src/game_tab/board/BoardPanel.cpp
@@ -0,0 +1,96 @@
+#include "BoardPanel.hpp"
+#include <wx/clipbrd.h>
+
+BoardPanel::BoardPanel(wxFrame *parent, Game *game)
+ : wxPanel(parent), game(game) {
+
+ wxBoxSizer *board_panel_sizer = new wxBoxSizer(wxVERTICAL);
+ board_canvas = new BoardCanvas((wxFrame *)this);
+ board_panel_sizer->Add(board_canvas, 1, wxEXPAND);
+
+ // Left Panel buttons
+ wxBoxSizer *board_panel_button_sizer = new wxBoxSizer(wxHORIZONTAL);
+ board_panel_button_sizer->Add(
+ new wxBitmapButton(this, SWAP_BTN, LoadPNG("swap")), 0);
+ board_panel_button_sizer->Add(
+ new wxBitmapButton(this, ZOOM_IN_BTN, LoadPNG("zoomin")), 0);
+ board_panel_button_sizer->Add(
+ new wxBitmapButton(this, ZOOM_OUT_BTN, LoadPNG("zoomout")), 0);
+ board_panel_button_sizer->Add(new wxButton(this, COPY_FEN_BTN, L"Copy FEN"),
+ 0, wxEXPAND);
+ board_panel_sizer->Add(board_panel_button_sizer, 0);
+ this->SetSizer(board_panel_sizer);
+
+ Bind(PLAY_MOVE_EVENT, &BoardPanel::OnPlay, this, wxID_ANY);
+ Bind(PREVIOUS_MOVE_EVENT, &BoardPanel::OnPreviousMove, this, wxID_ANY);
+ Bind(NEXT_MOVE_EVENT, &BoardPanel::OnNextMove, this, wxID_ANY);
+ Bind(wxEVT_BUTTON, &BoardPanel::OnSwap, this, SWAP_BTN);
+ Bind(wxEVT_BUTTON, &BoardPanel::OnZoomIn, this, ZOOM_IN_BTN);
+ Bind(wxEVT_BUTTON, &BoardPanel::OnZoomOut, this, ZOOM_OUT_BTN);
+ Bind(wxEVT_BUTTON, &BoardPanel::OnCopyFEN, this, COPY_FEN_BTN);
+}
+
+void BoardPanel::OnPreviousMove(wxCommandEvent &event) {
+ game->Previous();
+ Notify();
+ NotifyEditor();
+}
+
+void BoardPanel::OnZoomIn(wxCommandEvent &event) {
+ wxLogDebug("Clicked on zoom in");
+ board_canvas->Zoom(10);
+}
+
+void BoardPanel::OnZoomOut(wxCommandEvent &event) {
+ wxLogDebug("Clicked on zoom out");
+ board_canvas->Zoom(-10);
+}
+
+void BoardPanel::OnSwap(wxCommandEvent &event) {
+ wxLogDebug("Clicked on swap");
+ board_canvas->Swap();
+}
+
+void BoardPanel::OnNextMove(wxCommandEvent &event) {
+ wxLogDebug("Game tab received NEXT_MOVE_EVENT");
+ game->Next();
+ Notify();
+ NotifyEditor();
+}
+
+void BoardPanel::OnPlay(wxCommandEvent &event) {
+ wxLogDebug("Game tab received PLAY_MOVE_EVENT");
+ if (game->Play(event.GetString().ToStdString())) {
+ NotifyEditor();
+ }
+ Notify();
+}
+
+void BoardPanel::OnCopyFEN(wxCommandEvent &event) {
+ wxLogDebug("Clicked on copy fen");
+ if (wxTheClipboard->Open()) {
+ wxTheClipboard->SetData(new wxTextDataObject(game->GetFen()));
+ wxTheClipboard->Close();
+ }
+}
+
+void BoardPanel::Notify() {
+ 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);
+}
+
+void BoardPanel::NotifyEditor() {
+ wxCommandEvent previousEvent(GAME_CHANGE, GetId());
+ previousEvent.SetEventObject(this);
+ ProcessEvent(previousEvent);
+}
+
+void BoardPanel::ApplyPreferences() {
+ board_canvas->ApplyPreferences();
+}