/
scenelib.h
352 lines (287 loc) · 8.12 KB
/
scenelib.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
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
#pragma once
#include "inode.h"
#include "ilayer.h"
#include "iselectable.h"
#include "ipatch.h"
#include "ibrush.h"
#include "icameraview.h"
#include "scene/Node.h"
#include <stack>
inline bool Node_isPrimitive(const scene::INodePtr& node)
{
scene::INode::Type type = node->getNodeType();
// greebo: Changed this routine to use the nodeType enum instead of two dynamic casts
// There shouldn't be any discrepancies, but I'll leave this assertion in here for a while
assert((type == scene::INode::Type::Brush || type == scene::INode::Type::Patch) == (Node_isBrush(node) || Node_isPatch(node)));
return type == scene::INode::Type::Brush || type == scene::INode::Type::Patch;
}
namespace scene
{
// Reparents every visited primitive to the parent in the constructor arguments
class PrimitiveReparentor :
public scene::NodeVisitor
{
private:
scene::INodePtr _parent;
public:
PrimitiveReparentor(const scene::INodePtr& parent) :
_parent(parent)
{}
virtual bool pre(const scene::INodePtr& node) override
{
return false;
}
virtual void post(const scene::INodePtr& node) override
{
if (!Node_isPrimitive(node))
{
return;
}
// We need to keep the hard reference to the node, such that the refcount doesn't reach 0
scene::INodePtr nodeRef = node;
scene::INodePtr oldParent = nodeRef->getParent();
if (oldParent)
{
// greebo: remove the node from the old parent first
oldParent->removeChildNode(nodeRef);
}
_parent->addChildNode(nodeRef);
}
};
inline void parentPrimitives(const scene::INodePtr& subgraph, const scene::INodePtr& parent)
{
PrimitiveReparentor visitor(parent);
subgraph->traverseChildren(visitor);
}
/**
* Returns true if the given node contains
* child primitives. Being an entity is not enough.
*/
inline bool hasChildPrimitives(const INodePtr& node)
{
// A node without child nodes is not a group
if (!node->hasChildNodes())
{
return false;
}
bool hasPrimitives = false;
node->foreachNode([&] (const INodePtr& child)->bool
{
if (Node_isPrimitive(child))
{
hasPrimitives = true;
return false; // don't traverse any further
}
else
{
return true;
}
});
return hasPrimitives;
}
/**
* greebo: This removes the given node from its parent node.
* The node is also deselected beforehand.
*/
inline void removeNodeFromParent(const INodePtr& node)
{
// Check if the node has a parent in the first place
INodePtr parent = node->getParent();
if (parent != NULL)
{
// Unselect the node
Node_setSelected(node, false);
parent->removeChildNode(node);
}
}
/**
* This assigns every visited node to the given set of layers.
* Any previous assignments of the node get overwritten by this routine.
*/
class AssignNodeToLayersWalker :
public NodeVisitor
{
const LayerList& _layers;
public:
AssignNodeToLayersWalker(const LayerList& layers) :
_layers(layers)
{}
bool pre(const INodePtr& node)
{
// Pass the call to the single-node method
node->assignToLayers(_layers);
return true; // full traverse
}
};
class UpdateNodeVisibilityWalker :
public NodeVisitor
{
std::stack<bool> _visibilityStack;
ILayerManager& _layerManager;
public:
UpdateNodeVisibilityWalker(ILayerManager& layerManager) :
_layerManager(layerManager)
{}
bool pre(const INodePtr& node) override
{
// Update the node visibility and store the result
bool nodeIsVisible = _layerManager.updateNodeVisibility(node);
// Add a new element for this level
_visibilityStack.push(nodeIsVisible);
return true;
}
void post(const INodePtr& node) override
{
// Is this child visible?
bool childIsVisible = _visibilityStack.top();
_visibilityStack.pop();
if (childIsVisible)
{
// Show the node, regardless whether it was hidden before
// otherwise the parent would hide the visible children as well
node->disable(Node::eLayered);
}
if (node->checkStateFlag(Node::eLayered))
{
// Node is hidden by layers after update (and no children are visible), de-select
Node_setSelected(node, false);
}
if (childIsVisible && !_visibilityStack.empty())
{
// The child was visible, set this parent to true
_visibilityStack.top() = true;
}
}
};
/**
* greebo: This method inserts the given node into the given container
* and ensures that the container's layer visibility is updated.
*/
inline void addNodeToContainer(const INodePtr& node, const INodePtr& container)
{
// Insert the child
container->addChildNode(node);
// If the container is already connected to a root, check the layer visibility
auto rootNode = container->getRootNode();
if (rootNode)
{
// Ensure that worldspawn is visible
UpdateNodeVisibilityWalker walker(rootNode->getLayerManager());
container->traverse(walker);
}
}
} // namespace scene
inline bool Node_hasSelectedChildNodes(const scene::INodePtr& node)
{
bool selected = false;
node->foreachNode([&] (const scene::INodePtr& child)->bool
{
if (Node_isSelected(child))
{
selected = true;
return false; // stop searching
}
return true;
});
return selected;
}
namespace scene
{
inline void setNodeHidden(const INodePtr& node, bool hide)
{
if (!node->supportsStateFlag(Node::eHidden))
{
return;
}
if (hide)
{
node->enable(Node::eHidden);
}
else
{
node->disable(Node::eHidden);
}
}
inline void showNode(const INodePtr& node)
{
setNodeHidden(node, false);
}
inline void showSubgraph(const INodePtr& node)
{
showNode(node);
node->foreachNode([&](const INodePtr& child)
{
showNode(child);
return true;
});
}
inline void hideNode(const INodePtr& node)
{
setNodeHidden(node, true);
}
inline void hideSubgraph(const INodePtr& node)
{
hideNode(node);
node->foreachNode([&](const INodePtr& child)
{
hideNode(child);
return true;
});
}
/**
* greebo: This walker removes all encountered child nodes without
* traversing each node's children. This deselects all removed nodes as well.
*
* Use this to clear all children from a node:
*
* NodeRemover walker();
* node->traverse(walker);
*/
class NodeRemover :
public scene::NodeVisitor
{
public:
bool pre(const INodePtr& node) {
// Copy the node, the reference might point right to
// the parent's container
scene::INodePtr copy(node);
removeNodeFromParent(copy);
return false;
}
};
// Copies all visibility flags from the source to the target
inline void assignVisibilityFlagsFromNode(INode& target, const INode& source)
{
if (source.checkStateFlag(Node::eHidden) && target.supportsStateFlag(Node::eHidden))
{
target.enable(Node::eHidden);
}
if (source.checkStateFlag(Node::eFiltered) && target.supportsStateFlag(Node::eFiltered))
{
target.enable(Node::eFiltered);
}
if (source.checkStateFlag(Node::eExcluded) && target.supportsStateFlag(Node::eExcluded))
{
target.enable(Node::eExcluded);
}
if (source.checkStateFlag(Node::eLayered) && target.supportsStateFlag(Node::eLayered))
{
target.enable(Node::eLayered);
}
}
inline std::pair<Vector3, Vector3> getOriginAndAnglesToLookAtBounds(const AABB& aabb)
{
Vector3 origin(aabb.origin);
// Move the camera a bit off the AABB origin
origin += Vector3(aabb.extents.getLength() * 3, 0, aabb.extents.getLength() * 3);
// Rotate the camera a bit towards the "ground"
Vector3 angles(0, 0, 0);
angles[camera::CAMERA_PITCH] = -40;
angles[camera::CAMERA_YAW] = 180;
return std::make_pair(origin, angles);
}
inline std::pair<Vector3, Vector3> getOriginAndAnglesToLookAtNode(const scene::INode& node)
{
return getOriginAndAnglesToLookAtBounds(node.worldAABB());
}
} // namespace scene