Debian Codegraph @

plugins/dm.stimresponse/SREntity.cpp

raw
1 #include "SREntity.h"
2
3 #include "iregistry.h"
4 #include "itextstream.h"
5 #include "ieclass.h"
6 #include "igame.h"
7 #include "entitylib.h"
8
9 #include "SRPropertyLoader.h"
10 #include "SRPropertyRemover.h"
11 #include "SRPropertySaver.h"
12
13 #include <iostream>
14 #include "wxutil/Bitmap.h"
15 #include "wxutil/Icon.h"
16
17 namespace
18 {
19 const char* const GKEY_STIM_PROPERTIES = "/stimResponseSystem/properties//property";
20 }
21
22 SREntity::SREntity(Entity* source, StimTypes& stimTypes) :
23 _stimStore(new wxutil::TreeModel(getColumns(), true)),
24 _responseStore(new wxutil::TreeModel(getColumns(), true)),
25 _stimTypes(stimTypes)
26 {
27 loadKeys();
28 load(source);
29 }
30
31 int SREntity::getHighestIndex()
32 {
33 int index = 0;
34
35 for (const auto& i : _list)
36 {
37 if (i.getIndex() > index)
38 {
39 index = i.getIndex();
40 }
41 }
42
43 return index;
44 }
45
46 int SREntity::getHighestInheritedIndex()
47 {
48 int index = 0;
49
50 for (const auto& i : _list)
51 {
52 if (i.inherited() && i.getIndex() > index)
53 {
54 index = i.getIndex();
55 }
56 }
57
58 return index;
59 }
60
61 void SREntity::load(Entity* source)
62 {
63 // Clear all the items from the liststore
64 _stimStore->Clear();
65 _responseStore->Clear();
66
67 if (source == nullptr)
68 {
69 return;
70 }
71
72 // Get the entity class to scan the inherited values
73 IEntityClassPtr eclass = GlobalEntityClassManager().findOrInsert(
74 source->getKeyValue("classname"), true
75 );
76
77 // Instantiate a visitor class with the list of possible keys
78 // and the target list where all the S/Rs are stored
79 // Warning messages are stored in the <_warnings> string
80 SRPropertyLoader visitor(_keys, *this, _warnings);
81 eclass->forEachAttribute(std::ref(visitor));
82
83 // Scan the entity itself after the class has been searched
84 source->forEachKeyValue([&](const std::string& key, const std::string& value)
85 {
86 visitor.visitKeyValue(key, value);
87 });
88
89 // Populate the liststore
90 updateListStores();
91 }
92
93 void SREntity::remove(int index)
94 {
95 auto found = findByIndex(index);
96
97 if (found == _list.end() || found->inherited())
98 {
99 return;
100 }
101
102 _list.erase(found);
103
104 // Re-arrange the S/R indices. Otherwise he parser in the game engine
105 // walks up the indices and stops on the first missing index
106 // not noticing there might be higher indices to follow (#5193)
107 int highestInheritedIndex = getHighestInheritedIndex();
108 int nextIndex = highestInheritedIndex + 1;
109
110 // Iterate over all non-inherited objects and assign new indices
111 for (auto i = _list.begin(); i != _list.end(); ++i)
112 {
113 if (!i->inherited())
114 {
115 // Found a non-inherited stim, assign the index
116 i->setIndex(nextIndex++);
117 }
118 }
119
120 updateListStores();
121 }
122
123 int SREntity::duplicate(int fromIndex)
124 {
125 auto found = findByIndex(fromIndex);
126
127 if (found != _list.end())
128 {
129 int index = getHighestIndex() + 1;
130
131 // Copy the object to the new id
132 _list.push_back(StimResponse(*found));
133
134 // Set the index and the inheritance status
135 _list.back().setInherited(false);
136 _list.back().setIndex(index);
137
138 // Rebuild the liststores
139 updateListStores();
140
141 return index;
142 }
143
144 return -1;
145 }
146
147 void SREntity::updateListStores()
148 {
149 // Clear all the items from the liststore
150 _stimStore->Clear();
151 _responseStore->Clear();
152
153 // Now populate the liststore
154 for (auto& sr : _list)
155 {
156 auto row = sr.get("class") == "S" ? _stimStore->AddItem() : _responseStore->AddItem();
157
158 // Store the index into the liststore
159 row[getColumns().index] = sr.getIndex();
160
161 writeToListRow(row, sr);
162
163 row.SendItemAdded();
164 }
165 }
166
167 int SREntity::add()
168 {
169 return add(getHighestIndex() + 1).getIndex();
170 }
171
172 StimResponse& SREntity::add(int index)
173 {
174 // Create a new StimResponse object
175 _list.push_back(StimResponse());
176
177 // Set the index and the inheritance status
178 _list.back().setInherited(false);
179 _list.back().setIndex(index);
180 _list.back().set("class", "S");
181
182 return _list.back();
183 }
184
185 void SREntity::cleanEntity(Entity* target)
186 {
187 // Clean the entity from all the S/R spawnargs
188 SRPropertyRemover remover(target, _keys);
189 target->forEachKeyValue([&](const std::string& key, const std::string& value)
190 {
191 remover.visitKeyValue(key, value);
192 });
193
194 // scope ends here, SRPropertyRemover's destructor
195 // will now delete the keys
196 }
197
198 void SREntity::save(Entity* target)
199 {
200 if (target == nullptr)
201 {
202 return;
203 }
204
205 // Remove the S/R spawnargs from the entity
206 cleanEntity(target);
207
208 // Setup the saver object
209 SRPropertySaver saver(target, _keys);
210
211 for (auto& i : _list)
212 {
213 saver.visit(i);
214 }
215 }
216
217 wxDataViewItem SREntity::getIterForIndex(wxutil::TreeModel& targetStore, int index)
218 {
219 return targetStore.FindInteger(index, getColumns().index);
220 }
221
222 void SREntity::writeToListRow(wxutil::TreeModel::Row& row, StimResponse& sr)
223 {
224 StimType stimType = _stimTypes.get(sr.get("type"));
225
226 std::string stimTypeStr = stimType.caption;
227 stimTypeStr += (sr.inherited()) ? " (inherited) " : "";
228
229 std::string classIcon = (sr.get("class") == "R") ? ICON_RESPONSE : ICON_STIM;
230 classIcon += (sr.inherited()) ? SUFFIX_INHERITED : "";
231 classIcon += (sr.get("state") != "1") ? SUFFIX_INACTIVE : "";
232 classIcon += SUFFIX_EXTENSION;
233
234 wxutil::Icon icon(wxutil::GetLocalBitmap(stimType.icon));
235
236 wxDataViewItemAttr colour;
237 colour.SetColour(sr.inherited() ? wxColor(112,112,112) : wxColor(0,0,0));
238
239 const SRListColumns& cols = getColumns();
240
241 row[cols.index] = sr.getIndex();
242 row[cols.index].setAttr(colour);
243 row[cols.srClass] = wxVariant(wxutil::GetLocalBitmap(classIcon));
244 row[cols.caption] = wxVariant(wxDataViewIconText(stimTypeStr, icon));
245 row[cols.caption].setAttr(colour);
246 row[cols.inherited] = sr.inherited();
247 }
248
249 void SREntity::setProperty(int index, const std::string& key, const std::string& value)
250 {
251 // First, propagate the SR set() call
252 StimResponse& sr = get(index);
253 sr.set(key, value);
254
255 wxutil::TreeModel::Ptr targetStore = (sr.get("class") == "S") ? _stimStore : _responseStore;
256
257 wxDataViewItem item = getIterForIndex(*targetStore, index);
258
259 if (!item.IsOk())
260 {
261 rError() << "Cannot find S/R index in liststore: " << index << std::endl;
262 return;
263 }
264
265 wxutil::TreeModel::Row row(item, *targetStore);
266 writeToListRow(row, sr);
267 row.SendItemChanged();
268 }
269
270 StimResponse& SREntity::get(int index)
271 {
272 auto i = findByIndex(index);
273
274 return i != _list.end() ? *i : _emptyStimResponse;
275 }
276
277 const SRListColumns& SREntity::getColumns()
278 {
279 static SRListColumns _columns;
280 return _columns;
281 }
282
283 wxutil::TreeModel::Ptr SREntity::getStimStore()
284 {
285 return _stimStore;
286 }
287
288 wxutil::TreeModel::Ptr SREntity::getResponseStore()
289 {
290 return _responseStore;
291 }
292
293 // static key loader
294 void SREntity::loadKeys()
295 {
296 xml::NodeList propList = GlobalGameManager().currentGame()->getLocalXPath(GKEY_STIM_PROPERTIES);
297
298 for (const auto& node : propList)
299 {
300 // Create a new key and set the key name / class string
301 SRKey newKey;
302 newKey.key = node.getAttributeValue("name");
303 newKey.classes = node.getAttributeValue("classes");
304
305 // Add the key to the list
306 _keys.push_back(newKey);
307 }
308 }
309
310 SREntity::StimsAndResponses::iterator SREntity::findByIndex(int index)
311 {
312 for (StimsAndResponses::iterator i = _list.begin(); i != _list.end(); ++i)
313 {
314 if (i->getIndex() == index)
315 {
316 return i;
317 }
318 }
319
320 return _list.end();
321 }
322