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 void updateTransform()
121 m_transform.localToParent() = g_matrix4_identity;
122 matrix4_translate_by_vec3(m_transform.localToParent(), m_origin);
123 m_ray.direction = matrix4_transformed_direction(matrix4_rotation_for_z(degrees_to_radians(m_angle)), Vector3(1, 0, 0));
124 m_transformChanged();
126 typedef MemberCaller<GenericEntity, &GenericEntity::updateTransform> UpdateTransformCaller;
129 m_origin = m_originKey.m_origin;
132 typedef MemberCaller<GenericEntity, &GenericEntity::originChanged> OriginChangedCaller;
135 m_angle = m_angleKey.m_angle;
138 typedef MemberCaller<GenericEntity, &GenericEntity::angleChanged> AngleChangedCaller;
141 GenericEntity(EntityClass* eclass, scene::Node& node, const Callback& transformChanged, const Callback& evaluateTransform) :
143 m_originKey(OriginChangedCaller(*this)),
144 m_origin(ORIGINKEY_IDENTITY),
145 m_angleKey(AngleChangedCaller(*this)),
146 m_angle(ANGLEKEY_IDENTITY),
147 m_filter(m_entity, node),
149 m_nameKeys(m_entity),
151 m_aabb_solid(m_aabb_local),
152 m_aabb_wire(m_aabb_local),
153 m_renderName(m_named, g_vector3_identity),
154 m_transformChanged(transformChanged),
155 m_evaluateTransform(evaluateTransform)
159 GenericEntity(const GenericEntity& other, scene::Node& node, const Callback& transformChanged, const Callback& evaluateTransform) :
160 m_entity(other.m_entity),
161 m_originKey(OriginChangedCaller(*this)),
162 m_origin(ORIGINKEY_IDENTITY),
163 m_angleKey(AngleChangedCaller(*this)),
164 m_angle(ANGLEKEY_IDENTITY),
165 m_filter(m_entity, node),
167 m_nameKeys(m_entity),
169 m_aabb_solid(m_aabb_local),
170 m_aabb_wire(m_aabb_local),
171 m_renderName(m_named, g_vector3_identity),
172 m_transformChanged(transformChanged),
173 m_evaluateTransform(evaluateTransform)
178 InstanceCounter m_instanceCounter;
179 void instanceAttach(const scene::Path& path)
181 if(++m_instanceCounter.m_count == 1)
183 m_filter.instanceAttach();
184 m_entity.instanceAttach(path_find_mapfile(path.begin(), path.end()));
185 m_entity.attach(m_keyObservers);
188 void instanceDetach(const scene::Path& path)
190 if(--m_instanceCounter.m_count == 0)
192 m_entity.detach(m_keyObservers);
193 m_entity.instanceDetach(path_find_mapfile(path.begin(), path.end()));
194 m_filter.instanceDetach();
198 EntityKeyValues& getEntity()
202 const EntityKeyValues& getEntity() const
207 Namespaced& getNamespaced()
211 Nameable& getNameable()
215 TransformNode& getTransformNode()
220 const AABB& localAABB() const
225 VolumeIntersectionValue intersectVolume(const VolumeTest& volume, const Matrix4& localToWorld) const
227 return volume.TestAABB(localAABB(), localToWorld);
230 void renderArrow(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
234 renderer.addRenderable(m_arrow, localToWorld);
237 void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
239 renderer.SetState(m_entity.getEntityClass().m_state_fill, Renderer::eFullMaterials);
240 renderer.addRenderable(m_aabb_solid, localToWorld);
241 renderArrow(renderer, volume, localToWorld);
243 void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
245 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly);
246 renderer.addRenderable(m_aabb_wire, localToWorld);
247 renderArrow(renderer, volume, localToWorld);
250 renderer.addRenderable(m_renderName, localToWorld);
255 void testSelect(Selector& selector, SelectionTest& test, const Matrix4& localToWorld)
257 test.BeginMesh(localToWorld);
259 SelectionIntersection best;
260 aabb_testselect(m_aabb_local, test, best);
263 selector.addIntersection(best);
267 void translate(const Vector3& translation)
269 m_origin = origin_translated(m_origin, translation);
271 void rotate(const Quaternion& rotation)
273 m_angle = angle_rotated(m_angle, rotation);
275 void snapto(float snap)
277 m_originKey.m_origin = origin_snapped(m_originKey.m_origin, snap);
278 m_originKey.write(&m_entity);
280 void revertTransform()
282 m_origin = m_originKey.m_origin;
283 m_angle = m_angleKey.m_angle;
285 void freezeTransform()
287 m_originKey.m_origin = m_origin;
288 m_originKey.write(&m_entity);
289 m_angleKey.m_angle = m_angle;
290 m_angleKey.write(&m_entity);
292 void transformChanged()
295 m_evaluateTransform();
298 typedef MemberCaller<GenericEntity, &GenericEntity::transformChanged> TransformChangedCaller;
301 class GenericEntityInstance :
302 public TargetableInstance,
303 public TransformModifier,
305 public SelectionTestable
309 InstanceTypeCastTable m_casts;
313 m_casts = TargetableInstance::StaticTypeCasts::instance().get();
314 InstanceContainedCast<GenericEntityInstance, Bounded>::install(m_casts);
315 InstanceContainedCast<GenericEntityInstance, Cullable>::install(m_casts);
316 InstanceStaticCast<GenericEntityInstance, Renderable>::install(m_casts);
317 InstanceStaticCast<GenericEntityInstance, SelectionTestable>::install(m_casts);
318 InstanceStaticCast<GenericEntityInstance, Transformable>::install(m_casts);
319 InstanceIdentityCast<GenericEntityInstance>::install(m_casts);
321 InstanceTypeCastTable& get()
327 GenericEntity& m_contained;
328 mutable AABB m_bounds;
331 typedef LazyStatic<TypeCasts> StaticTypeCasts;
333 Bounded& get(NullType<Bounded>)
337 Cullable& get(NullType<Cullable>)
342 STRING_CONSTANT(Name, "GenericEntityInstance");
344 GenericEntityInstance(const scene::Path& path, scene::Instance* parent, GenericEntity& contained) :
345 TargetableInstance(path, parent, this, StaticTypeCasts::instance().get(), contained.getEntity(), *this),
346 TransformModifier(GenericEntity::TransformChangedCaller(contained), ApplyTransformCaller(*this)),
347 m_contained(contained)
349 m_contained.instanceAttach(Instance::path());
351 StaticRenderableConnectionLines::instance().attach(*this);
353 ~GenericEntityInstance()
355 StaticRenderableConnectionLines::instance().detach(*this);
357 m_contained.instanceDetach(Instance::path());
360 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
362 m_contained.renderSolid(renderer, volume, Instance::localToWorld());
364 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
366 m_contained.renderWireframe(renderer, volume, Instance::localToWorld());
369 void testSelect(Selector& selector, SelectionTest& test)
371 m_contained.testSelect(selector, test, Instance::localToWorld());
374 void evaluateTransform()
376 if(getType() == TRANSFORM_PRIMITIVE)
378 m_contained.translate(getTranslation());
379 m_contained.rotate(getRotation());
382 void applyTransform()
384 m_contained.revertTransform();
386 m_contained.freezeTransform();
388 typedef MemberCaller<GenericEntityInstance, &GenericEntityInstance::applyTransform> ApplyTransformCaller;
391 class GenericEntityNode :
392 public scene::Node::Symbiot,
393 public scene::Instantiable,
394 public scene::Cloneable
398 NodeTypeCastTable m_casts;
402 NodeStaticCast<GenericEntityNode, scene::Instantiable>::install(m_casts);
403 NodeStaticCast<GenericEntityNode, scene::Cloneable>::install(m_casts);
404 NodeContainedCast<GenericEntityNode, Snappable>::install(m_casts);
405 NodeContainedCast<GenericEntityNode, TransformNode>::install(m_casts);
406 NodeContainedCast<GenericEntityNode, Entity>::install(m_casts);
407 NodeContainedCast<GenericEntityNode, Nameable>::install(m_casts);
408 NodeContainedCast<GenericEntityNode, Namespaced>::install(m_casts);
410 NodeTypeCastTable& get()
417 InstanceSet m_instances;
420 GenericEntity m_contained;
423 typedef LazyStatic<TypeCasts> StaticTypeCasts;
425 Snappable& get(NullType<Snappable>)
429 TransformNode& get(NullType<TransformNode>)
431 return m_contained.getTransformNode();
433 Entity& get(NullType<Entity>)
435 return m_contained.getEntity();
437 Nameable& get(NullType<Nameable>)
439 return m_contained.getNameable();
441 Namespaced& get(NullType<Namespaced>)
443 return m_contained.getNamespaced();
446 GenericEntityNode(EntityClass* eclass) :
447 m_node(this, this, StaticTypeCasts::instance().get()),
448 m_contained(eclass, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSetEvaluateTransform<GenericEntityInstance>::Caller(m_instances))
451 GenericEntityNode(const GenericEntityNode& other) :
452 scene::Node::Symbiot(other),
453 scene::Instantiable(other),
454 scene::Cloneable(other),
455 m_node(this, this, StaticTypeCasts::instance().get()),
456 m_contained(other.m_contained, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSetEvaluateTransform<GenericEntityInstance>::Caller(m_instances))
468 scene::Node& clone() const
470 return (new GenericEntityNode(*this))->node();
473 scene::Instance* create(const scene::Path& path, scene::Instance* parent)
475 return new GenericEntityInstance(path, parent, m_contained);
477 void forEachInstance(const scene::Instantiable::Visitor& visitor)
479 m_instances.forEachInstance(visitor);
481 void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
483 m_instances.insert(observer, path, instance);
485 scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
487 return m_instances.erase(observer, path);
491 scene::Node& New_GenericEntity(EntityClass* eclass)
493 return (new GenericEntityNode(eclass))->node();