/
ComponentsDialog.cpp
544 lines (431 loc) · 15.9 KB
/
ComponentsDialog.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include "ComponentsDialog.h"
#include "Objective.h"
#include "ce/ComponentEditorFactory.h"
#include "itextstream.h"
#include "string/convert.h"
#include "i18n.h"
#include <vector>
#include "wxutil/ChoiceHelper.h"
#include <wx/panel.h>
#include <wx/button.h>
#include <wx/textctrl.h>
#include <wx/stattext.h>
#include <wx/choice.h>
#include <wx/sizer.h>
#include <wx/checkbox.h>
namespace objectives
{
/* CONSTANTS */
namespace
{
const char* const DIALOG_TITLE = N_("Edit Objective");
} // namespace
// Main constructor
ComponentsDialog::ComponentsDialog(wxWindow* parent, Objective& objective) :
DialogBase(_(DIALOG_TITLE), parent),
_objective(objective),
_componentList(new wxutil::TreeModel(_columns, true)),
_components(objective.components), // copy the components to our local working set
_updateMutex(false),
_updateNeeded(false)
{
wxPanel* mainPanel = loadNamedPanel(this, "ObjCompMainPanel");
// Dialog contains list view, edit panel and buttons
setupObjectiveEditPanel();
makeLabelBold(this, "ObjCompListLabel");
createListView();
setupEditPanel();
// Populate the list of components
populateObjectiveEditPanel();
populateComponents();
mainPanel->Layout();
mainPanel->Fit();
Fit();
CenterOnParent();
}
ComponentsDialog::~ComponentsDialog()
{
}
// Create the panel for editing the currently-selected objective
void ComponentsDialog::setupObjectiveEditPanel()
{
_objDescriptionEntry = findNamedObject<wxTextCtrl>(this, "ObjCompDescription");
_objStateCombo = findNamedObject<wxChoice>(this, "ObjCompInitialState");
_diffPanel.reset(new DifficultyPanel(findNamedObject<wxPanel>(this, "ObjCompDiffPanel")));
// Populate the list of states. This must be done in order to match the
// values in the enum, since the index will be used when writing to entity
_objStateCombo->Append(Objective::getStateText(Objective::INCOMPLETE),
new wxStringClientData(string::to_string(Objective::INCOMPLETE)));
_objStateCombo->Append(Objective::getStateText(Objective::COMPLETE),
new wxStringClientData(string::to_string(Objective::COMPLETE)));
_objStateCombo->Append(Objective::getStateText(Objective::INVALID),
new wxStringClientData(string::to_string(Objective::INVALID)));
_objStateCombo->Append(Objective::getStateText(Objective::FAILED),
new wxStringClientData(string::to_string(Objective::FAILED)));
_objMandatoryFlag = findNamedObject<wxCheckBox>(this, "ObjCompObjMandatory");
_objIrreversibleFlag = findNamedObject<wxCheckBox>(this, "ObjCompObjIrreversible");
_objOngoingFlag = findNamedObject<wxCheckBox>(this, "ObjCompObjOngoing");
_objVisibleFlag = findNamedObject<wxCheckBox>(this, "ObjCompObjVisible");
// Enabling objectives
_enablingObjs = findNamedObject<wxTextCtrl>(this, "ObjCompEnablingObjectives");
// Logic
_successLogic = findNamedObject<wxTextCtrl>(this, "ObjCompSuccessLogic");
_failureLogic = findNamedObject<wxTextCtrl>(this, "ObjCompFailureLogic");
// Completion/failure scripts
_completionScript = findNamedObject<wxTextCtrl>(this, "ObjCompCompletionScript");
_failureScript = findNamedObject<wxTextCtrl>(this, "ObjCompFailureScript");
// Completion/failure targets
_completionTarget = findNamedObject<wxTextCtrl>(this, "ObjCompCompletionTarget");
_failureTarget = findNamedObject<wxTextCtrl>(this, "ObjCompFailureTarget");
}
// Create list view
void ComponentsDialog::createListView()
{
// Create tree view and connect selection changed callback
wxPanel* treeViewPanel = findNamedObject<wxPanel>(this, "ObjCompListViewPanel");
_componentView = wxutil::TreeView::CreateWithModel(treeViewPanel, _componentList.get());
treeViewPanel->GetSizer()->Add(_componentView, 1, wxEXPAND);
treeViewPanel->SetMinClientSize(wxSize(-1, 90));
_componentView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
wxDataViewEventHandler(ComponentsDialog::_onSelectionChanged), NULL, this);
_componentView->AppendTextColumn("#", _columns.index.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
_componentView->AppendTextColumn(_("Type"), _columns.description.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
// Create Add and Delete buttons for components
wxButton* addButton = findNamedObject<wxButton>(this, "ObjCompAddComponentButton");
wxButton* delButton = findNamedObject<wxButton>(this, "ObjCompDeleteComponentButton");
addButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ComponentsDialog::_onAddComponent), NULL, this);
delButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ComponentsDialog::_onDeleteComponent), NULL, this);
}
// Create edit panel
void ComponentsDialog::setupEditPanel()
{
// Table
_editPanel = findNamedObject<wxPanel>(this, "ObjCompComponentEditPanel");
_editPanel->Enable(false); // disabled at start
// Component type dropdown
_typeCombo = findNamedObject<wxChoice>(this, "ObjCompComponentType");
_typeCombo->Connect(wxEVT_CHOICE, wxCommandEventHandler(ComponentsDialog::_onTypeChanged), NULL, this);
// Populate the combo box. The set is in ID order.
for (ComponentTypeSet::const_iterator i = ComponentType::SET_ALL().begin();
i != ComponentType::SET_ALL().end();
++i)
{
_typeCombo->Append(i->getDisplayName(), new wxStringClientData(string::to_string(i->getId())));
}
// Flags hbox
_stateFlag = findNamedObject<wxCheckBox>(this, "ObjCompSatisfiedAtStart");
_irreversibleFlag = findNamedObject<wxCheckBox>(this, "ObjCompIrreversible");
_invertedFlag = findNamedObject<wxCheckBox>(this, "ObjCompBooleanNOT");
_playerResponsibleFlag = findNamedObject<wxCheckBox>(this, "ObjCompPlayerResponsible");
_stateFlag->Connect(wxEVT_CHECKBOX, wxCommandEventHandler(ComponentsDialog::_onCompToggleChanged), NULL, this);
_irreversibleFlag->Connect(wxEVT_CHECKBOX, wxCommandEventHandler(ComponentsDialog::_onCompToggleChanged), NULL, this);
_invertedFlag->Connect(wxEVT_CHECKBOX, wxCommandEventHandler(ComponentsDialog::_onCompToggleChanged), NULL, this);
_playerResponsibleFlag->Connect(wxEVT_CHECKBOX, wxCommandEventHandler(ComponentsDialog::_onCompToggleChanged), NULL, this);
_compEditorPanel = findNamedObject<wxPanel>(this, "ObjCompEditorContainer");
}
// Populate the component list
void ComponentsDialog::populateComponents()
{
// Clear the list store
_componentList->Clear();
// Add components from the Objective to the list store
for (Objective::ComponentMap::iterator i = _components.begin();
i != _components.end();
++i)
{
wxutil::TreeModel::Row row = _componentList->AddItem();
row[_columns.index] = i->first;
row[_columns.description] = i->second.getString();
row.SendItemAdded();
}
_updateNeeded = true;
}
void ComponentsDialog::updateComponents()
{
// Traverse all components and update the items in the list
for (Objective::ComponentMap::iterator i = _components.begin();
i != _components.end();
++i)
{
// Find the item in the list store (0th column carries the ID)
wxDataViewItem item = _componentList->FindInteger(i->first, _columns.index);
// Check if we found the item
if (item.IsOk())
{
wxutil::TreeModel::Row row(item, *_componentList);
row[_columns.index] = i->first;
row[_columns.description] = i->second.getString();
row.SendItemChanged();
}
}
}
// Populate the edit panel
void ComponentsDialog::populateEditPanel(int index)
{
// Get the component
Component& comp = _components[index];
// Set the flags
_stateFlag->SetValue(comp.isSatisfied());
_irreversibleFlag->SetValue(comp.isIrreversible());
_invertedFlag->SetValue(comp.isInverted());
_playerResponsibleFlag->SetValue(comp.isPlayerResponsible());
// Change the type combo if necessary.
// Since the combo box was populated in
// ID order, we can simply use our ComponentType's ID as an index.
if (_typeCombo->GetSelection() != comp.getType().getId())
{
// Change the combo selection
_typeCombo->Select(comp.getType().getId());
// Trigger component change, to update editor panel
handleTypeChange();
}
else
{
// Update the ComponentEditor ourselves, since the new Component has the
// same type but we still want to refresh the panel with the new
// contents
changeComponentEditor(comp);
}
}
// Populate the edit panel widgets using the given objective number
void ComponentsDialog::populateObjectiveEditPanel()
{
// Disable callbacks while we're at it
_updateMutex = true;
// Get the objective
const Objective& obj = _objective;
// Set description text
_objDescriptionEntry->SetValue(obj.description);
// Update the difficulty panel
_diffPanel->populateFromObjective(obj);
// Set initial state enum
wxutil::ChoiceHelper::SelectItemByStoredId(_objStateCombo, static_cast<int>(obj.state));
// Set flags
_objIrreversibleFlag->SetValue(obj.irreversible);
_objOngoingFlag->SetValue(obj.ongoing);
_objMandatoryFlag->SetValue(obj.mandatory);
_objVisibleFlag->SetValue(obj.visible);
_enablingObjs->SetValue(obj.enablingObjs);
_successLogic->SetValue(obj.logic.successLogic);
_failureLogic->SetValue(obj.logic.failureLogic);
_completionScript->SetValue(obj.completionScript);
_failureScript->SetValue(obj.failureScript);
_completionTarget->SetValue(obj.completionTarget);
_failureTarget->SetValue(obj.failureTarget);
_updateMutex = false;
}
// Get selected component index
int ComponentsDialog::getSelectedIndex()
{
// Get the selection if valid
wxDataViewItem item = _componentView->GetSelection();
if (item.IsOk())
{
// Valid selection, return the contents of the index column
wxutil::TreeModel::Row row(item, *_componentList);
return row[_columns.index].getInteger();
}
else
{
return -1;
}
}
// Change component editor
void ComponentsDialog::changeComponentEditor(Component& compToEdit)
{
// greebo: Get a new component editor, any previous one will auto-destroy and
// remove its widget from the container.
_componentEditor = ce::ComponentEditorFactory::create(
_compEditorPanel, compToEdit.getType().getName(), compToEdit
);
if (_componentEditor)
{
// The widgets are constructed and filled with correct values,
// from this point on we can write updates to the component automatically
_componentEditor->setActive(true);
// Get the widget from the ComponentEditor and show it
// Pack the widget into the containing frame
_compEditorPanel->GetSizer()->Add(_componentEditor->getWidget(), 1, wxEXPAND | wxALL, 12);
_compEditorPanel->Fit();
findNamedObject<wxPanel>(this, "ObjCompMainPanel")->Fit();
Fit();
}
}
// Safely write the ComponentEditor contents to the Component
void ComponentsDialog::checkWriteComponent()
{
if (_componentEditor)
{
_componentEditor->writeToComponent();
}
}
void ComponentsDialog::save()
{
// Write the objective properties
_objective.description = _objDescriptionEntry->GetValue().ToStdString();
// Save the difficulty settings
_diffPanel->writeToObjective(_objective);
// Set the initial state enum value from the combo box index
_objective.state = static_cast<Objective::State>(
wxutil::ChoiceHelper::GetSelectionId(_objStateCombo));
// Determine which checkbox is toggled, then update the appropriate flag
_objective.mandatory = _objMandatoryFlag->GetValue();
_objective.visible = _objVisibleFlag->GetValue();
_objective.ongoing = _objOngoingFlag->GetValue();
_objective.irreversible = _objIrreversibleFlag->GetValue();
// Enabling objectives
_objective.enablingObjs = _enablingObjs->GetValue();
// Success/failure logic
_objective.logic.successLogic = _successLogic->GetValue();
_objective.logic.failureLogic = _failureLogic->GetValue();
// Completion/Failure script
_objective.completionScript = _completionScript->GetValue();
_objective.failureScript = _failureScript->GetValue();
// Completion/Failure targets
_objective.completionTarget = _completionTarget->GetValue();
_objective.failureTarget = _failureTarget->GetValue();
// Write the components
checkWriteComponent();
// Copy the working set over the ones in the objective
_objective.components.swap(_components);
}
int ComponentsDialog::ShowModal()
{
int returnCode = DialogBase::ShowModal();
if (returnCode == wxID_OK)
{
save();
}
return returnCode;
}
void ComponentsDialog::_onCompToggleChanged(wxCommandEvent& ev)
{
if (_updateMutex) return;
// Update the Component working copy. The selected index must be valid, since the
// edit panel is only sensitive if a component is selected
int idx = getSelectedIndex();
assert(idx >= 0);
Component& comp = _components[idx];
wxCheckBox* checkbox = dynamic_cast<wxCheckBox*>(ev.GetEventObject());
if (checkbox == _stateFlag)
{
comp.setSatisfied(checkbox->GetValue());
}
else if (checkbox == _irreversibleFlag)
{
comp.setIrreversible(checkbox->GetValue());
}
else if (checkbox == _invertedFlag)
{
comp.setInverted(checkbox->GetValue());
}
else if (checkbox == _playerResponsibleFlag)
{
comp.setPlayerResponsible(checkbox->GetValue());
}
// Update the list store
updateComponents();
}
void ComponentsDialog::handleSelectionChange()
{
// Save the existing ComponentEditor contents if req'd
checkWriteComponent();
// Disconnect notification callback
_componentChanged.disconnect();
// Get the selection if valid
wxDataViewItem item = _componentView->GetSelection();
if (!item.IsOk())
{
// Disable the edit panel and remove the ComponentEditor
_compEditorPanel->Enable(false);
_editPanel->Enable(false);
_componentEditor = objectives::ce::ComponentEditorPtr();
}
else
{
// Otherwise populate edit panel with the current component index
wxutil::TreeModel::Row row(item, *_componentList);
int index = row[_columns.index].getInteger();
populateEditPanel(index);
// Enable the edit panel
_compEditorPanel->Enable(true);
_editPanel->Enable(true);
// Subscribe to the component
Component& comp = _components[index];
_componentChanged = comp.signal_Changed().connect(
sigc::mem_fun(*this, &ComponentsDialog::_onComponentChanged));
}
}
void ComponentsDialog::_onComponentChanged()
{
wxDataViewItem item = _componentView->GetSelection();
if (!item.IsOk()) return;
updateComponents();
}
// Selection changed
void ComponentsDialog::_onSelectionChanged(wxDataViewEvent& ev)
{
handleSelectionChange();
}
// Add a new component
void ComponentsDialog::_onAddComponent(wxCommandEvent& ev)
{
Objective::ComponentMap& components = _components;
// Find an unused component number (starting from 1)
for (int idx = 1; idx < INT_MAX; ++idx)
{
if (components.find(idx) == components.end()) {
// Unused, add a new component here
Component comp;
components.insert(std::make_pair(idx, comp));
break;
}
}
// Refresh the component list
populateComponents();
}
// Remove a component
void ComponentsDialog::_onDeleteComponent(wxCommandEvent& ev)
{
// Delete the selected component
int idx = getSelectedIndex();
if (idx != -1)
{
// Remove the selection first, so our selection-changed callback does not
// attempt to writeToComponent() after the Component has already been deleted
_componentView->UnselectAll();
// UnselectAll doesn't seem to trigger a selection change
handleSelectionChange();
// Erase the actual component
_components.erase(idx);
}
// Refresh the list
populateComponents();
}
void ComponentsDialog::handleTypeChange()
{
// Get the current selection
int typeId = wxutil::ChoiceHelper::GetSelectionId(_typeCombo);
// Update the Objective object. The selected index must be valid, since the
// edit panel is only sensitive if a component is selected
int idx = getSelectedIndex();
assert(idx >= 0);
Component& comp = _components[idx];
// Store the newly-selected type in the Component
comp.setType(ComponentType::getComponentType(typeId));
// Change the ComponentEditor
changeComponentEditor(comp);
// Update the components list with the new display string
wxutil::TreeModel::Row row(_componentView->GetSelection(), *_componentList);
row[_columns.description] = comp.getString();
row.SendItemChanged();
_updateNeeded = true;
}
// Type combo changed
void ComponentsDialog::_onTypeChanged(wxCommandEvent& ev)
{
handleTypeChange();
}
} // namespace objectives