| 1 | /* |
| 2 | Copyright (C) 2001-2006, William Joseph. |
| 3 | All Rights Reserved. |
| 4 | |
| 5 | This file is part of GtkRadiant. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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 |
| 20 | */ |
| 21 | |
| 22 | ///\file |
| 23 | ///\brief Represents any Doom3 entity which does not have a fixed size specified in its entity-definition (e.g. func_static). |
| 24 | /// |
| 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. |
| 29 | |
| 30 | #include "doom3group.h" |
| 31 | |
| 32 | #include "cullable.h" |
| 33 | #include "renderable.h" |
| 34 | #include "editable.h" |
| 35 | #include "modelskin.h" |
| 36 | |
| 37 | #include "selectionlib.h" |
| 38 | #include "instancelib.h" |
| 39 | #include "transformlib.h" |
| 40 | #include "traverselib.h" |
| 41 | #include "entitylib.h" |
| 42 | #include "render.h" |
| 43 | #include "eclasslib.h" |
| 44 | #include "stream/stringstream.h" |
| 45 | #include "pivot.h" |
| 46 | |
| 47 | #include "targetable.h" |
| 48 | #include "origin.h" |
| 49 | #include "angle.h" |
| 50 | #include "rotation.h" |
| 51 | #include "model.h" |
| 52 | #include "filters.h" |
| 53 | #include "namedentity.h" |
| 54 | #include "keyobservers.h" |
| 55 | #include "namekeys.h" |
| 56 | #include "curve.h" |
| 57 | #include "modelskinkey.h" |
| 58 | |
| 59 | #include "entity.h" |
| 60 | |
| 61 | inline void PointVertexArray_testSelect( PointVertex* first, std::size_t count, SelectionTest& test, SelectionIntersection& best ){ |
| 62 | test.TestLineStrip( |
| 63 | VertexPointer( |
| 64 | reinterpret_cast<VertexPointer::pointer>( &first->vertex ), |
| 65 | sizeof( PointVertex ) |
| 66 | ), |
| 67 | IndexPointer::index_type( count ), |
| 68 | best |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | class Doom3Group : |
| 73 | public Bounded, |
| 74 | public Snappable |
| 75 | { |
| 76 | EntityKeyValues m_entity; |
| 77 | KeyObserverMap m_keyObservers; |
| 78 | TraversableNodeSet m_traverse; |
| 79 | MatrixTransform m_transform; |
| 80 | |
| 81 | SingletonModel m_model; |
| 82 | OriginKey m_originKey; |
| 83 | Vector3 m_origin; |
| 84 | |
| 85 | RotationKey m_rotationKey; |
| 86 | Float9 m_rotation; |
| 87 | |
| 88 | ClassnameFilter m_filter; |
| 89 | NamedEntity m_named; |
| 90 | NameKeys m_nameKeys; |
| 91 | TraversableObserverPairRelay m_traverseObservers; |
| 92 | Doom3GroupOrigin m_funcStaticOrigin; |
| 93 | RenderablePivot m_renderOrigin; |
| 94 | mutable Vector3 m_name_origin; |
| 95 | RenderableNamedEntity m_renderName; |
| 96 | ModelSkinKey m_skin; |
| 97 | |
| 98 | public: |
| 99 | NURBSCurve m_curveNURBS; |
| 100 | SignalHandlerId m_curveNURBSChanged; |
| 101 | CatmullRomSpline m_curveCatmullRom; |
| 102 | SignalHandlerId m_curveCatmullRomChanged; |
| 103 | private: |
| 104 | mutable AABB m_curveBounds; |
| 105 | |
| 106 | Callback<void()> m_transformChanged; |
| 107 | Callback<void()> m_evaluateTransform; |
| 108 | |
| 109 | CopiedString m_name; |
| 110 | CopiedString m_modelKey; |
| 111 | bool m_isModel; |
| 112 | |
| 113 | scene::Traversable* m_traversable; |
| 114 | |
| 115 | void construct(){ |
| 116 | default_rotation( m_rotation ); |
| 117 | |
| 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 ) ); |
| 128 | |
| 129 | m_traverseObservers.attach( m_funcStaticOrigin ); |
| 130 | m_isModel = false; |
| 131 | m_nameKeys.setKeyIsName( keyIsNameDoom3Doom3Group ); |
| 132 | attachTraverse(); |
| 133 | |
| 134 | m_entity.attach( m_keyObservers ); |
| 135 | } |
| 136 | void destroy(){ |
| 137 | m_entity.detach( m_keyObservers ); |
| 138 | |
| 139 | if ( isModel() ) { |
| 140 | detachModel(); |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | detachTraverse(); |
| 145 | } |
| 146 | |
| 147 | m_traverseObservers.detach( m_funcStaticOrigin ); |
| 148 | } |
| 149 | |
| 150 | void attachModel(){ |
| 151 | m_traversable = &m_model.getTraversable(); |
| 152 | m_model.attach( &m_traverseObservers ); |
| 153 | } |
| 154 | void detachModel(){ |
| 155 | m_traversable = 0; |
| 156 | m_model.detach( &m_traverseObservers ); |
| 157 | } |
| 158 | void attachTraverse(){ |
| 159 | m_traversable = &m_traverse; |
| 160 | m_traverse.attach( &m_traverseObservers ); |
| 161 | } |
| 162 | void detachTraverse(){ |
| 163 | m_traversable = 0; |
| 164 | m_traverse.detach( &m_traverseObservers ); |
| 165 | } |
| 166 | |
| 167 | bool isModel() const { |
| 168 | return m_isModel; |
| 169 | } |
| 170 | |
| 171 | void setIsModel( bool newValue ){ |
| 172 | if ( newValue && !m_isModel ) { |
| 173 | detachTraverse(); |
| 174 | attachModel(); |
| 175 | |
| 176 | m_nameKeys.setKeyIsName( Static<KeyIsName>::instance().m_keyIsName ); |
| 177 | m_model.modelChanged( m_modelKey.c_str() ); |
| 178 | } |
| 179 | else if ( !newValue && m_isModel ) { |
| 180 | detachModel(); |
| 181 | attachTraverse(); |
| 182 | |
| 183 | m_nameKeys.setKeyIsName( keyIsNameDoom3Doom3Group ); |
| 184 | } |
| 185 | m_isModel = newValue; |
| 186 | updateTransform(); |
| 187 | } |
| 188 | |
| 189 | void updateIsModel(){ |
| 190 | setIsModel( !string_equal( m_modelKey.c_str(), m_name.c_str() ) ); |
| 191 | } |
| 192 | |
| 193 | // vc 2k5 compiler fix |
| 194 | #if _MSC_VER >= 1400 |
| 195 | public: |
| 196 | #endif |
| 197 | |
| 198 | void nameChanged( const char* value ){ |
| 199 | m_name = value; |
| 200 | updateIsModel(); |
| 201 | } |
| 202 | typedef MemberCaller<Doom3Group, void(const char*), &Doom3Group::nameChanged> NameChangedCaller; |
| 203 | |
| 204 | void modelChanged( const char* value ){ |
| 205 | m_modelKey = value; |
| 206 | updateIsModel(); |
| 207 | if ( isModel() ) { |
| 208 | m_model.modelChanged( value ); |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | m_model.modelChanged( "" ); |
| 213 | } |
| 214 | } |
| 215 | typedef MemberCaller<Doom3Group, void(const char*), &Doom3Group::modelChanged> ModelChangedCaller; |
| 216 | |
| 217 | void updateTransform(){ |
| 218 | m_transform.localToParent() = g_matrix4_identity; |
| 219 | if ( isModel() ) { |
| 220 | matrix4_translate_by_vec3( m_transform.localToParent(), m_origin ); |
| 221 | matrix4_multiply_by_matrix4( m_transform.localToParent(), rotation_toMatrix( m_rotation ) ); |
| 222 | } |
| 223 | m_transformChanged(); |
| 224 | if ( !isModel() ) { |
| 225 | m_funcStaticOrigin.originChanged(); |
| 226 | } |
| 227 | } |
| 228 | typedef MemberCaller<Doom3Group, void(), &Doom3Group::updateTransform> UpdateTransformCaller; |
| 229 | |
| 230 | void originChanged(){ |
| 231 | m_origin = m_originKey.m_origin; |
| 232 | updateTransform(); |
| 233 | } |
| 234 | typedef MemberCaller<Doom3Group, void(), &Doom3Group::originChanged> OriginChangedCaller; |
| 235 | |
| 236 | void rotationChanged(){ |
| 237 | rotation_assign( m_rotation, m_rotationKey.m_rotation ); |
| 238 | updateTransform(); |
| 239 | } |
| 240 | typedef MemberCaller<Doom3Group, void(), &Doom3Group::rotationChanged> RotationChangedCaller; |
| 241 | |
| 242 | void skinChanged(){ |
| 243 | if ( isModel() ) { |
| 244 | scene::Node* node = m_model.getNode(); |
| 245 | if ( node != 0 ) { |
| 246 | Node_modelSkinChanged( *node ); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | typedef MemberCaller<Doom3Group, void(), &Doom3Group::skinChanged> SkinChangedCaller; |
| 251 | |
| 252 | public: |
| 253 | Doom3Group( EntityClass* eclass, scene::Node& node, const Callback<void()>& transformChanged, const Callback<void()>& boundsChanged, const Callback<void()>& evaluateTransform ) : |
| 254 | m_entity( eclass ), |
| 255 | m_originKey( OriginChangedCaller( *this ) ), |
| 256 | m_origin( ORIGINKEY_IDENTITY ), |
| 257 | m_rotationKey( RotationChangedCaller( *this ) ), |
| 258 | m_filter( m_entity, node ), |
| 259 | m_named( m_entity ), |
| 260 | m_nameKeys( m_entity ), |
| 261 | m_funcStaticOrigin( m_traverse, m_origin ), |
| 262 | m_name_origin( g_vector3_identity ), |
| 263 | m_renderName( m_named, m_name_origin ), |
| 264 | m_skin( SkinChangedCaller( *this ) ), |
| 265 | m_curveNURBS( boundsChanged ), |
| 266 | m_curveCatmullRom( boundsChanged ), |
| 267 | m_transformChanged( transformChanged ), |
| 268 | m_evaluateTransform( evaluateTransform ), |
| 269 | m_traversable( 0 ){ |
| 270 | construct(); |
| 271 | } |
| 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 ), |
| 278 | m_named( m_entity ), |
| 279 | m_nameKeys( m_entity ), |
| 280 | m_funcStaticOrigin( m_traverse, m_origin ), |
| 281 | m_name_origin( g_vector3_identity ), |
| 282 | m_renderName( m_named, m_name_origin ), |
| 283 | m_skin( SkinChangedCaller( *this ) ), |
| 284 | m_curveNURBS( boundsChanged ), |
| 285 | m_curveCatmullRom( boundsChanged ), |
| 286 | m_transformChanged( transformChanged ), |
| 287 | m_evaluateTransform( evaluateTransform ), |
| 288 | m_traversable( 0 ){ |
| 289 | construct(); |
| 290 | } |
| 291 | ~Doom3Group(){ |
| 292 | destroy(); |
| 293 | } |
| 294 | |
| 295 | InstanceCounter m_instanceCounter; |
| 296 | void instanceAttach( const scene::Path& path ){ |
| 297 | if ( ++m_instanceCounter.m_count == 1 ) { |
| 298 | m_filter.instanceAttach(); |
| 299 | m_entity.instanceAttach( path_find_mapfile( path.begin(), path.end() ) ); |
| 300 | m_traverse.instanceAttach( path_find_mapfile( path.begin(), path.end() ) ); |
| 301 | |
| 302 | m_funcStaticOrigin.enable(); |
| 303 | } |
| 304 | } |
| 305 | void instanceDetach( const scene::Path& path ){ |
| 306 | if ( --m_instanceCounter.m_count == 0 ) { |
| 307 | m_funcStaticOrigin.disable(); |
| 308 | |
| 309 | m_traverse.instanceDetach( path_find_mapfile( path.begin(), path.end() ) ); |
| 310 | m_entity.instanceDetach( path_find_mapfile( path.begin(), path.end() ) ); |
| 311 | m_filter.instanceDetach(); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | EntityKeyValues& getEntity(){ |
| 316 | return m_entity; |
| 317 | } |
| 318 | const EntityKeyValues& getEntity() const { |
| 319 | return m_entity; |
| 320 | } |
| 321 | |
| 322 | scene::Traversable& getTraversable(){ |
| 323 | return *m_traversable; |
| 324 | } |
| 325 | Namespaced& getNamespaced(){ |
| 326 | return m_nameKeys; |
| 327 | } |
| 328 | Nameable& getNameable(){ |
| 329 | return m_named; |
| 330 | } |
| 331 | TransformNode& getTransformNode(){ |
| 332 | return m_transform; |
| 333 | } |
| 334 | ModelSkin& getModelSkin(){ |
| 335 | return m_skin.get(); |
| 336 | } |
| 337 | |
| 338 | void attach( scene::Traversable::Observer* observer ){ |
| 339 | m_traverseObservers.attach( *observer ); |
| 340 | } |
| 341 | void detach( scene::Traversable::Observer* observer ){ |
| 342 | m_traverseObservers.detach( *observer ); |
| 343 | } |
| 344 | |
| 345 | const AABB& localAABB() const { |
| 346 | m_curveBounds = m_curveNURBS.m_bounds; |
| 347 | aabb_extend_by_aabb_safe( m_curveBounds, m_curveCatmullRom.m_bounds ); |
| 348 | return m_curveBounds; |
| 349 | } |
| 350 | |
| 351 | void renderSolid( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected ) const { |
| 352 | if ( isModel() && selected ) { |
| 353 | m_renderOrigin.render( renderer, volume, localToWorld ); |
| 354 | } |
| 355 | |
| 356 | renderer.SetState( m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly ); |
| 357 | renderer.SetState( m_entity.getEntityClass().m_state_wire, Renderer::eFullMaterials ); |
| 358 | |
| 359 | if ( !m_curveNURBS.m_renderCurve.m_vertices.empty() ) { |
| 360 | renderer.addRenderable( m_curveNURBS.m_renderCurve, localToWorld ); |
| 361 | } |
| 362 | if ( !m_curveCatmullRom.m_renderCurve.m_vertices.empty() ) { |
| 363 | renderer.addRenderable( m_curveCatmullRom.m_renderCurve, localToWorld ); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void renderWireframe( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected, const AABB& childBounds ) const { |
| 368 | renderSolid( renderer, volume, localToWorld, selected ); |
| 369 | |
| 370 | if ( g_showNames ) { |
| 371 | // draw models as usual |
| 372 | if ( !isModel() ) { |
| 373 | // don't draw the name for worldspawn |
| 374 | if ( !strcmp( m_entity.getEntityClass().name(), "worldspawn" ) ) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | // place name in the middle of the "children cloud" |
| 379 | m_name_origin = childBounds.origin; |
| 380 | } |
| 381 | |
| 382 | renderer.addRenderable( m_renderName, localToWorld ); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | void testSelect( Selector& selector, SelectionTest& test, SelectionIntersection& best ){ |
| 387 | PointVertexArray_testSelect( &m_curveNURBS.m_renderCurve.m_vertices[0], m_curveNURBS.m_renderCurve.m_vertices.size(), test, best ); |
| 388 | PointVertexArray_testSelect( &m_curveCatmullRom.m_renderCurve.m_vertices[0], m_curveCatmullRom.m_renderCurve.m_vertices.size(), test, best ); |
| 389 | } |
| 390 | |
| 391 | void translate( const Vector3& translation ){ |
| 392 | m_origin = origin_translated( m_origin, translation ); |
| 393 | } |
| 394 | void rotate( const Quaternion& rotation ){ |
| 395 | rotation_rotate( m_rotation, rotation ); |
| 396 | } |
| 397 | void snapto( float snap ){ |
| 398 | m_originKey.m_origin = origin_snapped( m_originKey.m_origin, snap ); |
| 399 | m_originKey.write( &m_entity ); |
| 400 | } |
| 401 | void revertTransform(){ |
| 402 | m_origin = m_originKey.m_origin; |
| 403 | rotation_assign( m_rotation, m_rotationKey.m_rotation ); |
| 404 | m_curveNURBS.m_controlPointsTransformed = m_curveNURBS.m_controlPoints; |
| 405 | m_curveCatmullRom.m_controlPointsTransformed = m_curveCatmullRom.m_controlPoints; |
| 406 | } |
| 407 | void freezeTransform(){ |
| 408 | m_originKey.m_origin = m_origin; |
| 409 | m_originKey.write( &m_entity ); |
| 410 | rotation_assign( m_rotationKey.m_rotation, m_rotation ); |
| 411 | m_rotationKey.write( &m_entity ); |
| 412 | m_curveNURBS.m_controlPoints = m_curveNURBS.m_controlPointsTransformed; |
| 413 | ControlPoints_write( m_curveNURBS.m_controlPoints, curve_Nurbs, m_entity ); |
| 414 | m_curveCatmullRom.m_controlPoints = m_curveCatmullRom.m_controlPointsTransformed; |
| 415 | ControlPoints_write( m_curveCatmullRom.m_controlPoints, curve_CatmullRomSpline, m_entity ); |
| 416 | } |
| 417 | void transformChanged(){ |
| 418 | revertTransform(); |
| 419 | m_evaluateTransform(); |
| 420 | updateTransform(); |
| 421 | m_curveNURBS.curveChanged(); |
| 422 | m_curveCatmullRom.curveChanged(); |
| 423 | } |
| 424 | typedef MemberCaller<Doom3Group, void(), &Doom3Group::transformChanged> TransformChangedCaller; |
| 425 | }; |
| 426 | |
| 427 | class Doom3GroupInstance : |
| 428 | public TargetableInstance, |
| 429 | public TransformModifier, |
| 430 | public Renderable, |
| 431 | public SelectionTestable, |
| 432 | public ComponentSelectionTestable, |
| 433 | public ComponentEditable, |
| 434 | public ComponentSnappable |
| 435 | { |
| 436 | class TypeCasts |
| 437 | { |
| 438 | InstanceTypeCastTable m_casts; |
| 439 | public: |
| 440 | TypeCasts(){ |
| 441 | m_casts = TargetableInstance::StaticTypeCasts::instance().get(); |
| 442 | InstanceContainedCast<Doom3GroupInstance, Bounded>::install( m_casts ); |
| 443 | InstanceStaticCast<Doom3GroupInstance, Renderable>::install( m_casts ); |
| 444 | InstanceStaticCast<Doom3GroupInstance, SelectionTestable>::install( m_casts ); |
| 445 | InstanceStaticCast<Doom3GroupInstance, ComponentSelectionTestable>::install( m_casts ); |
| 446 | InstanceStaticCast<Doom3GroupInstance, ComponentEditable>::install( m_casts ); |
| 447 | InstanceStaticCast<Doom3GroupInstance, ComponentSnappable>::install( m_casts ); |
| 448 | InstanceStaticCast<Doom3GroupInstance, Transformable>::install( m_casts ); |
| 449 | InstanceIdentityCast<Doom3GroupInstance>::install( m_casts ); |
| 450 | } |
| 451 | InstanceTypeCastTable& get(){ |
| 452 | return m_casts; |
| 453 | } |
| 454 | }; |
| 455 | |
| 456 | Doom3Group& m_contained; |
| 457 | CurveEdit m_curveNURBS; |
| 458 | CurveEdit m_curveCatmullRom; |
| 459 | mutable AABB m_aabb_component; |
| 460 | public: |
| 461 | |
| 462 | typedef LazyStatic<TypeCasts> StaticTypeCasts; |
| 463 | |
| 464 | |
| 465 | Bounded& get( NullType<Bounded>){ |
| 466 | return m_contained; |
| 467 | } |
| 468 | |
| 469 | STRING_CONSTANT( Name, "Doom3GroupInstance" ); |
| 470 | |
| 471 | Doom3GroupInstance( const scene::Path& path, scene::Instance* parent, Doom3Group& contained ) : |
| 472 | TargetableInstance( path, parent, this, StaticTypeCasts::instance().get(), contained.getEntity(), *this ), |
| 473 | TransformModifier( Doom3Group::TransformChangedCaller( contained ), ApplyTransformCaller( *this ) ), |
| 474 | m_contained( contained ), |
| 475 | m_curveNURBS( m_contained.m_curveNURBS.m_controlPointsTransformed, SelectionChangedComponentCaller( *this ) ), |
| 476 | m_curveCatmullRom( m_contained.m_curveCatmullRom.m_controlPointsTransformed, SelectionChangedComponentCaller( *this ) ){ |
| 477 | m_contained.instanceAttach( Instance::path() ); |
| 478 | m_contained.m_curveNURBSChanged = m_contained.m_curveNURBS.connect( CurveEdit::CurveChangedCaller( m_curveNURBS ) ); |
| 479 | m_contained.m_curveCatmullRomChanged = m_contained.m_curveCatmullRom.connect( CurveEdit::CurveChangedCaller( m_curveCatmullRom ) ); |
| 480 | |
| 481 | StaticRenderableConnectionLines::instance().attach( *this ); |
| 482 | } |
| 483 | ~Doom3GroupInstance(){ |
| 484 | StaticRenderableConnectionLines::instance().detach( *this ); |
| 485 | |
| 486 | m_contained.m_curveCatmullRom.disconnect( m_contained.m_curveCatmullRomChanged ); |
| 487 | m_contained.m_curveNURBS.disconnect( m_contained.m_curveNURBSChanged ); |
| 488 | m_contained.instanceDetach( Instance::path() ); |
| 489 | } |
| 490 | void renderSolid( Renderer& renderer, const VolumeTest& volume ) const { |
| 491 | m_contained.renderSolid( renderer, volume, Instance::localToWorld(), getSelectable().isSelected() ); |
| 492 | |
| 493 | m_curveNURBS.renderComponentsSelected( renderer, volume, localToWorld() ); |
| 494 | m_curveCatmullRom.renderComponentsSelected( renderer, volume, localToWorld() ); |
| 495 | } |
| 496 | void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const { |
| 497 | m_contained.renderWireframe( renderer, volume, Instance::localToWorld(), getSelectable().isSelected(), Instance::childBounds() ); |
| 498 | |
| 499 | m_curveNURBS.renderComponentsSelected( renderer, volume, localToWorld() ); |
| 500 | m_curveCatmullRom.renderComponentsSelected( renderer, volume, localToWorld() ); |
| 501 | } |
| 502 | void renderComponents( Renderer& renderer, const VolumeTest& volume ) const { |
| 503 | if ( GlobalSelectionSystem().ComponentMode() == SelectionSystem::eVertex ) { |
| 504 | m_curveNURBS.renderComponents( renderer, volume, localToWorld() ); |
| 505 | m_curveCatmullRom.renderComponents( renderer, volume, localToWorld() ); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | void testSelect( Selector& selector, SelectionTest& test ){ |
| 510 | test.BeginMesh( localToWorld() ); |
| 511 | SelectionIntersection best; |
| 512 | |
| 513 | m_contained.testSelect( selector, test, best ); |
| 514 | |
| 515 | if ( best.valid() ) { |
| 516 | Selector_add( selector, getSelectable(), best ); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | bool isSelectedComponents() const { |
| 521 | return m_curveNURBS.isSelected() || m_curveCatmullRom.isSelected(); |
| 522 | } |
| 523 | void setSelectedComponents( bool selected, SelectionSystem::EComponentMode mode ){ |
| 524 | if ( mode == SelectionSystem::eVertex ) { |
| 525 | m_curveNURBS.setSelected( selected ); |
| 526 | m_curveCatmullRom.setSelected( selected ); |
| 527 | } |
| 528 | } |
| 529 | void testSelectComponents( Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode ){ |
| 530 | if ( mode == SelectionSystem::eVertex ) { |
| 531 | test.BeginMesh( localToWorld() ); |
| 532 | m_curveNURBS.testSelect( selector, test ); |
| 533 | m_curveCatmullRom.testSelect( selector, test ); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | void transformComponents( const Matrix4& matrix ){ |
| 538 | if ( m_curveNURBS.isSelected() ) { |
| 539 | m_curveNURBS.transform( matrix ); |
| 540 | } |
| 541 | if ( m_curveCatmullRom.isSelected() ) { |
| 542 | m_curveCatmullRom.transform( matrix ); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | const AABB& getSelectedComponentsBounds() const { |
| 547 | m_aabb_component = AABB(); |
| 548 | m_curveNURBS.forEachSelected([&](const Vector3 &point) { |
| 549 | aabb_extend_by_point_safe(m_aabb_component, point); |
| 550 | }); |
| 551 | m_curveCatmullRom.forEachSelected([&](const Vector3 &point) { |
| 552 | aabb_extend_by_point_safe(m_aabb_component, point); |
| 553 | }); |
| 554 | return m_aabb_component; |
| 555 | } |
| 556 | |
| 557 | void snapComponents( float snap ){ |
| 558 | if ( m_curveNURBS.isSelected() ) { |
| 559 | m_curveNURBS.snapto( snap ); |
| 560 | m_curveNURBS.write( curve_Nurbs, m_contained.getEntity() ); |
| 561 | } |
| 562 | if ( m_curveCatmullRom.isSelected() ) { |
| 563 | m_curveCatmullRom.snapto( snap ); |
| 564 | m_curveCatmullRom.write( curve_CatmullRomSpline, m_contained.getEntity() ); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | void evaluateTransform(){ |
| 569 | if ( getType() == TRANSFORM_PRIMITIVE ) { |
| 570 | m_contained.translate( getTranslation() ); |
| 571 | m_contained.rotate( getRotation() ); |
| 572 | } |
| 573 | else |
| 574 | { |
| 575 | transformComponents( calculateTransform() ); |
| 576 | } |
| 577 | } |
| 578 | void applyTransform(){ |
| 579 | m_contained.revertTransform(); |
| 580 | evaluateTransform(); |
| 581 | m_contained.freezeTransform(); |
| 582 | } |
| 583 | typedef MemberCaller<Doom3GroupInstance, void(), &Doom3GroupInstance::applyTransform> ApplyTransformCaller; |
| 584 | |
| 585 | void selectionChangedComponent( const Selectable& selectable ){ |
| 586 | GlobalSelectionSystem().getObserver ( SelectionSystem::eComponent )( selectable ); |
| 587 | GlobalSelectionSystem().onComponentSelection( *this, selectable ); |
| 588 | } |
| 589 | typedef MemberCaller<Doom3GroupInstance, void(const Selectable&), &Doom3GroupInstance::selectionChangedComponent> SelectionChangedComponentCaller; |
| 590 | }; |
| 591 | |
| 592 | class Doom3GroupNode : |
| 593 | public scene::Node::Symbiot, |
| 594 | public scene::Instantiable, |
| 595 | public scene::Cloneable, |
| 596 | public scene::Traversable::Observer |
| 597 | { |
| 598 | class TypeCasts |
| 599 | { |
| 600 | NodeTypeCastTable m_casts; |
| 601 | public: |
| 602 | TypeCasts(){ |
| 603 | NodeStaticCast<Doom3GroupNode, scene::Instantiable>::install( m_casts ); |
| 604 | NodeStaticCast<Doom3GroupNode, scene::Cloneable>::install( m_casts ); |
| 605 | NodeContainedCast<Doom3GroupNode, scene::Traversable>::install( m_casts ); |
| 606 | NodeContainedCast<Doom3GroupNode, Snappable>::install( m_casts ); |
| 607 | NodeContainedCast<Doom3GroupNode, TransformNode>::install( m_casts ); |
| 608 | NodeContainedCast<Doom3GroupNode, Entity>::install( m_casts ); |
| 609 | NodeContainedCast<Doom3GroupNode, Nameable>::install( m_casts ); |
| 610 | NodeContainedCast<Doom3GroupNode, Namespaced>::install( m_casts ); |
| 611 | NodeContainedCast<Doom3GroupNode, ModelSkin>::install( m_casts ); |
| 612 | } |
| 613 | NodeTypeCastTable& get(){ |
| 614 | return m_casts; |
| 615 | } |
| 616 | }; |
| 617 | |
| 618 | |
| 619 | scene::Node m_node; |
| 620 | InstanceSet m_instances; |
| 621 | Doom3Group m_contained; |
| 622 | |
| 623 | void construct(){ |
| 624 | m_contained.attach( this ); |
| 625 | } |
| 626 | void destroy(){ |
| 627 | m_contained.detach( this ); |
| 628 | } |
| 629 | public: |
| 630 | |
| 631 | typedef LazyStatic<TypeCasts> StaticTypeCasts; |
| 632 | |
| 633 | scene::Traversable& get( NullType<scene::Traversable>){ |
| 634 | return m_contained.getTraversable(); |
| 635 | } |
| 636 | Snappable& get( NullType<Snappable>){ |
| 637 | return m_contained; |
| 638 | } |
| 639 | TransformNode& get( NullType<TransformNode>){ |
| 640 | return m_contained.getTransformNode(); |
| 641 | } |
| 642 | Entity& get( NullType<Entity>){ |
| 643 | return m_contained.getEntity(); |
| 644 | } |
| 645 | Nameable& get( NullType<Nameable>){ |
| 646 | return m_contained.getNameable(); |
| 647 | } |
| 648 | Namespaced& get( NullType<Namespaced>){ |
| 649 | return m_contained.getNamespaced(); |
| 650 | } |
| 651 | ModelSkin& get( NullType<ModelSkin>){ |
| 652 | return m_contained.getModelSkin(); |
| 653 | } |
| 654 | |
| 655 | Doom3GroupNode( EntityClass* eclass ) : |
| 656 | m_node( this, this, StaticTypeCasts::instance().get() ), |
| 657 | m_contained( eclass, m_node, InstanceSet::TransformChangedCaller( m_instances ), InstanceSet::BoundsChangedCaller( m_instances ), InstanceSetEvaluateTransform<Doom3GroupInstance>::Caller( m_instances ) ){ |
| 658 | construct(); |
| 659 | } |
| 660 | Doom3GroupNode( const Doom3GroupNode& other ) : |
| 661 | scene::Node::Symbiot( other ), |
| 662 | scene::Instantiable( other ), |
| 663 | scene::Cloneable( other ), |
| 664 | scene::Traversable::Observer( other ), |
| 665 | m_node( this, this, StaticTypeCasts::instance().get() ), |
| 666 | m_contained( other.m_contained, m_node, InstanceSet::TransformChangedCaller( m_instances ), InstanceSet::BoundsChangedCaller( m_instances ), InstanceSetEvaluateTransform<Doom3GroupInstance>::Caller( m_instances ) ){ |
| 667 | construct(); |
| 668 | } |
| 669 | ~Doom3GroupNode(){ |
| 670 | destroy(); |
| 671 | } |
| 672 | |
| 673 | void release(){ |
| 674 | delete this; |
| 675 | } |
| 676 | scene::Node& node(){ |
| 677 | return m_node; |
| 678 | } |
| 679 | |
| 680 | scene::Node& clone() const { |
| 681 | return ( new Doom3GroupNode( *this ) )->node(); |
| 682 | } |
| 683 | |
| 684 | void insert( scene::Node& child ){ |
| 685 | m_instances.insert( child ); |
| 686 | } |
| 687 | void erase( scene::Node& child ){ |
| 688 | m_instances.erase( child ); |
| 689 | } |
| 690 | |
| 691 | scene::Instance* create( const scene::Path& path, scene::Instance* parent ){ |
| 692 | return new Doom3GroupInstance( path, parent, m_contained ); |
| 693 | } |
| 694 | void forEachInstance( const scene::Instantiable::Visitor& visitor ){ |
| 695 | m_instances.forEachInstance( visitor ); |
| 696 | } |
| 697 | void insert( scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance ){ |
| 698 | m_instances.insert( observer, path, instance ); |
| 699 | } |
| 700 | scene::Instance* erase( scene::Instantiable::Observer* observer, const scene::Path& path ){ |
| 701 | return m_instances.erase( observer, path ); |
| 702 | } |
| 703 | }; |
| 704 | |
| 705 | void Doom3Group_construct(){ |
| 706 | CurveEdit::Type::instance().m_controlsShader = GlobalShaderCache().capture( "$POINT" ); |
| 707 | CurveEdit::Type::instance().m_selectedShader = GlobalShaderCache().capture( "$SELPOINT" ); |
| 708 | } |
| 709 | |
| 710 | void Doom3Group_destroy(){ |
| 711 | GlobalShaderCache().release( "$SELPOINT" ); |
| 712 | GlobalShaderCache().release( "$POINT" ); |
| 713 | } |
| 714 | |
| 715 | scene::Node& New_Doom3Group( EntityClass* eclass ){ |
| 716 | return ( new Doom3GroupNode( eclass ) )->node(); |
| 717 | } |