]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/doom3group.cpp
- Entity names are now drawn for group entities in Doom3 and Quake 3 mode (namespace)
[xonotic/netradiant.git] / plugins / entity / doom3group.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 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 {
63   test.TestLineStrip(
64     VertexPointer(
65       reinterpret_cast<VertexPointer::pointer>(&first->vertex),
66       sizeof(PointVertex)
67     ),
68     IndexPointer::index_type(count),
69     best
70   );
71 }
72
73 class Doom3Group :
74   public Bounded,
75   public Snappable
76 {
77   EntityKeyValues m_entity;
78   KeyObserverMap m_keyObservers;
79   TraversableNodeSet m_traverse;
80   MatrixTransform m_transform;
81
82   SingletonModel m_model;
83   OriginKey m_originKey;
84   Vector3 m_origin;
85   
86   RotationKey m_rotationKey;
87   Float9 m_rotation;
88
89   ClassnameFilter m_filter;
90   NamedEntity m_named;
91   NameKeys m_nameKeys;
92   TraversableObserverPairRelay m_traverseObservers;
93   Doom3GroupOrigin m_funcStaticOrigin;
94   RenderablePivot m_renderOrigin;
95   RenderableNamedEntity m_renderName;
96   mutable Vector3 m_name_origin;
97   ModelSkinKey m_skin;
98
99 public:
100   NURBSCurve m_curveNURBS;
101   SignalHandlerId m_curveNURBSChanged;
102   CatmullRomSpline m_curveCatmullRom;
103   SignalHandlerId m_curveCatmullRomChanged;
104 private:
105   mutable AABB m_curveBounds;
106
107   Callback m_transformChanged;
108   Callback m_evaluateTransform;
109
110   CopiedString m_name;
111   CopiedString m_modelKey;
112   bool m_isModel;
113
114   scene::Traversable* m_traversable;
115
116   void construct()
117   {
118     default_rotation(m_rotation);
119
120     m_keyObservers.insert("classname", ClassnameFilter::ClassnameChangedCaller(m_filter));
121     m_keyObservers.insert(Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller(m_named));
122     m_keyObservers.insert("model", Doom3Group::ModelChangedCaller(*this));
123     m_keyObservers.insert("origin", OriginKey::OriginChangedCaller(m_originKey));
124     m_keyObservers.insert("angle", RotationKey::AngleChangedCaller(m_rotationKey));
125     m_keyObservers.insert("rotation", RotationKey::RotationChangedCaller(m_rotationKey));
126     m_keyObservers.insert("name", NameChangedCaller(*this));
127     m_keyObservers.insert(curve_Nurbs, NURBSCurve::CurveChangedCaller(m_curveNURBS));
128     m_keyObservers.insert(curve_CatmullRomSpline, CatmullRomSpline::CurveChangedCaller(m_curveCatmullRom));
129     m_keyObservers.insert("skin", ModelSkinKey::SkinChangedCaller(m_skin));
130
131     m_traverseObservers.attach(m_funcStaticOrigin);
132     m_isModel = false;
133     m_nameKeys.setKeyIsName(keyIsNameDoom3Doom3Group);
134     attachTraverse();
135
136     m_entity.attach(m_keyObservers);
137   }
138   void destroy()
139   {
140     m_entity.detach(m_keyObservers);
141
142     if(isModel())
143     {
144       detachModel();
145     }
146     else
147     {
148       detachTraverse();
149     }
150
151     m_traverseObservers.detach(m_funcStaticOrigin);
152   }
153
154   void attachModel()
155   {
156     m_traversable = &m_model.getTraversable();
157     m_model.attach(&m_traverseObservers);
158   }
159   void detachModel()
160   {
161     m_traversable = 0;
162     m_model.detach(&m_traverseObservers);
163   }
164   void attachTraverse()
165   {
166     m_traversable = &m_traverse;
167     m_traverse.attach(&m_traverseObservers);
168   }
169   void detachTraverse()
170   {
171     m_traversable = 0;
172     m_traverse.detach(&m_traverseObservers);
173   }
174
175   bool isModel() const
176   {
177     return m_isModel;
178   }
179  
180   void setIsModel(bool newValue)
181   {
182     if(newValue && !m_isModel)
183     {
184       detachTraverse();
185       attachModel();
186
187       m_nameKeys.setKeyIsName(Static<KeyIsName>::instance().m_keyIsName);
188       m_model.modelChanged(m_modelKey.c_str());
189     }
190     else if(!newValue && m_isModel)
191     {
192       detachModel();
193       attachTraverse();
194
195       m_nameKeys.setKeyIsName(keyIsNameDoom3Doom3Group);
196     }
197     m_isModel = newValue;
198     updateTransform();
199   }
200
201   void updateIsModel()
202   {
203     setIsModel(!string_equal(m_modelKey.c_str(), m_name.c_str()));
204   }
205
206   void nameChanged(const char* value)
207   {
208     m_name = value;
209     updateIsModel();
210   }
211   typedef MemberCaller1<Doom3Group, const char*, &Doom3Group::nameChanged> NameChangedCaller;
212
213   void modelChanged(const char* value)
214   {
215     m_modelKey = value;
216     updateIsModel();
217     if(isModel())
218     {
219       m_model.modelChanged(value);
220     }
221     else
222     {
223       m_model.modelChanged("");
224     }
225   }
226   typedef MemberCaller1<Doom3Group, const char*, &Doom3Group::modelChanged> ModelChangedCaller;
227
228   void updateTransform()
229   {
230     m_transform.localToParent() = g_matrix4_identity;
231     if(isModel())
232     {
233       matrix4_translate_by_vec3(m_transform.localToParent(), m_origin);
234       matrix4_multiply_by_matrix4(m_transform.localToParent(), rotation_toMatrix(m_rotation));
235     }
236     m_transformChanged();
237     if(!isModel())
238     {
239       m_funcStaticOrigin.originChanged();
240     }
241   }
242   typedef MemberCaller<Doom3Group, &Doom3Group::updateTransform> UpdateTransformCaller;
243
244   void originChanged()
245   {
246     m_origin = m_originKey.m_origin;
247     updateTransform();
248   }
249   typedef MemberCaller<Doom3Group, &Doom3Group::originChanged> OriginChangedCaller;
250
251   void rotationChanged()
252   {
253     rotation_assign(m_rotation, m_rotationKey.m_rotation);
254     updateTransform();
255   }
256   typedef MemberCaller<Doom3Group, &Doom3Group::rotationChanged> RotationChangedCaller;
257
258   void skinChanged()
259   {
260     if(isModel())
261     {
262       scene::Node* node = m_model.getNode();
263       if(node != 0)
264       {
265         Node_modelSkinChanged(*node);
266       }
267     }
268   }
269   typedef MemberCaller<Doom3Group, &Doom3Group::skinChanged> SkinChangedCaller;
270
271 public:
272   Doom3Group(EntityClass* eclass, scene::Node& node, const Callback& transformChanged, const Callback& boundsChanged, const Callback& evaluateTransform) :
273     m_entity(eclass),
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_renderName(m_named, m_name_origin),
282         m_name_origin(g_vector3_identity),
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   {
290     construct();
291   }
292   Doom3Group(const Doom3Group& other, scene::Node& node, const Callback& transformChanged, const Callback& boundsChanged, const Callback& evaluateTransform) :
293     m_entity(other.m_entity),
294     m_originKey(OriginChangedCaller(*this)),
295     m_origin(ORIGINKEY_IDENTITY),
296     m_rotationKey(RotationChangedCaller(*this)),
297     m_filter(m_entity, node),
298     m_named(m_entity),
299     m_nameKeys(m_entity),
300     m_funcStaticOrigin(m_traverse, m_origin),
301     m_renderName(m_named, g_vector3_identity),
302     m_skin(SkinChangedCaller(*this)),
303     m_curveNURBS(boundsChanged),
304     m_curveCatmullRom(boundsChanged),
305     m_transformChanged(transformChanged),
306     m_evaluateTransform(evaluateTransform),
307     m_traversable(0)
308   {
309     construct();
310   }
311   ~Doom3Group()
312   {
313     destroy();
314   }
315
316   InstanceCounter m_instanceCounter;
317   void instanceAttach(const scene::Path& path)
318   {
319     if(++m_instanceCounter.m_count == 1)
320     {
321       m_filter.instanceAttach();
322       m_entity.instanceAttach(path_find_mapfile(path.begin(), path.end()));
323       m_traverse.instanceAttach(path_find_mapfile(path.begin(), path.end()));
324
325       m_funcStaticOrigin.enable();
326     }
327   }
328   void instanceDetach(const scene::Path& path)
329   {
330     if(--m_instanceCounter.m_count == 0)
331     {
332       m_funcStaticOrigin.disable();
333
334       m_traverse.instanceDetach(path_find_mapfile(path.begin(), path.end()));
335       m_entity.instanceDetach(path_find_mapfile(path.begin(), path.end()));
336       m_filter.instanceDetach();
337     }
338   }
339
340   EntityKeyValues& getEntity()
341   {
342     return m_entity;
343   }
344   const EntityKeyValues& getEntity() const
345   {
346     return m_entity;
347   }
348
349   scene::Traversable& getTraversable()
350   {
351     return *m_traversable;
352   }
353   Namespaced& getNamespaced()
354   {
355     return m_nameKeys;
356   }
357   Nameable& getNameable()
358   {
359     return m_named;
360   }
361   TransformNode& getTransformNode()
362   {
363     return m_transform;
364   }
365   ModelSkin& getModelSkin()
366   {
367     return m_skin.get();
368   }
369
370   void attach(scene::Traversable::Observer* observer)
371   {
372     m_traverseObservers.attach(*observer);
373   }
374   void detach(scene::Traversable::Observer* observer)
375   {
376     m_traverseObservers.detach(*observer);
377   }
378
379   const AABB& localAABB() const
380   {
381     m_curveBounds = m_curveNURBS.m_bounds;
382     aabb_extend_by_aabb_safe(m_curveBounds, m_curveCatmullRom.m_bounds);
383     return m_curveBounds;
384   }
385
386   void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected) const
387   {
388     if(isModel() && selected)
389     {
390       m_renderOrigin.render(renderer, volume, localToWorld);
391     }
392
393     renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly);
394     renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eFullMaterials);
395
396     if(!m_curveNURBS.m_renderCurve.m_vertices.empty())
397     {
398       renderer.addRenderable(m_curveNURBS.m_renderCurve, localToWorld);
399     }
400     if(!m_curveCatmullRom.m_renderCurve.m_vertices.empty())
401     {
402       renderer.addRenderable(m_curveCatmullRom.m_renderCurve, localToWorld);
403     }
404   }
405
406   void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected, const AABB& childBounds) const
407   {
408     renderSolid(renderer, volume, localToWorld, selected);
409
410     if(g_showNames)
411     {
412       // draw models as usual
413       if(!isModel())
414       {
415         // don't draw the name for worldspawn
416         if(!strcmp(m_entity.getEntityClass().name(), "worldspawn"))
417             return;
418
419                 // place name in the middle of the "children cloud"
420                 m_name_origin = childBounds.origin;
421       }
422
423       renderer.addRenderable(m_renderName, localToWorld);
424     }
425   }
426
427   void testSelect(Selector& selector, SelectionTest& test, SelectionIntersection& best)
428   {
429     PointVertexArray_testSelect(&m_curveNURBS.m_renderCurve.m_vertices[0], m_curveNURBS.m_renderCurve.m_vertices.size(), test, best);
430     PointVertexArray_testSelect(&m_curveCatmullRom.m_renderCurve.m_vertices[0], m_curveCatmullRom.m_renderCurve.m_vertices.size(), test, best);
431   }
432
433   void translate(const Vector3& translation)
434   {
435     m_origin = origin_translated(m_origin, translation);
436   }
437   void rotate(const Quaternion& rotation)
438   {
439     rotation_rotate(m_rotation, rotation);
440   }
441   void snapto(float snap)
442   {
443     m_originKey.m_origin = origin_snapped(m_originKey.m_origin, snap);
444     m_originKey.write(&m_entity);
445   }
446   void revertTransform()
447   {
448     m_origin = m_originKey.m_origin;
449     rotation_assign(m_rotation, m_rotationKey.m_rotation);
450     m_curveNURBS.m_controlPointsTransformed = m_curveNURBS.m_controlPoints;
451     m_curveCatmullRom.m_controlPointsTransformed = m_curveCatmullRom.m_controlPoints;
452   }
453   void freezeTransform()
454   {
455     m_originKey.m_origin = m_origin;
456     m_originKey.write(&m_entity);
457     rotation_assign(m_rotationKey.m_rotation, m_rotation);
458     m_rotationKey.write(&m_entity);
459     m_curveNURBS.m_controlPoints = m_curveNURBS.m_controlPointsTransformed;
460     ControlPoints_write(m_curveNURBS.m_controlPoints, curve_Nurbs, m_entity);
461     m_curveCatmullRom.m_controlPoints = m_curveCatmullRom.m_controlPointsTransformed;
462     ControlPoints_write(m_curveCatmullRom.m_controlPoints, curve_CatmullRomSpline, m_entity);
463   }
464   void transformChanged()
465   {
466     revertTransform();
467     m_evaluateTransform();
468     updateTransform();
469     m_curveNURBS.curveChanged();
470     m_curveCatmullRom.curveChanged();
471   }
472   typedef MemberCaller<Doom3Group, &Doom3Group::transformChanged> TransformChangedCaller;
473 };
474
475 class ControlPointAddBounds
476 {
477   AABB& m_bounds;
478 public:
479   ControlPointAddBounds(AABB& bounds) : m_bounds(bounds)
480   {
481   }
482   void operator()(const Vector3& point) const
483   {
484     aabb_extend_by_point_safe(m_bounds, point);
485   }
486 };
487
488 class Doom3GroupInstance :
489   public TargetableInstance,
490   public TransformModifier,
491   public Renderable,
492   public SelectionTestable,
493   public ComponentSelectionTestable,
494   public ComponentEditable,
495   public ComponentSnappable
496 {
497   class TypeCasts
498   {
499     InstanceTypeCastTable m_casts;
500   public:
501     TypeCasts()
502     {
503       m_casts = TargetableInstance::StaticTypeCasts::instance().get();
504       InstanceContainedCast<Doom3GroupInstance, Bounded>::install(m_casts);
505       InstanceStaticCast<Doom3GroupInstance, Renderable>::install(m_casts);
506       InstanceStaticCast<Doom3GroupInstance, SelectionTestable>::install(m_casts);
507       InstanceStaticCast<Doom3GroupInstance, ComponentSelectionTestable>::install(m_casts);
508       InstanceStaticCast<Doom3GroupInstance, ComponentEditable>::install(m_casts);
509       InstanceStaticCast<Doom3GroupInstance, ComponentSnappable>::install(m_casts);
510       InstanceStaticCast<Doom3GroupInstance, Transformable>::install(m_casts);
511       InstanceIdentityCast<Doom3GroupInstance>::install(m_casts);
512     }
513     InstanceTypeCastTable& get()
514     {
515       return m_casts;
516     }
517   };
518
519   Doom3Group& m_contained;
520   CurveEdit m_curveNURBS;
521   CurveEdit m_curveCatmullRom;
522   mutable AABB m_aabb_component;
523 public:
524
525   typedef LazyStatic<TypeCasts> StaticTypeCasts;
526
527
528   Bounded& get(NullType<Bounded>)
529   {
530     return m_contained;
531   }
532
533   STRING_CONSTANT(Name, "Doom3GroupInstance");
534
535   Doom3GroupInstance(const scene::Path& path, scene::Instance* parent, Doom3Group& contained) :
536     TargetableInstance(path, parent, this, StaticTypeCasts::instance().get(), contained.getEntity(), *this),
537     TransformModifier(Doom3Group::TransformChangedCaller(contained), ApplyTransformCaller(*this)),
538     m_contained(contained),
539     m_curveNURBS(m_contained.m_curveNURBS.m_controlPointsTransformed, SelectionChangedComponentCaller(*this)),
540     m_curveCatmullRom(m_contained.m_curveCatmullRom.m_controlPointsTransformed, SelectionChangedComponentCaller(*this))
541   {
542     m_contained.instanceAttach(Instance::path());
543     m_contained.m_curveNURBSChanged = m_contained.m_curveNURBS.connect(CurveEdit::CurveChangedCaller(m_curveNURBS));
544     m_contained.m_curveCatmullRomChanged = m_contained.m_curveCatmullRom.connect(CurveEdit::CurveChangedCaller(m_curveCatmullRom));
545
546     StaticRenderableConnectionLines::instance().attach(*this);
547   }
548   ~Doom3GroupInstance()
549   {
550     StaticRenderableConnectionLines::instance().detach(*this);
551
552     m_contained.m_curveCatmullRom.disconnect(m_contained.m_curveCatmullRomChanged);
553     m_contained.m_curveNURBS.disconnect(m_contained.m_curveNURBSChanged);
554     m_contained.instanceDetach(Instance::path());
555   }
556   void renderSolid(Renderer& renderer, const VolumeTest& volume) const
557   {
558     m_contained.renderSolid(renderer, volume, Instance::localToWorld(), getSelectable().isSelected());
559
560     m_curveNURBS.renderComponentsSelected(renderer, volume, localToWorld());
561     m_curveCatmullRom.renderComponentsSelected(renderer, volume, localToWorld());
562   }
563   void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
564   {
565         m_contained.renderWireframe(renderer, volume, Instance::localToWorld(), getSelectable().isSelected(), Instance::childBounds());
566
567     m_curveNURBS.renderComponentsSelected(renderer, volume, localToWorld());
568     m_curveCatmullRom.renderComponentsSelected(renderer, volume, localToWorld());
569   }
570   void renderComponents(Renderer& renderer, const VolumeTest& volume) const
571   {
572     if(GlobalSelectionSystem().ComponentMode() == SelectionSystem::eVertex)
573     {
574       m_curveNURBS.renderComponents(renderer, volume, localToWorld());
575       m_curveCatmullRom.renderComponents(renderer, volume, localToWorld());
576     }
577   }
578
579   void testSelect(Selector& selector, SelectionTest& test)
580   {
581     test.BeginMesh(localToWorld());
582     SelectionIntersection best;
583
584     m_contained.testSelect(selector, test, best);
585
586     if(best.valid())
587     {
588       Selector_add(selector, getSelectable(), best);
589     }
590   }
591
592   bool isSelectedComponents() const
593   {
594     return m_curveNURBS.isSelected() || m_curveCatmullRom.isSelected();
595   }
596   void setSelectedComponents(bool selected, SelectionSystem::EComponentMode mode)
597   {
598     if(mode == SelectionSystem::eVertex)
599     {
600       m_curveNURBS.setSelected(selected);
601       m_curveCatmullRom.setSelected(selected);
602     }
603   }
604   void testSelectComponents(Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode)
605   {
606     if(mode == SelectionSystem::eVertex)
607     {
608       test.BeginMesh(localToWorld());
609       m_curveNURBS.testSelect(selector, test);
610       m_curveCatmullRom.testSelect(selector, test);
611     }
612   }
613
614   void transformComponents(const Matrix4& matrix)
615   {
616     if(m_curveNURBS.isSelected())
617     {
618       m_curveNURBS.transform(matrix);
619     }
620     if(m_curveCatmullRom.isSelected())
621     {
622       m_curveCatmullRom.transform(matrix);
623     }
624   }
625
626   const AABB& getSelectedComponentsBounds() const
627   {
628     m_aabb_component = AABB();
629     m_curveNURBS.forEachSelected(ControlPointAddBounds(m_aabb_component));
630     m_curveCatmullRom.forEachSelected(ControlPointAddBounds(m_aabb_component));
631     return m_aabb_component;
632   }
633
634   void snapComponents(float snap)
635   {
636     if(m_curveNURBS.isSelected())
637     {
638       m_curveNURBS.snapto(snap);
639       m_curveNURBS.write(curve_Nurbs, m_contained.getEntity());
640     }
641     if(m_curveCatmullRom.isSelected())
642     {
643       m_curveCatmullRom.snapto(snap);
644       m_curveCatmullRom.write(curve_CatmullRomSpline, m_contained.getEntity());
645     }
646   }
647
648   void evaluateTransform()
649   {
650     if(getType() == TRANSFORM_PRIMITIVE)
651     {
652       m_contained.translate(getTranslation());
653       m_contained.rotate(getRotation());
654     }
655     else
656     {
657       transformComponents(calculateTransform());
658     }
659   }
660   void applyTransform()
661   {
662     m_contained.revertTransform();
663     evaluateTransform();
664     m_contained.freezeTransform();
665   }
666   typedef MemberCaller<Doom3GroupInstance, &Doom3GroupInstance::applyTransform> ApplyTransformCaller;
667
668   void selectionChangedComponent(const Selectable& selectable)
669   {
670     GlobalSelectionSystem().getObserver(SelectionSystem::eComponent)(selectable);
671     GlobalSelectionSystem().onComponentSelection(*this, selectable);
672   }
673   typedef MemberCaller1<Doom3GroupInstance, const Selectable&, &Doom3GroupInstance::selectionChangedComponent> SelectionChangedComponentCaller;
674 };
675
676 class Doom3GroupNode :
677   public scene::Node::Symbiot,
678   public scene::Instantiable,
679   public scene::Cloneable,
680   public scene::Traversable::Observer
681 {
682   class TypeCasts
683   {
684     NodeTypeCastTable m_casts;
685   public:
686     TypeCasts()
687     {
688       NodeStaticCast<Doom3GroupNode, scene::Instantiable>::install(m_casts);
689       NodeStaticCast<Doom3GroupNode, scene::Cloneable>::install(m_casts);
690       NodeContainedCast<Doom3GroupNode, scene::Traversable>::install(m_casts);
691       NodeContainedCast<Doom3GroupNode, Snappable>::install(m_casts);
692       NodeContainedCast<Doom3GroupNode, TransformNode>::install(m_casts);
693       NodeContainedCast<Doom3GroupNode, Entity>::install(m_casts);
694       NodeContainedCast<Doom3GroupNode, Nameable>::install(m_casts);
695       NodeContainedCast<Doom3GroupNode, Namespaced>::install(m_casts);
696       NodeContainedCast<Doom3GroupNode, ModelSkin>::install(m_casts);
697     }
698     NodeTypeCastTable& get()
699     {
700       return m_casts;
701     }
702   };
703
704
705   scene::Node m_node;
706   InstanceSet m_instances;
707   Doom3Group m_contained;
708
709   void construct()
710   {
711     m_contained.attach(this);
712   }
713   void destroy()
714   {
715     m_contained.detach(this);
716   }
717 public:
718
719   typedef LazyStatic<TypeCasts> StaticTypeCasts;
720
721   scene::Traversable& get(NullType<scene::Traversable>)
722   {
723     return m_contained.getTraversable();
724   }
725   Snappable& get(NullType<Snappable>)
726   {
727     return m_contained;
728   }
729   TransformNode& get(NullType<TransformNode>)
730   {
731     return m_contained.getTransformNode();
732   }
733   Entity& get(NullType<Entity>)
734   {
735     return m_contained.getEntity();
736   }
737   Nameable& get(NullType<Nameable>)
738   {
739     return m_contained.getNameable();
740   }
741   Namespaced& get(NullType<Namespaced>)
742   {
743     return m_contained.getNamespaced();
744   }
745   ModelSkin& get(NullType<ModelSkin>)
746   {
747     return m_contained.getModelSkin();
748   }
749
750   Doom3GroupNode(EntityClass* eclass) :
751     m_node(this, this, StaticTypeCasts::instance().get()),
752       m_contained(eclass, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSet::BoundsChangedCaller(m_instances), InstanceSetEvaluateTransform<Doom3GroupInstance>::Caller(m_instances))
753   {
754     construct();
755   }
756   Doom3GroupNode(const Doom3GroupNode& other) :
757     scene::Node::Symbiot(other),
758     scene::Instantiable(other),
759     scene::Cloneable(other),
760     scene::Traversable::Observer(other),
761     m_node(this, this, StaticTypeCasts::instance().get()),
762     m_contained(other.m_contained, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSet::BoundsChangedCaller(m_instances), InstanceSetEvaluateTransform<Doom3GroupInstance>::Caller(m_instances))
763   {
764     construct();
765   }
766   ~Doom3GroupNode()
767   {
768     destroy();
769   }
770
771   void release()
772   {
773     delete this;
774   }
775   scene::Node& node()
776   {
777     return m_node;
778   }
779
780   scene::Node& clone() const
781   {
782     return (new Doom3GroupNode(*this))->node();
783   }
784
785   void insert(scene::Node& child)
786   {
787     m_instances.insert(child);
788   }
789   void erase(scene::Node& child)
790   {
791     m_instances.erase(child);
792   }
793
794   scene::Instance* create(const scene::Path& path, scene::Instance* parent)
795   {
796     return new Doom3GroupInstance(path, parent, m_contained);
797   }
798   void forEachInstance(const scene::Instantiable::Visitor& visitor)
799   {
800     m_instances.forEachInstance(visitor);
801   }
802   void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
803   {
804     m_instances.insert(observer, path, instance);
805   }
806   scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
807   {
808     return m_instances.erase(observer, path);
809   }
810 };
811
812 void Doom3Group_construct()
813 {
814   CurveEdit::Type::instance().m_controlsShader = GlobalShaderCache().capture("$POINT");
815   CurveEdit::Type::instance().m_selectedShader = GlobalShaderCache().capture("$SELPOINT");
816 }
817
818 void Doom3Group_destroy()
819 {
820   GlobalShaderCache().release("$SELPOINT");
821   GlobalShaderCache().release("$POINT");
822 }
823
824 scene::Node& New_Doom3Group(EntityClass* eclass)
825 {
826   return (new Doom3GroupNode(eclass))->node();
827 }