| 1 |
#include "DifficultyEditor.h" |
| 2 |
|
| 3 |
#include "i18n.h" |
| 4 |
|
| 5 |
#include "wxutil/dataview/TreeView.h" |
| 6 |
#include "wxutil/ChoiceHelper.h" |
| 7 |
#include "wxutil/dialog/MessageBox.h" |
| 8 |
#include "wxutil/EntityClassChooser.h" |
| 9 |
|
| 10 |
#include <wx/panel.h> |
| 11 |
#include <wx/choice.h> |
| 12 |
#include <wx/textctrl.h> |
| 13 |
#include <wx/stattext.h> |
| 14 |
#include <wx/button.h> |
| 15 |
#include <wx/combobox.h> |
| 16 |
#include <wx/sizer.h> |
| 17 |
|
| 18 |
#include "ClassNameStore.h" |
| 19 |
|
| 20 |
#include "debugging/ScopedDebugTimer.h" |
| 21 |
|
| 22 |
namespace ui |
| 23 |
{ |
| 24 |
|
| 25 |
DifficultyEditor::DifficultyEditor(wxWindow* parent, |
| 26 |
const difficulty::DifficultySettingsPtr& settings) |
| 27 |
: _settings(settings) |
| 28 |
{ |
| 29 |
// The actual editor pane |
| 30 |
_editor = loadNamedPanel(parent, "DifficultyEditorMainPanel"); |
| 31 |
|
| 32 |
_settings->updateTreeModel(); |
| 33 |
|
| 34 |
populateWindow(); |
| 35 |
updateEditorWidgets(); |
| 36 |
} |
| 37 |
|
| 38 |
wxWindow* DifficultyEditor::getWidget() |
| 39 |
{ |
| 40 |
return _editor; |
| 41 |
} |
| 42 |
|
| 43 |
void DifficultyEditor::populateWindow() |
| 44 |
{ |
| 45 |
ScopedDebugTimer timer("DifficultyEditor::populateWindow()"); |
| 46 |
|
| 47 |
wxPanel* viewPanel = findNamedObject<wxPanel>(_editor, "DifficultyEditorTreeViewPanel"); |
| 48 |
|
| 49 |
_settingsView = wxutil::TreeView::CreateWithModel(viewPanel, _settings->getTreeStore().get()); |
| 50 |
_settingsView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED, |
| 51 |
wxDataViewEventHandler(DifficultyEditor::onSettingSelectionChange), NULL, this); |
| 52 |
viewPanel->GetSizer()->Add(_settingsView, 1, wxEXPAND); |
| 53 |
|
| 54 |
_settingsView->AppendTextColumn(_("Setting"), |
| 55 |
_settings->getColumns().description.getColumnIndex(), wxDATAVIEW_CELL_INERT, |
| 56 |
wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT); |
| 57 |
|
| 58 |
// Save a few shortcuts |
| 59 |
_spawnArgEntry = findNamedObject<wxTextCtrl>(_editor, "DifficultyEditorSpawnarg"); |
| 60 |
_argumentEntry = findNamedObject<wxTextCtrl>(_editor, "DifficultyEditorArgument"); |
| 61 |
|
| 62 |
// Set up the class name entry box and button |
| 63 |
_classEntry = findNamedObject<wxTextCtrl>(_editor, "ClassNameTextBox"); |
| 64 |
_classEntry->AutoComplete(ClassNameStore::Instance().getStringList()); |
| 65 |
wxButton* chooseClassBtn = findNamedObject<wxButton>(_editor, "ChooseClassButton"); |
| 66 |
chooseClassBtn->Bind( |
| 67 |
wxEVT_BUTTON, [&] (wxCommandEvent&) { chooseEntityClass(); } |
| 68 |
); |
| 69 |
|
| 70 |
// AppTypes |
| 71 |
_appTypeCombo = findNamedObject<wxChoice>(_editor, "DifficultyEditorApplicationType"); |
| 72 |
|
| 73 |
_appTypeCombo->Append(_("Assign"), new wxStringClientData(string::to_string(difficulty::Setting::EAssign))); |
| 74 |
_appTypeCombo->Append(_("Add"), new wxStringClientData(string::to_string(difficulty::Setting::EAdd))); |
| 75 |
_appTypeCombo->Append(_("Multiply"), new wxStringClientData(string::to_string(difficulty::Setting::EMultiply))); |
| 76 |
_appTypeCombo->Append(_("Ignore"), new wxStringClientData(string::to_string(difficulty::Setting::EIgnore))); |
| 77 |
|
| 78 |
_appTypeCombo->Connect(wxEVT_CHOICE, wxCommandEventHandler(DifficultyEditor::onAppTypeChange), NULL, this); |
| 79 |
|
| 80 |
// Buttons |
| 81 |
_saveSettingButton = findNamedObject<wxButton>(_editor, "DifficultyEditorSaveSettingButton"); |
| 82 |
_saveSettingButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(DifficultyEditor::onSettingSave), NULL, this); |
| 83 |
|
| 84 |
_deleteSettingButton = findNamedObject<wxButton>(_editor, "DifficultyEditorDeleteSettingButton"); |
| 85 |
_deleteSettingButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(DifficultyEditor::onSettingDelete), NULL, this); |
| 86 |
|
| 87 |
_createSettingButton = findNamedObject<wxButton>(_editor, "DifficultyEditorAddSettingButton"); |
| 88 |
_createSettingButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(DifficultyEditor::onSettingCreate), NULL, this); |
| 89 |
|
| 90 |
_refreshButton = findNamedObject<wxButton>(_editor, "DifficultyEditorRefreshButton"); |
| 91 |
_refreshButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(DifficultyEditor::onRefresh), NULL, this); |
| 92 |
|
| 93 |
_noteText = findNamedObject<wxStaticText>(_editor, "DifficultyEditorNoteText"); |
| 94 |
|
| 95 |
makeLabelBold(_editor, "DifficultyEditorSettingLabel"); |
| 96 |
} |
| 97 |
|
| 98 |
void DifficultyEditor::chooseEntityClass() |
| 99 |
{ |
| 100 |
// Show dialog and set the entry box text with the chosen entity class |
| 101 |
auto chosenEntity = wxutil::EntityClassChooser::ChooseEntityClass( |
| 102 |
wxutil::EntityClassChooser::Purpose::SelectClassname, _classEntry->GetValue().ToStdString() |
| 103 |
); |
| 104 |
|
| 105 |
if (!chosenEntity.empty()) |
| 106 |
{ |
| 107 |
_classEntry->SetValue(chosenEntity); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
int DifficultyEditor::getSelectedSettingId() |
| 112 |
{ |
| 113 |
wxDataViewItem item = _settingsView->GetSelection(); |
| 114 |
|
| 115 |
if (item.IsOk()) |
| 116 |
{ |
| 117 |
wxutil::TreeModel::Row row(item, *_settingsView->GetModel()); |
| 118 |
return row[_settings->getColumns().settingId].getInteger(); |
| 119 |
} |
| 120 |
|
| 121 |
return -1; |
| 122 |
} |
| 123 |
|
| 124 |
void DifficultyEditor::updateEditorWidgets() |
| 125 |
{ |
| 126 |
_updateActive = true; |
| 127 |
|
| 128 |
int id = getSelectedSettingId(); |
| 129 |
|
| 130 |
bool editWidgetsSensitive = false; |
| 131 |
|
| 132 |
std::string noteText; |
| 133 |
|
| 134 |
if (id != -1) |
| 135 |
{ |
| 136 |
// Lookup the setting using className/id combo |
| 137 |
difficulty::SettingPtr setting = _settings->getSettingById(id); |
| 138 |
|
| 139 |
if (setting != NULL) |
| 140 |
{ |
| 141 |
// Activate editing pane |
| 142 |
editWidgetsSensitive = true; |
| 143 |
|
| 144 |
if (_settings->isOverridden(setting)) |
| 145 |
{ |
| 146 |
editWidgetsSensitive = false; |
| 147 |
noteText += _("This default setting is overridden, cannot edit."); |
| 148 |
} |
| 149 |
|
| 150 |
_spawnArgEntry->SetValue(setting->spawnArg); |
| 151 |
_argumentEntry->SetValue(setting->argument); |
| 152 |
|
| 153 |
// Now select the eclass passed in the argument |
| 154 |
// Find the entity using a TreeModel traversor |
| 155 |
_classEntry->SetValue(setting->className); |
| 156 |
|
| 157 |
wxutil::ChoiceHelper::SelectItemByStoredId(_appTypeCombo, static_cast<int>(setting->appType)); |
| 158 |
|
| 159 |
// Set the sensitivity of the argument entry box |
| 160 |
_argumentEntry->Enable( |
| 161 |
(setting->appType == difficulty::Setting::EIgnore) ? false : true |
| 162 |
); |
| 163 |
|
| 164 |
// We have a treeview selection, lock the classname |
| 165 |
_classEntry->Enable(false); |
| 166 |
|
| 167 |
// Disable the deletion of default settings |
| 168 |
_deleteSettingButton->Enable((setting->isDefault) ? false : true); |
| 169 |
_saveSettingButton->Enable(true); |
| 170 |
} |
| 171 |
} |
| 172 |
else |
| 173 |
{ |
| 174 |
// Nothing selected, disable deletion |
| 175 |
_deleteSettingButton->Enable(false); |
| 176 |
_saveSettingButton->Enable(false); |
| 177 |
} |
| 178 |
|
| 179 |
// Set editing pane sensitivity |
| 180 |
findNamedObject<wxPanel>(_editor, "DifficultyEditorSettingsPanel")->Enable(editWidgetsSensitive); |
| 181 |
|
| 182 |
// Set the note text in any case |
| 183 |
_noteText->SetLabelMarkup(noteText); |
| 184 |
_noteText->Wrap(_noteText->GetSize().GetWidth()); |
| 185 |
|
| 186 |
_updateActive = false; |
| 187 |
} |
| 188 |
|
| 189 |
void DifficultyEditor::createSetting() |
| 190 |
{ |
| 191 |
// Unselect everything |
| 192 |
_settingsView->UnselectAll(); |
| 193 |
|
| 194 |
// Unlock editing widgets |
| 195 |
findNamedObject<wxPanel>(_editor, "DifficultyEditorSettingsPanel")->Enable(true); |
| 196 |
|
| 197 |
// Unlock class combo |
| 198 |
_classEntry->Enable(true); |
| 199 |
_saveSettingButton->Enable(true); |
| 200 |
|
| 201 |
_spawnArgEntry->SetValue(""); |
| 202 |
_argumentEntry->SetValue(""); |
| 203 |
} |
| 204 |
|
| 205 |
void DifficultyEditor::saveSetting() |
| 206 |
{ |
| 207 |
// Get the ID of the currently selected item (might be -1 if no selection) |
| 208 |
int id = getSelectedSettingId(); |
| 209 |
|
| 210 |
// Instantiate a new setting and fill the data in |
| 211 |
difficulty::SettingPtr setting(new difficulty::Setting); |
| 212 |
|
| 213 |
// Load the widget contents |
| 214 |
setting->className = _classEntry->GetValue(); |
| 215 |
|
| 216 |
if (setting->className.empty()) |
| 217 |
{ |
| 218 |
wxutil::Messagebox::ShowError(_("Classname cannot be left empty"), wxGetTopLevelParent(_classEntry)); |
| 219 |
return; |
| 220 |
} |
| 221 |
|
| 222 |
setting->spawnArg = _spawnArgEntry->GetValue(); |
| 223 |
setting->argument = _argumentEntry->GetValue(); |
| 224 |
|
| 225 |
if (setting->spawnArg.empty() || setting->argument.empty()) |
| 226 |
{ |
| 227 |
wxutil::Messagebox::ShowError(_("Spawnarg name and value cannot be left empty"), |
| 228 |
wxGetTopLevelParent(_spawnArgEntry)); |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
// Get the apptype from the dropdown list |
| 233 |
setting->appType = difficulty::Setting::EAssign; |
| 234 |
|
| 235 |
if (_appTypeCombo->GetSelection() != wxNOT_FOUND) |
| 236 |
{ |
| 237 |
int selected = wxutil::ChoiceHelper::GetSelectionId(_appTypeCombo); |
| 238 |
setting->appType = static_cast<difficulty::Setting::EApplicationType>(selected); |
| 239 |
} |
| 240 |
|
| 241 |
// Pass the data to the DifficultySettings class to handle it |
| 242 |
id = _settings->save(id, setting); |
| 243 |
|
| 244 |
// Update the treemodel |
| 245 |
_settings->updateTreeModel(); |
| 246 |
|
| 247 |
// Highlight the setting, it might have been newly created |
| 248 |
selectSettingById(id); |
| 249 |
} |
| 250 |
|
| 251 |
void DifficultyEditor::deleteSetting() |
| 252 |
{ |
| 253 |
// Get the ID of the currently selected item (might be -1 if no selection) |
| 254 |
int id = getSelectedSettingId(); |
| 255 |
|
| 256 |
// Instantiate a new setting and fill the data in |
| 257 |
difficulty::SettingPtr setting = _settings->getSettingById(id); |
| 258 |
|
| 259 |
if (setting == NULL || setting->isDefault) { |
| 260 |
// Don't delete NULL or default settings |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
// Remove the setting |
| 265 |
_settings->deleteSetting(id); |
| 266 |
} |
| 267 |
|
| 268 |
void DifficultyEditor::selectSettingById(int id) |
| 269 |
{ |
| 270 |
wxDataViewItem found = _settings->getTreeStore()->FindInteger(id, |
| 271 |
_settings->getColumns().settingId); |
| 272 |
|
| 273 |
_settingsView->Select(found); |
| 274 |
_settingsView->EnsureVisible(found); |
| 275 |
} |
| 276 |
|
| 277 |
void DifficultyEditor::onSettingSelectionChange(wxDataViewEvent& ev) |
| 278 |
{ |
| 279 |
// Update editor widgets |
| 280 |
updateEditorWidgets(); |
| 281 |
} |
| 282 |
|
| 283 |
void DifficultyEditor::onSettingSave(wxCommandEvent& ev) |
| 284 |
{ |
| 285 |
saveSetting(); |
| 286 |
} |
| 287 |
|
| 288 |
void DifficultyEditor::onSettingDelete(wxCommandEvent& ev) |
| 289 |
{ |
| 290 |
deleteSetting(); |
| 291 |
} |
| 292 |
|
| 293 |
void DifficultyEditor::onSettingCreate(wxCommandEvent& ev) |
| 294 |
{ |
| 295 |
createSetting(); |
| 296 |
} |
| 297 |
|
| 298 |
void DifficultyEditor::onRefresh(wxCommandEvent& ev) |
| 299 |
{ |
| 300 |
_settings->refreshTreeModel(); |
| 301 |
} |
| 302 |
|
| 303 |
void DifficultyEditor::onAppTypeChange(wxCommandEvent& ev) |
| 304 |
{ |
| 305 |
if (_updateActive) return; |
| 306 |
|
| 307 |
// Update the sensitivity of the argument entry widget |
| 308 |
int selected = wxutil::ChoiceHelper::GetSelectionId(_appTypeCombo); |
| 309 |
|
| 310 |
difficulty::Setting::EApplicationType appType = |
| 311 |
static_cast<difficulty::Setting::EApplicationType>(selected); |
| 312 |
|
| 313 |
_argumentEntry->Enable( |
| 314 |
(appType == difficulty::Setting::EIgnore) ? false : true |
| 315 |
); |
| 316 |
} |
| 317 |
|
| 318 |
} // namespace ui |