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;
63 Callback m_transformChanged;
67 m_keyObservers.insert("classname", ClassnameFilter::ClassnameChangedCaller(m_filter));
68 m_keyObservers.insert(Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller(m_named));
72 Group(EntityClass* eclass, scene::Node& node, const Callback& transformChanged) :
74 m_filter(m_entity, node),
77 m_renderName(m_named, g_vector3_identity),
78 m_transformChanged(transformChanged)
82 Group(const Group& other, scene::Node& node, const Callback& transformChanged) :
83 m_entity(other.m_entity),
84 m_filter(m_entity, node),
87 m_renderName(m_named, g_vector3_identity),
88 m_transformChanged(transformChanged)
93 InstanceCounter m_instanceCounter;
94 void instanceAttach(const scene::Path& path)
96 if(++m_instanceCounter.m_count == 1)
98 m_filter.instanceAttach();
99 m_entity.instanceAttach(path_find_mapfile(path.begin(), path.end()));
100 m_traverse.instanceAttach(path_find_mapfile(path.begin(), path.end()));
101 m_entity.attach(m_keyObservers);
104 void instanceDetach(const scene::Path& path)
106 if(--m_instanceCounter.m_count == 0)
108 m_entity.detach(m_keyObservers);
109 m_traverse.instanceDetach(path_find_mapfile(path.begin(), path.end()));
110 m_entity.instanceDetach(path_find_mapfile(path.begin(), path.end()));
111 m_filter.instanceDetach();
115 EntityKeyValues& getEntity()
119 const EntityKeyValues& getEntity() const
124 scene::Traversable& getTraversable()
128 Namespaced& getNamespaced()
132 Nameable& getNameable()
136 TransformNode& getTransformNode()
141 void attach(scene::Traversable::Observer* observer)
143 m_traverse.attach(observer);
145 void detach(scene::Traversable::Observer* observer)
147 m_traverse.detach(observer);
150 void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
152 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly);
154 void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
156 renderSolid(renderer, volume, localToWorld);
160 renderer.addRenderable(m_renderName, g_matrix4_identity);
167 class TransformableSetTranslation
171 TransformableSetTranslation(const Translation& value) : m_value(value)
174 void operator()(Transformable& transformable) const
176 transformable.setTranslation(m_value);
180 class TransformableSetRotation
184 TransformableSetRotation(const Rotation& value) : m_value(value)
187 void operator()(Transformable& transformable) const
189 transformable.setRotation(m_value);
193 class TransformableSetScale
197 TransformableSetScale(const Scale& value) : m_value(value)
200 void operator()(Transformable& transformable) const
202 transformable.setScale(m_value);
206 class TransformableSetType
208 TransformModifierType m_value;
210 TransformableSetType(const TransformModifierType& value) : m_value(value)
213 void operator()(Transformable& transformable) const
215 transformable.setType(m_value);
219 class TransformableFreezeTransform
221 TransformModifierType m_value;
223 void operator()(Transformable& transformable) const
225 transformable.freezeTransform();
229 template<typename Functor>
230 inline void Scene_forEachChildTransformable(const Functor& functor, const scene::Path& path)
232 GlobalSceneGraph().traverse_subgraph(ChildInstanceWalker< InstanceApply<Transformable, Functor> >(functor), path);
236 class GroupInstance :
237 public TargetableInstance,
239 public Transformable,
245 InstanceTypeCastTable m_casts;
249 m_casts = TargetableInstance::StaticTypeCasts::instance().get();
250 InstanceStaticCast<GroupInstance, Renderable>::install(m_casts);
252 InstanceStaticCast<GroupInstance, Transformable>::install(m_casts);
255 InstanceTypeCastTable& get()
263 typedef LazyStatic<TypeCasts> StaticTypeCasts;
265 GroupInstance(const scene::Path& path, scene::Instance* parent, Group& group) :
266 TargetableInstance(path, parent, this, StaticTypeCasts::instance().get(), group.getEntity(), *this),
269 m_contained.instanceAttach(Instance::path());
270 StaticRenderableConnectionLines::instance().attach(*this);
274 StaticRenderableConnectionLines::instance().detach(*this);
275 m_contained.instanceDetach(Instance::path());
277 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
279 m_contained.renderSolid(renderer, volume, Instance::localToWorld());
281 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
283 m_contained.renderWireframe(renderer, volume, Instance::localToWorld());
287 void setType(TransformModifierType type)
289 Scene_forEachChildTransformable(TransformableSetType(type), Instance::path());
291 void setTranslation(const Translation& value)
293 Scene_forEachChildTransformable(TransformableSetTranslation(value), Instance::path());
295 void setRotation(const Rotation& value)
297 Scene_forEachChildTransformable(TransformableSetRotation(value), Instance::path());
299 void setScale(const Scale& value)
301 Scene_forEachChildTransformable(TransformableSetScale(value), Instance::path());
303 void freezeTransform()
305 Scene_forEachChildTransformable(TransformableFreezeTransform(), Instance::path());
308 void evaluateTransform()
315 public scene::Node::Symbiot,
316 public scene::Instantiable,
317 public scene::Cloneable,
318 public scene::Traversable::Observer
322 NodeTypeCastTable m_casts;
326 NodeStaticCast<GroupNode, scene::Instantiable>::install(m_casts);
327 NodeStaticCast<GroupNode, scene::Cloneable>::install(m_casts);
328 NodeContainedCast<GroupNode, scene::Traversable>::install(m_casts);
329 NodeContainedCast<GroupNode, TransformNode>::install(m_casts);
330 NodeContainedCast<GroupNode, Entity>::install(m_casts);
331 NodeContainedCast<GroupNode, Nameable>::install(m_casts);
332 NodeContainedCast<GroupNode, Namespaced>::install(m_casts);
334 NodeTypeCastTable& get()
342 InstanceSet m_instances;
347 m_contained.attach(this);
351 m_contained.detach(this);
356 typedef LazyStatic<TypeCasts> StaticTypeCasts;
358 scene::Traversable& get(NullType<scene::Traversable>)
360 return m_contained.getTraversable();
362 TransformNode& get(NullType<TransformNode>)
364 return m_contained.getTransformNode();
366 Entity& get(NullType<Entity>)
368 return m_contained.getEntity();
370 Nameable& get(NullType<Nameable>)
372 return m_contained.getNameable();
374 Namespaced& get(NullType<Namespaced>)
376 return m_contained.getNamespaced();
379 GroupNode(EntityClass* eclass) :
380 m_node(this, this, StaticTypeCasts::instance().get()),
381 m_contained(eclass, m_node, InstanceSet::TransformChangedCaller(m_instances))
385 GroupNode(const GroupNode& other) :
386 scene::Node::Symbiot(other),
387 scene::Instantiable(other),
388 scene::Cloneable(other),
389 scene::Traversable::Observer(other),
390 m_node(this, this, StaticTypeCasts::instance().get()),
391 m_contained(other.m_contained, m_node, InstanceSet::TransformChangedCaller(m_instances))
409 scene::Node& clone() const
411 return (new GroupNode(*this))->node();
414 void insert(scene::Node& child)
416 m_instances.insert(child);
418 void erase(scene::Node& child)
420 m_instances.erase(child);
423 scene::Instance* create(const scene::Path& path, scene::Instance* parent)
425 return new GroupInstance(path, parent, m_contained);
427 void forEachInstance(const scene::Instantiable::Visitor& visitor)
429 m_instances.forEachInstance(visitor);
431 void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
433 m_instances.insert(observer, path, instance);
435 scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
437 return m_instances.erase(observer, path);
441 scene::Node& New_Group(EntityClass* eclass)
443 return (new GroupNode(eclass))->node();