/
EntityInspector.cpp
374 lines (295 loc) · 12.4 KB
/
EntityInspector.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "RadiantTest.h"
#include <sigc++/trackable.h>
#include <map>
#include "icommandsystem.h"
#include "algorithm/Scene.h"
#include "iselectable.h"
#include "selection/EntitySelection.h"
namespace test
{
namespace
{
scene::INodePtr selectEntity(const std::string& name)
{
auto parent = GlobalMapModule().getRoot();
auto entity = algorithm::getEntityByName(parent, name);
Node_setSelected(entity, true);
return entity;
}
// Helper class representing the list of key values shown in the entity inspector
class KeyValueStore :
public sigc::trackable
{
private:
std::unique_ptr<selection::EntitySelection> _selectionTracker;
public:
static constexpr const char* DifferingValues = "[differing values]";
KeyValueStore()
{
_selectionTracker.reset(new selection::EntitySelection);
_selectionTracker->getSpawnargs().signal_KeyAdded().connect(
sigc::mem_fun(this, &KeyValueStore::onKeyAdded)
);
_selectionTracker->getSpawnargs().signal_KeyRemoved().connect(
sigc::mem_fun(this, &KeyValueStore::onKeyRemoved)
);
_selectionTracker->getSpawnargs().signal_KeyValueSetChanged().connect(
sigc::mem_fun(this, &KeyValueStore::onKeyValueSetChanged)
);
}
~KeyValueStore()
{
_selectionTracker.reset();
}
std::map<std::string, std::string> store;
selection::CollectiveSpawnargs& getSpawnargs()
{
return _selectionTracker->getSpawnargs();
}
void rescanSelection()
{
_selectionTracker->update();
}
private:
void onKeyAdded(const std::string& key, const std::string& value)
{
store[key] = value;
}
void onKeyRemoved(const std::string& key)
{
store.erase(key);
}
void onKeyValueSetChanged(const std::string& key, const std::string& uniqueValue)
{
store[key] = uniqueValue.empty() ? DifferingValues : uniqueValue;
}
};
inline void expectUnique(const KeyValueStore& keyValueStore, const std::string& key, const std::string& value)
{
if (keyValueStore.store.count(key) == 0)
{
FAIL() << "Key Value Store doesn't contain " << key << " = " << value;
return;
}
EXPECT_EQ(keyValueStore.store.at(key), value) <<
"Key Value Store should contain " << key << " = " << value << ", but value was " << keyValueStore.store.at(key);
}
inline void expectNonUnique(const KeyValueStore& keyValueStore, const std::string& key)
{
if (keyValueStore.store.count(key) == 0)
{
FAIL() << "Key Value Store doesn't contain " << key;
return;
}
EXPECT_EQ(keyValueStore.store.at(key), KeyValueStore::DifferingValues) <<
"Key Value Store should contain " << key << " = " << KeyValueStore::DifferingValues <<
", but value was " << keyValueStore.store.at(key);
}
inline void expectNotListed(const KeyValueStore& keyValueStore, const std::string& key)
{
EXPECT_EQ(keyValueStore.store.count(key), 0) <<
"Key Value Store should not contain " << key;
}
}
using EntityInspectorTest = RadiantTest;
TEST_F(EntityInspectorTest, EmptySelection)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
keyValueStore.rescanSelection();
EXPECT_TRUE(keyValueStore.store.empty()) << "Shouldn't have any key values without selection";
}
TEST_F(EntityInspectorTest, DeselectAllEntities)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
selectEntity("light_torchflame_1");
keyValueStore.rescanSelection();
EXPECT_FALSE(keyValueStore.store.empty());
GlobalSelectionSystem().setSelectedAll(false);
keyValueStore.rescanSelection();
EXPECT_TRUE(keyValueStore.store.empty()) << "Shouldn't have any key values without selection";
}
inline void assumeLightTorchflame1Spawnargs(const KeyValueStore& keyValueStore)
{
// All of the entity's spawnargs should be present and showing their value
expectUnique(keyValueStore, "canBeBlownOut", "0");
expectUnique(keyValueStore, "classname", "light_torchflame");
expectUnique(keyValueStore, "name", "light_torchflame_1");
expectUnique(keyValueStore, "origin", "0 -64 64");
expectUnique(keyValueStore, "light_center", "0 0 0");
expectUnique(keyValueStore, "light_radius", "126 102 79");
expectUnique(keyValueStore, "canBeBlownOut", "0");
expectUnique(keyValueStore, "unique_to_1", "unique_value");
}
TEST_F(EntityInspectorTest, SingleEntitySelection)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
selectEntity("light_torchflame_1");
keyValueStore.rescanSelection();
// All of the entity's spawnargs should be present and showing their value
assumeLightTorchflame1Spawnargs(keyValueStore);
}
TEST_F(EntityInspectorTest, TwoEntitiesSelected)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
selectEntity("light_torchflame_1");
keyValueStore.rescanSelection();
assumeLightTorchflame1Spawnargs(keyValueStore);
selectEntity("light_torchflame_2");
keyValueStore.rescanSelection();
// Shared spawnargs with the same values
expectUnique(keyValueStore, "canBeBlownOut", "0");
expectUnique(keyValueStore, "classname", "light_torchflame");
expectUnique(keyValueStore, "light_center", "0 0 0");
expectUnique(keyValueStore, "light_radius", "126 102 79");
// Shared keys, but differing values
expectNonUnique(keyValueStore, "name");
expectNonUnique(keyValueStore, "origin");
// These are exclusively present on one of the two entities
expectNotListed(keyValueStore, "unique_to_1");
expectNotListed(keyValueStore, "unique_to_2");
}
TEST_F(EntityInspectorTest, ThreeEntitiesSelected)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
selectEntity("light_torchflame_1");
keyValueStore.rescanSelection();
assumeLightTorchflame1Spawnargs(keyValueStore);
selectEntity("light_torchflame_2");
selectEntity("light_torchflame_3");
keyValueStore.rescanSelection();
// Shared spawnargs with the same values
expectUnique(keyValueStore, "classname", "light_torchflame");
expectUnique(keyValueStore, "light_center", "0 0 0");
expectUnique(keyValueStore, "light_radius", "126 102 79");
// Shared keys, but differing values
expectNonUnique(keyValueStore, "canBeBlownOut");
expectNonUnique(keyValueStore, "name");
expectNonUnique(keyValueStore, "origin");
// These are exclusively present on one of the three entities
expectNotListed(keyValueStore, "unique_to_1");
expectNotListed(keyValueStore, "unique_to_2");
expectNotListed(keyValueStore, "unique_to_3");
}
TEST_F(EntityInspectorTest, AssimilateKeyValues)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
auto entity1 = selectEntity("light_torchflame_1");
auto entity2 = selectEntity("light_torchflame_2");
auto entity3 = selectEntity("light_torchflame_3");
keyValueStore.rescanSelection();
// Shared key, but differing values
expectNonUnique(keyValueStore, "canBeBlownOut");
expectNotListed(keyValueStore, "unique_to_1");
// Set one of the two canBeBlownOut keys to "1", the value on entity 2 is still "0"
Node_getEntity(entity1)->setKeyValue("canBeBlownOut", "1");
keyValueStore.rescanSelection();
expectNonUnique(keyValueStore, "canBeBlownOut");
// Set the remaining value to "1", now all three should be the same
Node_getEntity(entity2)->setKeyValue("canBeBlownOut", "1");
keyValueStore.rescanSelection();
expectUnique(keyValueStore, "canBeBlownOut", "1");
expectNotListed(keyValueStore, "unique_to_1");
// unique_to_1 hasn't been present on entity 2 and 3 yet
Node_getEntity(entity2)->setKeyValue("unique_to_1", "unique_value");
keyValueStore.rescanSelection();
expectNotListed(keyValueStore, "unique_to_1");
Node_getEntity(entity3)->setKeyValue("unique_to_1", "unique_value");
keyValueStore.rescanSelection();
expectUnique(keyValueStore, "unique_to_1", "unique_value"); // should be listed now
}
TEST_F(EntityInspectorTest, AssignDifferingKeyValues)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
auto entity1 = selectEntity("light_torchflame_1");
auto entity2 = selectEntity("light_torchflame_2");
auto entity3 = selectEntity("light_torchflame_3");
keyValueStore.rescanSelection();
expectUnique(keyValueStore, "light_center", "0 0 0");
// Changing the value should make it appear as non-unique
Node_getEntity(entity3)->setKeyValue("light_center", "0 0 1");
keyValueStore.rescanSelection();
expectNonUnique(keyValueStore, "light_center");
}
TEST_F(EntityInspectorTest, RemoveUniqueKeyValue)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
auto entity1 = selectEntity("light_torchflame_1");
keyValueStore.rescanSelection();
expectUnique(keyValueStore, "light_center", "0 0 0");
// Remove the light_center key from entity 2, it should disappear
Node_getEntity(entity1)->setKeyValue("light_center", "");
keyValueStore.rescanSelection();
expectNotListed(keyValueStore, "light_center");
}
TEST_F(EntityInspectorTest, AddKeyValueToSingleSelectedEntity)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
auto entity1 = selectEntity("light_torchflame_1");
keyValueStore.rescanSelection();
assumeLightTorchflame1Spawnargs(keyValueStore);
// Add a new key, it should appear
Node_getEntity(entity1)->setKeyValue("custom_key", "do it");
keyValueStore.rescanSelection();
expectUnique(keyValueStore, "custom_key", "do it");
}
TEST_F(EntityInspectorTest, RemoveOneSharedKeyValue)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
auto entity1 = selectEntity("light_torchflame_1");
auto entity2 = selectEntity("light_torchflame_2");
auto entity3 = selectEntity("light_torchflame_3");
keyValueStore.rescanSelection();
expectUnique(keyValueStore, "light_center", "0 0 0");
// Remove the light_center key from entity 2, it should disappear
Node_getEntity(entity2)->setKeyValue("light_center", "");
keyValueStore.rescanSelection();
expectNotListed(keyValueStore, "light_center");
}
TEST_F(EntityInspectorTest, ReAddOneSharedKeyValue)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
auto entity1 = selectEntity("light_torchflame_1");
auto entity2 = selectEntity("light_torchflame_2");
auto entity3 = selectEntity("light_torchflame_3");
Node_getEntity(entity2)->setKeyValue("light_center", "");
keyValueStore.rescanSelection();
// Since entity 2 doesn't have the ligh_center key, it should not be listed
expectNotListed(keyValueStore, "light_center");
auto sharedValue = Node_getEntity(entity2)->getKeyValue("light_center");
Node_getEntity(entity2)->setKeyValue("light_center", sharedValue);
keyValueStore.rescanSelection();
// It should be listed again
expectUnique(keyValueStore, "light_center", "0 0 0");
}
TEST_F(EntityInspectorTest, DeselectOneEntity)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
auto entity1 = selectEntity("light_torchflame_1");
auto entity2 = selectEntity("light_torchflame_2");
auto entity3 = selectEntity("light_torchflame_3");
keyValueStore.rescanSelection();
// Shared spawnargs with the same values
expectUnique(keyValueStore, "classname", "light_torchflame");
// Shared key, but differing values
expectNonUnique(keyValueStore, "canBeBlownOut");
// De-select entity3, it should make the canBeBlownOut key value unique
Node_setSelected(entity3, false);
keyValueStore.rescanSelection();
// This one is unique now
expectUnique(keyValueStore, "canBeBlownOut", "0");
// The other ones are still listed
expectUnique(keyValueStore, "classname", "light_torchflame");
}
}