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 Doom3 entity which does not have a fixed size specified in its entity-definition (e.g. func_static).
25 /// This entity behaves as a group only when the "model" key is empty or is the same as the "name" key. Otherwise it behaves as a model.
26 /// When behaving as a group, the "origin" key is the translation to be applied to all brushes (not patches) grouped under this entity.
27 /// When behaving as a model, the "origin", "angle" and "rotation" keys directly control the entity's local-to-parent transform.
28 /// When either the "curve_Nurbs" or "curve_CatmullRomSpline" keys define a curve, the curve is rendered and can be edited.
30 #include "doom3group.h"
33 #include "renderable.h"
35 #include "modelskin.h"
37 #include "selectionlib.h"
38 #include "instancelib.h"
39 #include "transformlib.h"
40 #include "traverselib.h"
41 #include "entitylib.h"
43 #include "eclasslib.h"
44 #include "stream/stringstream.h"
47 #include "targetable.h"
53 #include "namedentity.h"
54 #include "keyobservers.h"
57 #include "modelskinkey.h"
61 inline void PointVertexArray_testSelect( PointVertex* first, std::size_t count, SelectionTest& test, SelectionIntersection& best ){
64 reinterpret_cast<VertexPointer::pointer>( &first->vertex ),
67 IndexPointer::index_type( count ),
76 EntityKeyValues m_entity;
77 KeyObserverMap m_keyObservers;
78 TraversableNodeSet m_traverse;
79 MatrixTransform m_transform;
81 SingletonModel m_model;
82 OriginKey m_originKey;
85 RotationKey m_rotationKey;
88 ClassnameFilter m_filter;
91 TraversableObserverPairRelay m_traverseObservers;
92 Doom3GroupOrigin m_funcStaticOrigin;
93 RenderablePivot m_renderOrigin;
94 RenderableNamedEntity m_renderName;
95 mutable Vector3 m_name_origin;
99 NURBSCurve m_curveNURBS;
100 SignalHandlerId m_curveNURBSChanged;
101 CatmullRomSpline m_curveCatmullRom;
102 SignalHandlerId m_curveCatmullRomChanged;
104 mutable AABB m_curveBounds;
106 Callback<void()> m_transformChanged;
107 Callback<void()> m_evaluateTransform;
110 CopiedString m_modelKey;
113 scene::Traversable* m_traversable;
116 default_rotation( m_rotation );
118 m_keyObservers.insert( "classname", ClassnameFilter::ClassnameChangedCaller( m_filter ) );
119 m_keyObservers.insert( Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller( m_named ) );
120 m_keyObservers.insert( "model", Doom3Group::ModelChangedCaller( *this ) );
121 m_keyObservers.insert( "origin", OriginKey::OriginChangedCaller( m_originKey ) );
122 m_keyObservers.insert( "angle", RotationKey::AngleChangedCaller( m_rotationKey ) );
123 m_keyObservers.insert( "rotation", RotationKey::RotationChangedCaller( m_rotationKey ) );
124 m_keyObservers.insert( "name", NameChangedCaller( *this ) );
125 m_keyObservers.insert( curve_Nurbs, NURBSCurve::CurveChangedCaller( m_curveNURBS ) );
126 m_keyObservers.insert( curve_CatmullRomSpline, CatmullRomSpline::CurveChangedCaller( m_curveCatmullRom ) );
127 m_keyObservers.insert( "skin", ModelSkinKey::SkinChangedCaller( m_skin ) );
129 m_traverseObservers.attach( m_funcStaticOrigin );
131 m_nameKeys.setKeyIsName( keyIsNameDoom3Doom3Group );
134 m_entity.attach( m_keyObservers );
137 m_entity.detach( m_keyObservers );
147 m_traverseObservers.detach( m_funcStaticOrigin );
151 m_traversable = &m_model.getTraversable();
152 m_model.attach( &m_traverseObservers );
156 m_model.detach( &m_traverseObservers );
158 void attachTraverse(){
159 m_traversable = &m_traverse;
160 m_traverse.attach( &m_traverseObservers );
162 void detachTraverse(){
164 m_traverse.detach( &m_traverseObservers );
167 bool isModel() const {
171 void setIsModel( bool newValue ){
172 if ( newValue && !m_isModel ) {
176 m_nameKeys.setKeyIsName( Static<KeyIsName>::instance().m_keyIsName );
177 m_model.modelChanged( m_modelKey.c_str() );
179 else if ( !newValue && m_isModel ) {
183 m_nameKeys.setKeyIsName( keyIsNameDoom3Doom3Group );
185 m_isModel = newValue;
189 void updateIsModel(){
190 setIsModel( !string_equal( m_modelKey.c_str(), m_name.c_str() ) );
193 // vc 2k5 compiler fix
198 void nameChanged( const char* value ){
202 typedef MemberCaller<Doom3Group, void(const char*), &Doom3Group::nameChanged> NameChangedCaller;
204 void modelChanged( const char* value ){
208 m_model.modelChanged( value );
212 m_model.modelChanged( "" );
215 typedef MemberCaller<Doom3Group, void(const char*), &Doom3Group::modelChanged> ModelChangedCaller;
217 void updateTransform(){
218 m_transform.localToParent() = g_matrix4_identity;
220 matrix4_translate_by_vec3( m_transform.localToParent(), m_origin );
221 matrix4_multiply_by_matrix4( m_transform.localToParent(), rotation_toMatrix( m_rotation ) );
223 m_transformChanged();
225 m_funcStaticOrigin.originChanged();
228 typedef MemberCaller<Doom3Group, void(), &Doom3Group::updateTransform> UpdateTransformCaller;
230 void originChanged(){
231 m_origin = m_originKey.m_origin;
234 typedef MemberCaller<Doom3Group, void(), &Doom3Group::originChanged> OriginChangedCaller;
236 void rotationChanged(){
237 rotation_assign( m_rotation, m_rotationKey.m_rotation );
240 typedef MemberCaller<Doom3Group, void(), &Doom3Group::rotationChanged> RotationChangedCaller;
244 scene::Node* node = m_model.getNode();
246 Node_modelSkinChanged( *node );
250 typedef MemberCaller<Doom3Group, void(), &Doom3Group::skinChanged> SkinChangedCaller;
253 Doom3Group( EntityClass* eclass, scene::Node& node, const Callback<void()>& transformChanged, const Callback<void()>& boundsChanged, const Callback<void()>& evaluateTransform ) :
255 m_originKey( OriginChangedCaller( *this ) ),
256 m_origin( ORIGINKEY_IDENTITY ),
257 m_rotationKey( RotationChangedCaller( *this ) ),
258 m_filter( m_entity, node ),
260 m_nameKeys( m_entity ),
261 m_funcStaticOrigin( m_traverse, m_origin ),
262 m_renderName( m_named, m_name_origin ),
263 m_name_origin( g_vector3_identity ),
264 m_skin( SkinChangedCaller( *this ) ),
265 m_curveNURBS( boundsChanged ),
266 m_curveCatmullRom( boundsChanged ),
267 m_transformChanged( transformChanged ),
268 m_evaluateTransform( evaluateTransform ),
272 Doom3Group( const Doom3Group& other, scene::Node& node, const Callback<void()>& transformChanged, const Callback<void()>& boundsChanged, const Callback<void()>& evaluateTransform ) :
273 m_entity( other.m_entity ),
274 m_originKey( OriginChangedCaller( *this ) ),
275 m_origin( ORIGINKEY_IDENTITY ),
276 m_rotationKey( RotationChangedCaller( *this ) ),
277 m_filter( m_entity, node ),
279 m_nameKeys( m_entity ),
280 m_funcStaticOrigin( m_traverse, m_origin ),
281 m_renderName( m_named, g_vector3_identity ),
282 m_skin( SkinChangedCaller( *this ) ),
283 m_curveNURBS( boundsChanged ),
284 m_curveCatmullRom( boundsChanged ),
285 m_transformChanged( transformChanged ),
286 m_evaluateTransform( evaluateTransform ),
294 InstanceCounter m_instanceCounter;
295 void instanceAttach( const scene::Path& path ){
296 if ( ++m_instanceCounter.m_count == 1 ) {
297 m_filter.instanceAttach();
298 m_entity.instanceAttach( path_find_mapfile( path.begin(), path.end() ) );
299 m_traverse.instanceAttach( path_find_mapfile( path.begin(), path.end() ) );
301 m_funcStaticOrigin.enable();
304 void instanceDetach( const scene::Path& path ){
305 if ( --m_instanceCounter.m_count == 0 ) {
306 m_funcStaticOrigin.disable();
308 m_traverse.instanceDetach( path_find_mapfile( path.begin(), path.end() ) );
309 m_entity.instanceDetach( path_find_mapfile( path.begin(), path.end() ) );
310 m_filter.instanceDetach();
314 EntityKeyValues& getEntity(){
317 const EntityKeyValues& getEntity() const {
321 scene::Traversable& getTraversable(){
322 return *m_traversable;
324 Namespaced& getNamespaced(){
327 Nameable& getNameable(){
330 TransformNode& getTransformNode(){
333 ModelSkin& getModelSkin(){
337 void attach( scene::Traversable::Observer* observer ){
338 m_traverseObservers.attach( *observer );
340 void detach( scene::Traversable::Observer* observer ){
341 m_traverseObservers.detach( *observer );
344 const AABB& localAABB() const {
345 m_curveBounds = m_curveNURBS.m_bounds;
346 aabb_extend_by_aabb_safe( m_curveBounds, m_curveCatmullRom.m_bounds );
347 return m_curveBounds;
350 void renderSolid( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected ) const {
351 if ( isModel() && selected ) {
352 m_renderOrigin.render( renderer, volume, localToWorld );
355 renderer.SetState( m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly );
356 renderer.SetState( m_entity.getEntityClass().m_state_wire, Renderer::eFullMaterials );
358 if ( !m_curveNURBS.m_renderCurve.m_vertices.empty() ) {
359 renderer.addRenderable( m_curveNURBS.m_renderCurve, localToWorld );
361 if ( !m_curveCatmullRom.m_renderCurve.m_vertices.empty() ) {
362 renderer.addRenderable( m_curveCatmullRom.m_renderCurve, localToWorld );
366 void renderWireframe( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected, const AABB& childBounds ) const {
367 renderSolid( renderer, volume, localToWorld, selected );
370 // draw models as usual
372 // don't draw the name for worldspawn
373 if ( !strcmp( m_entity.getEntityClass().name(), "worldspawn" ) ) {
377 // place name in the middle of the "children cloud"
378 m_name_origin = childBounds.origin;
381 renderer.addRenderable( m_renderName, localToWorld );
385 void testSelect( Selector& selector, SelectionTest& test, SelectionIntersection& best ){
386 PointVertexArray_testSelect( &m_curveNURBS.m_renderCurve.m_vertices[0], m_curveNURBS.m_renderCurve.m_vertices.size(), test, best );
387 PointVertexArray_testSelect( &m_curveCatmullRom.m_renderCurve.m_vertices[0], m_curveCatmullRom.m_renderCurve.m_vertices.size(), test, best );
390 void translate( const Vector3& translation ){
391 m_origin = origin_translated( m_origin, translation );
393 void rotate( const Quaternion& rotation ){
394 rotation_rotate( m_rotation, rotation );
396 void snapto( float snap ){
397 m_originKey.m_origin = origin_snapped( m_originKey.m_origin, snap );
398 m_originKey.write( &m_entity );
400 void revertTransform(){
401 m_origin = m_originKey.m_origin;
402 rotation_assign( m_rotation, m_rotationKey.m_rotation );
403 m_curveNURBS.m_controlPointsTransformed = m_curveNURBS.m_controlPoints;
404 m_curveCatmullRom.m_controlPointsTransformed = m_curveCatmullRom.m_controlPoints;
406 void freezeTransform(){
407 m_originKey.m_origin = m_origin;
408 m_originKey.write( &m_entity );
409 rotation_assign( m_rotationKey.m_rotation, m_rotation );
410 m_rotationKey.write( &m_entity );
411 m_curveNURBS.m_controlPoints = m_curveNURBS.m_controlPointsTransformed;
412 ControlPoints_write( m_curveNURBS.m_controlPoints, curve_Nurbs, m_entity );
413 m_curveCatmullRom.m_controlPoints = m_curveCatmullRom.m_controlPointsTransformed;
414 ControlPoints_write( m_curveCatmullRom.m_controlPoints, curve_CatmullRomSpline, m_entity );
416 void transformChanged(){
418 m_evaluateTransform();
420 m_curveNURBS.curveChanged();
421 m_curveCatmullRom.curveChanged();
423 typedef MemberCaller<Doom3Group, void(), &Doom3Group::transformChanged> TransformChangedCaller;
426 class Doom3GroupInstance :
427 public TargetableInstance,
428 public TransformModifier,
430 public SelectionTestable,
431 public ComponentSelectionTestable,
432 public ComponentEditable,
433 public ComponentSnappable
437 InstanceTypeCastTable m_casts;
440 m_casts = TargetableInstance::StaticTypeCasts::instance().get();
441 InstanceContainedCast<Doom3GroupInstance, Bounded>::install( m_casts );
442 InstanceStaticCast<Doom3GroupInstance, Renderable>::install( m_casts );
443 InstanceStaticCast<Doom3GroupInstance, SelectionTestable>::install( m_casts );
444 InstanceStaticCast<Doom3GroupInstance, ComponentSelectionTestable>::install( m_casts );
445 InstanceStaticCast<Doom3GroupInstance, ComponentEditable>::install( m_casts );
446 InstanceStaticCast<Doom3GroupInstance, ComponentSnappable>::install( m_casts );
447 InstanceStaticCast<Doom3GroupInstance, Transformable>::install( m_casts );
448 InstanceIdentityCast<Doom3GroupInstance>::install( m_casts );
450 InstanceTypeCastTable& get(){
455 Doom3Group& m_contained;
456 CurveEdit m_curveNURBS;
457 CurveEdit m_curveCatmullRom;
458 mutable AABB m_aabb_component;
461 typedef LazyStatic<TypeCasts> StaticTypeCasts;
464 Bounded& get( NullType<Bounded>){
468 STRING_CONSTANT( Name, "Doom3GroupInstance" );
470 Doom3GroupInstance( const scene::Path& path, scene::Instance* parent, Doom3Group& contained ) :
471 TargetableInstance( path, parent, this, StaticTypeCasts::instance().get(), contained.getEntity(), *this ),
472 TransformModifier( Doom3Group::TransformChangedCaller( contained ), ApplyTransformCaller( *this ) ),
473 m_contained( contained ),
474 m_curveNURBS( m_contained.m_curveNURBS.m_controlPointsTransformed, SelectionChangedComponentCaller( *this ) ),
475 m_curveCatmullRom( m_contained.m_curveCatmullRom.m_controlPointsTransformed, SelectionChangedComponentCaller( *this ) ){
476 m_contained.instanceAttach( Instance::path() );
477 m_contained.m_curveNURBSChanged = m_contained.m_curveNURBS.connect( CurveEdit::CurveChangedCaller( m_curveNURBS ) );
478 m_contained.m_curveCatmullRomChanged = m_contained.m_curveCatmullRom.connect( CurveEdit::CurveChangedCaller( m_curveCatmullRom ) );
480 StaticRenderableConnectionLines::instance().attach( *this );
482 ~Doom3GroupInstance(){
483 StaticRenderableConnectionLines::instance().detach( *this );
485 m_contained.m_curveCatmullRom.disconnect( m_contained.m_curveCatmullRomChanged );
486 m_contained.m_curveNURBS.disconnect( m_contained.m_curveNURBSChanged );
487 m_contained.instanceDetach( Instance::path() );
489 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
490 m_contained.renderSolid( renderer, volume, Instance::localToWorld(), getSelectable().isSelected() );
492 m_curveNURBS.renderComponentsSelected( renderer, volume, localToWorld() );
493 m_curveCatmullRom.renderComponentsSelected( renderer, volume, localToWorld() );
495 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
496 m_contained.renderWireframe( renderer, volume, Instance::localToWorld(), getSelectable().isSelected(), Instance::childBounds() );
498 m_curveNURBS.renderComponentsSelected( renderer, volume, localToWorld() );
499 m_curveCatmullRom.renderComponentsSelected( renderer, volume, localToWorld() );
501 void renderComponents( Renderer& renderer, const VolumeTest& volume ) const {
502 if ( GlobalSelectionSystem().ComponentMode() == SelectionSystem::eVertex ) {
503 m_curveNURBS.renderComponents( renderer, volume, localToWorld() );
504 m_curveCatmullRom.renderComponents( renderer, volume, localToWorld() );
508 void testSelect( Selector& selector, SelectionTest& test ){
509 test.BeginMesh( localToWorld() );
510 SelectionIntersection best;
512 m_contained.testSelect( selector, test, best );
514 if ( best.valid() ) {
515 Selector_add( selector, getSelectable(), best );
519 bool isSelectedComponents() const {
520 return m_curveNURBS.isSelected() || m_curveCatmullRom.isSelected();
522 void setSelectedComponents( bool selected, SelectionSystem::EComponentMode mode ){
523 if ( mode == SelectionSystem::eVertex ) {
524 m_curveNURBS.setSelected( selected );
525 m_curveCatmullRom.setSelected( selected );
528 void testSelectComponents( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode ){
529 if ( mode == SelectionSystem::eVertex ) {
530 test.BeginMesh( localToWorld() );
531 m_curveNURBS.testSelect( selector, test );
532 m_curveCatmullRom.testSelect( selector, test );
536 void transformComponents( const Matrix4& matrix ){
537 if ( m_curveNURBS.isSelected() ) {
538 m_curveNURBS.transform( matrix );
540 if ( m_curveCatmullRom.isSelected() ) {
541 m_curveCatmullRom.transform( matrix );
545 const AABB& getSelectedComponentsBounds() const {
546 m_aabb_component = AABB();
547 m_curveNURBS.forEachSelected([&](const Vector3 &point) {
548 aabb_extend_by_point_safe(m_aabb_component, point);
550 m_curveCatmullRom.forEachSelected([&](const Vector3 &point) {
551 aabb_extend_by_point_safe(m_aabb_component, point);
553 return m_aabb_component;
556 void snapComponents( float snap ){
557 if ( m_curveNURBS.isSelected() ) {
558 m_curveNURBS.snapto( snap );
559 m_curveNURBS.write( curve_Nurbs, m_contained.getEntity() );
561 if ( m_curveCatmullRom.isSelected() ) {
562 m_curveCatmullRom.snapto( snap );
563 m_curveCatmullRom.write( curve_CatmullRomSpline, m_contained.getEntity() );
567 void evaluateTransform(){
568 if ( getType() == TRANSFORM_PRIMITIVE ) {
569 m_contained.translate( getTranslation() );
570 m_contained.rotate( getRotation() );
574 transformComponents( calculateTransform() );
577 void applyTransform(){
578 m_contained.revertTransform();
580 m_contained.freezeTransform();
582 typedef MemberCaller<Doom3GroupInstance, void(), &Doom3GroupInstance::applyTransform> ApplyTransformCaller;
584 void selectionChangedComponent( const Selectable& selectable ){
585 GlobalSelectionSystem().getObserver ( SelectionSystem::eComponent )( selectable );
586 GlobalSelectionSystem().onComponentSelection( *this, selectable );
588 typedef MemberCaller<Doom3GroupInstance, void(const Selectable&), &Doom3GroupInstance::selectionChangedComponent> SelectionChangedComponentCaller;
591 class Doom3GroupNode :
592 public scene::Node::Symbiot,
593 public scene::Instantiable,
594 public scene::Cloneable,
595 public scene::Traversable::Observer
599 NodeTypeCastTable m_casts;
602 NodeStaticCast<Doom3GroupNode, scene::Instantiable>::install( m_casts );
603 NodeStaticCast<Doom3GroupNode, scene::Cloneable>::install( m_casts );
604 NodeContainedCast<Doom3GroupNode, scene::Traversable>::install( m_casts );
605 NodeContainedCast<Doom3GroupNode, Snappable>::install( m_casts );
606 NodeContainedCast<Doom3GroupNode, TransformNode>::install( m_casts );
607 NodeContainedCast<Doom3GroupNode, Entity>::install( m_casts );
608 NodeContainedCast<Doom3GroupNode, Nameable>::install( m_casts );
609 NodeContainedCast<Doom3GroupNode, Namespaced>::install( m_casts );
610 NodeContainedCast<Doom3GroupNode, ModelSkin>::install( m_casts );
612 NodeTypeCastTable& get(){
619 InstanceSet m_instances;
620 Doom3Group m_contained;
623 m_contained.attach( this );
626 m_contained.detach( this );
630 typedef LazyStatic<TypeCasts> StaticTypeCasts;
632 scene::Traversable& get( NullType<scene::Traversable>){
633 return m_contained.getTraversable();
635 Snappable& get( NullType<Snappable>){
638 TransformNode& get( NullType<TransformNode>){
639 return m_contained.getTransformNode();
641 Entity& get( NullType<Entity>){
642 return m_contained.getEntity();
644 Nameable& get( NullType<Nameable>){
645 return m_contained.getNameable();
647 Namespaced& get( NullType<Namespaced>){
648 return m_contained.getNamespaced();
650 ModelSkin& get( NullType<ModelSkin>){
651 return m_contained.getModelSkin();
654 Doom3GroupNode( EntityClass* eclass ) :
655 m_node( this, this, StaticTypeCasts::instance().get() ),
656 m_contained( eclass, m_node, InstanceSet::TransformChangedCaller( m_instances ), InstanceSet::BoundsChangedCaller( m_instances ), InstanceSetEvaluateTransform<Doom3GroupInstance>::Caller( m_instances ) ){
659 Doom3GroupNode( const Doom3GroupNode& other ) :
660 scene::Node::Symbiot( other ),
661 scene::Instantiable( other ),
662 scene::Cloneable( other ),
663 scene::Traversable::Observer( other ),
664 m_node( this, this, StaticTypeCasts::instance().get() ),
665 m_contained( other.m_contained, m_node, InstanceSet::TransformChangedCaller( m_instances ), InstanceSet::BoundsChangedCaller( m_instances ), InstanceSetEvaluateTransform<Doom3GroupInstance>::Caller( m_instances ) ){
679 scene::Node& clone() const {
680 return ( new Doom3GroupNode( *this ) )->node();
683 void insert( scene::Node& child ){
684 m_instances.insert( child );
686 void erase( scene::Node& child ){
687 m_instances.erase( child );
690 scene::Instance* create( const scene::Path& path, scene::Instance* parent ){
691 return new Doom3GroupInstance( path, parent, m_contained );
693 void forEachInstance( const scene::Instantiable::Visitor& visitor ){
694 m_instances.forEachInstance( visitor );
696 void insert( scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance ){
697 m_instances.insert( observer, path, instance );
699 scene::Instance* erase( scene::Instantiable::Observer* observer, const scene::Path& path ){
700 return m_instances.erase( observer, path );
704 void Doom3Group_construct(){
705 CurveEdit::Type::instance().m_controlsShader = GlobalShaderCache().capture( "$POINT" );
706 CurveEdit::Type::instance().m_selectedShader = GlobalShaderCache().capture( "$SELPOINT" );
709 void Doom3Group_destroy(){
710 GlobalShaderCache().release( "$SELPOINT" );
711 GlobalShaderCache().release( "$POINT" );
714 scene::Node& New_Doom3Group( EntityClass* eclass ){
715 return ( new Doom3GroupNode( eclass ) )->node();