Debian Codegraph @

plugins/dm.objectives/MissionLogicDialog.cpp

raw
1 #include "MissionLogicDialog.h"
2 #include "ObjectiveEntity.h"
3
4 #include "i18n.h"
5 #include "string/string.h"
6
7 #include <fmt/format.h>
8
9 #include <wx/sizer.h>
10 #include <wx/stattext.h>
11
12 namespace objectives
13 {
14
15 namespace
16 {
17
18 const char* const DIALOG_TITLE = N_("Edit Mission Logic");
19
20 const char* const STANDARD_LOGIC_DESCR =
21 N_("This is the standard logic for all difficulty levels");
22
23 const char* const DIFFICULTY_LOGIC_DESCR =
24 N_("These logics override the standard logic for the given difficulty level\n"
25 "if the logic string is non-empty.");
26
27 }
28
29 // Main constructor
30 MissionLogicDialog::MissionLogicDialog(wxWindow* parent, ObjectiveEntity& objectiveEnt) :
31 DialogBase(_(DIALOG_TITLE), parent),
32 _objectiveEnt(objectiveEnt)
33 {
34 SetSizer(new wxBoxSizer(wxVERTICAL));
35
36 wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
37
38 GetSizer()->Add(vbox, 1, wxEXPAND | wxALL, 12);
39 GetSizer()->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT | wxALL, 12);
40
41 // Create one logic editor for each difficulty level plus the default one
42 createLogicEditors();
43
44 // Overall VBox for labels and alignments
45 wxStaticText* defaultLabel = new wxStaticText(this, wxID_ANY, _("Default Logic"));
46 defaultLabel->SetFont(defaultLabel->GetFont().Bold());
47 vbox->Add(defaultLabel, 0, wxBOTTOM, 12);
48
49 // Default Logic
50 wxBoxSizer* defaultVBox = new wxBoxSizer(wxVERTICAL);
51 defaultVBox->Add(new wxStaticText(this, wxID_ANY, _(STANDARD_LOGIC_DESCR)), 0, wxBOTTOM, 6);
52 defaultVBox->Add(_logicEditors[-1], 0, wxBOTTOM | wxEXPAND, 6);
53
54 vbox->Add(defaultVBox, 0, wxLEFT | wxEXPAND, 12);
55
56 // Now add all difficulty-specific editors
57 vbox->Add(new wxStaticText(this, wxID_ANY, _("Difficulty-specific Logic")), 0, wxBOTTOM, 12);
58
59 wxBoxSizer* diffVBox = new wxBoxSizer(wxVERTICAL);
60 diffVBox->Add(new wxStaticText(this, wxID_ANY, _(DIFFICULTY_LOGIC_DESCR)), 0, wxBOTTOM, 6);
61
62 // Iterate over all editors for levels that are greater or equal 0
63 for (LogicEditorMap::iterator i = _logicEditors.lower_bound(0);
64 i != _logicEditors.end(); ++i)
65 {
66 std::string logicStr = fmt::format(_("Logic for Difficulty Level {0:d}"), i->first);
67
68 wxStaticText* logicLabel = new wxStaticText(this, wxID_ANY, logicStr);
69 logicLabel->SetFont(logicLabel->GetFont().Bold());
70 diffVBox->Add(logicLabel, 0, wxBOTTOM, 6);
71
72 diffVBox->Add(i->second, 0, wxEXPAND | wxBOTTOM, 6);
73 }
74
75 vbox->Add(diffVBox, 0, wxLEFT | wxEXPAND, 12);
76
77 // Populate the logic strings
78 populateLogicEditors();
79
80 Layout();
81 Fit();
82 }
83
84 void MissionLogicDialog::createLogicEditors()
85 {
86 // Create the default logic editor
87 _logicEditors[-1] = new LogicEditor(this);
88
89 // TODO: Connect this plugin to the difficulty plugin (which can be optional)
90 // to find out how many difficulty levels there are (and what their names are)
91 _logicEditors[0] = new LogicEditor(this);
92 _logicEditors[1] = new LogicEditor(this);
93 _logicEditors[2] = new LogicEditor(this);
94 }
95
96 void MissionLogicDialog::populateLogicEditors()
97 {
98 // TODO: Connect this plugin to the difficulty plugin (which can be optional)
99 // to find out how many difficulty levels there are
100 for (int i = -1; i <= 2; i++)
101 {
102 LogicPtr logic = _objectiveEnt.getMissionLogic(i);
103
104 // FIXME: Hm, maybe it would be better to pass the Logic object itself to the editor?
105 _logicEditors[i]->setSuccessLogicStr(logic->successLogic);
106 _logicEditors[i]->setFailureLogicStr(logic->failureLogic);
107 }
108 }
109
110 int MissionLogicDialog::ShowModal()
111 {
112 int returnCode = DialogBase::ShowModal();
113
114 if (returnCode == wxID_OK)
115 {
116 save();
117 }
118
119 return returnCode;
120 }
121
122 void MissionLogicDialog::save()
123 {
124 // TODO: Connect this plugin to the difficulty plugin (which can be optional)
125 // to find out how many difficulty levels there are
126 for (int i = -1; i <= 2; i++)
127 {
128 LogicPtr logic = _objectiveEnt.getMissionLogic(i);
129
130 // FIXME: Hm, maybe it would be better to pass the Logic object itself to the editor?
131 logic->successLogic = _logicEditors[i]->getSuccessLogicStr();
132 logic->failureLogic = _logicEditors[i]->getFailureLogicStr();
133 }
134 }
135
136 } // namespace objectives