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 does not have a fixed size specified in its entity-definition (except misc_model).
25 /// This entity behaves as a group, i.e. it contains brushes.
28 #include "renderable.h"
31 #include "selectionlib.h"
32 #include "instancelib.h"
33 #include "transformlib.h"
34 #include "traverselib.h"
35 #include "entitylib.h"
37 #include "eclasslib.h"
39 #include "targetable.h"
44 #include "namedentity.h"
45 #include "keyobservers.h"
52 EntityKeyValues m_entity;
53 KeyObserverMap m_keyObservers;
54 MatrixTransform m_transform;
55 TraversableNodeSet m_traverse;
57 ClassnameFilter m_filter;
61 RenderableNamedEntity m_renderName;
62 mutable Vector3 m_name_origin;
64 Callback m_transformChanged;
68 m_keyObservers.insert("classname", ClassnameFilter::ClassnameChangedCaller(m_filter));
69 m_keyObservers.insert(Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller(m_named));
73 Group(EntityClass* eclass, scene::Node& node, const Callback& transformChanged) :
75 m_filter(m_entity, node),
78 m_renderName(m_named, m_name_origin),
79 m_name_origin(g_vector3_identity),
80 m_transformChanged(transformChanged)
84 Group(const Group& other, scene::Node& node, const Callback& transformChanged) :
85 m_entity(other.m_entity),
86 m_filter(m_entity, node),
89 m_renderName(m_named, g_vector3_identity),
90 m_transformChanged(transformChanged)
95 InstanceCounter m_instanceCounter;
96 void instanceAttach(const scene::Path& path)
98 if(++m_instanceCounter.m_count == 1)
100 m_filter.instanceAttach();
101 m_entity.instanceAttach(path_find_mapfile(path.begin(), path.end()));
102 m_traverse.instanceAttach(path_find_mapfile(path.begin(), path.end()));
103 m_entity.attach(m_keyObservers);
106 void instanceDetach(const scene::Path& path)
108 if(--m_instanceCounter.m_count == 0)
110 m_entity.detach(m_keyObservers);
111 m_traverse.instanceDetach(path_find_mapfile(path.begin(), path.end()));
112 m_entity.instanceDetach(path_find_mapfile(path.begin(), path.end()));
113 m_filter.instanceDetach();
117 EntityKeyValues& getEntity()
121 const EntityKeyValues& getEntity() const
126 scene::Traversable& getTraversable()
130 Namespaced& getNamespaced()
134 Nameable& getNameable()
138 TransformNode& getTransformNode()
143 void attach(scene::Traversable::Observer* observer)
145 m_traverse.attach(observer);
147 void detach(scene::Traversable::Observer* observer)
149 m_traverse.detach(observer);
152 void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
154 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly);
157 void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, const AABB& childBounds) const
159 renderSolid(renderer, volume, localToWorld);
163 // don't draw the name for worldspawn
164 if(!strcmp(m_entity.getEntityClass().name(), "worldspawn"))
167 // place name in the middle of the "children cloud"
168 m_name_origin = childBounds.origin;
170 renderer.addRenderable(m_renderName, localToWorld);
176 class TransformableSetTranslation
180 TransformableSetTranslation(const Translation& value) : m_value(value)
183 void operator()(Transformable& transformable) const
185 transformable.setTranslation(m_value);
189 class TransformableSetRotation
193 TransformableSetRotation(const Rotation& value) : m_value(value)
196 void operator()(Transformable& transformable) const
198 transformable.setRotation(m_value);
202 class TransformableSetScale
206 TransformableSetScale(const Scale& value) : m_value(value)
209 void operator()(Transformable& transformable) const
211 transformable.setScale(m_value);
215 class TransformableSetType
217 TransformModifierType m_value;
219 TransformableSetType(const TransformModifierType& value) : m_value(value)
222 void operator()(Transformable& transformable) const
224 transformable.setType(m_value);
228 class TransformableFreezeTransform
230 TransformModifierType m_value;
232 void operator()(Transformable& transformable) const
234 transformable.freezeTransform();
238 template<typename Functor>
239 inline void Scene_forEachChildTransformable(const Functor& functor, const scene::Path& path)
241 GlobalSceneGraph().traverse_subgraph(ChildInstanceWalker< InstanceApply<Transformable, Functor> >(functor), path);
245 class GroupInstance :
246 public TargetableInstance,
248 public Transformable,
254 InstanceTypeCastTable m_casts;
258 m_casts = TargetableInstance::StaticTypeCasts::instance().get();
259 InstanceStaticCast<GroupInstance, Renderable>::install(m_casts);
261 InstanceStaticCast<GroupInstance, Transformable>::install(m_casts);
264 InstanceTypeCastTable& get()
272 typedef LazyStatic<TypeCasts> StaticTypeCasts;
274 GroupInstance(const scene::Path& path, scene::Instance* parent, Group& group) :
275 TargetableInstance(path, parent, this, StaticTypeCasts::instance().get(), group.getEntity(), *this),
278 m_contained.instanceAttach(Instance::path());
279 StaticRenderableConnectionLines::instance().attach(*this);
283 StaticRenderableConnectionLines::instance().detach(*this);
284 m_contained.instanceDetach(Instance::path());
286 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
288 m_contained.renderSolid(renderer, volume, Instance::localToWorld());
290 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
292 m_contained.renderWireframe(renderer, volume, Instance::localToWorld(), Instance::childBounds());
296 void setType(TransformModifierType type)
298 Scene_forEachChildTransformable(TransformableSetType(type), Instance::path());
300 void setTranslation(const Translation& value)
302 Scene_forEachChildTransformable(TransformableSetTranslation(value), Instance::path());
304 void setRotation(const Rotation& value)
306 Scene_forEachChildTransformable(TransformableSetRotation(value), Instance::path());
308 void setScale(const Scale& value)
310 Scene_forEachChildTransformable(TransformableSetScale(value), Instance::path());
312 void freezeTransform()
314 Scene_forEachChildTransformable(TransformableFreezeTransform(), Instance::path());
317 void evaluateTransform()
324 public scene::Node::Symbiot,
325 public scene::Instantiable,
326 public scene::Cloneable,
327 public scene::Traversable::Observer
331 NodeTypeCastTable m_casts;
335 NodeStaticCast<GroupNode, scene::Instantiable>::install(m_casts);
336 NodeStaticCast<GroupNode, scene::Cloneable>::install(m_casts);
337 NodeContainedCast<GroupNode, scene::Traversable>::install(m_casts);
338 NodeContainedCast<GroupNode, TransformNode>::install(m_casts);
339 NodeContainedCast<GroupNode, Entity>::install(m_casts);
340 NodeContainedCast<GroupNode, Nameable>::install(m_casts);
341 NodeContainedCast<GroupNode, Namespaced>::install(m_casts);
343 NodeTypeCastTable& get()
351 InstanceSet m_instances;
356 m_contained.attach(this);
360 m_contained.detach(this);
365 typedef LazyStatic<TypeCasts> StaticTypeCasts;
367 scene::Traversable& get(NullType<scene::Traversable>)
369 return m_contained.getTraversable();
371 TransformNode& get(NullType<TransformNode>)
373 return m_contained.getTransformNode();
375 Entity& get(NullType<Entity>)
377 return m_contained.getEntity();
379 Nameable& get(NullType<Nameable>)
381 return m_contained.getNameable();
383 Namespaced& get(NullType<Namespaced>)
385 return m_contained.getNamespaced();
388 GroupNode(EntityClass* eclass) :
389 m_node(this, this, StaticTypeCasts::instance().get()),
390 m_contained(eclass, m_node, InstanceSet::TransformChangedCaller(m_instances))
394 GroupNode(const GroupNode& other) :
395 scene::Node::Symbiot(other),
396 scene::Instantiable(other),
397 scene::Cloneable(other),
398 scene::Traversable::Observer(other),
399 m_node(this, this, StaticTypeCasts::instance().get()),
400 m_contained(other.m_contained, m_node, InstanceSet::TransformChangedCaller(m_instances))
418 scene::Node& clone() const
420 return (new GroupNode(*this))->node();
423 void insert(scene::Node& child)
425 m_instances.insert(child);
427 void erase(scene::Node& child)
429 m_instances.erase(child);
432 scene::Instance* create(const scene::Path& path, scene::Instance* parent)
434 return new GroupInstance(path, parent, m_contained);
436 void forEachInstance(const scene::Instantiable::Visitor& visitor)
438 m_instances.forEachInstance(visitor);
440 void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
442 m_instances.insert(observer, path, instance);
444 scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
446 return m_instances.erase(observer, path);
450 scene::Node& New_Group(EntityClass* eclass)
452 return (new GroupNode(eclass))->node();