Debian Codegraph @

plugins/dm.editing/AIEditingPanel.cpp

raw
1 #include "AIEditingPanel.h"
2
3 #include "i18n.h"
4 #include "iselection.h"
5 #include "ieclass.h"
6 #include "imap.h"
7 #include "itextstream.h"
8 #include "ui/ientityinspector.h"
9 #include "iundo.h"
10 #include "selectionlib.h"
11
12 #include "SpawnargLinkedCheckbox.h"
13 #include "SpawnargLinkedSpinButton.h"
14
15 #include <wx/stattext.h>
16 #include <wx/sizer.h>
17 #include <wx/button.h>
18 #include "wxutil/Bitmap.h"
19 #include <wx/panel.h>
20 #include <wx/scrolwin.h>
21
22 #include <functional>
23
24 namespace ui
25 {
26
27 AIEditingPanel::AIEditingPanel(wxWindow* parent) :
28 DockablePanel(parent),
29 _mainPanel(new wxScrolledWindow(this, wxID_ANY)),
30 _entity(nullptr),
31 _rescanSelectionOnIdle(true)
32 {
33 SetSizer(new wxBoxSizer(wxVERTICAL));
34 GetSizer()->Add(_mainPanel, 1, wxEXPAND);
35
36 constructWidgets();
37 }
38
39 AIEditingPanel::~AIEditingPanel()
40 {
41 if (panelIsActive())
42 {
43 disconnectListeners();
44 }
45 }
46
47 void AIEditingPanel::onPanelActivated()
48 {
49 connectListeners();
50
51 _rescanSelectionOnIdle = true;
52 requestIdleCallback();
53 }
54
55 void AIEditingPanel::onPanelDeactivated()
56 {
57 disconnectListeners();
58 }
59
60 void AIEditingPanel::connectListeners()
61 {
62 _selectionChangedSignal = GlobalSelectionSystem().signal_selectionChanged().connect(
63 sigc::mem_fun(*this, &AIEditingPanel::onSelectionChanged)
64 );
65
66 _undoHandler = GlobalMapModule().signal_postUndo().connect(
67 sigc::mem_fun(*this, &AIEditingPanel::updateWidgetsFromSelection));
68 _redoHandler = GlobalMapModule().signal_postRedo().connect(
69 sigc::mem_fun(*this, &AIEditingPanel::updateWidgetsFromSelection));
70 }
71
72 void AIEditingPanel::disconnectListeners()
73 {
74 _undoHandler.disconnect();
75 _redoHandler.disconnect();
76
77 _selectionChangedSignal.disconnect();
78
79 if (_entity != nullptr)
80 {
81 _entity->detachObserver(this);
82 _entity = nullptr;
83 }
84 }
85
86 void AIEditingPanel::onIdle()
87 {
88 if (_rescanSelectionOnIdle)
89 {
90 _rescanSelectionOnIdle = false;
91 rescanSelection();
92 }
93 }
94
95 void AIEditingPanel::constructWidgets()
96 {
97 _mainPanel->SetScrollRate(0, 3);
98 _mainPanel->SetSizer(new wxBoxSizer(wxVERTICAL));
99
100 wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
101 _mainPanel->GetSizer()->Add(vbox, 1, wxEXPAND | wxALL, 12);
102
103 // Populate the map with all the widgets linked to 1/0 spawnargs
104 _checkboxes["canOperateDoors"] = new SpawnargLinkedCheckbox(_mainPanel, _("Can operate Doors"), "canOperateDoors");
105 _checkboxes["canLightTorches"] = new SpawnargLinkedCheckbox(_mainPanel, _("Can light Torches"), "canLightTorches");
106 _checkboxes["canOperateSwitchLights"] = new SpawnargLinkedCheckbox(_mainPanel, _("Can operate Switch Lights"), "canOperateSwitchLights");
107 _checkboxes["canOperateElevators"] = new SpawnargLinkedCheckbox(_mainPanel, _("Can operate Elevators"), "canOperateElevators");
108 _checkboxes["canGreet"] = new SpawnargLinkedCheckbox(_mainPanel, _("Can greet others"), "canGreet");
109 _checkboxes["canSearch"] = new SpawnargLinkedCheckbox(_mainPanel, _("Can search"), "canSearch");
110 _checkboxes["is_civilian"] = new SpawnargLinkedCheckbox(_mainPanel, _("AI is civilian"), "is_civilian");
111 _checkboxes["sleeping"] = new SpawnargLinkedCheckbox(_mainPanel, _("Start sleeping"), "sleeping");
112 _checkboxes["lay_down_left"] = new SpawnargLinkedCheckbox(_mainPanel, _("Lay down to the left"), "lay_down_left");
113 _checkboxes["sitting"] = new SpawnargLinkedCheckbox(_mainPanel, _("Start sitting"), "sitting");
114 _checkboxes["patrol"] = new SpawnargLinkedCheckbox(_mainPanel, _("Patrol"), "patrol");
115 _checkboxes["animal_patrol"] = new SpawnargLinkedCheckbox(_mainPanel, _("Animal Patrol Mode"), "animal_patrol");
116 _checkboxes["alert_idle"] = new SpawnargLinkedCheckbox(_mainPanel, _("Start in Alert Idle State"), "alert_idle");
117 _checkboxes["disable_alert_idle"] = new SpawnargLinkedCheckbox(_mainPanel, _("Disable Alert Idle State"), "disable_alert_idle");
118 _checkboxes["drunk"] = new SpawnargLinkedCheckbox(_mainPanel, _("Drunk"), "drunk");
119 _checkboxes["shoulderable"] = new SpawnargLinkedCheckbox(_mainPanel, _("Body is shoulderable"), "shoulderable");
120 _checkboxes["neverdormant"] = new SpawnargLinkedCheckbox(_mainPanel, _("AI doesn't think outside the player PVS"), "neverdormant", true); // inverse logic
121
122 _checkboxes["can_drown"] = new SpawnargLinkedCheckbox(_mainPanel, _("AI can drown"), "can_drown");
123 _checkboxes["can_drown"]->setDefaultValueForMissingKeyValue(true);
124
125 _checkboxes["can_be_flatfooted"] = new SpawnargLinkedCheckbox(_mainPanel, _("AI can be flatfooted"), "can_be_flatfooted");
126 _checkboxes["ko_alert_immune"] = new SpawnargLinkedCheckbox(_mainPanel, _("AI is immune to KOs at high alert levels"), "ko_alert_immune");
127 _checkboxes["ko_immune"] = new SpawnargLinkedCheckbox(_mainPanel, _("AI is immune to KOs"), "ko_immune");
128 _checkboxes["gas_immune"] = new SpawnargLinkedCheckbox(_mainPanel, _("AI is immune to Gas"), "gas_immune");
129
130 _spinButtons["team"] = new SpawnargLinkedSpinButton(_mainPanel, _("Team"), "team", 0, 99, 1, 0);
131 _spinButtons["rank"] = new SpawnargLinkedSpinButton(_mainPanel, _("Rank"), "rank", 0, 99, 1, 0);
132 _spinButtons["sit_down_angle"] = new SpawnargLinkedSpinButton(_mainPanel, _("Sitting Angle"), "sit_down_angle", -179, 180, 1, 0);
133 _spinButtons["drunk_acuity_factor"] = new SpawnargLinkedSpinButton(_mainPanel, _("Drunk Acuity Factor"), "drunk_acuity_factor", 0, 10, 0.1, 2);
134 _spinButtons["acuity_vis"] = new SpawnargLinkedSpinButton(_mainPanel, _("Visual Acuity"), "acuity_vis", 0, 100, 1, 0);
135 _spinButtons["acuity_aud"] = new SpawnargLinkedSpinButton(_mainPanel, _("Audio Acuity"), "acuity_aud", 0, 100, 1, 0);
136
137 _spinButtons["fov"] = new SpawnargLinkedSpinButton(_mainPanel, _("Horizontal FOV"), "fov", 0, 360, 1, 0);
138 _spinButtons["fov_vert"] = new SpawnargLinkedSpinButton(_mainPanel, _("Vertical FOV"), "fov_vert", 0, 180, 1, 0);
139
140 _spinButtons["min_interleave_think_dist"] = new SpawnargLinkedSpinButton(_mainPanel, _("Min. Interleave Distance"), "min_interleave_think_dist", 0, 60000, 50, 0);
141 _spinButtons["max_interleave_think_dist"] = new SpawnargLinkedSpinButton(_mainPanel, _("Max. Interleave Distance"), "max_interleave_think_dist", 0, 60000, 50, 0);
142
143 _spinButtons["health"] = new SpawnargLinkedSpinButton(_mainPanel, _("Health"), "health", 0, 9999999, 5, 0);
144 _spinButtons["health_critical"] = new SpawnargLinkedSpinButton(_mainPanel, _("Critical Health"), "health_critical", 0, 1000, 5, 0);
145 _spinButtons["melee_range"] = new SpawnargLinkedSpinButton(_mainPanel, _("Melee Range"), "melee_range", 0, 200, 1, 0);
146
147 {
148 // Appearance widgets
149 vbox->Add(createSectionLabel(_("Appearance")), 0, wxTOP | wxBOTTOM, 6);
150
151 wxFlexGridSizer* table = new wxFlexGridSizer(3, 3, 4, 12);
152 vbox->Add(table, 0, wxEXPAND | wxLEFT, 18);
153
154 createChooserRow(table, _("Skin: "), _("Choose skin..."), "icon_skin.png", "skin");
155 createChooserRow(table, _("Head: "), _("Choose AI head..."), "icon_model.png", "def_head");
156 createChooserRow(table, _("Vocal Set: "), _("Choose Vocal Set..."), "icon_sound.png", "def_vocal_set");
157 }
158
159 {
160 // Behaviour widgets
161 vbox->Add(createSectionLabel(_("Behaviour")), 0, wxTOP | wxBOTTOM, 6);
162
163 wxGridSizer* table = new wxGridSizer(10, 2, 4, 12);
164 vbox->Add(table, 0, wxLEFT, 18);
165
166 // Team & Rank
167 table->Add(createSpinButtonHbox(_spinButtons["team"]), 0, wxALIGN_CENTER_VERTICAL | wxEXPAND);
168 table->Add(createSpinButtonHbox(_spinButtons["rank"]), 0, wxALIGN_CENTER_VERTICAL | wxEXPAND);
169
170 // Civilian flag
171 table->Add(_checkboxes["is_civilian"], 0, wxALIGN_CENTER_VERTICAL);
172 table->Add(new wxPanel(_mainPanel, wxID_ANY), 0); // empty dummy
173
174 // Sitting
175 table->Add(_checkboxes["sitting"], 0, wxALIGN_CENTER_VERTICAL);
176 table->Add(createSpinButtonHbox(_spinButtons["sit_down_angle"]), 0, wxALIGN_CENTER_VERTICAL | wxEXPAND);
177
178 // Sleeping
179 table->Add(_checkboxes["sleeping"], 0, wxALIGN_CENTER_VERTICAL);
180 table->Add(_checkboxes["lay_down_left"], 0, wxALIGN_CENTER_VERTICAL);
181
182 // Patrolling
183 table->Add(_checkboxes["patrol"], 0, wxALIGN_CENTER_VERTICAL);
184 table->Add(_checkboxes["animal_patrol"], 0, wxALIGN_CENTER_VERTICAL);
185
186 // Acuity
187 table->Add(createSpinButtonHbox(_spinButtons["acuity_vis"]), 0, wxALIGN_CENTER_VERTICAL | wxEXPAND);
188 table->Add(createSpinButtonHbox(_spinButtons["acuity_aud"]), 0, wxALIGN_CENTER_VERTICAL | wxEXPAND);
189
190 // FOV
191 table->Add(createSpinButtonHbox(_spinButtons["fov"]), 0, wxALIGN_CENTER_VERTICAL | wxEXPAND);
192 table->Add(createSpinButtonHbox(_spinButtons["fov_vert"]), 0, wxALIGN_CENTER_VERTICAL | wxEXPAND);
193
194 // Drunk
195 table->Add(_checkboxes["drunk"], 0, wxALIGN_CENTER_VERTICAL);
196 table->Add(createSpinButtonHbox(_spinButtons["drunk_acuity_factor"]), 0, wxALIGN_CENTER_VERTICAL | wxEXPAND);
197
198 // Alert Idle Control
199 table->Add(_checkboxes["alert_idle"], 0, wxALIGN_CENTER_VERTICAL);
200 table->Add(_checkboxes["disable_alert_idle"], 0, wxALIGN_CENTER_VERTICAL);
201
202 table->Add(_checkboxes["shoulderable"], 0, wxALIGN_CENTER_VERTICAL);
203 }
204
205 {
206 // Abilities widgets
207 vbox->Add(createSectionLabel(_("Abilities")), 0, wxTOP | wxBOTTOM, 6);
208
209 wxGridSizer* table = new wxGridSizer(3, 2, 4, 12);
210 vbox->Add(table, 0, wxLEFT, 18);
211
212 table->Add(_checkboxes["canOperateDoors"], 0, wxALIGN_CENTER_VERTICAL);
213 table->Add(_checkboxes["canOperateElevators"], 0, wxALIGN_CENTER_VERTICAL);
214 table->Add(_checkboxes["canLightTorches"], 0, wxALIGN_CENTER_VERTICAL);
215 table->Add(_checkboxes["canOperateSwitchLights"], 0, wxALIGN_CENTER_VERTICAL);
216 table->Add(_checkboxes["canGreet"], 0, wxALIGN_CENTER_VERTICAL);
217 table->Add(_checkboxes["canSearch"], 0, wxALIGN_CENTER_VERTICAL);
218 }
219
220 {
221 // Optimization widgets
222 vbox->Add(createSectionLabel(_("Optimization")), 0, wxTOP | wxBOTTOM, 6);
223
224 wxGridSizer* table = new wxGridSizer(3, 1, 4, 12);
225 vbox->Add(table, 0, wxLEFT, 18);
226
227 table->Add(_checkboxes["neverdormant"], 0, wxALIGN_CENTER_VERTICAL);
228 table->Add(createSpinButtonHbox(_spinButtons["min_interleave_think_dist"]), 0, wxALIGN_CENTER_VERTICAL | wxEXPAND);
229 table->Add(createSpinButtonHbox(_spinButtons["max_interleave_think_dist"]), 0, wxALIGN_CENTER_VERTICAL | wxEXPAND);
230 }
231
232 {
233 // Health / Combat widgets
234 vbox->Add(createSectionLabel(_("Health / Combat")), 0, wxTOP | wxBOTTOM, 6);
235
236 wxGridSizer* table = new wxGridSizer(5, 2, 4, 12);
237 vbox->Add(table, 0, wxLEFT, 18);
238
239 table->Add(createSpinButtonHbox(_spinButtons["health"]), 0, wxALIGN_CENTER_VERTICAL | wxEXPAND);
240 table->Add(createSpinButtonHbox(_spinButtons["health_critical"]), 0, wxALIGN_CENTER_VERTICAL | wxEXPAND);
241
242 table->Add(createSpinButtonHbox(_spinButtons["melee_range"]), 0, wxALIGN_CENTER_VERTICAL | wxEXPAND);
243
244 table->Add(_checkboxes["can_drown"], 0, wxALIGN_CENTER_VERTICAL);
245 table->Add(_checkboxes["can_be_flatfooted"], 0, wxALIGN_CENTER_VERTICAL);
246
247 table->Add(_checkboxes["ko_immune"], 0, wxALIGN_CENTER_VERTICAL);
248 table->Add(_checkboxes["gas_immune"], 0, wxALIGN_CENTER_VERTICAL);
249
250 table->Add(_checkboxes["ko_alert_immune"], 0, wxALIGN_CENTER_VERTICAL);
251 }
252 }
253
254 wxSizer* AIEditingPanel::createSpinButtonHbox(SpawnargLinkedSpinButton* spinButton)
255 {
256 wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
257
258 wxStaticText* label = new wxStaticText(spinButton->GetParent(), wxID_ANY, spinButton->getLabel() + ":");
259 hbox->Add(label, 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, 6);
260 hbox->Add(spinButton, 0, wxALIGN_CENTER_VERTICAL);
261
262 return hbox;
263 }
264
265 void AIEditingPanel::createChooserRow(wxSizer* table, const std::string& rowLabel,
266 const std::string& buttonLabel, const std::string& buttonIcon,
267 const std::string& key)
268 {
269 table->Add(new wxStaticText(_mainPanel, wxID_ANY, rowLabel), 0, wxALIGN_CENTER_VERTICAL);
270
271 _labels[key] = new wxStaticText(_mainPanel, wxID_ANY, "");
272 table->Add(_labels[key], 1, wxALIGN_CENTER_VERTICAL);
273
274 // Create the skin browse button
275 wxButton* browseButton = new wxButton(_mainPanel, wxID_ANY, buttonLabel);
276 browseButton->SetBitmap(wxutil::GetLocalBitmap(buttonIcon));
277 browseButton->Bind(wxEVT_BUTTON, std::bind(&AIEditingPanel::onBrowseButton, this, std::placeholders::_1, key));
278
279 table->Add(browseButton, 0, wxALIGN_RIGHT);
280 }
281
282 wxStaticText* AIEditingPanel::createSectionLabel(const std::string& text)
283 {
284 wxStaticText* label = new wxStaticText(_mainPanel, wxID_ANY, text);
285 label->SetFont(label->GetFont().Bold());
286
287 return label;
288 }
289
290 Entity* AIEditingPanel::getEntityFromSelection()
291 {
292 Entity* entity = NULL;
293
294 const SelectionInfo& info = GlobalSelectionSystem().getSelectionInfo();
295
296 if (info.entityCount == 1 && info.totalCount == info.entityCount)
297 {
298 GlobalSelectionSystem().foreachSelected([&] (const scene::INodePtr& node)
299 {
300 Entity* candidate = Node_getEntity(node);
301
302 if (candidate != nullptr && candidate->isOfType("atdm:ai_base"))
303 {
304 entity = candidate;
305 }
306 });
307 }
308
309 return entity;
310 }
311
312 void AIEditingPanel::updatePanelSensitivity()
313 {
314 _mainPanel->Enable(_entity != nullptr);
315 _mainPanel->Layout();
316 }
317
318 void AIEditingPanel::onKeyInsert(const std::string& key, EntityKeyValue& value)
319 {
320 // On entity key change, we load all values afresh
321 updateWidgetsFromSelection();
322 }
323
324 void AIEditingPanel::onKeyChange(const std::string& key, const std::string& val)
325 {
326 // On entity key change, we load all values afresh
327 updateWidgetsFromSelection();
328 }
329
330 void AIEditingPanel::onKeyErase(const std::string& key, EntityKeyValue& value)
331 {
332 // On entity key change, we load all values afresh
333 updateWidgetsFromSelection();
334 }
335
336 void AIEditingPanel::postUndo()
337 {
338 updateWidgetsFromSelection();
339 }
340
341 void AIEditingPanel::postRedo()
342 {
343 updateWidgetsFromSelection();
344 }
345
346 void AIEditingPanel::updateWidgetsFromSelection()
347 {
348 // Write the entity object to each checkbox even if it is NULL
349 // This will update the widget state from the entity's properties
350 std::for_each(_checkboxes.begin(), _checkboxes.end(), [&] (CheckboxMap::value_type& pair)
351 {
352 pair.second->setEntity(_entity);
353 });
354
355 std::for_each(_spinButtons.begin(), _spinButtons.end(), [&] (SpinButtonMap::value_type& pair)
356 {
357 pair.second->setEntity(_entity);
358 });
359
360 // Some dependencies
361 _checkboxes["lay_down_left"]->Enable(_checkboxes["sleeping"]->GetValue());
362 _spinButtons["sit_down_angle"]->Enable(_checkboxes["sitting"]->GetValue());
363 _spinButtons["drunk_acuity_factor"]->Enable(_checkboxes["drunk"]->GetValue());
364
365 std::for_each(_labels.begin(), _labels.end(), [&] (LabelMap::value_type& pair)
366 {
367 pair.second->SetLabelText(_entity != nullptr ? _entity->getKeyValue(pair.first) : "");
368 });
369 }
370
371 void AIEditingPanel::rescanSelection()
372 {
373 // Load the new entity from the selection
374 auto newEntity = getEntityFromSelection();
375
376 if (newEntity != _entity)
377 {
378 // Disconnect from any previous entity
379 if (_entity != nullptr)
380 {
381 _entity->detachObserver(this);
382 _entity = nullptr; // issue #4282
383 }
384
385 if (newEntity != nullptr)
386 {
387 _entity = newEntity;
388 _entity->attachObserver(this);
389 }
390 }
391
392 updatePanelSensitivity();
393 updateWidgetsFromSelection();
394 }
395
396 void AIEditingPanel::onSelectionChanged(const ISelectable&)
397 {
398 _rescanSelectionOnIdle = true;
399 requestIdleCallback();
400 }
401
402 void AIEditingPanel::onBrowseButton(wxCommandEvent& ev, const std::string& key)
403 {
404 if (_entity == nullptr) return;
405
406 // Look up the property editor dialog
407 auto dialog = GlobalEntityInspector().createDialog(key);
408
409 if (dialog)
410 {
411 std::string oldValue = _entity->getKeyValue(key);
412 std::string newValue = dialog->runDialog(_entity, key);
413
414 if (newValue != oldValue)
415 {
416 UndoableCommand cmd("editAIProperty");
417 _entity->setKeyValue(key, newValue);
418 _mainPanel->Layout();
419 }
420 }
421 else
422 {
423 rError() << "Could not find a property editor implementing the IPropertyEditorDialog interface for key "
424 << key << std::endl;
425 }
426 }
427
428 } // namespace