Debian Codegraph @

plugins/dm.objectives/ObjectivesEditor.cpp

raw
1 #include "ObjectivesEditor.h"
2
3 #include "ObjectiveEntityFinder.h"
4 #include "RandomOrigin.h"
5 #include "TargetList.h"
6 #include "ComponentsDialog.h"
7 #include "MissionLogicDialog.h"
8 #include "ObjectiveConditionsDialog.h"
9 #include "util/ObjectivesException.h"
10
11 #include "i18n.h"
12 #include "iscenegraph.h"
13 #include "ui/imainframe.h"
14 #include "iregistry.h"
15 #include "ieclass.h"
16 #include "igame.h"
17 #include "ientity.h"
18
19 #include "wxutil/dialog/MessageBox.h"
20
21 #include <wx/button.h>
22 #include <wx/panel.h>
23
24 #include <fmt/format.h>
25
26 namespace objectives
27 {
28
29 // CONSTANTS
30 namespace
31 {
32 const char* const DIALOG_TITLE = N_("Mission Objectives");
33
34 const std::string RKEY_ROOT = "user/ui/objectivesEditor/";
35 const std::string RKEY_WINDOW_STATE = RKEY_ROOT + "window";
36 const std::string GKEY_OBJECTIVE_ENTS = "/objectivesEditor//objectivesEClass";
37 }
38
39 // Constructor creates widgets
40 ObjectivesEditor::ObjectivesEditor() :
41 DialogBase(_(DIALOG_TITLE)),
42 _objectiveEntityList(new wxutil::TreeModel(_objEntityColumns, true)),
43 _objectiveList(new wxutil::TreeModel(_objectiveColumns, true))
44 {
45 wxPanel* mainPanel = loadNamedPanel(this, "ObjDialogMainPanel");
46
47 // Setup signals and tree views
48 setupEntitiesPanel();
49 setupObjectivesPanel();
50
51 // Buttons not associated with a treeview panel
52 wxButton* successLogicButton = findNamedObject<wxButton>(this, "ObjDialogSuccessLogicButton");
53 successLogicButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ObjectivesEditor::_onEditLogic), NULL, this);
54 successLogicButton->Enable(false);
55
56 wxButton* objCondButton = findNamedObject<wxButton>(this, "ObjDialogObjConditionsButton");
57 objCondButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ObjectivesEditor::_onEditObjConditions), NULL, this);
58 objCondButton->Enable(false);
59
60 findNamedObject<wxButton>(this, "ObjDialogCancelButton")->Connect(
61 wxEVT_BUTTON, wxCommandEventHandler(ObjectivesEditor::_onCancel), NULL, this);
62
63 findNamedObject<wxButton>(this, "ObjDialogOkButton")->Connect(
64 wxEVT_BUTTON, wxCommandEventHandler(ObjectivesEditor::_onOK), NULL, this);
65
66 _objectiveEClasses.clear();
67
68 xml::NodeList nodes = GlobalGameManager().currentGame()->getLocalXPath(GKEY_OBJECTIVE_ENTS);
69
70 for (const xml::Node& node : nodes)
71 {
72 _objectiveEClasses.push_back(node.getAttributeValue("name"));
73 }
74
75 mainPanel->Layout();
76 mainPanel->Fit();
77 Fit();
78 CenterOnParent();
79
80 // Remember the previous position or set up defaults
81 _windowPosition.initialise(this, RKEY_WINDOW_STATE, 0.5f, 0.9f);
82 }
83
84 // Create the objects panel (for manipulating the target_addobjectives objects)
85 void ObjectivesEditor::setupEntitiesPanel()
86 {
87 makeLabelBold(this, "ObjDialogEntityLabel");
88
89 // Tree view listing the target_addobjectives entities
90 wxPanel* entityPanel = findNamedObject<wxPanel>(this, "ObjDialogEntityPanel");
91
92 // Entity Tree View
93 _objectiveEntityView = wxutil::TreeView::CreateWithModel(entityPanel, _objectiveEntityList.get(), wxDV_NO_HEADER);
94 entityPanel->GetSizer()->Add(_objectiveEntityView, 1, wxEXPAND);
95
96 _objectiveEntityView->AppendToggleColumn(_("Start"), _objEntityColumns.startActive.getColumnIndex(),
97 wxDATAVIEW_CELL_ACTIVATABLE, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
98 _objectiveEntityView->AppendTextColumn("", _objEntityColumns.displayName.getColumnIndex(),
99 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
100
101 _objectiveEntityView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
102 wxDataViewEventHandler(ObjectivesEditor::_onEntitySelectionChanged), NULL, this);
103
104 // Active-at-start column (checkbox)
105 _objectiveEntityView->Connect(wxEVT_DATAVIEW_ITEM_EDITING_DONE,
106 wxDataViewEventHandler(ObjectivesEditor::_onStartActiveCellToggled), NULL, this);
107
108 // Connect button signals
109 findNamedObject<wxButton>(this, "ObjDialogAddEntityButton")->Connect(
110 wxEVT_BUTTON, wxCommandEventHandler(ObjectivesEditor::_onAddEntity), NULL, this);
111
112 wxButton* deleteButton = findNamedObject<wxButton>(this, "ObjDialogDeleteEntityButton");
113 deleteButton->Enable(false); // disabled at start
114 deleteButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ObjectivesEditor::_onDeleteEntity), NULL, this);
115 }
116
117 // Create the main objective editing widgets
118 void ObjectivesEditor::setupObjectivesPanel()
119 {
120 makeLabelBold(this, "ObjDialogObjectivesLabel");
121 makeLabelBold(this, "ObjDialogLogicLabel");
122
123 wxPanel* panel = findNamedObject<wxPanel>(this, "ObjDialogObjectivesPanel");
124
125 // Entity Tree View
126 _objectiveView = wxutil::TreeView::CreateWithModel(panel, _objectiveList.get());
127 panel->GetSizer()->Add(_objectiveView, 1, wxEXPAND);
128
129 // Key and value text columns
130 _objectiveView->AppendTextColumn("#", _objectiveColumns.objNumber.getColumnIndex(),
131 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
132 _objectiveView->AppendTextColumn(_("Description"), _objectiveColumns.description.getColumnIndex(),
133 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
134 _objectiveView->AppendTextColumn(_("Diff."), _objectiveColumns.difficultyLevel.getColumnIndex(),
135 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
136
137 _objectiveView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &ObjectivesEditor::_onObjectiveSelectionChanged, this);
138 _objectiveView->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &ObjectivesEditor::_onObjectiveActivated, this);
139
140 wxButton* addButton = findNamedObject<wxButton>(this, "ObjDialogAddObjectiveButton");
141 addButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ObjectivesEditor::_onAddObjective), NULL, this);
142
143 wxButton* editObjButton = findNamedObject<wxButton>(this, "ObjDialogEditObjectiveButton");
144 editObjButton->Enable(false); // not enabled without selection
145 editObjButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ObjectivesEditor::_onEditObjective), NULL, this);
146
147 wxButton* moveUpObjButton = findNamedObject<wxButton>(this, "ObjDialogMoveObjUpButton");
148 moveUpObjButton->Enable(false); // not enabled without selection
149 moveUpObjButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ObjectivesEditor::_onMoveUpObjective), NULL, this);
150
151 wxButton* moveDownObjButton = findNamedObject<wxButton>(this, "ObjDialogMoveObjDownButton");
152 moveDownObjButton->Enable(false); // not enabled without selection
153 moveDownObjButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ObjectivesEditor::_onMoveDownObjective), NULL, this);
154
155 wxButton* delObjButton = findNamedObject<wxButton>(this, "ObjDialogDeleteObjectiveButton");
156 delObjButton->Enable(false); // not enabled without selection
157 delObjButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ObjectivesEditor::_onDeleteObjective), NULL, this);
158
159 wxButton* clearObjButton = findNamedObject<wxButton>(this, "ObjDialogClearObjectiveButton");
160 clearObjButton->Enable(false); // requires >0 objectives
161 clearObjButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ObjectivesEditor::_onClearObjectives), NULL, this);
162
163 findNamedObject<wxPanel>(this, "ObjDialogObjectiveButtonPanel")->Enable(false);
164 }
165
166 void ObjectivesEditor::clear()
167 {
168 // Clear internal data
169 _worldSpawn = NULL;
170 _entities.clear();
171 _curEntity = _entities.end();
172
173 // Clear the list boxes
174 _objectiveEntityList->Clear();
175 _objectiveList->Clear();
176
177 _curObjective = wxDataViewItem();
178 updateObjectiveButtonPanel();
179 }
180
181 // Populate widgets with map data
182 void ObjectivesEditor::populateWidgets()
183 {
184 // Clear internal data first
185 clear();
186
187 // Use an ObjectiveEntityFinder to walk the map and add any objective
188 // entities to the liststore and entity map
189 ObjectiveEntityFinder finder(
190 _objectiveEntityList, _objEntityColumns, _entities, _objectiveEClasses
191 );
192 GlobalSceneGraph().root()->traverse(finder);
193
194 // Select the first entity in the list for convenience
195 wxDataViewItemArray children;
196 _objectiveEntityList->GetChildren(_objectiveEntityList->GetRoot(), children);
197
198 if (!children.empty())
199 {
200 _objectiveEntityView->Select(children.front());
201 handleEntitySelectionChange();
202 }
203
204 // Set the worldspawn entity and populate the active-at-start column
205 _worldSpawn = finder.getWorldSpawn();
206 if (_worldSpawn != NULL)
207 {
208 populateActiveAtStart();
209 }
210 }
211
212 // Populate the active-at-start column.
213 void ObjectivesEditor::populateActiveAtStart()
214 {
215 // Construct the list of entities targeted by the worldspawn
216 TargetList targets(_worldSpawn);
217
218 // Iterate through each row in the entity list. For each Entity*, get its
219 // name and check if the worldspawn entity has a "target" key for this
220 // entity name. This indicates that the objective entity will be active at
221 // game start.
222 _objectiveEntityList->ForeachNode([&] (wxutil::TreeModel::Row& row)
223 {
224 std::string name = row[_objEntityColumns.entityName];
225
226 ObjectiveEntityPtr obj = _entities[name];
227
228 // Test if the worldspawn is targeting this entity by passing the
229 // target list to the objective entity.
230 if (obj->isOnTargetList(targets))
231 {
232 row[_objEntityColumns.startActive] = true;
233 }
234 });
235 }
236
237 int ObjectivesEditor::ShowModal()
238 {
239 // Restore the position
240 _windowPosition.applyPosition();
241
242 populateWidgets();
243
244 int returnValue = DialogBase::ShowModal();
245
246 // Tell the position tracker to save the information
247 _windowPosition.saveToPath(RKEY_WINDOW_STATE);
248
249 // Clear all data before hiding
250 clear();
251
252 return returnValue;
253 }
254
255 // Static method to display dialog
256 void ObjectivesEditor::DisplayDialog(const cmd::ArgumentList& args)
257 {
258 // Create a new dialog instance
259 ObjectivesEditor* _instance = new ObjectivesEditor;
260
261 try
262 {
263 // Show the instance
264 _instance->ShowModal();
265 }
266 catch (ObjectivesException& e)
267 {
268 wxutil::Messagebox::ShowError(
269 std::string(_("Exception occurred: ")) + e.what()
270 );
271 }
272
273 _instance->Destroy();
274 }
275
276 // Refresh the objectives list from the ObjectiveEntity
277 void ObjectivesEditor::refreshObjectivesList()
278 {
279 _curObjective = wxDataViewItem();
280 updateObjectiveButtonPanel();
281
282 // Clear and refresh the objective list
283 _objectiveList->Clear();
284 _curEntity->second->populateListStore(*_objectiveList, _objectiveColumns);
285
286 // If there is at least one objective, make the Clear button available
287 wxButton* clearObjButton = findNamedObject<wxButton>(this, "ObjDialogClearObjectiveButton");
288 clearObjButton->Enable(!_curEntity->second->isEmpty());
289 }
290
291 // Get the currently selected objective
292 Objective& ObjectivesEditor::getCurrentObjective()
293 {
294 // Get the objective index from the list
295 wxutil::TreeModel::Row row(_curObjective, *_objectiveList);
296 int objNum = row[_objectiveColumns.objNumber].getInteger();
297
298 // Pass the index to the ObjectiveEntity to get an actual Objective
299 return _curEntity->second->getObjective(objNum);
300 }
301
302 void ObjectivesEditor::_onCancel(wxCommandEvent& ev)
303 {
304 // Close the window without saving
305 EndModal(wxID_CANCEL);
306 }
307
308 // OK button
309 void ObjectivesEditor::_onOK(wxCommandEvent& ev)
310 {
311 // Write all ObjectiveEntity data to the underlying entities
312 for (ObjectiveEntityMap::iterator i = _entities.begin();
313 i != _entities.end();
314 ++i)
315 {
316 i->second->writeToEntity();
317 }
318
319 // Close the window
320 EndModal(wxID_OK);
321 }
322
323 void ObjectivesEditor::selectObjectiveByIndex(int index)
324 {
325 if (index == -1) return;
326
327 // Select the new objective
328 wxDataViewItem newObjLoc = _objectiveList->FindInteger(index,
329 _objectiveColumns.objNumber);
330
331 _objectiveView->Select(newObjLoc);
332 _curObjective = newObjLoc;
333 updateObjectiveButtonPanel();
334 }
335
336 // Callback for "start active" cell toggle in entities list
337 void ObjectivesEditor::_onStartActiveCellToggled(wxDataViewEvent& ev)
338 {
339 }
340
341 // Callback for objective entity selection changed in list box
342 void ObjectivesEditor::_onEntitySelectionChanged(wxDataViewEvent& ev)
343 {
344 handleEntitySelectionChange();
345 }
346
347 void ObjectivesEditor::handleEntitySelectionChange()
348 {
349 // Clear the objectives list
350 _objectiveList->Clear();
351
352 updateEditorButtonPanel();
353 }
354
355 void ObjectivesEditor::updateObjectiveButtonPanel()
356 {
357 wxButton* editObjButton = findNamedObject<wxButton>(this, "ObjDialogEditObjectiveButton");
358 wxButton* delObjButton = findNamedObject<wxButton>(this, "ObjDialogDeleteObjectiveButton");
359 wxButton* moveUpButton = findNamedObject<wxButton>(this, "ObjDialogMoveObjUpButton");
360 wxButton* moveDownButton = findNamedObject<wxButton>(this, "ObjDialogMoveObjDownButton");
361
362 if (_curObjective.IsOk())
363 {
364 // Enable the edit and delete buttons
365 editObjButton->Enable(true);
366 delObjButton->Enable(true);
367
368 // Check if this is the first command in the list, get the ID of the
369 // selected item
370 wxutil::TreeModel::Row row(_curObjective, *_objectiveList);
371 int index = row[_objectiveColumns.objNumber].getInteger();
372
373 int highestIndex = _curEntity->second->getHighestObjIndex();
374 int lowestIndex = _curEntity->second->getLowestObjIndex();
375
376 bool hasNext = (highestIndex != -1 && highestIndex > index);
377 bool hasPrev = (lowestIndex != -1 && lowestIndex < index);
378
379 moveUpButton->Enable(hasPrev);
380 moveDownButton->Enable(hasNext);
381 }
382 else
383 {
384 // Disable the edit, delete and move buttons
385 editObjButton->Enable(false);
386 delObjButton->Enable(false);
387 moveUpButton->Enable(false);
388 moveDownButton->Enable(false);
389 }
390 }
391
392 // Callback for current objective selection changed
393 void ObjectivesEditor::_onObjectiveSelectionChanged(wxDataViewEvent& ev)
394 {
395 // Get the selection
396 _curObjective = ev.GetItem();
397
398 updateObjectiveButtonPanel();
399 }
400
401 void ObjectivesEditor::_onObjectiveActivated(wxDataViewEvent& ev)
402 {
403 if (ev.GetItem().IsOk())
404 {
405 doEditObjective();
406 }
407 }
408
409 void ObjectivesEditor::updateEditorButtonPanel()
410 {
411 wxButton* delEntityButton = findNamedObject<wxButton>(this, "ObjDialogDeleteEntityButton");
412 wxPanel* objButtonPanel = findNamedObject<wxPanel>(this, "ObjDialogObjectiveButtonPanel");
413
414 wxButton* successLogicButton = findNamedObject<wxButton>(this, "ObjDialogSuccessLogicButton");
415 wxButton* objCondButton = findNamedObject<wxButton>(this, "ObjDialogObjConditionsButton");
416
417 // Get the selection
418 wxDataViewItem item = _objectiveEntityView->GetSelection();
419
420 if (item.IsOk())
421 {
422 // Get name of the entity and find the corresponding ObjectiveEntity in
423 // the map
424 wxutil::TreeModel::Row row(item, *_objectiveEntityList);
425 std::string name = row[_objEntityColumns.entityName];
426
427 // Save the current selection and refresh the objectives list
428 _curEntity = _entities.find(name);
429 refreshObjectivesList();
430
431 // Enable the delete button and objectives panel
432 delEntityButton->Enable(true);
433 objButtonPanel->Enable(true);
434
435 // Enable buttons
436 successLogicButton->Enable(true);
437 objCondButton->Enable(true);
438 }
439 else
440 {
441 // No selection, disable the delete button and clear the objective
442 // panel
443 delEntityButton->Enable(false);
444 objButtonPanel->Enable(false);
445
446 // Disable mission logic button
447 successLogicButton->Enable(false);
448 objCondButton->Enable(false);
449 }
450 }
451
452 // Add a new objectives entity button
453 void ObjectivesEditor::_onAddEntity(wxCommandEvent& ev)
454 {
455 if (_objectiveEClasses.empty())
456 {
457 // Objective entityclass(es) not defined
458 wxutil::Messagebox::ShowError(
459 _("Unable to create Objective Entity: classes not defined in registry.")
460 );
461 return;
462 }
463
464 const std::string& objEClass = _objectiveEClasses.front();
465
466 // Obtain the entity class object
467 IEntityClassPtr eclass = GlobalEntityClassManager().findClass(objEClass);
468
469 if (eclass)
470 {
471 // Construct a Node of this entity type
472 IEntityNodePtr node(GlobalEntityModule().createEntity(eclass));
473
474 // Create a random offset
475 node->getEntity().setKeyValue("origin", RandomOrigin::generate(128));
476
477 // Insert the node into the scene graph
478 assert(GlobalSceneGraph().root());
479 GlobalSceneGraph().root()->addChildNode(node);
480
481 // Refresh the widgets
482 populateWidgets();
483 }
484 else
485 {
486 // Objective entityclass was not found
487 wxutil::Messagebox::ShowError(
488 fmt::format(_("Unable to create Objective Entity: class '{0}' not found."), objEClass)
489 );
490 }
491 }
492
493 // Delete entity button
494 void ObjectivesEditor::_onDeleteEntity(wxCommandEvent& ev)
495 {
496 // Get the selection
497 wxDataViewItem item = _objectiveEntityView->GetSelection();
498
499 if (item.IsOk())
500 {
501 // Get the name of the selected entity
502 wxutil::TreeModel::Row row(item, *_objectiveEntityList);
503 std::string name = row[_objEntityColumns.entityName];
504
505 // Instruct the ObjectiveEntity to delete its world node, and then
506 // remove it from the map
507 _entities[name]->deleteWorldNode();
508 _entities.erase(name);
509
510 // Update the widgets to remove the selection from the list
511 populateWidgets();
512
513 updateEditorButtonPanel();
514 }
515 }
516
517 // Add a new objective
518 void ObjectivesEditor::_onAddObjective(wxCommandEvent& ev)
519 {
520 // Add a new objective to the ObjectiveEntity and refresh the list store
521 _curEntity->second->addObjective();
522 refreshObjectivesList();
523 }
524
525 void ObjectivesEditor::doEditObjective()
526 {
527 // Display the ComponentsDialog
528 ComponentsDialog* compDialog = new ComponentsDialog(this, getCurrentObjective());
529
530 compDialog->ShowModal(); // show and block
531 compDialog->Destroy();
532
533 // Repopulate the objective list
534 refreshObjectivesList();
535 }
536
537 // Edit an existing objective
538 void ObjectivesEditor::_onEditObjective(wxCommandEvent& ev)
539 {
540 doEditObjective();
541 }
542
543 void ObjectivesEditor::_onMoveUpObjective(wxCommandEvent& ev)
544 {
545 // get the current index
546 wxutil::TreeModel::Row row(_curObjective, *_objectiveList);
547 int index = row[_objectiveColumns.objNumber].getInteger();
548
549 // Pass the call to the general method
550 int newIndex = _curEntity->second->moveObjective(index, -1);
551
552 refreshObjectivesList();
553 selectObjectiveByIndex(newIndex);
554 }
555
556 void ObjectivesEditor::_onMoveDownObjective(wxCommandEvent& ev)
557 {
558 // get the current index
559 wxutil::TreeModel::Row row(_curObjective, *_objectiveList);
560 int index = row[_objectiveColumns.objNumber].getInteger();
561
562 // Pass the call to the general method
563 int newIndex = _curEntity->second->moveObjective(index, +1);
564
565 refreshObjectivesList();
566 selectObjectiveByIndex(newIndex);
567 }
568
569 // Delete an objective
570 void ObjectivesEditor::_onDeleteObjective(wxCommandEvent& ev)
571 {
572 // Get the index of the current objective
573 wxutil::TreeModel::Row row(_curObjective, *_objectiveList);
574 int index = row[_objectiveColumns.objNumber].getInteger();
575
576 // Tell the ObjectiveEntity to delete this objective
577 _curEntity->second->deleteObjective(index);
578
579 // Repopulate the objective list
580 refreshObjectivesList();
581 }
582
583 // Clear the objectives
584 void ObjectivesEditor::_onClearObjectives(wxCommandEvent& ev)
585 {
586 // Clear the entity and refresh the list
587 _curEntity->second->clearObjectives();
588 refreshObjectivesList();
589 }
590
591 void ObjectivesEditor::_onEditLogic(wxCommandEvent& ev)
592 {
593 MissionLogicDialog* dialog = new MissionLogicDialog(this, *_curEntity->second);
594
595 dialog->ShowModal();
596 dialog->Destroy();
597
598 refreshObjectivesList();
599 }
600
601 void ObjectivesEditor::_onEditObjConditions(wxCommandEvent& ev)
602 {
603 ObjectiveConditionsDialog* dialog = new ObjectiveConditionsDialog(this, *_curEntity->second);
604
605 dialog->ShowModal();
606 dialog->Destroy();
607
608 refreshObjectivesList();
609 }
610
611 } // namespace objectives