Debian Codegraph @

plugins/dm.difficulty/DifficultyDialog.cpp

raw
1 #include "DifficultyDialog.h"
2
3 #include "i18n.h"
4 #include "iundo.h"
5 #include "ui/imainframe.h"
6 #include "iscenegraph.h"
7
8 #include "gamelib.h"
9 #include "registry/registry.h"
10 #include "string/string.h"
11 #include "wxutil/dialog/Dialog.h"
12 #include "wxutil/EntryAbortedException.h"
13
14 #include <iostream>
15
16 #include <wx/notebook.h>
17 #include <wx/panel.h>
18 #include <wx/artprov.h>
19 #include <wx/sizer.h>
20 #include <wx/button.h>
21
22 namespace ui
23 {
24
25 namespace
26 {
27 const char* const WINDOW_TITLE = N_("Difficulty Editor");
28 }
29
30 DifficultyDialog::DifficultyDialog() :
31 DialogBase(_(WINDOW_TITLE))
32 {
33 // Load the settings
34 _settingsManager.loadSettings();
35
36 // Create the widgets
37 populateWindow();
38 }
39
40 void DifficultyDialog::createDifficultyEditors()
41 {
42 int numLevels = game::current::getValue<int>(GKEY_DIFFICULTY_LEVELS);
43
44 for (int i = 0; i < numLevels; i++)
45 {
46 // Acquire the settings object
47 difficulty::DifficultySettingsPtr settings = _settingsManager.getSettings(i);
48 if (settings)
49 {
50 // Construct the editor for this difficulty level and add it to our
51 // internal list of editors
52 std::string diffName = _settingsManager.getDifficultyName(i);
53 auto editor = std::make_shared<DifficultyEditor>(_notebook,
54 settings);
55 _editors.push_back(editor);
56
57 // Insert the editor's widget as a new page in the choicebook
58 wxWindow* editorWidget = editor->getWidget();
59 editorWidget->Reparent(_notebook);
60 _notebook->AddPage(editorWidget, diffName, false);
61 }
62 }
63 }
64
65 void DifficultyDialog::populateWindow()
66 {
67 SetSizer(new wxBoxSizer(wxVERTICAL));
68
69 // Create the notebook and add it to the vbox
70 _notebook = new wxChoicebook(this, wxID_ANY);
71 _notebook->SetMinClientSize(wxSize(800, 400));
72
73 // Add the edit button alongside the dropdown
74 wxSizer* choiceSizer = _notebook->GetControlSizer();
75 wxButton* editBtn = new wxButton(
76 _notebook, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
77 wxBU_EXACTFIT | wxBU_NOTEXT
78 );
79 editBtn->Bind(wxEVT_BUTTON,
80 [&] (wxCommandEvent&) { editCurrentDifficultyName(); });
81 editBtn->SetBitmap(wxArtProvider::GetBitmap("darkradiant:edit.png"));
82 choiceSizer->Add(editBtn, 0, wxEXPAND | wxLEFT, 6);
83
84 // Create and pack the editors
85 createDifficultyEditors();
86
87 GetSizer()->Add(_notebook, 1, wxEXPAND | wxALL, 12);
88
89 wxButton* okButton = new wxButton(this, wxID_OK);
90 wxButton* cancelButton = new wxButton(this, wxID_CANCEL);
91
92 okButton->Bind(wxEVT_BUTTON, [&] (wxCommandEvent&) { EndModal(wxID_OK); });
93 cancelButton->Bind(wxEVT_BUTTON, [&] (wxCommandEvent&) { EndModal(wxID_CANCEL); });
94
95 wxBoxSizer* buttonSizer = new wxBoxSizer(wxHORIZONTAL);
96 buttonSizer->Add(cancelButton);
97 buttonSizer->AddSpacer(6);
98 buttonSizer->Add(okButton);
99
100 GetSizer()->Add(buttonSizer, 0, wxALIGN_RIGHT | wxALL, 12);
101
102 Layout();
103 Fit();
104 }
105
106 void DifficultyDialog::editCurrentDifficultyName()
107 {
108 // Initialise an EditNameDialog with the current tab text as the initial
109 // name to edit
110 int curDiffLevel = _notebook->GetSelection(); // assume tabs numbered from 0
111
112 try
113 {
114 std::string oldName = _notebook->GetPageText(curDiffLevel).ToStdString();
115
116 std::string newName = wxutil::Dialog::TextEntryDialog(
117 _("Difficulty name"), _("New name:"),
118 oldName, this
119 );
120
121 // Don't allow setting it to an empty name
122 if (!newName.empty() && oldName != newName)
123 {
124 // Change the difficulty name in the map
125 _settingsManager.setDifficultyName(curDiffLevel, newName);
126
127 // Change the displayed name in the dialog
128 _notebook->SetPageText(curDiffLevel, newName);
129 }
130 }
131 catch (wxutil::EntryAbortedException&)
132 {
133 // do nothing in case the user hit cancel
134 }
135 }
136
137 void DifficultyDialog::save()
138 {
139 // Consistency check can go here
140
141 // Scoped undo object
142 UndoableCommand command("editDifficulty");
143
144 // Save the working set to the entity
145 _settingsManager.saveSettings();
146 }
147
148 int DifficultyDialog::ShowModal()
149 {
150 int returnCode = DialogBase::ShowModal();
151
152 if (returnCode == wxID_OK)
153 {
154 save();
155 }
156
157 return returnCode;
158 }
159
160 // Static command target
161 void DifficultyDialog::ShowDialog(const cmd::ArgumentList& args)
162 {
163 // Construct a new instance and enter the main loop
164 DifficultyDialog editor;
165 editor.ShowModal();
166 }
167
168 } // namespace ui