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