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
22 #if !defined ( INCLUDED_SCENELIB_H )
23 #define INCLUDED_SCENELIB_H
25 #include "iscenegraph.h"
26 #include "iselection.h"
32 #include "math/aabb.h"
33 #include "transformlib.h"
34 #include "generic/callback.h"
35 #include "generic/reference.h"
36 #include "container/stack.h"
37 #include "typesystem.h"
42 template<typename Element> class BasicVector3;
43 typedef BasicVector3<float> Vector3;
44 template<typename Element> class BasicVector4;
45 typedef BasicVector4<float> Vector4;
47 typedef Vector4 Quaternion;
50 class ComponentSelectionTestable
53 STRING_CONSTANT( Name, "ComponentSelectionTestable" );
55 virtual bool isSelectedComponents() const = 0;
56 virtual void setSelectedComponents( bool select, SelectionSystem::EComponentMode mode ) = 0;
57 virtual void testSelectComponents( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode ) = 0;
60 class ComponentEditable
63 STRING_CONSTANT( Name, "ComponentEditable" );
65 virtual const AABB& getSelectedComponentsBounds() const = 0;
68 class ComponentSnappable
71 STRING_CONSTANT( Name, "ComponentSnappable" );
73 virtual void snapComponents( float snap ) = 0;
79 STRING_CONSTANT( Name, "Bounded" );
81 virtual const AABB& localAABB() const = 0;
87 STRING_CONSTANT( Name, "BrushDoom3" );
89 virtual void setDoom3GroupOrigin( const Vector3& origin ) = 0;
95 typedef TypeCastTable<NODETYPEID_MAX> NodeTypeCastTable;
97 template<typename Type>
98 class NodeType : public StaticTypeSystemInitialiser
102 typedef typename Type::Name Name;
103 NodeType() : m_typeId( NODETYPEID_NONE ){
104 StaticTypeSystemInitialiser::instance().addInitialiser( InitialiseCaller( *this ) );
107 m_typeId = GlobalSceneGraph().getNodeTypeId( Name() );
109 typedef MemberCaller<NodeType<Type>, &NodeType<Type>::initialise> InitialiseCaller;
111 #if defined( _DEBUG )
112 ASSERT_MESSAGE( m_typeId != NODETYPEID_NONE, "node-type " << makeQuoted( Name() ) << " used before being initialised" );
118 template<typename Type>
122 enum unnamed0 { SIZE = NODETYPEID_MAX };
123 static TypeId getTypeId(){
124 return Static< NodeType<Type> >::instance().getTypeId();
128 template<typename Type, typename Base>
129 class NodeStaticCast :
130 public CastInstaller<
131 StaticNodeType<Base>,
132 StaticCast<Type, Base>
137 template<typename Type, typename Contained>
138 class NodeContainedCast :
139 public CastInstaller<
140 StaticNodeType<Contained>,
141 ContainedCast<Type, Contained>
146 template<typename Type>
147 class NodeIdentityCast :
148 public CastInstaller<
149 StaticNodeType<Type>,
160 enum unnamed0 { eVisible = 0 };
161 enum unnamed1 { eHidden = 1 << 0 };
162 enum unnamed2 { eFiltered = 1 << 1 };
163 enum unnamed3 { eExcluded = 1 << 2 };
168 virtual void release() = 0;
172 unsigned int m_state;
173 std::size_t m_refcount;
176 NodeTypeCastTable& m_casts;
185 Node( Symbiot* symbiot, void* node, NodeTypeCastTable& casts ) :
188 m_symbiot( symbiot ),
197 ASSERT_MESSAGE( m_refcount < ( 1 << 24 ), "Node::decref: uninitialised refcount" );
201 ASSERT_MESSAGE( m_refcount < ( 1 << 24 ), "Node::decref: uninitialised refcount" );
202 if ( --m_refcount == 0 ) {
203 m_symbiot->release();
206 std::size_t getReferenceCount() const {
210 void* cast( TypeId typeId ) const {
211 return m_casts.cast( typeId, m_node );
214 void enable( unsigned int state ){
217 void disable( unsigned int state ){
221 return m_state == eVisible;
224 return ( m_state & eExcluded ) != 0;
228 class NullNode : public Node::Symbiot
230 NodeTypeCastTable m_casts;
233 NullNode() : m_node( this, 0, m_casts ){
244 template<typename Type>
248 static Type* cast( scene::Node& node ){
249 return static_cast<Type*>( node.cast( StaticNodeType<Type>::getTypeId() ) );
251 static const Type* cast( const scene::Node& node ){
252 return static_cast<const Type*>( node.cast( StaticNodeType<Type>::getTypeId() ) );
257 inline scene::Instantiable* Node_getInstantiable( scene::Node& node ){
258 return NodeTypeCast<scene::Instantiable>::cast( node );
261 inline scene::Traversable* Node_getTraversable( scene::Node& node ){
262 return NodeTypeCast<scene::Traversable>::cast( node );
265 inline void Node_traverseSubgraph( scene::Node& node, const scene::Traversable::Walker& walker ){
266 if ( walker.pre( node ) ) {
267 scene::Traversable* traversable = Node_getTraversable( node );
268 if ( traversable != 0 ) {
269 traversable->traverse( walker );
275 inline TransformNode* Node_getTransformNode( scene::Node& node ){
276 return NodeTypeCast<TransformNode>::cast( node );
279 inline bool operator<( scene::Node& node, scene::Node& other ){
280 return &node < &other;
282 inline bool operator==( scene::Node& node, scene::Node& other ){
283 return &node == &other;
285 inline bool operator!=( scene::Node& node, scene::Node& other ){
286 return !::operator==( node, other );
290 inline scene::Node& NewNullNode(){
291 return ( new scene::NullNode )->node();
294 inline void Path_deleteTop( const scene::Path& path ){
295 Node_getTraversable( path.parent() )->erase( path.top() );
302 class delete_all : public scene::Traversable::Walker
304 scene::Node& m_parent;
306 delete_all( scene::Node& parent ) : m_parent( parent ){
308 bool pre( scene::Node& node ) const {
311 void post( scene::Node& node ) const {
312 Node_getTraversable( m_parent )->erase( node );
316 inline void DeleteSubgraph( scene::Node& subgraph ){
317 Node_getTraversable( subgraph )->traverse( delete_all( subgraph ) );
321 class EntityUndefined
324 STRING_CONSTANT( Name, "Entity" );
327 inline bool Node_isEntity( scene::Node& node ){
328 return NodeTypeCast<EntityUndefined>::cast( node ) != 0;
331 template<typename Functor>
332 class EntityWalker : public scene::Graph::Walker
334 const Functor& functor;
336 EntityWalker( const Functor& functor ) : functor( functor ){
338 bool pre( const scene::Path& path, scene::Instance& instance ) const {
339 if ( Node_isEntity( path.top() ) ) {
347 template<typename Functor>
348 inline const Functor& Scene_forEachEntity( const Functor& functor ){
349 GlobalSceneGraph().traverse( EntityWalker<Functor>( functor ) );
356 STRING_CONSTANT( Name, "Brush" );
359 inline bool Node_isBrush( scene::Node& node ){
360 return NodeTypeCast<BrushUndefined>::cast( node ) != 0;
366 STRING_CONSTANT( Name, "Patch" );
369 inline bool Node_isPatch( scene::Node& node ){
370 return NodeTypeCast<PatchUndefined>::cast( node ) != 0;
373 inline bool Node_isPrimitive( scene::Node& node ){
375 return Node_isBrush( node ) || Node_isPatch( node );
377 return !node.isRoot();
381 class ParentBrushes : public scene::Traversable::Walker
383 scene::Node& m_parent;
385 ParentBrushes( scene::Node& parent )
386 : m_parent( parent ){
388 bool pre( scene::Node& node ) const {
391 void post( scene::Node& node ) const {
392 if ( Node_isPrimitive( node ) ) {
393 Node_getTraversable( m_parent )->insert( node );
398 inline void parentBrushes( scene::Node& subgraph, scene::Node& parent ){
399 Node_getTraversable( subgraph )->traverse( ParentBrushes( parent ) );
402 class HasBrushes : public scene::Traversable::Walker
406 HasBrushes( bool& hasBrushes )
407 : m_hasBrushes( hasBrushes ){
410 bool pre( scene::Node& node ) const {
411 if ( !Node_isPrimitive( node ) ) {
412 m_hasBrushes = false;
418 inline bool node_is_group( scene::Node& node ){
419 scene::Traversable* traversable = Node_getTraversable( node );
420 if ( traversable != 0 ) {
421 bool hasBrushes = false;
422 traversable->traverse( HasBrushes( hasBrushes ) );
428 typedef TypeCastTable<INSTANCETYPEID_MAX> InstanceTypeCastTable;
430 template<typename Type>
431 class InstanceType : public StaticTypeSystemInitialiser
435 typedef typename Type::Name Name;
436 InstanceType() : m_typeId( INSTANCETYPEID_NONE ){
437 StaticTypeSystemInitialiser::instance().addInitialiser( InitialiseCaller( *this ) );
440 m_typeId = GlobalSceneGraph().getInstanceTypeId( Name() );
442 typedef MemberCaller<InstanceType<Type>, &InstanceType<Type>::initialise> InitialiseCaller;
444 #if defined( _DEBUG )
445 ASSERT_MESSAGE( m_typeId != INSTANCETYPEID_NONE, "instance-type " << makeQuoted( Name() ) << " used before being initialised" );
451 template<typename Type>
452 class StaticInstanceType
455 enum unnamed0 { SIZE = INSTANCETYPEID_MAX };
456 static TypeId getTypeId(){
457 return Static< InstanceType<Type> >::instance().getTypeId();
461 template<typename Type, typename Base>
462 class InstanceStaticCast :
463 public CastInstaller<
464 StaticInstanceType<Base>,
465 StaticCast<Type, Base>
470 template<typename Type, typename Contained>
471 class InstanceContainedCast :
472 public CastInstaller<
473 StaticInstanceType<Contained>,
474 ContainedCast<Type, Contained>
479 template<typename Type>
480 class InstanceIdentityCast :
481 public CastInstaller<
482 StaticInstanceType<Type>,
489 inline Selectable* Instance_getSelectable( scene::Instance& instance );
490 inline const Selectable* Instance_getSelectable( const scene::Instance& instance );
492 inline Bounded* Instance_getBounded( scene::Instance& instance );
493 inline const Bounded* Instance_getBounded( const scene::Instance& instance );
499 class AABBAccumulateWalker : public scene::Graph::Walker
502 mutable std::size_t m_depth;
504 AABBAccumulateWalker( AABB& aabb ) : m_aabb( aabb ), m_depth( 0 ){
506 bool pre( const scene::Path& path, scene::Instance& instance ) const {
507 if ( m_depth == 1 ) {
508 aabb_extend_by_aabb_safe( m_aabb, instance.worldAABB() );
510 return ++m_depth != 2;
512 void post( const scene::Path& path, scene::Instance& instance ) const {
518 class TransformChangedWalker : public scene::Graph::Walker
521 bool pre( const scene::Path& path, scene::Instance& instance ) const {
522 instance.transformChangedLocal();
527 class ParentSelectedChangedWalker : public scene::Graph::Walker
530 bool pre( const scene::Path& path, scene::Instance& instance ) const {
531 instance.parentSelectedChanged();
536 class ChildSelectedWalker : public scene::Graph::Walker
538 bool& m_childSelected;
539 mutable std::size_t m_depth;
541 ChildSelectedWalker( bool& childSelected ) : m_childSelected( childSelected ), m_depth( 0 ){
542 m_childSelected = false;
544 bool pre( const scene::Path& path, scene::Instance& instance ) const {
545 if ( m_depth == 1 && !m_childSelected ) {
546 m_childSelected = instance.isSelected() || instance.childSelected();
548 return ++m_depth != 2;
550 void post( const scene::Path& path, scene::Instance& instance ) const {
558 InstanceTypeCastTable& m_casts;
560 mutable Matrix4 m_local2world;
561 mutable AABB m_bounds;
562 mutable AABB m_childBounds;
563 mutable bool m_transformChanged;
564 mutable bool m_transformMutex;
565 mutable bool m_boundsChanged;
566 mutable bool m_boundsMutex;
567 mutable bool m_childBoundsChanged;
568 mutable bool m_childBoundsMutex;
569 mutable bool m_isSelected;
570 mutable bool m_isSelectedChanged;
571 mutable bool m_childSelected;
572 mutable bool m_childSelectedChanged;
573 mutable bool m_parentSelected;
574 mutable bool m_parentSelectedChanged;
575 Callback m_childSelectedChangedCallback;
576 Callback m_transformChangedCallback;
579 void evaluateTransform() const {
580 if ( m_transformChanged ) {
581 ASSERT_MESSAGE( !m_transformMutex, "re-entering transform evaluation" );
582 m_transformMutex = true;
584 m_local2world = ( m_parent != 0 ) ? m_parent->localToWorld() : g_matrix4_identity;
585 TransformNode* transformNode = Node_getTransformNode( m_path.top() );
586 if ( transformNode != 0 ) {
587 matrix4_multiply_by_matrix4( m_local2world, transformNode->localToParent() );
590 m_transformMutex = false;
591 m_transformChanged = false;
594 void evaluateChildBounds() const {
595 if ( m_childBoundsChanged ) {
596 ASSERT_MESSAGE( !m_childBoundsMutex, "re-entering bounds evaluation" );
597 m_childBoundsMutex = true;
599 m_childBounds = AABB();
601 GlobalSceneGraph().traverse_subgraph( AABBAccumulateWalker( m_childBounds ), m_path );
603 m_childBoundsMutex = false;
604 m_childBoundsChanged = false;
607 void evaluateBounds() const {
608 if ( m_boundsChanged ) {
609 ASSERT_MESSAGE( !m_boundsMutex, "re-entering bounds evaluation" );
610 m_boundsMutex = true;
612 m_bounds = childBounds();
614 const Bounded* bounded = Instance_getBounded( *this );
615 if ( bounded != 0 ) {
616 aabb_extend_by_aabb_safe(
618 aabb_for_oriented_aabb_safe( bounded->localAABB(), localToWorld() )
622 m_boundsMutex = false;
623 m_boundsChanged = false;
627 Instance( const scene::Instance& other );
628 Instance& operator=( const scene::Instance& other );
631 Instance( const scene::Path& path, Instance* parent, void* instance, InstanceTypeCastTable& casts ) :
634 m_instance( instance ),
636 m_local2world( g_matrix4_identity ),
637 m_transformChanged( true ),
638 m_transformMutex( false ),
639 m_boundsChanged( true ),
640 m_boundsMutex( false ),
641 m_childBoundsChanged( true ),
642 m_childBoundsMutex( false ),
643 m_isSelectedChanged( true ),
644 m_childSelectedChanged( true ),
645 m_parentSelectedChanged( true ){
646 ASSERT_MESSAGE( ( parent == 0 ) == ( path.size() == 1 ), "instance has invalid parent" );
651 const scene::Path& path() const {
655 void* cast( TypeId typeId ) const {
656 return m_casts.cast( typeId, m_instance );
659 const Matrix4& localToWorld() const {
661 return m_local2world;
663 void transformChangedLocal(){
664 ASSERT_NOTNULL( m_parent );
665 m_transformChanged = true;
666 m_boundsChanged = true;
667 m_childBoundsChanged = true;
668 m_transformChangedCallback();
670 void transformChanged(){
671 GlobalSceneGraph().traverse_subgraph( TransformChangedWalker(), m_path );
674 void setTransformChangedCallback( const Callback& callback ){
675 m_transformChangedCallback = callback;
679 const AABB& worldAABB() const {
683 const AABB& childBounds() const {
684 evaluateChildBounds();
685 return m_childBounds;
687 void boundsChanged(){
688 m_boundsChanged = true;
689 m_childBoundsChanged = true;
690 if ( m_parent != 0 ) {
691 m_parent->boundsChanged();
693 GlobalSceneGraph().boundsChanged();
696 void childSelectedChanged(){
697 m_childSelectedChanged = true;
698 m_childSelectedChangedCallback();
699 if ( m_parent != 0 ) {
700 m_parent->childSelectedChanged();
703 bool childSelected() const {
704 if ( m_childSelectedChanged ) {
705 m_childSelectedChanged = false;
706 GlobalSceneGraph().traverse_subgraph( ChildSelectedWalker( m_childSelected ), m_path );
708 return m_childSelected;
711 void setChildSelectedChangedCallback( const Callback& callback ){
712 m_childSelectedChangedCallback = callback;
714 void selectedChanged(){
715 m_isSelectedChanged = true;
716 if ( m_parent != 0 ) {
717 m_parent->childSelectedChanged();
719 GlobalSceneGraph().traverse_subgraph( ParentSelectedChangedWalker(), m_path );
721 bool isSelected() const {
722 if ( m_isSelectedChanged ) {
723 m_isSelectedChanged = false;
724 const Selectable* selectable = Instance_getSelectable( *this );
725 m_isSelected = selectable != 0 && selectable->isSelected();
730 void parentSelectedChanged(){
731 m_parentSelectedChanged = true;
733 bool parentSelected() const {
734 if ( m_parentSelectedChanged ) {
735 m_parentSelectedChanged = false;
736 m_parentSelected = m_parent != 0 && ( m_parent->isSelected() || m_parent->parentSelected() );
738 return m_parentSelected;
743 template<typename Type>
744 class InstanceTypeCast
747 static Type* cast( scene::Instance& instance ){
748 return static_cast<Type*>( instance.cast( StaticInstanceType<Type>::getTypeId() ) );
750 static const Type* cast( const scene::Instance& instance ){
751 return static_cast<const Type*>( instance.cast( StaticInstanceType<Type>::getTypeId() ) );
755 template<typename Functor>
756 class InstanceWalker : public scene::Graph::Walker
758 const Functor& m_functor;
760 InstanceWalker( const Functor& functor ) : m_functor( functor ){
762 bool pre( const scene::Path& path, scene::Instance& instance ) const {
763 m_functor( instance );
768 template<typename Functor>
769 class ChildInstanceWalker : public scene::Graph::Walker
771 const Functor& m_functor;
772 mutable std::size_t m_depth;
774 ChildInstanceWalker( const Functor& functor ) : m_functor( functor ), m_depth( 0 ){
776 bool pre( const scene::Path& path, scene::Instance& instance ) const {
777 if ( m_depth == 1 ) {
778 m_functor( instance );
780 return ++m_depth != 2;
782 void post( const scene::Path& path, scene::Instance& instance ) const {
787 template<typename Type, typename Functor>
788 class InstanceApply : public Functor
791 InstanceApply( const Functor& functor ) : Functor( functor ){
793 void operator()( scene::Instance& instance ) const {
794 Type* result = InstanceTypeCast<Type>::cast( instance );
796 Functor::operator()( *result );
801 inline Selectable* Instance_getSelectable( scene::Instance& instance ){
802 return InstanceTypeCast<Selectable>::cast( instance );
804 inline const Selectable* Instance_getSelectable( const scene::Instance& instance ){
805 return InstanceTypeCast<Selectable>::cast( instance );
808 template<typename Functor>
809 inline void Scene_forEachChildSelectable( const Functor& functor, const scene::Path& path ){
810 GlobalSceneGraph().traverse_subgraph( ChildInstanceWalker< InstanceApply<Selectable, Functor> >( functor ), path );
813 class SelectableSetSelected
817 SelectableSetSelected( bool selected ) : m_selected( selected ){
819 void operator()( Selectable& selectable ) const {
820 selectable.setSelected( m_selected );
824 inline Bounded* Instance_getBounded( scene::Instance& instance ){
825 return InstanceTypeCast<Bounded>::cast( instance );
827 inline const Bounded* Instance_getBounded( const scene::Instance& instance ){
828 return InstanceTypeCast<Bounded>::cast( instance );
831 inline Transformable* Instance_getTransformable( scene::Instance& instance ){
832 return InstanceTypeCast<Transformable>::cast( instance );
834 inline const Transformable* Instance_getTransformable( const scene::Instance& instance ){
835 return InstanceTypeCast<Transformable>::cast( instance );
839 inline ComponentSelectionTestable* Instance_getComponentSelectionTestable( scene::Instance& instance ){
840 return InstanceTypeCast<ComponentSelectionTestable>::cast( instance );
843 inline ComponentEditable* Instance_getComponentEditable( scene::Instance& instance ){
844 return InstanceTypeCast<ComponentEditable>::cast( instance );
847 inline ComponentSnappable* Instance_getComponentSnappable( scene::Instance& instance ){
848 return InstanceTypeCast<ComponentSnappable>::cast( instance );
852 inline void Instance_setSelected( scene::Instance& instance, bool selected ){
853 Selectable* selectable = Instance_getSelectable( instance );
854 if ( selectable != 0 ) {
855 selectable->setSelected( selected );
859 inline bool Instance_isSelected( scene::Instance& instance ){
860 Selectable* selectable = Instance_getSelectable( instance );
861 if ( selectable != 0 ) {
862 return selectable->isSelected();
867 inline scene::Instance& findInstance( const scene::Path& path ){
868 scene::Instance* instance = GlobalSceneGraph().find( path );
869 ASSERT_MESSAGE( instance != 0, "findInstance: path not found in scene-graph" );
873 inline void selectPath( const scene::Path& path, bool selected ){
874 Instance_setSelected( findInstance( path ), selected );
877 class SelectChildren : public scene::Traversable::Walker
879 mutable scene::Path m_path;
881 SelectChildren( const scene::Path& root )
884 bool pre( scene::Node& node ) const {
885 m_path.push( makeReference( node ) );
886 selectPath( m_path, true );
889 void post( scene::Node& node ) const {
894 inline void Entity_setSelected( scene::Instance& entity, bool selected ){
895 scene::Node& node = entity.path().top();
896 if ( node_is_group( node ) ) {
897 Node_getTraversable( node )->traverse( SelectChildren( entity.path() ) );
901 Instance_setSelected( entity, selected );
905 inline bool Entity_isSelected( scene::Instance& entity ){
906 if ( node_is_group( entity.path().top() ) ) {
907 return entity.childSelected();
909 return Instance_isSelected( entity );
914 class InstanceCounter
917 unsigned int m_count;
918 InstanceCounter() : m_count( 0 ){
926 virtual void increment() = 0;
927 virtual void decrement() = 0;
930 #include "generic/callback.h"
932 class SimpleCounter : public Counter
934 Callback m_countChanged;
937 void setCountChangedCallback( const Callback& countChanged ){
938 m_countChanged = countChanged;
948 std::size_t get() const {
954 template<typename Contained>
955 class ConstReference;
956 typedef ConstReference<scene::Path> PathConstReference;
958 #include "generic/referencecounted.h"
959 typedef SmartReference<scene::Node, IncRefDecRefCounter<scene::Node> > NodeSmartReference;