Debian Codegraph @

plugins/dm.stimresponse/StimResponse.cpp

raw
1 #include "StimResponse.h"
2
3 #include "string/string.h"
4 #include <iostream>
5
6 StimResponse::StimResponse() :
7 _inherited(false),
8 _index(0)
9 {}
10
11 // Copy constructor
12 StimResponse::StimResponse(const StimResponse& other) :
13 _inherited(other._inherited),
14 _properties(other._properties),
15 _index(other._index)
16 {}
17
18 /** greebo: Gets the property value string or "" if not defined/empty
19 */
20 std::string StimResponse::get(const std::string& key) {
21
22 PropertyMap::iterator i = _properties.find(key);
23
24 if (i != _properties.end()) {
25 return _properties[key].value;
26 }
27 else {
28 // Not found, return an empty string
29 return "";
30 }
31 }
32
33 int StimResponse::getIndex() const {
34 return _index;
35 }
36
37 void StimResponse::setIndex(int index) {
38 if (!_inherited) {
39 _index = index;
40 }
41 }
42
43 void StimResponse::set(const std::string& key, const std::string& value, bool inherited) {
44 if (_inherited && !inherited) {
45 // This is an override operation
46 if (_properties.find(key) != _properties.end()) {
47 // Value exists, write to the <value> member
48 _properties[key].value = value;
49 }
50 else {
51 // Value doesn't exist yet, initialise and write an empty "backup" value
52 // This indicates that the value was not set in the inheritance tree
53 Property p;
54 p.value = value;
55 p.origValue = "";
56 _properties[key] = p;
57 }
58 }
59 else {
60 // Ordinary behaviour (just write the value)
61 Property p;
62 p.value = value;
63 p.origValue = value;
64 _properties[key] = p;
65 }
66 }
67
68 bool StimResponse::isOverridden(const std::string& key) {
69 if (_properties.find(key) != _properties.end()) {
70 // Key exists, compare origValue and value
71 // If these are not equal, the key has been overridden
72 return (_properties[key].value != _properties[key].origValue);
73 }
74 return false;
75 }
76
77 void StimResponse::setInherited(bool inherited) {
78 _inherited = inherited;
79 }
80
81 bool StimResponse::inherited() const {
82 return _inherited;
83 }
84
85 ResponseEffect& StimResponse::getResponseEffect(const unsigned int index) {
86 EffectMap::iterator found = _effects.find(index);
87
88 if (found == _effects.end()) {
89 // ResponseEffect doesn't exist yet, create a new, empty one
90 _effects[index] = ResponseEffect();
91 _effects[index].setInherited(_inherited);
92 }
93
94 return _effects[index];
95 }
96
97 void StimResponse::sortEffects() {
98 EffectMap newMap;
99
100 // Re-index the effects to avoid gaps in the indexing
101 int newIndex = 1;
102 for (EffectMap::iterator i = _effects.begin();
103 i != _effects.end();
104 i++, newIndex++)
105 {
106 // Copy the visited ResponseEffect to the new index
107 newMap[newIndex] = i->second;
108 }
109
110 // Replace the old map with the sorted one
111 _effects = newMap;
112 }
113
114 void StimResponse::moveEffect(const unsigned int fromIndex,
115 const unsigned int toIndex)
116 {
117 EffectMap::iterator from = _effects.find(fromIndex);
118 EffectMap::iterator to = _effects.find(toIndex);
119
120 if (from != _effects.end() && to != _effects.end()) {
121 // Copy the ResponseEffects from the map
122 ResponseEffect fromEffect = from->second;
123 ResponseEffect toEffect = to->second;
124
125 // Write them back at the swapped locations
126 _effects[fromIndex] = toEffect;
127 _effects[toIndex] = fromEffect;
128 }
129 }
130
131 unsigned int StimResponse::highestEffectIndex() {
132 unsigned int returnValue = 0;
133
134 // Search for the highest index
135 for (EffectMap::iterator i = _effects.begin(); i != _effects.end(); i++) {
136 if (i->first > returnValue) {
137 returnValue = i->first;
138 }
139 }
140
141 return returnValue;
142 }
143
144 void StimResponse::addEffect(const unsigned int index) {
145 // Resort the effects, it may be unsorted when loaded fresh from the entity
146 sortEffects();
147
148 EffectMap::iterator found = _effects.find(index);
149
150 if (found == _effects.end()) {
151 unsigned int newIndex = highestEffectIndex() + 1;
152 // No item found (index could be -1), append to the end of the list
153 _effects[newIndex] = ResponseEffect();
154 _effects[newIndex].setInherited(_inherited);
155 _effects[newIndex].setName(
156 ResponseEffectTypes::Instance().getFirstEffectName()
157 );
158 }
159 else {
160 EffectMap newMap;
161
162 // Traverse the current effect list from back to front and
163 // increase all indices >= index and insert a new effect
164 for (EffectMap::reverse_iterator i = _effects.rbegin();
165 i != _effects.rend();
166 i++)
167 {
168 // Increase all indices >= index
169 if (i->first >= index) {
170 // Store the item to index + 1
171 newMap[i->first + 1] = i->second;
172 }
173 else {
174 // All smaller indices get copied
175 newMap[i->first] = i->second;
176 }
177
178 // If we are exactly at the insert point, insert the new effect
179 if (i->first == index) {
180 newMap[i->first] = ResponseEffect();
181 newMap[i->first].setInherited(_inherited);
182 newMap[i->first].setName(
183 ResponseEffectTypes::Instance().getFirstEffectName()
184 );
185 }
186 }
187
188 // Replace the old map with the new one
189 _effects = newMap;
190 }
191 }
192
193 void StimResponse::deleteEffect(const unsigned int index) {
194 EffectMap::iterator found = _effects.find(index);
195
196 if (found != _effects.end()) {
197 // Remove the item from the map
198 _effects.erase(found);
199 }
200
201 // Re-index the effects in the map
202 sortEffects();
203 }
204
205 StimResponse::EffectMap& StimResponse::getEffects() {
206 return _effects;
207 }
208
209 wxutil::TreeModel::Ptr StimResponse::createEffectsStore()
210 {
211 const Columns& columns = getColumns();
212
213 _effectStore = new wxutil::TreeModel(columns, true);
214
215 for (EffectMap::iterator i = _effects.begin(); i != _effects.end(); ++i)
216 {
217 wxutil::TreeModel::Row row = _effectStore->AddItem();
218
219 // Store the ID into the liststore
220 row[columns.index] = static_cast<int>(i->first);
221 row[columns.caption] = i->second.getCaption();
222 row[columns.arguments] = i->second.getArgumentStr();
223 }
224
225 return _effectStore;
226 }
227
228 const StimResponse::Columns& StimResponse::getColumns()
229 {
230 static Columns _columns;
231 return _columns;
232 }