File: GenericEntityNode.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (366 lines) | stat: -rw-r--r-- 9,054 bytes parent folder | download | duplicates (2)
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
#include "GenericEntityNode.h"
#include "../EntitySettings.h"

#include "math/Frustum.h"

namespace entity
{

GenericEntityNode::GenericEntityNode(const IEntityClassPtr& eclass) :
	EntityNode(eclass),
	m_originKey(std::bind(&GenericEntityNode::originChanged, this)),
	m_origin(ORIGINKEY_IDENTITY),
	m_angleKey(std::bind(&GenericEntityNode::angleChanged, this)),
	m_angle(AngleKey::IDENTITY),
	m_rotationKey(std::bind(&GenericEntityNode::rotationChanged, this)),
    _renderableArrow(*this),
    _renderableBox(*this, localAABB(), m_origin),
	_allow3Drotations(_spawnArgs.getKeyValue("editor_rotatable") == "1"),
	_3DdirectionUsesUp(eclass->isOfType("func_emitter") || eclass->isOfType("func_splat"))
{}

GenericEntityNode::GenericEntityNode(const GenericEntityNode& other) :
	EntityNode(other),
	Snappable(other),
	m_originKey(std::bind(&GenericEntityNode::originChanged, this)),
	m_origin(ORIGINKEY_IDENTITY),
	m_angleKey(std::bind(&GenericEntityNode::angleChanged, this)),
	m_angle(AngleKey::IDENTITY),
	m_rotationKey(std::bind(&GenericEntityNode::rotationChanged, this)),
    _renderableArrow(*this),
    _renderableBox(*this, localAABB(), m_origin),
	_allow3Drotations(_spawnArgs.getKeyValue("editor_rotatable") == "1"),
	_3DdirectionUsesUp(other._eclass->isOfType("func_emitter") || other._eclass->isOfType("func_splat"))
{}

std::shared_ptr<GenericEntityNode> GenericEntityNode::Create(const IEntityClassPtr& eclass)
{
	auto instance = std::make_shared<GenericEntityNode>(eclass);
	instance->construct();

	return instance;
}

void GenericEntityNode::construct()
{
	EntityNode::construct();

	m_aabb_local = _spawnArgs.getEntityClass()->getBounds();
	m_ray.origin = m_aabb_local.getOrigin();
	m_ray.direction = Vector3(1, 0, 0);
	m_rotation.setIdentity();

	if (!_allow3Drotations)
	{
		// Ordinary rotation (2D around z axis), use angle key observer
        static_assert(std::is_base_of_v<sigc::trackable, AngleKey>);
		observeKey("angle", sigc::mem_fun(m_angleKey, &AngleKey::angleChanged));
	}
	else
	{
		// Full 3D rotations allowed, observe both keys using the rotation key observer
        static_assert(std::is_base_of_v<sigc::trackable, RotationKey>);
		observeKey("angle", sigc::mem_fun(m_rotationKey, &RotationKey::angleChanged));
        observeKey("rotation", sigc::mem_fun(m_rotationKey, &RotationKey::rotationChanged));
    }

    static_assert(std::is_base_of_v<sigc::trackable, OriginKey>);
	observeKey("origin", sigc::mem_fun(m_originKey, &OriginKey::onKeyValueChanged));
}

void GenericEntityNode::snapto(float snap)
{
	m_originKey.snap(snap);
	m_originKey.write(_spawnArgs);
}

const AABB& GenericEntityNode::localAABB() const
{
	return m_aabb_local;
}

void GenericEntityNode::testSelect(Selector& selector, SelectionTest& test)
{
	EntityNode::testSelect(selector, test);

	test.BeginMesh(localToWorld());

	SelectionIntersection best;
	aabb_testselect(m_aabb_local, test, best);
	if(best.isValid()) {
		selector.addIntersection(best);
	}
}

scene::INodePtr GenericEntityNode::clone() const
{
	auto node = std::shared_ptr<GenericEntityNode>(new GenericEntityNode(*this));
	node->construct();
    node->constructClone(*this);

    // When we clone objects which allow 3D rotations, if it uses angle instead of rotation
    // as a key, the angle will get clobbered with a default rotation key of "", so we
    // need to set it properly
    if (_allow3Drotations)
    {
        std::string angleKey = _spawnArgs.getKeyValue("angle");
        if (!angleKey.empty())
        {
            node->m_rotationKey.angleChanged(angleKey);
        }
    }

	return node;
}

void GenericEntityNode::onPreRender(const VolumeTest& volume)
{
    EntityNode::onPreRender(volume);

    const auto& shader = getRenderState() == RenderState::Active ? getColourShader() : getInactiveShader();

    _renderableBox.update(shader);
    _renderableArrow.update(shader);
}

void GenericEntityNode::renderHighlights(IRenderableCollector& collector, const VolumeTest& volume)
{
    EntityNode::renderHighlights(collector, volume);

    collector.addHighlightRenderable(_renderableArrow, Matrix4::getIdentity());
    collector.addHighlightRenderable(_renderableBox, Matrix4::getIdentity());
}

void GenericEntityNode::setRenderSystem(const RenderSystemPtr& renderSystem)
{
    EntityNode::setRenderSystem(renderSystem);

    // Clear the geometry from any previous shader
    clearRenderables();
}

const Vector3& GenericEntityNode::getDirection() const
{
	return m_ray.direction;
}

void GenericEntityNode::rotate(const Quaternion& rotation)
{
	if (_allow3Drotations)
	{
        m_rotation.rotate(rotation);
	}
	else
	{
		m_angle = AngleKey::getRotatedValue(m_angle, rotation);
	}
}

void GenericEntityNode::_revertTransform()
{
	m_origin = m_originKey.get();

	if (_allow3Drotations)
	{
		m_rotation = m_rotationKey.m_rotation;
	}
	else
	{
		m_angle = m_angleKey.getValue();
	}
}

void GenericEntityNode::_freezeTransform()
{
	m_originKey.set(m_origin);
	m_originKey.write(_spawnArgs);

	if (_allow3Drotations)
	{
		m_rotationKey.m_rotation = m_rotation;
		m_rotationKey.m_rotation.writeToEntity(&_spawnArgs);
	}
	else
	{
		m_angleKey.setValue(m_angle);
		m_angleKey.write(&_spawnArgs);
	}
}

void GenericEntityNode::updateTransform()
{
	setLocalToParent(Matrix4::getTranslation(m_origin));

	if (_allow3Drotations)
	{
		// Almost all entities should use forward (x-axis)
		Vector3 axis = g_vector3_axis_x;
		if (_3DdirectionUsesUp)
		{
			axis = g_vector3_axis_z;
		}
		m_ray.direction = m_rotation.getMatrix4().transformDirection(Vector3(axis));
	}
	else
	{
        m_ray.direction = Matrix4::getRotationAboutZ(math::Degrees(m_angle))
                                  .transformDirection(Vector3(1, 0, 0));
    }

    updateRenderables();
	transformChanged();
}

void GenericEntityNode::_onTransformationChanged()
{
	if (getType() == TRANSFORM_PRIMITIVE)
	{
        _revertTransform();

		m_origin += getTranslation();
		rotate(getRotation());

		updateTransform();
	}
}

void GenericEntityNode::_applyTransformation()
{
	if (getType() == TRANSFORM_PRIMITIVE)
	{
		_revertTransform();

		m_origin += getTranslation();
		rotate(getRotation());

        _freezeTransform();
	}
}

const Vector3& GenericEntityNode::getUntransformedOrigin()
{
    return m_originKey.get();
}

const Vector3& GenericEntityNode::getWorldPosition() const
{
    return m_origin;
}

void GenericEntityNode::onChildAdded(const scene::INodePtr& child)
{
    EntityNode::onChildAdded(child);

    _renderableBox.setFillMode(true);

    // Check if this node has any actual models/particles as children
    Node::foreachNode([&](const scene::INodePtr& node)
    {
        // We consider all non-path-connection childnodes as "models"
        if (child->getNodeType() != scene::INode::Type::EntityConnection)
        {
            _renderableBox.setFillMode(false);
            return false; // stop traversal
        }

        return true;
    });
}

void GenericEntityNode::onChildRemoved(const scene::INodePtr& child)
{
    EntityNode::onChildRemoved(child);

    _renderableBox.setFillMode(true);

    // Check if this node has any actual models/particles as children
    Node::foreachNode([&](const scene::INodePtr& node)
    {
        // We consider all non-path-connection childnodes as "models"
        // Ignore the child itself as this event is raised before the node is actually removed.
        if (node != child && child->getNodeType() != scene::INode::Type::EntityConnection)
        {
            _renderableBox.setFillMode(false);
            return false; // stop traversal
        }

        return true;
    });
}

void GenericEntityNode::onInsertIntoScene(scene::IMapRootNode& root)
{
    // Call the base class first
    EntityNode::onInsertIntoScene(root);

    updateRenderables();
}

void GenericEntityNode::onRemoveFromScene(scene::IMapRootNode& root)
{
    // Call the base class first
    EntityNode::onRemoveFromScene(root);

    clearRenderables();
}

void GenericEntityNode::onVisibilityChanged(bool isVisibleNow)
{
    EntityNode::onVisibilityChanged(isVisibleNow);

    if (isVisibleNow)
    {
        updateRenderables();
    }
    else
    {
        clearRenderables();
    }
}

void GenericEntityNode::originChanged()
{
	m_origin = m_originKey.get();
	updateTransform();
}

void GenericEntityNode::angleChanged()
{
	// Ignore the angle key when 3D rotations are enabled
	if (_allow3Drotations) return;

	m_angle = m_angleKey.getValue();
	updateTransform();
}

void GenericEntityNode::rotationChanged()
{
	// Ignore the rotation key, when in 2D "angle" mode
	if (!_allow3Drotations) return;

	m_rotation = m_rotationKey.m_rotation;
	updateTransform();
}

void GenericEntityNode::updateRenderables()
{
    _renderableBox.queueUpdate();
    _renderableArrow.queueUpdate();
}

void GenericEntityNode::clearRenderables()
{
    _renderableBox.clear();
    _renderableArrow.clear();
}

void GenericEntityNode::onRenderStateChanged()
{
    EntityNode::onRenderStateChanged();

    clearRenderables();
    updateRenderables();
}


} // namespace entity