/
SREntity.cpp
323 lines (254 loc) · 6.93 KB
/
SREntity.cpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include "SREntity.h"
#include "iregistry.h"
#include "itextstream.h"
#include "ieclass.h"
#include "igame.h"
#include "entitylib.h"
#include "SRPropertyLoader.h"
#include "SRPropertyRemover.h"
#include "SRPropertySaver.h"
#include <iostream>
#include "wxutil/Bitmap.h"
namespace
{
const char* const GKEY_STIM_PROPERTIES = "/stimResponseSystem/properties//property";
}
SREntity::SREntity(Entity* source, StimTypes& stimTypes) :
_stimStore(new wxutil::TreeModel(getColumns(), true)),
_responseStore(new wxutil::TreeModel(getColumns(), true)),
_stimTypes(stimTypes)
{
loadKeys();
load(source);
}
int SREntity::getHighestIndex()
{
int index = 0;
for (const auto& i : _list)
{
if (i.getIndex() > index)
{
index = i.getIndex();
}
}
return index;
}
int SREntity::getHighestInheritedIndex()
{
int index = 0;
for (const auto& i : _list)
{
if (i.inherited() && i.getIndex() > index)
{
index = i.getIndex();
}
}
return index;
}
void SREntity::load(Entity* source)
{
// Clear all the items from the liststore
_stimStore->Clear();
_responseStore->Clear();
if (source == nullptr)
{
return;
}
// Get the entity class to scan the inherited values
IEntityClassPtr eclass = GlobalEntityClassManager().findOrInsert(
source->getKeyValue("classname"), true
);
// Instantiate a visitor class with the list of possible keys
// and the target list where all the S/Rs are stored
// Warning messages are stored in the <_warnings> string
SRPropertyLoader visitor(_keys, *this, _warnings);
eclass->forEachAttribute(std::ref(visitor));
// Scan the entity itself after the class has been searched
source->forEachKeyValue([&](const std::string& key, const std::string& value)
{
visitor.visitKeyValue(key, value);
});
// Populate the liststore
updateListStores();
}
void SREntity::remove(int index)
{
auto found = findByIndex(index);
if (found == _list.end() || found->inherited())
{
return;
}
_list.erase(found);
// Re-arrange the S/R indices. Otherwise he parser in the game engine
// walks up the indices and stops on the first missing index
// not noticing there might be higher indices to follow (#5193)
int highestInheritedIndex = getHighestInheritedIndex();
int nextIndex = highestInheritedIndex + 1;
// Iterate over all non-inherited objects and assign new indices
for (auto i = _list.begin(); i != _list.end(); ++i)
{
if (!i->inherited())
{
// Found a non-inherited stim, assign the index
i->setIndex(nextIndex++);
}
}
updateListStores();
}
int SREntity::duplicate(int fromIndex)
{
auto found = findByIndex(fromIndex);
if (found != _list.end())
{
int index = getHighestIndex() + 1;
// Copy the object to the new id
_list.push_back(StimResponse(*found));
// Set the index and the inheritance status
_list.back().setInherited(false);
_list.back().setIndex(index);
// Rebuild the liststores
updateListStores();
return index;
}
return -1;
}
void SREntity::updateListStores()
{
// Clear all the items from the liststore
_stimStore->Clear();
_responseStore->Clear();
// Now populate the liststore
for (auto& sr : _list)
{
auto row = sr.get("class") == "S" ? _stimStore->AddItem() : _responseStore->AddItem();
// Store the index into the liststore
row[getColumns().index] = sr.getIndex();
writeToListRow(row, sr);
row.SendItemAdded();
}
}
int SREntity::add()
{
return add(getHighestIndex() + 1).getIndex();
}
StimResponse& SREntity::add(int index)
{
// Create a new StimResponse object
_list.push_back(StimResponse());
// Set the index and the inheritance status
_list.back().setInherited(false);
_list.back().setIndex(index);
_list.back().set("class", "S");
return _list.back();
}
void SREntity::cleanEntity(Entity* target)
{
// Clean the entity from all the S/R spawnargs
SRPropertyRemover remover(target, _keys);
target->forEachKeyValue([&](const std::string& key, const std::string& value)
{
remover.visitKeyValue(key, value);
});
// scope ends here, SRPropertyRemover's destructor
// will now delete the keys
}
void SREntity::save(Entity* target)
{
if (target == nullptr)
{
return;
}
// Remove the S/R spawnargs from the entity
cleanEntity(target);
// Setup the saver object
SRPropertySaver saver(target, _keys);
for (auto& i : _list)
{
saver.visit(i);
}
}
wxDataViewItem SREntity::getIterForIndex(wxutil::TreeModel& targetStore, int index)
{
return targetStore.FindInteger(index, getColumns().index);
}
void SREntity::writeToListRow(wxutil::TreeModel::Row& row, StimResponse& sr)
{
StimType stimType = _stimTypes.get(sr.get("type"));
std::string stimTypeStr = stimType.caption;
stimTypeStr += (sr.inherited()) ? " (inherited) " : "";
std::string classIcon = (sr.get("class") == "R") ? ICON_RESPONSE : ICON_STIM;
classIcon += (sr.inherited()) ? SUFFIX_INHERITED : "";
classIcon += (sr.get("state") != "1") ? SUFFIX_INACTIVE : "";
classIcon += SUFFIX_EXTENSION;
wxBitmap iconBmp = wxutil::GetLocalBitmap(stimType.icon);
wxIcon icon;
icon.CopyFromBitmap(iconBmp);
wxDataViewItemAttr colour;
colour.SetColour(sr.inherited() ? wxColor(112,112,112) : wxColor(0,0,0));
const SRListColumns& cols = getColumns();
row[cols.index] = sr.getIndex();
row[cols.index] = colour;
row[cols.srClass] = wxVariant(wxutil::GetLocalBitmap(classIcon));
row[cols.caption] = wxVariant(wxDataViewIconText(stimTypeStr, icon));
row[cols.caption] = colour;
row[cols.inherited] = sr.inherited();
}
void SREntity::setProperty(int index, const std::string& key, const std::string& value)
{
// First, propagate the SR set() call
StimResponse& sr = get(index);
sr.set(key, value);
wxutil::TreeModel::Ptr targetStore = (sr.get("class") == "S") ? _stimStore : _responseStore;
wxDataViewItem item = getIterForIndex(*targetStore, index);
if (!item.IsOk())
{
rError() << "Cannot find S/R index in liststore: " << index << std::endl;
return;
}
wxutil::TreeModel::Row row(item, *targetStore);
writeToListRow(row, sr);
row.SendItemChanged();
}
StimResponse& SREntity::get(int index)
{
auto i = findByIndex(index);
return i != _list.end() ? *i : _emptyStimResponse;
}
const SRListColumns& SREntity::getColumns()
{
static SRListColumns _columns;
return _columns;
}
wxutil::TreeModel::Ptr SREntity::getStimStore()
{
return _stimStore;
}
wxutil::TreeModel::Ptr SREntity::getResponseStore()
{
return _responseStore;
}
// static key loader
void SREntity::loadKeys()
{
xml::NodeList propList = GlobalGameManager().currentGame()->getLocalXPath(GKEY_STIM_PROPERTIES);
for (const auto& node : propList)
{
// Create a new key and set the key name / class string
SRKey newKey;
newKey.key = node.getAttributeValue("name");
newKey.classes = node.getAttributeValue("classes");
// Add the key to the list
_keys.push_back(newKey);
}
}
SREntity::StimsAndResponses::iterator SREntity::findByIndex(int index)
{
for (StimsAndResponses::iterator i = _list.begin(); i != _list.end(); ++i)
{
if (i->getIndex() == index)
{
return i;
}
}
return _list.end();
}