OChess v0.0.2
Programmer's Manual
BoardPrefs.hpp
1#include "game_tab/left_panel/board/BoardCanvas.hpp"
2#include "ochess.hpp"
3#include <wx/combobox.h>
4#include <wx/dir.h>
5#include <wx/filename.h>
6#include <wx/preferences.h>
7#include <wx/spinctrl.h>
8#include <wx/stdpaths.h>
9#ifdef __APPLE__
10#include <wx/bmpbndl.h>
11#endif
17 BoardCanvas *real_board_canvas;
18 wxFileName pieces_path;
19 wxFileName squares_path;
20
21public:
22 BoardPrefsPanel(wxWindow *parent) : PrefsBoard(parent) {
23 wxBoxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
24 real_board_canvas = new BoardCanvas((wxFrame *)board_canvas, 40, true);
25 sizer->Add(real_board_canvas, 1, wxEXPAND, 5);
26 board_canvas->SetSizerAndFit(sizer);
27
28 wxStandardPaths p = wxStandardPaths::Get();
29 pieces_path = wxFileName(p.GetExecutablePath());
30 pieces_path = pieces_path.GetPath() + "/assets/pieces";
31 squares_path = wxFileName(pieces_path);
32 squares_path = squares_path.GetPath() + "/boards";
33 wxLogDebug(squares_path.GetFullPath());
34
35 Bind(wxEVT_LISTBOX, &BoardPrefsPanel::OnConfChange, this, wxID_ANY);
36 Bind(wxEVT_SPINCTRL, &BoardPrefsPanel::OnConfChange, this, wxID_ANY);
37 }
38 void OnConfChange(wxCommandEvent &event) {
39 UNUSED(event);
40 ApplyPreferences();
41 real_board_canvas->ApplyPreferences();
42 }
43
44 virtual bool TransferDataToWindow() {
45 wxLogDebug("Load!");
46
47 wxDir pieces_dir(pieces_path.GetFullPath());
48 wxString filename;
49 bool cont = pieces_dir.GetFirst(&filename, wxEmptyString, wxDIR_DEFAULT);
50 piece_theme->Append("default");
51 while (cont) {
52 wxFileName fn(filename);
53 fn.ClearExt();
54 piece_theme->Append(fn.GetName());
55 cont = pieces_dir.GetNext(&filename);
56 }
57
58 wxDir squares_dir(squares_path.GetFullPath());
59 cont = squares_dir.GetFirst(&filename, wxEmptyString, wxDIR_DEFAULT);
60 square_theme->Append("default");
61 while (cont) {
62 wxFileName fn(filename);
63 fn.ClearExt();
64 square_theme->Append(fn.GetName());
65 cont = squares_dir.GetNext(&filename);
66 }
67
68 CONFIG_OPEN(config);
69 piece_theme->SetStringSelection(
70 config->Read("board/theme/pieces/name", "default"));
71 square_theme->SetStringSelection(
72 config->Read("board/theme/squares/name", "default"));
73 show_side_badge->SetValue(config->Read("board/show_side_badge", true));
74 show_captures->SetValue(config->Read("board/show_captures", true));
75 black_by_default->SetValue(config->Read("board/black_by_default", false));
76 corner_radius->SetValue(config->Read("board/corner_radius", 8));
77 square_size->SetValue(config->Read("board/square_size", 80));
78 CONFIG_CLOSE(config);
79
80 return true;
81 }
82
83 void ApplyPreferences() {
84 CONFIG_OPEN(config);
85 wxString cur_theme = piece_theme->GetString(piece_theme->GetSelection());
86 config->Write("board/theme/pieces/name", cur_theme);
87 config->Write("board/theme/pieces/path",
88 pieces_path.GetFullPath() + "/" + cur_theme + ".png");
89 cur_theme = square_theme->GetString(square_theme->GetSelection());
90 config->Write("board/theme/squares/name", cur_theme);
91 config->Write("board/theme/squares/path",
92 squares_path.GetFullPath() + "/" + cur_theme + ".png");
93
94 config->Write("board/show_side_badge", show_side_badge->GetValue());
95 config->Write("board/show_captures", show_captures->GetValue());
96 config->Write("board/black_by_default", black_by_default->GetValue());
97 config->Write("board/corner_radius", corner_radius->GetValue());
98 config->Write("board/square_size", square_size->GetValue());
99
100 CONFIG_CLOSE(config);
101 }
102
103 virtual bool TransferDataFromWindow() {
104 ApplyPreferences();
105 MAINWIN->ApplyPreferences();
106 return (true);
107 }
108};
109
114class BoardPrefs : public wxPreferencesPage {
115public:
116 virtual wxString GetName() const { return "Board"; }
117 virtual wxBitmap GetLargeIcon() {
118 return wxArtProvider::GetBitmap(wxART_HELP, wxART_TOOLBAR);
119 }
120 #ifdef __APPLE__
121 virtual wxBitmapBundle GetIcon() {
122 return wxBitmapBundle::FromBitmap(LoadPNG("ui_rook"));
123 }
124 #endif
125 virtual wxWindow *CreateWindow(wxWindow *parent) {
126 return new BoardPrefsPanel(parent);
127 }
128};
This class draws the chess board (squares, pieces, arrows and every other board related components).
Definition: BoardCanvas.hpp:113
Configuration page for the BoardCanvas.
Definition: BoardPrefs.hpp:16
Interface for wxWidgets to load BoardPrefsPanel into the preference window.
Definition: BoardPrefs.hpp:114
Class PrefsBoard.
Definition: gui.h:229
wxBitmap LoadPNG(std::string icon, wxSize size)
Load an icon from embedded binary resources and rescale it to a specified size.
Definition: binres.cpp:7