aboutsummaryrefslogtreecommitdiff
path: root/src/game_tab/GameTab.cpp
blob: 3326f125ca1c1ee933bc39301b226105d098c264 (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
#include "GameTab.hpp"
#include <wx/clipbrd.h>

wxDEFINE_EVENT(GAME_CHANGE, wxCommandEvent);

GameTab::GameTab(wxFrame *parent, std::shared_ptr<Game> game)
    : wxPanel(parent), game(game), TabInfos(TabInfos::GAME) {
  // Splitter
  wxSplitterWindow *splitter = new wxSplitterWindow(this, wxID_ANY);
  splitter->SetSashGravity(0.8);
  // Panels
  game->BuildAndVerify();
  board_panel = new GameTabLeftPanel((wxFrame *)splitter, game);
  editor_panel = new GameTabRightPanel((wxFrame *)splitter, game);
  splitter->SplitVertically(board_panel, editor_panel);

  // Setup splitter
  wxBoxSizer *topSizer = new wxBoxSizer(wxHORIZONTAL);
  topSizer->Add(splitter, 1, wxEXPAND);
  SetSizerAndFit(topSizer);

  // Refresh panels
  wxCommandEvent event(REFRESH_TAB_TITLE, GetId());
  event.SetEventObject(this);
  OnRefreshTabTitle(event);
  board_panel->Notify(false, false);
  editor_panel->Notify();

  board_panel->Bind(wxEVT_TOOL,&GameTab::OnToolClick,this);
  Bind(REFRESH_TAB_TITLE, &GameTab::OnRefreshTabTitle, this, wxID_ANY);
  Bind(GAME_CHANGE, &GameTab::OnGameChange, this, wxID_ANY);
  splitter->Bind(wxEVT_KEY_DOWN, [p=this,bp=board_panel,ed=editor_panel](wxKeyEvent &e){
    if(e.GetKeyCode() == WXK_RIGHT){
      bp->NextMove(true);
    } else if(e.GetKeyCode() == WXK_LEFT){
      bp->PreviousMove(true);
    }
    ed->Notify();
    });
  splitter->Bind(wxEVT_KEY_UP, [p=this,bp=board_panel,ed=editor_panel](wxKeyEvent &e){
    if(e.GetKeyCode() == WXK_RIGHT){
      bp->NextMove(false);
    } else if(e.GetKeyCode() == WXK_LEFT){
      bp->PreviousMove(false);
    }
    ed->Notify();
    });
}

void GameTab::OnToolClick(wxCommandEvent &event){
  short id=event.GetId();
  if(id==0){
    if(!related_file.size()>0){
      wxFileDialog
      newFileDialog(this, _("Save Game"), "", "",
                    "PGN files (*.pgn)|*.pgn", wxFD_SAVE|wxFD_OVERWRITE_PROMPT);
      if (newFileDialog.ShowModal() == wxID_CANCEL)
        return;
      // Create and open new db
      related_file = newFileDialog.GetPath().ToStdString();
    }
    SaveGame(related_file,game);
  } else if(id==1){
    Game *g=new Game(&(*game));
    wxGetApp().NewGame(std::shared_ptr<Game>(g));
  }
}

void GameTab::OnGameChange(wxCommandEvent &event) {
  board_panel->Notify(false,false);
  editor_panel->Notify();
}

void GameTab::OnRefreshTabTitle(wxCommandEvent &event) {
  std::string white = game->GetTag("White");
  std::string black = game->GetTag("Black");
  if (white.size() == 0 && black.size() == 0) {
    SetLabel("New Game");
  } else {
    SetLabel(white + "-" + black);
  }
  event.SetEventObject(this);
  event.Skip();
}

void GameTab::ApplyPreferences() {
  board_panel->ApplyPreferences();
  editor_panel->ApplyPreferences();
}