aboutsummaryrefslogtreecommitdiff
path: root/src/game_tab/left_panel/GameTabLeftPanel.cpp
blob: 79894d0efb034f65a2c15a3895a987aa2a28b910 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "GameTabLeftPanel.hpp"
#include <wx/clipbrd.h>

GameTabLeftPanel::GameTabLeftPanel(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, &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);
  Bind(wxEVT_BUTTON, &GameTabLeftPanel::OnCopyFEN, this, COPY_FEN_BTN);
}

void GameTabLeftPanel::OnPreviousMove(wxCommandEvent &event) {
  game->Previous();
  Notify();
  NotifyEditor();
}

void GameTabLeftPanel::OnZoomIn(wxCommandEvent &event) {
  wxLogDebug("Clicked on zoom in");
  board_canvas->Zoom(10);
}

void GameTabLeftPanel::OnZoomOut(wxCommandEvent &event) {
  wxLogDebug("Clicked on zoom out");
  board_canvas->Zoom(-10);
}

void GameTabLeftPanel::OnSwap(wxCommandEvent &event) {
  wxLogDebug("Clicked on swap");
  board_canvas->Swap();
}

void GameTabLeftPanel::OnNextMove(wxCommandEvent &event) {
  wxLogDebug("Game tab received NEXT_MOVE_EVENT");
  game->Next();
  Notify();
  NotifyEditor();
}

void GameTabLeftPanel::OnPlay(wxCommandEvent &event) {
  wxLogDebug("Game tab received PLAY_MOVE_EVENT");
  if (game->Play(event.GetString().ToStdString())) {
    NotifyEditor();
  }
  Notify();
}

void GameTabLeftPanel::OnCopyFEN(wxCommandEvent &event) {
  wxLogDebug("Clicked on copy fen");
  if (wxTheClipboard->Open()) {
    wxTheClipboard->SetData(new wxTextDataObject(game->GetFen()));
    wxTheClipboard->Close();
  }
}

void GameTabLeftPanel::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 GameTabLeftPanel::NotifyEditor() {
  wxCommandEvent previousEvent(GAME_CHANGE, GetId());
  previousEvent.SetEventObject(this);
  ProcessEvent(previousEvent);
}

void GameTabLeftPanel::ApplyPreferences() {
  board_canvas->ApplyPreferences();
}