/
EntityInspector.cpp
526 lines (410 loc) · 17.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include "RadiantTest.h"
#include <sigc++/trackable.h>
#include <map>
#include "icommandsystem.h"
#include "algorithm/Scene.h"
#include "iselectable.h"
#include "selection/EntitySelection.h"
#include "algorithm/Entity.h"
#include "scenelib.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::CollectiveSpawnargs> _spawnargs;
std::unique_ptr<selection::EntitySelection> _selectionTracker;
public:
static constexpr const char* DifferingValues = "[differing values]";
KeyValueStore()
{
_spawnargs.reset(new selection::CollectiveSpawnargs);
_selectionTracker.reset(new selection::EntitySelection(*_spawnargs));
_spawnargs->signal_KeyAdded().connect(
sigc::mem_fun(this, &KeyValueStore::onKeyAdded)
);
_spawnargs->signal_KeyRemoved().connect(
sigc::mem_fun(this, &KeyValueStore::onKeyRemoved)
);
_spawnargs->signal_KeyValueSetChanged().connect(
sigc::mem_fun(this, &KeyValueStore::onKeyValueSetChanged)
);
}
~KeyValueStore()
{
_selectionTracker.reset();
_spawnargs.reset();
}
std::map<std::string, std::string> store;
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, ChangeUniqueKeyValue)
{
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");
// Change the light_center value
Node_getEntity(entity1)->setKeyValue("light_center", "0 0 1");
keyValueStore.rescanSelection();
expectUnique(keyValueStore, "light_center", "0 0 1");
}
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(entity1)->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");
}
TEST_F(EntityInspectorTest, UndoRedoKeyValueChange)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
auto entity1Node = selectEntity("light_torchflame_1");
auto entity1 = Node_getEntity(entity1Node);
keyValueStore.rescanSelection();
// All of the entity's spawnargs should be present and showing their value
assumeLightTorchflame1Spawnargs(keyValueStore);
auto keyValuesBeforeChange = algorithm::getAllKeyValuePairs(entity1);
constexpr const char* ChangedValue = "changed_value";
{
UndoableCommand cmd("ChangeKeyValue");
entity1->setKeyValue("unique_to_1", ChangedValue);
}
for (const auto& pair : keyValuesBeforeChange)
{
EXPECT_EQ(keyValueStore.store[pair.first], pair.first == "unique_to_1" ? ChangedValue : pair.second)
<< "Keyvalues not matching up after change";
}
// Undo, this should revert the change and immediately update the store
GlobalUndoSystem().undo();
for (const auto& pair : keyValuesBeforeChange)
{
EXPECT_EQ(keyValueStore.store[pair.first], pair.second) << "Keyvalues not matching up after undo";
}
// Redo and check again
GlobalUndoSystem().redo();
for (const auto& pair : keyValuesBeforeChange)
{
EXPECT_EQ(keyValueStore.store[pair.first], pair.first == "unique_to_1" ? ChangedValue : pair.second)
<< "Keyvalues not matching up after redo";
}
}
TEST_F(EntityInspectorTest, UndoRedoKeyValueAdditionRemoval)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));
auto entity1Node = selectEntity("light_torchflame_1");
auto entity1 = Node_getEntity(entity1Node);
keyValueStore.rescanSelection();
// All of the entity's spawnargs should be present and showing their value
assumeLightTorchflame1Spawnargs(keyValueStore);
constexpr const char* NewKey = "NewKey";
constexpr const char* NewValue = "NewValue";
auto keyValuesBeforeChange = algorithm::getAllKeyValuePairs(entity1);
{
UndoableCommand cmd("AddAndRemoveKeyValues");
// Add a new key
entity1->setKeyValue(NewKey, NewValue);
// Remove the unique key
entity1->setKeyValue("unique_to_1", "");
}
// Check that the change got reflected
expectNotListed(keyValueStore, "unique_to_1");
expectUnique(keyValueStore, NewKey, NewValue);
for (const auto& pair : keyValuesBeforeChange)
{
if (pair.first != "unique_to_1")
{
EXPECT_EQ(keyValueStore.store[pair.first], pair.second) << "Keyvalues not matching up after change";
}
}
// Undo, this should revert the two changes
GlobalUndoSystem().undo();
expectNotListed(keyValueStore, NewKey);
// All other values should be restored again
for (const auto& pair : keyValuesBeforeChange)
{
EXPECT_EQ(keyValueStore.store[pair.first], pair.second) << "Keyvalues not matching up after undo";
}
// Redo and check the opposite
GlobalUndoSystem().redo();
expectNotListed(keyValueStore, "unique_to_1");
expectUnique(keyValueStore, NewKey, NewValue);
for (const auto& pair : keyValuesBeforeChange)
{
if (pair.first != "unique_to_1")
{
EXPECT_EQ(keyValueStore.store[pair.first], pair.second) << "Keyvalues not matching up after redo";
}
}
}
TEST_F(EntityInspectorTest, DeletedEntitiesAreSafelyUntracked)
{
KeyValueStore keyValueStore;
// Create a single entity
auto cls = GlobalEntityClassManager().findClass("atdm:ai_builder_guard");
auto guardNode = GlobalEntityModule().createEntity(cls);
// Create a weak reference to check whether the entity is gone
std::weak_ptr<IEntityNode> weakGuardNode(guardNode);
scene::addNodeToContainer(guardNode, GlobalMapModule().getRoot());
Node_setSelected(guardNode, true);
keyValueStore.rescanSelection();
EXPECT_FALSE(keyValueStore.store.empty()) << "No key values visible after selecting the entity";
guardNode.reset();
// Close the existing map, this should release all references
GlobalCommandSystem().executeCommand("NewMap");
EXPECT_TRUE(weakGuardNode.expired()) << "Guard node is still alive after changing maps";
keyValueStore.rescanSelection();
EXPECT_TRUE(keyValueStore.store.empty()) << "No key values should be visible after changing maps";
}
}