/
EntitySelection.h
228 lines (184 loc) · 6.4 KB
/
EntitySelection.h
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
#pragma once
#include <list>
#include "inode.h"
#include "ientity.h"
#include "iselection.h"
#include "iselectable.h"
#include "imapmerge.h"
namespace selection
{
// Helper object used by the Entity Inspector to track the
// selected entities in the scene.
class EntitySelection final
{
private:
class SpawnargTracker final :
public Entity::Observer
{
private:
EntitySelection& _owner;
Entity* _entity;
scene::INodeWeakPtr _node;
public:
SpawnargTracker(EntitySelection& owner, const scene::INodePtr& node) :
_owner(owner),
_entity(Node_getEntity(node)),
_node(node)
{
assert(_entity != nullptr);
_entity->attachObserver(this);
}
SpawnargTracker(const SpawnargTracker& other) = delete;
SpawnargTracker& operator=(const SpawnargTracker& other) = delete;
~SpawnargTracker()
{
if (_node.expired())
{
return; // cannot unsubscribe, the node is gone
}
_entity->detachObserver(this);
_owner.cleanupEntity(_entity);
}
scene::INodePtr getNode() const
{
return _node.lock();
}
void onKeyInsert(const std::string& key, EntityKeyValue& value) override
{
_owner.onKeyInsert(_entity, key, value);
}
void onKeyChange(const std::string& key, const std::string& value) override
{
_owner.onKeyChange(_entity, key, value);
}
void onKeyErase(const std::string& key, EntityKeyValue& value) override
{
_owner.onKeyErase(_entity, key, value);
}
};
std::list<SpawnargTracker> _trackedEntities;
using KeyValues = std::map<std::string, std::string>;
// Map Entity instances to key value pairs
std::map<Entity*, KeyValues> _keyValuesByEntity;
// Map Keys to Entities
std::map<std::string, std::set<Entity*>> _entitiesByKey;
public:
std::size_t size() const
{
return _trackedEntities.size();
}
scene::INodePtr getSingleSelectedEntity() const
{
if (size() != 1)
{
throw std::runtime_error("Entity selection has count != 1");
}
return _trackedEntities.front().getNode();
}
// Rescan the selection system for entities to observe
void update()
{
std::set<scene::INodePtr> selectedAndTracked;
// Untrack all nodes that have been deleted or deselected in the meantime
for (auto tracked = _trackedEntities.begin(); tracked != _trackedEntities.end();)
{
auto node = tracked->getNode();
if (!node || !Node_isSelected(node))
{
// This entity is gone, remove the tracker entry
_trackedEntities.erase(tracked++);
continue;
}
selectedAndTracked.insert(node);
++tracked;
}
// Check the selection system for new candidates
GlobalSelectionSystem().foreachSelected([&](const scene::INodePtr& selected)
{
// Get the entity this node is attached to - selected a worldspawn brush
// will let the entity inspector interact with the world entity
auto entity = getEntityForNode(selected);
if (!entity || selectedAndTracked.count(selected) > 0)
{
return; // not an entity or already tracked
}
// Not yet registered, create a new tracker
_trackedEntities.emplace_back(*this, entity);
selectedAndTracked.emplace(std::move(entity));
});
}
void foreachKey(const std::function<void(const std::string&, const std::set<Entity*>&)>& functor)
{
for (const auto& pair : _entitiesByKey)
{
functor(pair.first, pair.second);
}
}
private:
scene::INodePtr getEntityForNode(const scene::INodePtr& selected)
{
// The root node must not be selected (this can happen if Invert Selection is
// activated with an empty scene, or by direct selection in the entity list).
if (selected->isRoot()) return {};
if (Node_isEntity(selected))
{
return selected; // the node itself is an entity
}
// The node is not an entity, check its parent
auto selectedNodeParent = selected->getParent();
if (Node_isEntity(selectedNodeParent))
{
return selectedNodeParent;
}
// Finally, we may encounter merge node types
if (selected->getNodeType() == scene::INode::Type::MergeAction &&
GlobalMapModule().getEditMode() == IMap::EditMode::Merge)
{
auto mergeAction = std::dynamic_pointer_cast<scene::IMergeActionNode>(selected);
assert(mergeAction);
if (mergeAction && Node_isEntity(mergeAction->getAffectedNode()))
{
// Use the entity of the merge node
return mergeAction->getAffectedNode();
}
}
return {}; // nothing of interest
}
void onKeyInsert(Entity* entity, const std::string& key, EntityKeyValue& value)
{
auto kv = _keyValuesByEntity.try_emplace(entity);
kv.first->second.emplace(key, value.get());
auto entityList = _entitiesByKey.try_emplace(key);
entityList.first->second.emplace(entity);
}
void onKeyChange(Entity* entity, const std::string& key, const std::string& value)
{
auto kv = _keyValuesByEntity.try_emplace(entity);
kv.first->second[key] = value;
// On key change, we don't need to update the entitiesByKey map
}
void onKeyErase(Entity* entity, const std::string& key, EntityKeyValue& value)
{
auto kv = _keyValuesByEntity.try_emplace(entity);
kv.first->second.erase(key);
auto entityList = _entitiesByKey.find(key);
if (entityList != _entitiesByKey.end())
{
entityList->second.erase(entity);
if (entityList->second.empty())
{
_entitiesByKey.erase(entityList);
}
}
}
void cleanupEntity(Entity* entity)
{
_keyValuesByEntity.erase(entity);
// Remove the entity from all key-mapped lists
for (auto& entityList : _entitiesByKey)
{
entityList.second.erase(entity);
}
}
};
}