Debian Codegraph @

plugins/dm.objectives/ObjectiveConditionsDialog.cpp

raw
1 #include "ObjectiveConditionsDialog.h"
2
3 #include "ui/imainframe.h"
4 #include "i18n.h"
5 #include "itextstream.h"
6
7 #include "string/convert.h"
8
9 #include <wx/button.h>
10 #include <wx/panel.h>
11 #include <wx/spinctrl.h>
12 #include <wx/choice.h>
13
14 #include "wxutil/ChoiceHelper.h"
15 #include "fmt/format.h"
16
17 #include "ObjectiveEntity.h"
18
19 namespace objectives
20 {
21
22 namespace
23 {
24 const char* const DIALOG_TITLE = N_("Edit Objective Conditions");
25
26 const std::string RKEY_ROOT = "user/ui/objectivesEditor/conditionsDialog/";
27 const std::string RKEY_WINDOW_STATE = RKEY_ROOT + "window";
28 }
29
30 ObjectiveConditionsDialog::ObjectiveConditionsDialog(wxWindow* parent, ObjectiveEntity& objectiveEnt) :
31 DialogBase(_(DIALOG_TITLE), parent),
32 _objectiveEnt(objectiveEnt),
33 _objectiveConditionList(new wxutil::TreeModel(_objConditionColumns, true)),
34 _srcObjState(NULL),
35 _type(NULL),
36 _value(NULL),
37 _targetObj(NULL),
38 _updateActive(false)
39 {
40 wxPanel* mainPanel = loadNamedPanel(this, "ObjCondDialogMainPanel");
41
42 makeLabelBold(this, "ObjCondDialogTopLabel");
43 makeLabelBold(this, "ObjCondDialogConditionLabel");
44 makeLabelBold(this, "ObjCondDialogSentenceLabel");
45
46 // OK and CANCEL actions
47 findNamedObject<wxButton>(this, "ObjCondDialogCancelButton")->Connect(
48 wxEVT_BUTTON, wxCommandEventHandler(ObjectiveConditionsDialog::_onCancel), NULL, this);
49 findNamedObject<wxButton>(this, "ObjCondDialogOkButton")->Connect(
50 wxEVT_BUTTON, wxCommandEventHandler(ObjectiveConditionsDialog::_onOK), NULL, this);
51
52 // Connect the window position tracker
53 _windowPosition.loadFromPath(RKEY_WINDOW_STATE);
54 _windowPosition.connect(this);
55 _windowPosition.applyPosition();
56
57 // Copy the objective conditions to our working set
58 _objConditions = _objectiveEnt.getObjectiveConditions();
59
60 setupConditionsPanel();
61 setupConditionEditPanel();
62
63 updateSentence();
64
65 mainPanel->Layout();
66 mainPanel->Fit();
67 Fit();
68 }
69
70 void ObjectiveConditionsDialog::setupConditionsPanel()
71 {
72 wxPanel* condPanel = findNamedObject<wxPanel>(this, "ObjCondDialogConditionViewPanel");
73
74 // Tree view listing the conditions
75 _conditionsView = wxutil::TreeView::CreateWithModel(condPanel, _objectiveConditionList.get(), wxDV_NO_HEADER);
76 condPanel->GetSizer()->Add(_conditionsView, 1, wxEXPAND);
77
78 _conditionsView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
79 wxDataViewEventHandler(ObjectiveConditionsDialog::_onConditionSelectionChanged), NULL, this);
80
81 // Number column
82 _conditionsView->AppendTextColumn("", _objConditionColumns.conditionNumber.getColumnIndex(),
83 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
84
85 // Description
86 _conditionsView->AppendTextColumn("", _objConditionColumns.description.getColumnIndex(),
87 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
88
89 // Connect button signals
90 wxButton* addButton = findNamedObject<wxButton>(this, "ObjCondDialogAddConditionButton");
91 addButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ObjectiveConditionsDialog::_onAddObjCondition), NULL, this);
92
93 wxButton* delButton = findNamedObject<wxButton>(this, "ObjCondDialogDeleteConditionButton");
94 delButton->Enable(false); // disabled at start
95 delButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ObjectiveConditionsDialog::_onDelObjCondition), NULL, this);
96 }
97
98 void ObjectiveConditionsDialog::setupConditionEditPanel()
99 {
100 // Initially everything is insensitive
101 findNamedObject<wxButton>(this, "ObjCondDialogDeleteConditionButton")->Enable(false);
102
103 // Disable details controls
104 findNamedObject<wxPanel>(this, "ObjCondDialogConditionEditPanel")->Enable(false);
105
106 // Set ranges for spin buttons
107 wxSpinCtrl* srcMission = findNamedObject<wxSpinCtrl>(this, "ObjCondDialogSourceMission");
108 srcMission->SetRange(1, 99);
109 srcMission->SetValue(1);
110 srcMission->Connect(wxEVT_SPINCTRL, wxSpinEventHandler(ObjectiveConditionsDialog::_onSrcMissionChanged), NULL, this);
111
112 wxSpinCtrl* srcObj = findNamedObject<wxSpinCtrl>(this, "ObjCondDialogSourceObjective");
113 srcObj->SetRange(1, 999);
114 srcObj->SetValue(1);
115 srcObj->Connect(wxEVT_SPINCTRL, wxSpinEventHandler(ObjectiveConditionsDialog::_onSrcObjChanged), NULL, this);
116
117 // State dropdown
118 _srcObjState = findNamedObject<wxChoice>(this, "ObjCondDialogSourceObjectiveState");
119
120 // Populate the list of states. This must be done in order to match the
121 // values in the enum, since the index will be used when writing to entity
122 _srcObjState->Append(Objective::getStateText(Objective::INCOMPLETE),
123 new wxStringClientData(string::to_string(Objective::INCOMPLETE)));
124 _srcObjState->Append(Objective::getStateText(Objective::COMPLETE),
125 new wxStringClientData(string::to_string(Objective::COMPLETE)));
126 _srcObjState->Append(Objective::getStateText(Objective::INVALID),
127 new wxStringClientData(string::to_string(Objective::INVALID)));
128 _srcObjState->Append(Objective::getStateText(Objective::FAILED),
129 new wxStringClientData(string::to_string(Objective::FAILED)));
130
131 _srcObjState->Connect(wxEVT_CHOICE,
132 wxCommandEventHandler(ObjectiveConditionsDialog::_onSrcStateChanged), NULL, this);
133
134 // Objectives dropdown
135 _targetObj = findNamedObject<wxChoice>(this, "ObjCondDialogTargetObjective");
136
137 // Populate the liststore
138 _objectiveEnt.populateChoice(_targetObj);
139
140 _targetObj->Connect(wxEVT_CHOICE,
141 wxCommandEventHandler(ObjectiveConditionsDialog::_onTargetObjChanged), NULL, this);
142
143 _type = findNamedObject<wxChoice>(this, "ObjCondDialogAction");
144
145 _type->Append(_("Change Objective State"),
146 new wxStringClientData(string::to_string(0)));
147 _type->Append(_("Change Visibility"),
148 new wxStringClientData(string::to_string(1)));
149 _type->Append(_("Change Mandatory Flag"),
150 new wxStringClientData(string::to_string(2)));
151
152 _type->Connect(wxEVT_CHOICE,
153 wxCommandEventHandler(ObjectiveConditionsDialog::_onTypeChanged), NULL, this);
154
155 _value = findNamedObject<wxChoice>(this, "ObjCondDialogActionValue");
156
157 // Will be populated later on
158 _value->Connect(wxEVT_CHOICE,
159 wxCommandEventHandler(ObjectiveConditionsDialog::_onValueChanged), NULL, this);
160 }
161
162 ObjectiveCondition& ObjectiveConditionsDialog::getCurrentObjectiveCondition()
163 {
164 wxutil::TreeModel::Row row(_curCondition, *_objectiveConditionList);
165 int index = row[_objConditionColumns.conditionNumber].getInteger();
166
167 return *_objConditions[index];
168 }
169
170 void ObjectiveConditionsDialog::loadValuesFromCondition()
171 {
172 _updateActive = true;
173
174 ObjectiveCondition& cond = getCurrentObjectiveCondition();
175
176 // Source mission number
177 wxSpinCtrl* srcMission = findNamedObject<wxSpinCtrl>(this, "ObjCondDialogSourceMission");
178 srcMission->SetValue(cond.sourceMission + 1); // +1 since user-visible values are 1-based
179
180 // Source objective number
181 wxSpinCtrl* srcObj = findNamedObject<wxSpinCtrl>(this, "ObjCondDialogSourceObjective");
182 srcObj->SetValue(cond.sourceObjective + 1); // +1 since user-visible values are 1-based
183
184 // Source objective state
185 wxutil::ChoiceHelper::SelectItemByStoredId(_srcObjState, cond.sourceState);
186
187 // Find objective in dropdown list (stored objective numbers are 1-based)
188 wxutil::ChoiceHelper::SelectItemByStoredId(_targetObj, cond.targetObjective+1);
189
190 // Set condition type and load possible value types
191 wxutil::ChoiceHelper::SelectItemByStoredId(_type, cond.type);
192
193 refreshPossibleValues();
194
195 updateSentence();
196
197 _updateActive = false;
198 }
199
200 void ObjectiveConditionsDialog::refreshPossibleValues()
201 {
202 ObjectiveCondition& cond = getCurrentObjectiveCondition();
203
204 // Remove all items from the dropdown
205 _value->Clear();
206
207 // Load possible value selections based on the selected type
208 switch (cond.type)
209 {
210 case ObjectiveCondition::CHANGE_STATE:
211 _value->Append(fmt::format(_("Set state to {0}"), Objective::getStateText(Objective::INCOMPLETE)));
212 _value->Append(fmt::format(_("Set state to {0}"), Objective::getStateText(Objective::COMPLETE)));
213 _value->Append(fmt::format(_("Set state to {0}"), Objective::getStateText(Objective::INVALID)));
214 _value->Append(fmt::format(_("Set state to {0}"), Objective::getStateText(Objective::FAILED)));
215
216 if (cond.value >= Objective::NUM_STATES)
217 {
218 cond.value = Objective::FAILED;
219 }
220
221 _value->Select(cond.value);
222 break;
223
224 case ObjectiveCondition::CHANGE_VISIBILITY:
225 _value->Append(_("Set Invisible"));
226 _value->Append(_("Set Visible"));
227
228 if (cond.value > 1)
229 {
230 cond.value = 1;
231 }
232
233 _value->Select(cond.value);
234 break;
235
236 case ObjectiveCondition::CHANGE_MANDATORY:
237 _value->Append(_("Clear mandatory flag"));
238 _value->Append(_("Set mandatory flag"));
239
240 if (cond.value > 1)
241 {
242 cond.value = 1;
243 }
244
245 _value->Select(cond.value);
246 break;
247
248 default:
249 rWarning() << "Unknown type encountered while refreshing condition edit panel." << std::endl;
250 break;
251 };
252 }
253
254 void ObjectiveConditionsDialog::_onConditionSelectionChanged(wxDataViewEvent& ev)
255 {
256 wxButton* delObjCondButton = findNamedObject<wxButton>(this, "ObjCondDialogDeleteConditionButton");
257
258 // Get the selection
259 _curCondition = _conditionsView->GetSelection();
260
261 if (_curCondition.IsOk())
262 {
263 delObjCondButton->Enable(true);
264
265 loadValuesFromCondition();
266
267 // Enable details controls
268 findNamedObject<wxPanel>(this, "ObjCondDialogConditionEditPanel")->Enable(true);
269 }
270 else
271 {
272 // No selection, disable the delete button
273 delObjCondButton->Enable(false);
274
275 // Disable details controls
276 findNamedObject<wxPanel>(this, "ObjCondDialogConditionEditPanel")->Enable(false);
277 }
278 }
279
280 void ObjectiveConditionsDialog::_onAddObjCondition(wxCommandEvent& ev)
281 {
282 for (int i = 1; i < INT_MAX; ++i)
283 {
284 ObjectiveEntity::ConditionMap::iterator found = _objConditions.find(i);
285
286 if (found == _objConditions.end())
287 {
288 // Create a new condition
289 ObjectiveConditionPtr cond(new ObjectiveCondition);
290 _objConditions[i] = cond;
291
292 // Set some default values, such that it doesn't end up invalid
293 cond->sourceMission = 0;
294 cond->sourceObjective = 0;
295 cond->sourceState = Objective::INCOMPLETE;
296 cond->targetObjective = 0;
297 cond->type = ObjectiveCondition::CHANGE_STATE;
298 cond->value = 0;
299
300 // Refresh the dialog
301 populateWidgets();
302
303 // Select the new condition
304 wxDataViewItem foundItem = _objectiveConditionList->FindInteger(i,
305 _objConditionColumns.conditionNumber);
306
307 if (foundItem.IsOk())
308 {
309 _conditionsView->Select(foundItem);
310 }
311
312 return;
313 }
314 }
315
316 throw std::runtime_error("Ran out of free objective condition indices.");
317 }
318
319 void ObjectiveConditionsDialog::_onDelObjCondition(wxCommandEvent& ev)
320 {
321 assert(_curCondition.IsOk());
322
323 // Get the index of the current objective condition
324 wxutil::TreeModel::Row row(_curCondition, *_objectiveConditionList);
325 int index = row[_objConditionColumns.conditionNumber].getInteger();
326
327 _objConditions.erase(index);
328
329 // Repopulate the dialog
330 populateWidgets();
331 }
332
333 void ObjectiveConditionsDialog::_onTypeChanged(wxCommandEvent& ev)
334 {
335 if (_updateActive || !isConditionSelected()) return;
336
337 ObjectiveCondition& cond = getCurrentObjectiveCondition();
338
339 cond.type = static_cast<ObjectiveCondition::Type>(wxutil::ChoiceHelper::GetSelectionId(_type));
340
341 // Inhibit _onValueChanged calls
342 _updateActive = true;
343
344 refreshPossibleValues();
345
346 _updateActive = false;
347
348 updateSentence();
349 }
350
351 void ObjectiveConditionsDialog::_onSrcMissionChanged(wxSpinEvent& ev)
352 {
353 if (_updateActive || !isConditionSelected()) return;
354
355 ObjectiveCondition& cond = getCurrentObjectiveCondition();
356
357 // Subtract 1 from the source mission, we need 0-based values
358 wxSpinCtrl* srcMission = findNamedObject<wxSpinCtrl>(this, "ObjCondDialogSourceMission");
359 cond.sourceMission = srcMission->GetValue() - 1;
360
361 updateSentence();
362 }
363
364 void ObjectiveConditionsDialog::_onSrcObjChanged(wxSpinEvent& ev)
365 {
366 if (_updateActive || !isConditionSelected()) return;
367
368 ObjectiveCondition& cond = getCurrentObjectiveCondition();
369
370 // Subtract 1 from the source objective, we need 0-based values
371 wxSpinCtrl* srcObj = findNamedObject<wxSpinCtrl>(this, "ObjCondDialogSourceObjective");
372 cond.sourceObjective = srcObj->GetValue() - 1;
373
374 updateSentence();
375 }
376
377 void ObjectiveConditionsDialog::_onSrcStateChanged(wxCommandEvent& ev)
378 {
379 if (_updateActive || !isConditionSelected()) return;
380
381 ObjectiveCondition& cond = getCurrentObjectiveCondition();
382
383 int state = wxutil::ChoiceHelper::GetSelectionId(_srcObjState);
384
385 assert(state >= Objective::INCOMPLETE && state < Objective::NUM_STATES);
386 cond.sourceState = static_cast<Objective::State>(state);
387
388 updateSentence();
389 }
390
391 void ObjectiveConditionsDialog::_onTargetObjChanged(wxCommandEvent& ev)
392 {
393 if (_updateActive || !isConditionSelected()) return;
394
395 int objNum = wxutil::ChoiceHelper::GetSelectionId(_targetObj);
396
397 if (objNum == -1)
398 {
399 return;
400 }
401
402 ObjectiveCondition& cond = getCurrentObjectiveCondition();
403
404 cond.targetObjective = objNum - 1; // reduce by one, liststore has 1-based numbers
405
406 updateSentence();
407 }
408
409 void ObjectiveConditionsDialog::_onValueChanged(wxCommandEvent& ev)
410 {
411 if (_updateActive || !isConditionSelected()) return;
412
413 ObjectiveCondition& cond = getCurrentObjectiveCondition();
414
415 cond.value = _value->GetSelection();
416
417 updateSentence();
418 }
419
420 void ObjectiveConditionsDialog::clear()
421 {
422 // Clear the list
423 _objectiveConditionList->Clear();
424 }
425
426 int ObjectiveConditionsDialog::ShowModal()
427 {
428 // Restore the position
429 _windowPosition.applyPosition();
430
431 populateWidgets();
432
433 int returnCode = DialogBase::ShowModal();
434
435 // Tell the position tracker to save the information
436 _windowPosition.saveToPath(RKEY_WINDOW_STATE);
437
438 // Clear all data before hiding
439 clear();
440
441 return returnCode;
442 }
443
444 void ObjectiveConditionsDialog::populateWidgets()
445 {
446 // Clear internal data first
447 clear();
448
449 for (ObjectiveEntity::ConditionMap::const_iterator i = _objConditions.begin();
450 i != _objConditions.end(); ++i)
451 {
452 wxutil::TreeModel::Row row = _objectiveConditionList->AddItem();
453
454 row[_objConditionColumns.conditionNumber] = i->first;
455 row[_objConditionColumns.description] = getDescription(*i->second);
456
457 row.SendItemAdded();
458 }
459 }
460
461 std::string ObjectiveConditionsDialog::getDescription(const ObjectiveCondition& cond)
462 {
463 return fmt::format(_("Condition affecting objective {0:d}"), (cond.targetObjective+1));
464 }
465
466 void ObjectiveConditionsDialog::_onCancel(wxCommandEvent& ev)
467 {
468 EndModal(wxID_CANCEL);
469 }
470
471 void ObjectiveConditionsDialog::save()
472 {
473 _objectiveEnt.setObjectiveConditions(_objConditions);
474 }
475
476 void ObjectiveConditionsDialog::_onOK(wxCommandEvent& ev)
477 {
478 save();
479 EndModal(wxID_OK);
480 }
481
482 bool ObjectiveConditionsDialog::isConditionSelected()
483 {
484 return _conditionsView->GetSelection().IsOk();
485 }
486
487 std::string ObjectiveConditionsDialog::getSentence(const ObjectiveCondition& cond)
488 {
489 std::string str = "";
490
491 if (cond.isValid())
492 {
493 str = fmt::format(_("If Objective {0} in Mission {1} is in state '{2}' do the following: "),
494 (cond.sourceObjective+1), (cond.sourceMission+1), Objective::getStateText(cond.sourceState));
495
496 str += "\n";
497
498 std::string actionStr = "";
499 int objNum = cond.targetObjective + 1; // the user-visible objectives are numbered differently
500
501 switch (cond.type)
502 {
503 case ObjectiveCondition::CHANGE_STATE:
504 actionStr = fmt::format(_("Set State on Objective {0} to {1}"),
505 objNum, Objective::getStateText(static_cast<Objective::State>(cond.value)));
506 break;
507
508 case ObjectiveCondition::CHANGE_VISIBILITY:
509 if (cond.value != 0)
510 {
511 actionStr = fmt::format(_("Make Objective {0} visible"), objNum);
512 }
513 else
514 {
515 actionStr = fmt::format(_("Make Objective {0} invisible"), objNum);
516 }
517 break;
518
519 case ObjectiveCondition::CHANGE_MANDATORY:
520 if (cond.value != 0)
521 {
522 actionStr = fmt::format(_("Make Objective {0} mandatory"), objNum);
523 }
524 else
525 {
526 actionStr = fmt::format(_("Make Objective {0} not mandatory"), objNum);
527 }
528 break;
529 default:
530 break;
531 };
532
533 str += actionStr;
534 }
535 else
536 {
537 str = _("This condition is not valid or complete yet.");
538 }
539
540 return str;
541 }
542
543 void ObjectiveConditionsDialog::updateSentence()
544 {
545 wxStaticText* label = findNamedObject<wxStaticText>(this, "ObjCondDialogSentence");
546
547 if (isConditionSelected())
548 {
549 label->SetLabelMarkup(getSentence(getCurrentObjectiveCondition()));
550 }
551 else
552 {
553 label->SetLabelMarkup("");
554 }
555
556 wxPanel* mainPanel = findNamedObject<wxPanel>(this, "ObjCondDialogMainPanel");
557
558 mainPanel->Layout();
559 mainPanel->Fit();
560 Fit();
561 }
562
563 } // namespace