2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 ///\brief Represents any entity which has a fixed size specified in its entity-definition and does not display a model (e.g. info_player_start).
25 /// This entity displays an axis-aligned bounding box of the size and colour specified in its entity-definition.
26 /// The "origin" key directly controls the entity's local-to-parent transform.
27 /// An arrow is drawn to visualise the "angle" key.
30 #include "renderable.h"
33 #include "math/frustum.h"
34 #include "selectionlib.h"
35 #include "instancelib.h"
36 #include "transformlib.h"
37 #include "entitylib.h"
39 #include "eclasslib.h"
40 #include "math/line.h"
42 #include "targetable.h"
46 #include "namedentity.h"
47 #include "keyobservers.h"
54 class RenderableArrow : public OpenGLRenderable
59 RenderableArrow(const Ray& ray)
64 void render(RenderStateFlags state) const
66 arrow_draw(m_ray.origin, m_ray.direction);
70 inline void read_aabb(AABB& aabb, const EntityClass& eclass)
72 aabb = aabb_for_minmax(eclass.mins, eclass.maxs);
81 EntityKeyValues m_entity;
82 KeyObserverMap m_keyObservers;
83 MatrixTransform m_transform;
85 OriginKey m_originKey;
90 ClassnameFilter m_filter;
97 RenderableArrow m_arrow;
98 RenderableSolidAABB m_aabb_solid;
99 RenderableWireframeAABB m_aabb_wire;
100 RenderableNamedEntity m_renderName;
102 Callback m_transformChanged;
103 Callback m_evaluateTransform;
107 read_aabb(m_aabb_local, m_entity.getEntityClass());
108 m_ray.origin = m_aabb_local.origin;
109 m_ray.direction[0] = 1;
110 m_ray.direction[1] = 0;
111 m_ray.direction[2] = 0;
113 m_keyObservers.insert("classname", ClassnameFilter::ClassnameChangedCaller(m_filter));
114 m_keyObservers.insert(Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller(m_named));
115 m_keyObservers.insert("angle", AngleKey::AngleChangedCaller(m_angleKey));
116 m_keyObservers.insert("origin", OriginKey::OriginChangedCaller(m_originKey));
119 // vc 2k5 compiler fix
124 void updateTransform()
126 m_transform.localToParent() = g_matrix4_identity;
127 matrix4_translate_by_vec3(m_transform.localToParent(), m_origin);
128 m_ray.direction = matrix4_transformed_direction(matrix4_rotation_for_z(degrees_to_radians(m_angle)), Vector3(1, 0, 0));
129 m_transformChanged();
131 typedef MemberCaller<GenericEntity, &GenericEntity::updateTransform> UpdateTransformCaller;
134 m_origin = m_originKey.m_origin;
137 typedef MemberCaller<GenericEntity, &GenericEntity::originChanged> OriginChangedCaller;
140 m_angle = m_angleKey.m_angle;
143 typedef MemberCaller<GenericEntity, &GenericEntity::angleChanged> AngleChangedCaller;
146 GenericEntity(EntityClass* eclass, scene::Node& node, const Callback& transformChanged, const Callback& evaluateTransform) :
148 m_originKey(OriginChangedCaller(*this)),
149 m_origin(ORIGINKEY_IDENTITY),
150 m_angleKey(AngleChangedCaller(*this)),
151 m_angle(ANGLEKEY_IDENTITY),
152 m_filter(m_entity, node),
154 m_nameKeys(m_entity),
156 m_aabb_solid(m_aabb_local),
157 m_aabb_wire(m_aabb_local),
158 m_renderName(m_named, g_vector3_identity),
159 m_transformChanged(transformChanged),
160 m_evaluateTransform(evaluateTransform)
164 GenericEntity(const GenericEntity& other, scene::Node& node, const Callback& transformChanged, const Callback& evaluateTransform) :
165 m_entity(other.m_entity),
166 m_originKey(OriginChangedCaller(*this)),
167 m_origin(ORIGINKEY_IDENTITY),
168 m_angleKey(AngleChangedCaller(*this)),
169 m_angle(ANGLEKEY_IDENTITY),
170 m_filter(m_entity, node),
172 m_nameKeys(m_entity),
174 m_aabb_solid(m_aabb_local),
175 m_aabb_wire(m_aabb_local),
176 m_renderName(m_named, g_vector3_identity),
177 m_transformChanged(transformChanged),
178 m_evaluateTransform(evaluateTransform)
183 InstanceCounter m_instanceCounter;
184 void instanceAttach(const scene::Path& path)
186 if(++m_instanceCounter.m_count == 1)
188 m_filter.instanceAttach();
189 m_entity.instanceAttach(path_find_mapfile(path.begin(), path.end()));
190 m_entity.attach(m_keyObservers);
193 void instanceDetach(const scene::Path& path)
195 if(--m_instanceCounter.m_count == 0)
197 m_entity.detach(m_keyObservers);
198 m_entity.instanceDetach(path_find_mapfile(path.begin(), path.end()));
199 m_filter.instanceDetach();
203 EntityKeyValues& getEntity()
207 const EntityKeyValues& getEntity() const
212 Namespaced& getNamespaced()
216 Nameable& getNameable()
220 TransformNode& getTransformNode()
225 const AABB& localAABB() const
230 VolumeIntersectionValue intersectVolume(const VolumeTest& volume, const Matrix4& localToWorld) const
232 return volume.TestAABB(localAABB(), localToWorld);
235 void renderArrow(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
239 renderer.addRenderable(m_arrow, localToWorld);
242 void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
244 renderer.SetState(m_entity.getEntityClass().m_state_fill, Renderer::eFullMaterials);
245 renderer.addRenderable(m_aabb_solid, localToWorld);
246 renderArrow(renderer, volume, localToWorld);
248 void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
250 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly);
251 renderer.addRenderable(m_aabb_wire, localToWorld);
252 renderArrow(renderer, volume, localToWorld);
255 renderer.addRenderable(m_renderName, localToWorld);
260 void testSelect(Selector& selector, SelectionTest& test, const Matrix4& localToWorld)
262 test.BeginMesh(localToWorld);
264 SelectionIntersection best;
265 aabb_testselect(m_aabb_local, test, best);
268 selector.addIntersection(best);
272 void translate(const Vector3& translation)
274 m_origin = origin_translated(m_origin, translation);
276 void rotate(const Quaternion& rotation)
278 m_angle = angle_rotated(m_angle, rotation);
280 void snapto(float snap)
282 m_originKey.m_origin = origin_snapped(m_originKey.m_origin, snap);
283 m_originKey.write(&m_entity);
285 void revertTransform()
287 m_origin = m_originKey.m_origin;
288 m_angle = m_angleKey.m_angle;
290 void freezeTransform()
292 m_originKey.m_origin = m_origin;
293 m_originKey.write(&m_entity);
294 m_angleKey.m_angle = m_angle;
295 m_angleKey.write(&m_entity);
297 void transformChanged()
300 m_evaluateTransform();
303 typedef MemberCaller<GenericEntity, &GenericEntity::transformChanged> TransformChangedCaller;
306 class GenericEntityInstance :
307 public TargetableInstance,
308 public TransformModifier,
310 public SelectionTestable
314 InstanceTypeCastTable m_casts;
318 m_casts = TargetableInstance::StaticTypeCasts::instance().get();
319 InstanceContainedCast<GenericEntityInstance, Bounded>::install(m_casts);
320 InstanceContainedCast<GenericEntityInstance, Cullable>::install(m_casts);
321 InstanceStaticCast<GenericEntityInstance, Renderable>::install(m_casts);
322 InstanceStaticCast<GenericEntityInstance, SelectionTestable>::install(m_casts);
323 InstanceStaticCast<GenericEntityInstance, Transformable>::install(m_casts);
324 InstanceIdentityCast<GenericEntityInstance>::install(m_casts);
326 InstanceTypeCastTable& get()
332 GenericEntity& m_contained;
333 mutable AABB m_bounds;
336 typedef LazyStatic<TypeCasts> StaticTypeCasts;
338 Bounded& get(NullType<Bounded>)
342 Cullable& get(NullType<Cullable>)
347 STRING_CONSTANT(Name, "GenericEntityInstance");
349 GenericEntityInstance(const scene::Path& path, scene::Instance* parent, GenericEntity& contained) :
350 TargetableInstance(path, parent, this, StaticTypeCasts::instance().get(), contained.getEntity(), *this),
351 TransformModifier(GenericEntity::TransformChangedCaller(contained), ApplyTransformCaller(*this)),
352 m_contained(contained)
354 m_contained.instanceAttach(Instance::path());
356 StaticRenderableConnectionLines::instance().attach(*this);
358 ~GenericEntityInstance()
360 StaticRenderableConnectionLines::instance().detach(*this);
362 m_contained.instanceDetach(Instance::path());
365 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
367 m_contained.renderSolid(renderer, volume, Instance::localToWorld());
369 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
371 m_contained.renderWireframe(renderer, volume, Instance::localToWorld());
374 void testSelect(Selector& selector, SelectionTest& test)
376 m_contained.testSelect(selector, test, Instance::localToWorld());
379 void evaluateTransform()
381 if(getType() == TRANSFORM_PRIMITIVE)
383 m_contained.translate(getTranslation());
384 m_contained.rotate(getRotation());
387 void applyTransform()
389 m_contained.revertTransform();
391 m_contained.freezeTransform();
393 typedef MemberCaller<GenericEntityInstance, &GenericEntityInstance::applyTransform> ApplyTransformCaller;
396 class GenericEntityNode :
397 public scene::Node::Symbiot,
398 public scene::Instantiable,
399 public scene::Cloneable
403 NodeTypeCastTable m_casts;
407 NodeStaticCast<GenericEntityNode, scene::Instantiable>::install(m_casts);
408 NodeStaticCast<GenericEntityNode, scene::Cloneable>::install(m_casts);
409 NodeContainedCast<GenericEntityNode, Snappable>::install(m_casts);
410 NodeContainedCast<GenericEntityNode, TransformNode>::install(m_casts);
411 NodeContainedCast<GenericEntityNode, Entity>::install(m_casts);
412 NodeContainedCast<GenericEntityNode, Nameable>::install(m_casts);
413 NodeContainedCast<GenericEntityNode, Namespaced>::install(m_casts);
415 NodeTypeCastTable& get()
422 InstanceSet m_instances;
425 GenericEntity m_contained;
428 typedef LazyStatic<TypeCasts> StaticTypeCasts;
430 Snappable& get(NullType<Snappable>)
434 TransformNode& get(NullType<TransformNode>)
436 return m_contained.getTransformNode();
438 Entity& get(NullType<Entity>)
440 return m_contained.getEntity();
442 Nameable& get(NullType<Nameable>)
444 return m_contained.getNameable();
446 Namespaced& get(NullType<Namespaced>)
448 return m_contained.getNamespaced();
451 GenericEntityNode(EntityClass* eclass) :
452 m_node(this, this, StaticTypeCasts::instance().get()),
453 m_contained(eclass, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSetEvaluateTransform<GenericEntityInstance>::Caller(m_instances))
456 GenericEntityNode(const GenericEntityNode& other) :
457 scene::Node::Symbiot(other),
458 scene::Instantiable(other),
459 scene::Cloneable(other),
460 m_node(this, this, StaticTypeCasts::instance().get()),
461 m_contained(other.m_contained, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSetEvaluateTransform<GenericEntityInstance>::Caller(m_instances))
473 scene::Node& clone() const
475 return (new GenericEntityNode(*this))->node();
478 scene::Instance* create(const scene::Path& path, scene::Instance* parent)
480 return new GenericEntityInstance(path, parent, m_contained);
482 void forEachInstance(const scene::Instantiable::Visitor& visitor)
484 m_instances.forEachInstance(visitor);
486 void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
488 m_instances.insert(observer, path, instance);
490 scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
492 return m_instances.erase(observer, path);
496 scene::Node& New_GenericEntity(EntityClass* eclass)
498 return (new GenericEntityNode(eclass))->node();