Debian Codegraph @

plugins/dm.stimresponse/EffectEditor.cpp

raw
1 #include "EffectEditor.h"
2
3 #include "iscenegraph.h"
4 #include "iregistry.h"
5 #include "entitylib.h"
6 #include "wxutil/dataview/TreeModel.h"
7 #include "ResponseEditor.h"
8 #include <iostream>
9 #include "i18n.h"
10 #include "gamelib.h"
11
12 #include "wxutil/ChoiceHelper.h"
13 #include <wx/choice.h>
14 #include <wx/checkbox.h>
15 #include <wx/stattext.h>
16
17 namespace ui
18 {
19
20 namespace
21 {
22 const char* const WINDOW_TITLE = N_("Edit Response Effect");
23 // The name of the _SELF entity as parsed by the response scripts
24 const char* const GKEY_ENTITY_SELF = "/stimResponseSystem/selfEntity";
25 }
26
27 EffectEditor::EffectEditor(wxWindow* parent,
28 StimResponse& response,
29 const unsigned int effectIndex,
30 StimTypes& stimTypes,
31 ui::ResponseEditor& editor) :
32 DialogBase(_(WINDOW_TITLE), parent),
33 _argTable(NULL),
34 _response(response),
35 _effectIndex(effectIndex),
36 _backup(_response.getResponseEffect(_effectIndex)),
37 _editor(editor),
38 _stimTypes(stimTypes)
39 {
40 SetSizer(new wxBoxSizer(wxVERTICAL));
41
42 // Create the widgets
43 populateWindow();
44
45 // Search the scenegraph for entities
46 populateEntityListStore();
47
48 // Initialise the widgets with the current data
49 ResponseEffect& effect = _response.getResponseEffect(_effectIndex);
50
51 // Setup the selectionfinder to search for the name string
52 wxutil::ChoiceHelper::SelectItemByStoredString(_effectTypeCombo, effect.getName());
53
54 _stateToggle->SetValue(effect.isActive());
55
56 // Parse the argument types from the effect and create the widgets
57 createArgumentWidgets(effect);
58
59 Layout();
60 Fit();
61 }
62
63 int EffectEditor::ShowModal()
64 {
65 int returnCode = DialogBase::ShowModal();
66
67 if (returnCode == wxID_OK)
68 {
69 save();
70 }
71 else
72 {
73 revert();
74 }
75
76 return returnCode;
77 }
78
79 void EffectEditor::populateWindow()
80 {
81 // Create the overall vbox
82 wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
83
84 // Pack everything into the main window
85 GetSizer()->Add(vbox, 1, wxEXPAND | wxALL, 12);
86 GetSizer()->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT | wxALL, 12);
87
88 _effectTypeCombo = new wxChoice(this, wxID_ANY);
89
90 // Connect the signal to get notified of further changes
91 _effectTypeCombo->Connect(wxEVT_CHOICE, wxCommandEventHandler(EffectEditor::onEffectTypeChange), NULL, this);
92
93 // Retrieve the map from the ResponseEffectTypes object
94 ResponseEffectTypeMap& effectTypes =
95 ResponseEffectTypes::Instance().getMap();
96
97 // Now populate the list store with all the possible effect types
98 for (ResponseEffectTypeMap::iterator i = effectTypes.begin();
99 i != effectTypes.end(); ++i)
100 {
101 // Store the effect name as client data
102 _effectTypeCombo->Append(i->second->getAttributeValue("editor_caption"),
103 new wxStringClientData(i->first));
104 }
105
106 wxBoxSizer* effectHBox = new wxBoxSizer(wxHORIZONTAL);
107
108 wxStaticText* effectLabel = new wxStaticText(this, wxID_ANY, _("Effect:"));
109
110 effectHBox->Add(effectLabel, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, 6);
111 effectHBox->Add(_effectTypeCombo);
112
113 vbox->Add(effectHBox, 0, wxBOTTOM | wxEXPAND, 6);
114
115 _stateToggle = new wxCheckBox(this, wxID_ANY, _("Active"));
116 _stateToggle->Connect(wxEVT_CHECKBOX, wxCommandEventHandler(EffectEditor::onStateToggle), NULL, this);
117
118 vbox->Add(_stateToggle, 0, wxBOTTOM, 6);
119
120 wxStaticText* argLabel = new wxStaticText(this, wxID_ANY, _("Arguments"));
121 argLabel->SetFont(argLabel->GetFont().Bold());
122 vbox->Add(argLabel, 0, wxBOTTOM, 6);
123
124 _argTable = new wxFlexGridSizer(3, 6, 12);
125 vbox->Add(_argTable, 0, wxEXPAND | wxLEFT, 12);
126 }
127
128 void EffectEditor::effectTypeChanged()
129 {
130 std::string newEffectName("");
131
132 // Get the currently selected effect name from the combo box
133 if (_effectTypeCombo->GetSelection() != wxNOT_FOUND)
134 {
135 wxStringClientData* data = dynamic_cast<wxStringClientData*>(
136 _effectTypeCombo->GetClientObject(_effectTypeCombo->GetSelection()));
137 assert(data != NULL);
138
139 newEffectName = data->GetData().ToStdString();
140 }
141
142 // Get the effect
143 ResponseEffect& effect = _response.getResponseEffect(_effectIndex);
144
145 // Set the name of the effect and trigger an argument list update
146 effect.setName(newEffectName);
147 effect.clearArgumentList();
148 effect.buildArgumentList();
149
150 // Rebuild the argument list basing on this new effect
151 createArgumentWidgets(effect);
152 }
153
154 void EffectEditor::createArgumentWidgets(ResponseEffect& effect)
155 {
156 ResponseEffect::ArgumentList& list = effect.getArguments();
157
158 // Remove all possible previous items from the list
159 _argumentItems.clear();
160
161 // Clear the old table
162 _argTable->DeleteWindows();
163
164 for (ResponseEffect::ArgumentList::iterator i = list.begin();
165 i != list.end(); ++i)
166 {
167 //int index = i->first;
168 ResponseEffect::Argument& arg = i->second;
169 ArgumentItemPtr item;
170
171 if (arg.type == "s")
172 {
173 // Create a new string argument item
174 item = ArgumentItemPtr(new StringArgument(this, arg));
175 }
176 else if (arg.type == "f")
177 {
178 // Create a new string argument item
179 item = ArgumentItemPtr(new FloatArgument(this, arg));
180 }
181 else if (arg.type == "v")
182 {
183 // Create a new vector argument item
184 item = ArgumentItemPtr(new VectorArgument(this, arg));
185 }
186 else if (arg.type == "e")
187 {
188 // Create a new string argument item
189 item = ArgumentItemPtr(new EntityArgument(this, arg, _entityChoices));
190 }
191 else if (arg.type == "b")
192 {
193 // Create a new bool item
194 item = ArgumentItemPtr(new BooleanArgument(this, arg));
195 }
196 else if (arg.type == "t")
197 {
198 // Create a new stim type item
199 item = ArgumentItemPtr(new StimTypeArgument(this, arg, _stimTypes));
200 }
201
202 if (item != NULL)
203 {
204 _argumentItems.push_back(item);
205
206 if (arg.type != "b")
207 {
208 // The label
209 _argTable->Add(item->getLabelWidget(), 0, wxALIGN_CENTER_VERTICAL);
210
211 // The edit widgets
212 _argTable->Add(item->getEditWidget(), 1, wxEXPAND);
213 }
214 else
215 {
216 // The label
217 _argTable->Add(new wxStaticText(this, wxID_ANY, ""), 0, wxALIGN_CENTER_VERTICAL);
218
219 // The edit widgets
220 _argTable->Add(item->getEditWidget(), 1, wxEXPAND);
221 }
222
223 // The help widgets
224 _argTable->Add(item->getHelpWidget(), 0, wxALIGN_CENTER_VERTICAL);
225 }
226 }
227
228 _argTable->Layout();
229 Layout();
230 Fit();
231 }
232
233 void EffectEditor::save()
234 {
235 // Request each argument item to save the content into the argument
236 for (std::size_t i = 0; i < _argumentItems.size(); ++i)
237 {
238 _argumentItems[i]->save();
239 }
240
241 // Call the update routine of the parent editor
242 _editor.update();
243 }
244
245 // Traverse the scenegraph to populate the tree model
246 void EffectEditor::populateEntityListStore()
247 {
248 _entityChoices.Clear();
249
250 std::string selfEntity = game::current::getValue<std::string>(GKEY_ENTITY_SELF);
251
252 // Append the name to the list store
253 _entityChoices.Add(selfEntity);
254
255 // Create a scenegraph walker to traverse the graph
256 class EntityFinder :
257 public scene::NodeVisitor
258 {
259 // List to add names to
260 wxArrayString& _list;
261 public:
262 // Constructor
263 EntityFinder(wxArrayString& list) :
264 _list(list)
265 {}
266
267 // Visit function
268 bool pre(const scene::INodePtr& node)
269 {
270 Entity* entity = Node_getEntity(node);
271
272 if (entity != NULL)
273 {
274 // Get the entity name and append it to the list store
275 _list.Add(entity->getKeyValue("name"));
276
277 return false; // don't traverse children if entity found
278 }
279
280 return true; // traverse children otherwise
281 }
282 } finder(_entityChoices);
283
284 GlobalSceneGraph().root()->traverse(finder);
285 }
286
287 void EffectEditor::revert()
288 {
289 _response.getResponseEffect(_effectIndex) = _backup;
290 }
291
292 void EffectEditor::onStateToggle(wxCommandEvent& ev)
293 {
294 _response.getResponseEffect(_effectIndex).setActive(_stateToggle->GetValue());
295 }
296
297 void EffectEditor::onEffectTypeChange(wxCommandEvent& ev)
298 {
299 effectTypeChanged();
300 }
301
302 } // namespace ui