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"
50 /// The "origin" key directly controls the entity's local-to-parent transform.
54 EntityKeyValues m_entity;
55 KeyObserverMap m_keyObservers;
56 MatrixTransform m_transform;
57 TraversableNodeSet m_traverse;
59 ClassnameFilter m_filter;
63 OriginKey m_originKey;
66 RenderableNamedEntity m_renderName;
67 mutable Vector3 m_name_origin;
69 Callback m_transformChanged;
70 Callback m_evaluateTransform;
73 m_keyObservers.insert( "classname", ClassnameFilter::ClassnameChangedCaller( m_filter ) );
74 m_keyObservers.insert( Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller( m_named ) );
75 m_keyObservers.insert( "origin", OriginKey::OriginChangedCaller( m_originKey ) );
79 Group( EntityClass* eclass, scene::Node& node, const Callback& transformChanged, const Callback& evaluateTransform ) :
81 m_filter( m_entity, node ),
83 m_nameKeys( m_entity ),
84 m_originKey( OriginChangedCaller( *this ) ),
85 m_origin( ORIGINKEY_IDENTITY ),
86 m_renderName( m_named, m_name_origin ),
87 m_name_origin( g_vector3_identity ),
88 m_transformChanged( transformChanged ),
89 m_evaluateTransform( evaluateTransform ){
92 Group( const Group& other, scene::Node& node, const Callback& transformChanged, const Callback& evaluateTransform ) :
93 m_entity( other.m_entity ),
94 m_filter( m_entity, node ),
96 m_nameKeys( m_entity ),
97 m_originKey( OriginChangedCaller( *this ) ),
98 m_origin( ORIGINKEY_IDENTITY ),
99 m_renderName( m_named, g_vector3_identity ),
100 m_transformChanged( transformChanged ),
101 m_evaluateTransform( evaluateTransform ){
105 InstanceCounter m_instanceCounter;
106 void instanceAttach( const scene::Path& path ){
107 if ( ++m_instanceCounter.m_count == 1 ) {
108 m_filter.instanceAttach();
109 m_entity.instanceAttach( path_find_mapfile( path.begin(), path.end() ) );
110 m_traverse.instanceAttach( path_find_mapfile( path.begin(), path.end() ) );
111 m_entity.attach( m_keyObservers );
114 void instanceDetach( const scene::Path& path ){
115 if ( --m_instanceCounter.m_count == 0 ) {
116 m_entity.detach( m_keyObservers );
117 m_traverse.instanceDetach( path_find_mapfile( path.begin(), path.end() ) );
118 m_entity.instanceDetach( path_find_mapfile( path.begin(), path.end() ) );
119 m_filter.instanceDetach();
123 EntityKeyValues& getEntity(){
126 const EntityKeyValues& getEntity() const {
130 scene::Traversable& getTraversable(){
133 Namespaced& getNamespaced(){
136 Nameable& getNameable(){
139 TransformNode& getTransformNode(){
143 void attach( scene::Traversable::Observer* observer ){
144 m_traverse.attach( observer );
146 void detach( scene::Traversable::Observer* observer ){
147 m_traverse.detach( observer );
150 void renderSolid( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld ) const {
151 renderer.SetState( m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly );
154 void renderWireframe( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, const AABB& childBounds ) const {
155 renderSolid( renderer, volume, localToWorld );
158 // don't draw the name for worldspawn
159 if ( !strcmp( m_entity.getEntityClass().name(), "worldspawn" ) ) {
163 // place name in the middle of the "children cloud"
164 m_name_origin = childBounds.origin;
166 renderer.addRenderable( m_renderName, localToWorld );
170 void updateTransform(){
171 m_transform.localToParent() = g_matrix4_identity;
172 matrix4_translate_by_vec3( m_transform.localToParent(), m_origin );
173 m_transformChanged();
175 typedef MemberCaller<Group, &Group::updateTransform> UpdateTransformCaller;
176 void originChanged(){
177 m_origin = m_originKey.m_origin;
180 typedef MemberCaller<Group, &Group::originChanged> OriginChangedCaller;
182 void translate( const Vector3& translation ){
183 m_origin = origin_translated( m_origin, translation );
186 void revertTransform(){
187 m_origin = m_originKey.m_origin;
189 void freezeTransform(){
190 m_originKey.m_origin = m_origin;
191 m_originKey.write( &m_entity );
193 void transformChanged(){
195 m_evaluateTransform();
198 typedef MemberCaller<Group, &Group::transformChanged> TransformChangedCaller;
202 class GroupInstance :
203 public TargetableInstance,
204 public TransformModifier,
209 InstanceTypeCastTable m_casts;
212 m_casts = TargetableInstance::StaticTypeCasts::instance().get();
213 InstanceStaticCast<GroupInstance, Renderable>::install( m_casts );
215 InstanceTypeCastTable& get(){
222 typedef LazyStatic<TypeCasts> StaticTypeCasts;
224 GroupInstance( const scene::Path& path, scene::Instance* parent, Group& group ) :
225 TargetableInstance( path, parent, this, StaticTypeCasts::instance().get(), group.getEntity(), *this ),
226 TransformModifier( Group::TransformChangedCaller( group ), ApplyTransformCaller( *this ) ),
227 m_contained( group ){
228 m_contained.instanceAttach( Instance::path() );
229 StaticRenderableConnectionLines::instance().attach( *this );
232 StaticRenderableConnectionLines::instance().detach( *this );
233 m_contained.instanceDetach( Instance::path() );
235 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
236 m_contained.renderSolid( renderer, volume, Instance::localToWorld() );
238 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
239 m_contained.renderWireframe( renderer, volume, Instance::localToWorld(), Instance::childBounds() );
242 STRING_CONSTANT( Name, "GroupInstance" );
245 void evaluateTransform(){
246 if ( getType() == TRANSFORM_PRIMITIVE ) {
247 m_contained.translate( getTranslation() );
250 void applyTransform(){
251 m_contained.revertTransform();
253 m_contained.freezeTransform();
255 typedef MemberCaller<GroupInstance, &GroupInstance::applyTransform> ApplyTransformCaller;
259 public scene::Node::Symbiot,
260 public scene::Instantiable,
261 public scene::Cloneable,
262 public scene::Traversable::Observer
266 NodeTypeCastTable m_casts;
269 NodeStaticCast<GroupNode, scene::Instantiable>::install( m_casts );
270 NodeStaticCast<GroupNode, scene::Cloneable>::install( m_casts );
271 NodeContainedCast<GroupNode, scene::Traversable>::install( m_casts );
272 NodeContainedCast<GroupNode, TransformNode>::install( m_casts );
273 NodeContainedCast<GroupNode, Entity>::install( m_casts );
274 NodeContainedCast<GroupNode, Nameable>::install( m_casts );
275 NodeContainedCast<GroupNode, Namespaced>::install( m_casts );
277 NodeTypeCastTable& get(){
284 InstanceSet m_instances;
288 m_contained.attach( this );
291 m_contained.detach( this );
296 typedef LazyStatic<TypeCasts> StaticTypeCasts;
298 scene::Traversable& get( NullType<scene::Traversable>){
299 return m_contained.getTraversable();
301 TransformNode& get( NullType<TransformNode>){
302 return m_contained.getTransformNode();
304 Entity& get( NullType<Entity>){
305 return m_contained.getEntity();
307 Nameable& get( NullType<Nameable>){
308 return m_contained.getNameable();
310 Namespaced& get( NullType<Namespaced>){
311 return m_contained.getNamespaced();
314 GroupNode( EntityClass* eclass ) :
315 m_node( this, this, StaticTypeCasts::instance().get() ),
316 m_contained( eclass, m_node, InstanceSet::TransformChangedCaller( m_instances ), InstanceSetEvaluateTransform<GroupInstance>::Caller( m_instances ) ){
319 GroupNode( const GroupNode& other ) :
320 scene::Node::Symbiot( other ),
321 scene::Instantiable( other ),
322 scene::Cloneable( other ),
323 scene::Traversable::Observer( other ),
324 m_node( this, this, StaticTypeCasts::instance().get() ),
325 m_contained( other.m_contained, m_node, InstanceSet::TransformChangedCaller( m_instances ), InstanceSetEvaluateTransform<GroupInstance>::Caller( m_instances ) ){
339 scene::Node& clone() const {
340 return ( new GroupNode( *this ) )->node();
343 void insert( scene::Node& child ){
344 m_instances.insert( child );
346 void erase( scene::Node& child ){
347 m_instances.erase( child );
350 scene::Instance* create( const scene::Path& path, scene::Instance* parent ){
351 return new GroupInstance( path, parent, m_contained );
353 void forEachInstance( const scene::Instantiable::Visitor& visitor ){
354 m_instances.forEachInstance( visitor );
356 void insert( scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance ){
357 m_instances.insert( observer, path, instance );
359 scene::Instance* erase( scene::Instantiable::Observer* observer, const scene::Path& path ){
360 return m_instances.erase( observer, path );
364 scene::Node& New_Group( EntityClass* eclass ){
365 return ( new GroupNode( eclass ) )->node();