Debian Codegraph @

plugins/dm.editing/SpawnargLinkedCheckbox.h

raw
1 #pragma once
2
3 #include "iundo.h"
4 #include "ieclass.h"
5 #include "ientity.h"
6 #include <wx/checkbox.h>
7
8 namespace ui
9 {
10
11 /**
12 * An enhanced checkbox that is updating the named
13 * entity property (spawnarg) when toggled.
14 *
15 * The logic toggled = "1" can be optionally inversed such that
16 * an unchecked box reflects a property value of "1".
17 */
18 class SpawnargLinkedCheckbox :
19 public wxCheckBox
20 {
21 private:
22 bool _inverseLogic;
23
24 std::string _propertyName;
25
26 Entity* _entity;
27
28 bool _updateLock;
29
30 bool _defaultValueForMissingKeyValue;
31
32 public:
33 SpawnargLinkedCheckbox(wxWindow* parent, const std::string& label,
34 const std::string& propertyName,
35 bool inverseLogic = false) :
36 wxCheckBox(parent, wxID_ANY, label),
37 _inverseLogic(inverseLogic),
38 _propertyName(propertyName),
39 _entity(NULL),
40 _updateLock(false),
41 _defaultValueForMissingKeyValue(false)
42 {
43 Connect(wxEVT_CHECKBOX, wxCommandEventHandler(SpawnargLinkedCheckbox::onToggle), NULL, this);
44 }
45
46 void setDefaultValueForMissingKeyValue(bool defaultValue)
47 {
48 _defaultValueForMissingKeyValue = defaultValue;
49 }
50
51 // Sets the edited Entity object
52 void setEntity(Entity* entity)
53 {
54 _entity = entity;
55
56 if (_entity == NULL)
57 {
58 SetToolTip("");
59 return;
60 }
61
62 SetToolTip(_propertyName + ": "
63 + _entity->getEntityClass()->getAttributeDescription(_propertyName));
64
65 std::string keyValue = _entity->getKeyValue(_propertyName);
66
67 bool value = _entity->getKeyValue(_propertyName) == "1";
68
69 // Missing spawnargs (value is empty) get the default value assigned
70 if (keyValue.empty())
71 {
72 value = _defaultValueForMissingKeyValue;
73 }
74
75 _updateLock = true;
76 SetValue(_inverseLogic ? !value : value);
77 _updateLock = false;
78 }
79
80 protected:
81 void onToggle(wxCommandEvent& ev)
82 {
83 ev.Skip();
84
85 // Update the spawnarg if we have a valid entity
86 if (!_updateLock && _entity != NULL)
87 {
88 UndoableCommand cmd("editAIProperties");
89
90 std::string newValue = "";
91
92 if (_inverseLogic)
93 {
94 newValue = GetValue() ? "0" : "1"; // Active => "0"
95 }
96 else
97 {
98 newValue = GetValue() ? "1" : "0";
99 }
100
101 // Check if the new value conincides with an inherited one
102 if (_entity->getEntityClass()->getAttributeValue(_propertyName) == newValue)
103 {
104 // in which case the property just gets removed from the entity
105 newValue = "";
106 }
107
108 _entity->setKeyValue(_propertyName, newValue);
109 }
110 }
111 };
112
113 } // namespace