]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/eclassmodel.cpp
Merge remote-tracking branch 'ttimo/master'
[xonotic/netradiant.git] / plugins / entity / eclassmodel.cpp
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 entity which has a fixed size specified in its entity-definition and displays a model (e.g. ammo_bfg).
24 ///
25 /// This entity displays the model specified in its entity-definition.
26 /// The "origin" and "angle" keys directly control the entity's local-to-parent transform.
27 /// The "rotation" key directly controls the entity's local-to-parent transform for Doom3 only.
28
29 #include "eclassmodel.h"
30
31 #include "cullable.h"
32 #include "renderable.h"
33 #include "editable.h"
34
35 #include "selectionlib.h"
36 #include "instancelib.h"
37 #include "transformlib.h"
38 #include "traverselib.h"
39 #include "entitylib.h"
40 #include "render.h"
41 #include "eclasslib.h"
42 #include "pivot.h"
43
44 #include "targetable.h"
45 #include "origin.h"
46 #include "angle.h"
47 #include "rotation.h"
48 #include "model.h"
49 #include "filters.h"
50 #include "namedentity.h"
51 #include "keyobservers.h"
52 #include "namekeys.h"
53 #include "modelskinkey.h"
54
55 #include "entity.h"
56
57 class EclassModel :
58         public Snappable
59 {
60 MatrixTransform m_transform;
61 EntityKeyValues m_entity;
62 KeyObserverMap m_keyObservers;
63
64 OriginKey m_originKey;
65 Vector3 m_origin;
66 AngleKey m_angleKey;
67 float m_angle;
68 RotationKey m_rotationKey;
69 Float9 m_rotation;
70 SingletonModel m_model;
71
72 ClassnameFilter m_filter;
73 NamedEntity m_named;
74 NameKeys m_nameKeys;
75 RenderablePivot m_renderOrigin;
76 RenderableNamedEntity m_renderName;
77 ModelSkinKey m_skin;
78
79 Callback m_transformChanged;
80 Callback m_evaluateTransform;
81
82 void construct(){
83         default_rotation( m_rotation );
84
85         m_keyObservers.insert( "classname", ClassnameFilter::ClassnameChangedCaller( m_filter ) );
86         m_keyObservers.insert( Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller( m_named ) );
87         if ( g_gameType == eGameTypeDoom3 ) {
88                 m_keyObservers.insert( "angle", RotationKey::AngleChangedCaller( m_rotationKey ) );
89                 m_keyObservers.insert( "rotation", RotationKey::RotationChangedCaller( m_rotationKey ) );
90         }
91         else
92         {
93                 m_keyObservers.insert( "angle", AngleKey::AngleChangedCaller( m_angleKey ) );
94         }
95         m_keyObservers.insert( "origin", OriginKey::OriginChangedCaller( m_originKey ) );
96 }
97
98 // vc 2k5 compiler fix
99 #if _MSC_VER >= 1400
100 public:
101 #endif
102
103 void updateTransform(){
104         m_transform.localToParent() = g_matrix4_identity;
105         matrix4_translate_by_vec3( m_transform.localToParent(), m_origin );
106
107         if ( g_gameType == eGameTypeDoom3 ) {
108                 matrix4_multiply_by_matrix4( m_transform.localToParent(), rotation_toMatrix( m_rotation ) );
109         }
110         else
111         {
112                 matrix4_multiply_by_matrix4( m_transform.localToParent(), matrix4_rotation_for_z_degrees( m_angle ) );
113         }
114
115         m_transformChanged();
116 }
117 typedef MemberCaller<EclassModel, &EclassModel::updateTransform> UpdateTransformCaller;
118
119 void originChanged(){
120         m_origin = m_originKey.m_origin;
121         updateTransform();
122 }
123 typedef MemberCaller<EclassModel, &EclassModel::originChanged> OriginChangedCaller;
124 void angleChanged(){
125         m_angle = m_angleKey.m_angle;
126         updateTransform();
127 }
128 typedef MemberCaller<EclassModel, &EclassModel::angleChanged> AngleChangedCaller;
129 void rotationChanged(){
130         rotation_assign( m_rotation, m_rotationKey.m_rotation );
131         updateTransform();
132 }
133 typedef MemberCaller<EclassModel, &EclassModel::rotationChanged> RotationChangedCaller;
134
135 void skinChanged(){
136         scene::Node* node = m_model.getNode();
137         if ( node != 0 ) {
138                 Node_modelSkinChanged( *node );
139         }
140 }
141 typedef MemberCaller<EclassModel, &EclassModel::skinChanged> SkinChangedCaller;
142
143 public:
144
145 EclassModel( EntityClass* eclass, scene::Node& node, const Callback& transformChanged, const Callback& evaluateTransform ) :
146         m_entity( eclass ),
147         m_originKey( OriginChangedCaller( *this ) ),
148         m_origin( ORIGINKEY_IDENTITY ),
149         m_angleKey( AngleChangedCaller( *this ) ),
150         m_angle( ANGLEKEY_IDENTITY ),
151         m_rotationKey( RotationChangedCaller( *this ) ),
152         m_filter( m_entity, node ),
153         m_named( m_entity ),
154         m_nameKeys( m_entity ),
155         m_renderName( m_named, g_vector3_identity ),
156         m_skin( SkinChangedCaller( *this ) ),
157         m_transformChanged( transformChanged ),
158         m_evaluateTransform( evaluateTransform ){
159         construct();
160 }
161 EclassModel( const EclassModel& other, scene::Node& node, const Callback& transformChanged, const Callback& evaluateTransform ) :
162         m_entity( other.m_entity ),
163         m_originKey( OriginChangedCaller( *this ) ),
164         m_origin( ORIGINKEY_IDENTITY ),
165         m_angleKey( AngleChangedCaller( *this ) ),
166         m_angle( ANGLEKEY_IDENTITY ),
167         m_rotationKey( RotationChangedCaller( *this ) ),
168         m_filter( m_entity, node ),
169         m_named( m_entity ),
170         m_nameKeys( m_entity ),
171         m_renderName( m_named, g_vector3_identity ),
172         m_skin( SkinChangedCaller( *this ) ),
173         m_transformChanged( transformChanged ),
174         m_evaluateTransform( evaluateTransform ){
175         construct();
176 }
177
178 InstanceCounter m_instanceCounter;
179 void instanceAttach( const scene::Path& path ){
180         if ( ++m_instanceCounter.m_count == 1 ) {
181                 m_filter.instanceAttach();
182                 m_entity.instanceAttach( path_find_mapfile( path.begin(), path.end() ) );
183                 m_entity.attach( m_keyObservers );
184                 m_model.modelChanged( m_entity.getEntityClass().modelpath() );
185                 m_skin.skinChanged( m_entity.getEntityClass().skin() );
186         }
187 }
188 void instanceDetach( const scene::Path& path ){
189         if ( --m_instanceCounter.m_count == 0 ) {
190                 m_skin.skinChanged( "" );
191                 m_model.modelChanged( "" );
192                 m_entity.detach( m_keyObservers );
193                 m_entity.instanceDetach( path_find_mapfile( path.begin(), path.end() ) );
194                 m_filter.instanceDetach();
195         }
196 }
197
198 EntityKeyValues& getEntity(){
199         return m_entity;
200 }
201 const EntityKeyValues& getEntity() const {
202         return m_entity;
203 }
204
205 scene::Traversable& getTraversable(){
206         return m_model.getTraversable();
207 }
208 Namespaced& getNamespaced(){
209         return m_nameKeys;
210 }
211 Nameable& getNameable(){
212         return m_named;
213 }
214 TransformNode& getTransformNode(){
215         return m_transform;
216 }
217 ModelSkin& getModelSkin(){
218         return m_skin.get();
219 }
220
221 void attach( scene::Traversable::Observer* observer ){
222         m_model.attach( observer );
223 }
224 void detach( scene::Traversable::Observer* observer ){
225         m_model.detach( observer );
226 }
227
228 void renderSolid( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected ) const {
229         if ( selected ) {
230                 m_renderOrigin.render( renderer, volume, localToWorld );
231         }
232
233         renderer.SetState( m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly );
234 }
235 void renderWireframe( Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected ) const {
236         renderSolid( renderer, volume, localToWorld, selected );
237         if ( g_showNames ) {
238                 renderer.addRenderable( m_renderName, localToWorld );
239         }
240 }
241
242 void translate( const Vector3& translation ){
243         m_origin = origin_translated( m_origin, translation );
244 }
245 void rotate( const Quaternion& rotation ){
246         if ( g_gameType == eGameTypeDoom3 ) {
247                 rotation_rotate( m_rotation, rotation );
248         }
249         else
250         {
251                 m_angle = angle_rotated( m_angle, rotation );
252         }
253 }
254 void snapto( float snap ){
255         m_originKey.m_origin = origin_snapped( m_originKey.m_origin, snap );
256         m_originKey.write( &m_entity );
257 }
258 void revertTransform(){
259         m_origin = m_originKey.m_origin;
260         if ( g_gameType == eGameTypeDoom3 ) {
261                 rotation_assign( m_rotation, m_rotationKey.m_rotation );
262         }
263         else
264         {
265                 m_angle = m_angleKey.m_angle;
266         }
267 }
268 void freezeTransform(){
269         m_originKey.m_origin = m_origin;
270         m_originKey.write( &m_entity );
271         if ( g_gameType == eGameTypeDoom3 ) {
272                 rotation_assign( m_rotationKey.m_rotation, m_rotation );
273                 m_rotationKey.write( &m_entity );
274         }
275         else
276         {
277                 m_angleKey.m_angle = m_angle;
278                 m_angleKey.write( &m_entity );
279         }
280 }
281 void transformChanged(){
282         revertTransform();
283         m_evaluateTransform();
284         updateTransform();
285 }
286 typedef MemberCaller<EclassModel, &EclassModel::transformChanged> TransformChangedCaller;
287 };
288
289 class EclassModelInstance : public TargetableInstance, public TransformModifier, public Renderable
290 {
291 class TypeCasts
292 {
293 InstanceTypeCastTable m_casts;
294 public:
295 TypeCasts(){
296         m_casts = TargetableInstance::StaticTypeCasts::instance().get();
297         InstanceStaticCast<EclassModelInstance, Renderable>::install( m_casts );
298         InstanceStaticCast<EclassModelInstance, Transformable>::install( m_casts );
299         InstanceIdentityCast<EclassModelInstance>::install( m_casts );
300 }
301 InstanceTypeCastTable& get(){
302         return m_casts;
303 }
304 };
305
306 EclassModel& m_contained;
307 public:
308 typedef LazyStatic<TypeCasts> StaticTypeCasts;
309
310 STRING_CONSTANT( Name, "EclassModelInstance" );
311
312 EclassModelInstance( const scene::Path& path, scene::Instance* parent, EclassModel& contained ) :
313         TargetableInstance( path, parent, this, StaticTypeCasts::instance().get(), contained.getEntity(), *this ),
314         TransformModifier( EclassModel::TransformChangedCaller( contained ), ApplyTransformCaller( *this ) ),
315         m_contained( contained ){
316         m_contained.instanceAttach( Instance::path() );
317
318         StaticRenderableConnectionLines::instance().attach( *this );
319 }
320 ~EclassModelInstance(){
321         StaticRenderableConnectionLines::instance().detach( *this );
322
323         m_contained.instanceDetach( Instance::path() );
324 }
325 void renderSolid( Renderer& renderer, const VolumeTest& volume ) const {
326         m_contained.renderSolid( renderer, volume, Instance::localToWorld(), getSelectable().isSelected() );
327 }
328 void renderWireframe( Renderer& renderer, const VolumeTest& volume ) const {
329         m_contained.renderWireframe( renderer, volume, Instance::localToWorld(), getSelectable().isSelected() );
330 }
331
332 void evaluateTransform(){
333         if ( getType() == TRANSFORM_PRIMITIVE ) {
334                 m_contained.translate( getTranslation() );
335                 m_contained.rotate( getRotation() );
336         }
337 }
338 void applyTransform(){
339         m_contained.revertTransform();
340         evaluateTransform();
341         m_contained.freezeTransform();
342 }
343 typedef MemberCaller<EclassModelInstance, &EclassModelInstance::applyTransform> ApplyTransformCaller;
344 };
345
346 class EclassModelNode :
347         public scene::Node::Symbiot,
348         public scene::Instantiable,
349         public scene::Cloneable,
350         public scene::Traversable::Observer
351 {
352 class TypeCasts
353 {
354 NodeTypeCastTable m_casts;
355 public:
356 TypeCasts(){
357         NodeStaticCast<EclassModelNode, scene::Instantiable>::install( m_casts );
358         NodeStaticCast<EclassModelNode, scene::Cloneable>::install( m_casts );
359         NodeContainedCast<EclassModelNode, scene::Traversable>::install( m_casts );
360         NodeContainedCast<EclassModelNode, Snappable>::install( m_casts );
361         NodeContainedCast<EclassModelNode, TransformNode>::install( m_casts );
362         NodeContainedCast<EclassModelNode, Entity>::install( m_casts );
363         NodeContainedCast<EclassModelNode, Nameable>::install( m_casts );
364         NodeContainedCast<EclassModelNode, Namespaced>::install( m_casts );
365         NodeContainedCast<EclassModelNode, ModelSkin>::install( m_casts );
366 }
367 NodeTypeCastTable& get(){
368         return m_casts;
369 }
370 };
371
372
373 scene::Node m_node;
374 InstanceSet m_instances;
375 EclassModel m_contained;
376
377 void construct(){
378         m_contained.attach( this );
379 }
380 void destroy(){
381         m_contained.detach( this );
382 }
383
384 public:
385 typedef LazyStatic<TypeCasts> StaticTypeCasts;
386
387 scene::Traversable& get( NullType<scene::Traversable>){
388         return m_contained.getTraversable();
389 }
390 Snappable& get( NullType<Snappable>){
391         return m_contained;
392 }
393 TransformNode& get( NullType<TransformNode>){
394         return m_contained.getTransformNode();
395 }
396 Entity& get( NullType<Entity>){
397         return m_contained.getEntity();
398 }
399 Nameable& get( NullType<Nameable>){
400         return m_contained.getNameable();
401 }
402 Namespaced& get( NullType<Namespaced>){
403         return m_contained.getNamespaced();
404 }
405 ModelSkin& get( NullType<ModelSkin>){
406         return m_contained.getModelSkin();
407 }
408
409 EclassModelNode( EntityClass* eclass ) :
410         m_node( this, this, StaticTypeCasts::instance().get() ),
411         m_contained( eclass, m_node, InstanceSet::TransformChangedCaller( m_instances ), InstanceSetEvaluateTransform<EclassModelInstance>::Caller( m_instances ) ){
412         construct();
413 }
414 EclassModelNode( const EclassModelNode& other ) :
415         scene::Node::Symbiot( other ),
416         scene::Instantiable( other ),
417         scene::Cloneable( other ),
418         scene::Traversable::Observer( other ),
419         m_node( this, this, StaticTypeCasts::instance().get() ),
420         m_contained( other.m_contained, m_node, InstanceSet::TransformChangedCaller( m_instances ), InstanceSetEvaluateTransform<EclassModelInstance>::Caller( m_instances ) ){
421         construct();
422 }
423 ~EclassModelNode(){
424         destroy();
425 }
426 void release(){
427         delete this;
428 }
429 scene::Node& node(){
430         return m_node;
431 }
432
433 void insert( scene::Node& child ){
434         m_instances.insert( child );
435 }
436 void erase( scene::Node& child ){
437         m_instances.erase( child );
438 }
439
440 scene::Node& clone() const {
441         return ( new EclassModelNode( *this ) )->node();
442 }
443
444 scene::Instance* create( const scene::Path& path, scene::Instance* parent ){
445         return new EclassModelInstance( path, parent, m_contained );
446 }
447 void forEachInstance( const scene::Instantiable::Visitor& visitor ){
448         m_instances.forEachInstance( visitor );
449 }
450 void insert( scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance ){
451         m_instances.insert( observer, path, instance );
452 }
453 scene::Instance* erase( scene::Instantiable::Observer* observer, const scene::Path& path ){
454         return m_instances.erase( observer, path );
455 }
456 };
457
458 scene::Node& New_EclassModel( EntityClass* eclass ){
459         return ( new EclassModelNode( eclass ) )->node();
460 }