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)
65 reinterpret_cast<VertexPointer::pointer>(&first->vertex),
68 IndexPointer::index_type(count),
77 EntityKeyValues m_entity;
78 KeyObserverMap m_keyObservers;
79 TraversableNodeSet m_traverse;
80 MatrixTransform m_transform;
82 SingletonModel m_model;
83 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;
98 NURBSCurve m_curveNURBS;
99 SignalHandlerId m_curveNURBSChanged;
100 CatmullRomSpline m_curveCatmullRom;
101 SignalHandlerId m_curveCatmullRomChanged;
103 mutable AABB m_curveBounds;
105 Callback m_transformChanged;
106 Callback m_evaluateTransform;
109 CopiedString m_modelKey;
112 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);
138 m_entity.detach(m_keyObservers);
149 m_traverseObservers.detach(m_funcStaticOrigin);
154 m_traversable = &m_model.getTraversable();
155 m_model.attach(&m_traverseObservers);
160 m_model.detach(&m_traverseObservers);
162 void attachTraverse()
164 m_traversable = &m_traverse;
165 m_traverse.attach(&m_traverseObservers);
167 void detachTraverse()
170 m_traverse.detach(&m_traverseObservers);
178 void setIsModel(bool newValue)
180 if(newValue && !m_isModel)
185 m_nameKeys.setKeyIsName(Static<KeyIsName>::instance().m_keyIsName);
186 m_model.modelChanged(m_modelKey.c_str());
188 else if(!newValue && m_isModel)
193 m_nameKeys.setKeyIsName(keyIsNameDoom3Doom3Group);
195 m_isModel = newValue;
201 setIsModel(!string_empty(m_modelKey.c_str()) && !string_equal(m_modelKey.c_str(), m_name.c_str()));
204 void nameChanged(const char* value)
209 typedef MemberCaller1<Doom3Group, const char*, &Doom3Group::nameChanged> NameChangedCaller;
211 void modelChanged(const char* value)
217 m_model.modelChanged(value);
221 m_model.modelChanged("");
224 typedef MemberCaller1<Doom3Group, const char*, &Doom3Group::modelChanged> ModelChangedCaller;
226 void updateTransform()
228 m_transform.localToParent() = g_matrix4_identity;
231 matrix4_translate_by_vec3(m_transform.localToParent(), m_originKey.m_origin);
232 matrix4_multiply_by_matrix4(m_transform.localToParent(), rotation_toMatrix(m_rotationKey.m_rotation));
234 m_transformChanged();
237 m_funcStaticOrigin.originChanged();
240 typedef MemberCaller<Doom3Group, &Doom3Group::updateTransform> UpdateTransformCaller;
244 m_origin = m_originKey.m_origin;
247 typedef MemberCaller<Doom3Group, &Doom3Group::originChanged> OriginChangedCaller;
249 void rotationChanged()
251 rotation_assign(m_rotation, m_rotationKey.m_rotation);
254 typedef MemberCaller<Doom3Group, &Doom3Group::rotationChanged> RotationChangedCaller;
260 scene::Node* node = m_model.getNode();
263 Node_modelSkinChanged(*node);
267 typedef MemberCaller<Doom3Group, &Doom3Group::skinChanged> SkinChangedCaller;
270 Doom3Group(EntityClass* eclass, scene::Node& node, const Callback& transformChanged, const Callback& boundsChanged, const Callback& evaluateTransform) :
272 m_originKey(OriginChangedCaller(*this)),
273 m_origin(ORIGINKEY_IDENTITY),
274 m_rotationKey(RotationChangedCaller(*this)),
275 m_filter(m_entity, node),
277 m_nameKeys(m_entity),
278 m_funcStaticOrigin(m_traverse, m_origin),
279 m_renderName(m_named, g_vector3_identity),
280 m_skin(SkinChangedCaller(*this)),
281 m_curveNURBS(boundsChanged),
282 m_curveCatmullRom(boundsChanged),
283 m_transformChanged(transformChanged),
284 m_evaluateTransform(evaluateTransform),
289 Doom3Group(const Doom3Group& other, scene::Node& node, const Callback& transformChanged, const Callback& boundsChanged, const Callback& evaluateTransform) :
290 m_entity(other.m_entity),
291 m_originKey(OriginChangedCaller(*this)),
292 m_origin(ORIGINKEY_IDENTITY),
293 m_rotationKey(RotationChangedCaller(*this)),
294 m_filter(m_entity, node),
296 m_nameKeys(m_entity),
297 m_funcStaticOrigin(m_traverse, m_origin),
298 m_renderName(m_named, g_vector3_identity),
299 m_skin(SkinChangedCaller(*this)),
300 m_curveNURBS(boundsChanged),
301 m_curveCatmullRom(boundsChanged),
302 m_transformChanged(transformChanged),
303 m_evaluateTransform(evaluateTransform),
313 InstanceCounter m_instanceCounter;
314 void instanceAttach(const scene::Path& path)
316 if(++m_instanceCounter.m_count == 1)
318 m_filter.instanceAttach();
319 m_entity.instanceAttach(path_find_mapfile(path.begin(), path.end()));
320 m_traverse.instanceAttach(path_find_mapfile(path.begin(), path.end()));
322 m_funcStaticOrigin.enable();
325 void instanceDetach(const scene::Path& path)
327 if(--m_instanceCounter.m_count == 0)
329 m_funcStaticOrigin.disable();
331 m_traverse.instanceDetach(path_find_mapfile(path.begin(), path.end()));
332 m_entity.instanceDetach(path_find_mapfile(path.begin(), path.end()));
333 m_filter.instanceDetach();
337 EntityKeyValues& getEntity()
341 const EntityKeyValues& getEntity() const
346 scene::Traversable& getTraversable()
348 return *m_traversable;
350 Namespaced& getNamespaced()
354 Nameable& getNameable()
358 TransformNode& getTransformNode()
362 ModelSkin& getModelSkin()
367 void attach(scene::Traversable::Observer* observer)
369 m_traverseObservers.attach(*observer);
371 void detach(scene::Traversable::Observer* observer)
373 m_traverseObservers.detach(*observer);
376 const AABB& localAABB() const
378 m_curveBounds = m_curveNURBS.m_bounds;
379 aabb_extend_by_aabb_safe(m_curveBounds, m_curveCatmullRom.m_bounds);
380 return m_curveBounds;
383 void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected) const
385 if(isModel() && selected)
387 m_renderOrigin.render(renderer, volume, localToWorld);
390 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly);
391 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eFullMaterials);
393 if(!m_curveNURBS.m_renderCurve.m_vertices.empty())
395 renderer.addRenderable(m_curveNURBS.m_renderCurve, localToWorld);
397 if(!m_curveCatmullRom.m_renderCurve.m_vertices.empty())
399 renderer.addRenderable(m_curveCatmullRom.m_renderCurve, localToWorld);
402 void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected) const
404 renderSolid(renderer, volume, localToWorld, selected);
405 if(g_showNames && isModel())
407 renderer.addRenderable(m_renderName, localToWorld);
411 void testSelect(Selector& selector, SelectionTest& test, SelectionIntersection& best)
413 PointVertexArray_testSelect(&m_curveNURBS.m_renderCurve.m_vertices[0], m_curveNURBS.m_renderCurve.m_vertices.size(), test, best);
414 PointVertexArray_testSelect(&m_curveCatmullRom.m_renderCurve.m_vertices[0], m_curveCatmullRom.m_renderCurve.m_vertices.size(), test, best);
417 void translate(const Vector3& translation)
419 m_origin = origin_translated(m_origin, translation);
421 void rotate(const Quaternion& rotation)
423 rotation_rotate(m_rotation, rotation);
425 void snapto(float snap)
427 m_originKey.m_origin = origin_snapped(m_originKey.m_origin, snap);
428 m_originKey.write(&m_entity);
430 void revertTransform()
432 m_origin = m_originKey.m_origin;
433 rotation_assign(m_rotation, m_rotationKey.m_rotation);
434 m_curveNURBS.m_controlPointsTransformed = m_curveNURBS.m_controlPoints;
435 m_curveCatmullRom.m_controlPointsTransformed = m_curveCatmullRom.m_controlPoints;
437 void freezeTransform()
439 m_originKey.m_origin = m_origin;
440 m_originKey.write(&m_entity);
441 rotation_assign(m_rotationKey.m_rotation, m_rotation);
442 m_rotationKey.write(&m_entity);
443 m_curveNURBS.m_controlPoints = m_curveNURBS.m_controlPointsTransformed;
444 ControlPoints_write(m_curveNURBS.m_controlPoints, curve_Nurbs, m_entity);
445 m_curveCatmullRom.m_controlPoints = m_curveCatmullRom.m_controlPointsTransformed;
446 ControlPoints_write(m_curveCatmullRom.m_controlPoints, curve_CatmullRomSpline, m_entity);
448 void transformChanged()
451 m_evaluateTransform();
453 m_curveNURBS.curveChanged();
454 m_curveCatmullRom.curveChanged();
456 typedef MemberCaller<Doom3Group, &Doom3Group::transformChanged> TransformChangedCaller;
459 class ControlPointAddBounds
463 ControlPointAddBounds(AABB& bounds) : m_bounds(bounds)
466 void operator()(const Vector3& point) const
468 aabb_extend_by_point_safe(m_bounds, point);
472 class Doom3GroupInstance :
473 public TargetableInstance,
474 public TransformModifier,
476 public SelectionTestable,
477 public ComponentSelectionTestable,
478 public ComponentEditable,
479 public ComponentSnappable
483 InstanceTypeCastTable m_casts;
487 m_casts = TargetableInstance::StaticTypeCasts::instance().get();
488 InstanceContainedCast<Doom3GroupInstance, Bounded>::install(m_casts);
489 InstanceStaticCast<Doom3GroupInstance, Renderable>::install(m_casts);
490 InstanceStaticCast<Doom3GroupInstance, SelectionTestable>::install(m_casts);
491 InstanceStaticCast<Doom3GroupInstance, ComponentSelectionTestable>::install(m_casts);
492 InstanceStaticCast<Doom3GroupInstance, ComponentEditable>::install(m_casts);
493 InstanceStaticCast<Doom3GroupInstance, ComponentSnappable>::install(m_casts);
494 InstanceStaticCast<Doom3GroupInstance, Transformable>::install(m_casts);
495 InstanceIdentityCast<Doom3GroupInstance>::install(m_casts);
497 InstanceTypeCastTable& get()
503 Doom3Group& m_contained;
504 CurveEdit m_curveNURBS;
505 CurveEdit m_curveCatmullRom;
506 mutable AABB m_aabb_component;
509 typedef LazyStatic<TypeCasts> StaticTypeCasts;
512 Bounded& get(NullType<Bounded>)
517 STRING_CONSTANT(Name, "Doom3GroupInstance");
519 Doom3GroupInstance(const scene::Path& path, scene::Instance* parent, Doom3Group& contained) :
520 TargetableInstance(path, parent, this, StaticTypeCasts::instance().get(), contained.getEntity(), *this),
521 TransformModifier(Doom3Group::TransformChangedCaller(contained), ApplyTransformCaller(*this)),
522 m_contained(contained),
523 m_curveNURBS(m_contained.m_curveNURBS.m_controlPointsTransformed, SelectionChangedComponentCaller(*this)),
524 m_curveCatmullRom(m_contained.m_curveCatmullRom.m_controlPointsTransformed, SelectionChangedComponentCaller(*this))
526 m_contained.instanceAttach(Instance::path());
527 m_contained.m_curveNURBSChanged = m_contained.m_curveNURBS.connect(CurveEdit::CurveChangedCaller(m_curveNURBS));
528 m_contained.m_curveCatmullRomChanged = m_contained.m_curveCatmullRom.connect(CurveEdit::CurveChangedCaller(m_curveCatmullRom));
530 StaticRenderableConnectionLines::instance().attach(*this);
532 ~Doom3GroupInstance()
534 StaticRenderableConnectionLines::instance().detach(*this);
536 m_contained.m_curveCatmullRom.disconnect(m_contained.m_curveCatmullRomChanged);
537 m_contained.m_curveNURBS.disconnect(m_contained.m_curveNURBSChanged);
538 m_contained.instanceDetach(Instance::path());
540 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
542 m_contained.renderSolid(renderer, volume, Instance::localToWorld(), getSelectable().isSelected());
544 m_curveNURBS.renderComponentsSelected(renderer, volume, localToWorld());
545 m_curveCatmullRom.renderComponentsSelected(renderer, volume, localToWorld());
547 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
549 m_contained.renderWireframe(renderer, volume, Instance::localToWorld(), getSelectable().isSelected());
551 m_curveNURBS.renderComponentsSelected(renderer, volume, localToWorld());
552 m_curveCatmullRom.renderComponentsSelected(renderer, volume, localToWorld());
554 void renderComponents(Renderer& renderer, const VolumeTest& volume) const
556 if(GlobalSelectionSystem().ComponentMode() == SelectionSystem::eVertex)
558 m_curveNURBS.renderComponents(renderer, volume, localToWorld());
559 m_curveCatmullRom.renderComponents(renderer, volume, localToWorld());
563 void testSelect(Selector& selector, SelectionTest& test)
565 test.BeginMesh(localToWorld());
566 SelectionIntersection best;
568 m_contained.testSelect(selector, test, best);
572 Selector_add(selector, getSelectable(), best);
576 bool isSelectedComponents() const
578 return m_curveNURBS.isSelected() || m_curveCatmullRom.isSelected();
580 void setSelectedComponents(bool selected, SelectionSystem::EComponentMode mode)
582 if(mode == SelectionSystem::eVertex)
584 m_curveNURBS.setSelected(selected);
585 m_curveCatmullRom.setSelected(selected);
588 void testSelectComponents(Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode)
590 if(mode == SelectionSystem::eVertex)
592 test.BeginMesh(localToWorld());
593 m_curveNURBS.testSelect(selector, test);
594 m_curveCatmullRom.testSelect(selector, test);
598 void transformComponents(const Matrix4& matrix)
600 if(m_curveNURBS.isSelected())
602 m_curveNURBS.transform(matrix);
604 if(m_curveCatmullRom.isSelected())
606 m_curveCatmullRom.transform(matrix);
610 const AABB& getSelectedComponentsBounds() const
612 m_aabb_component = AABB();
613 m_curveNURBS.forEachSelected(ControlPointAddBounds(m_aabb_component));
614 m_curveCatmullRom.forEachSelected(ControlPointAddBounds(m_aabb_component));
615 return m_aabb_component;
618 void snapComponents(float snap)
620 if(m_curveNURBS.isSelected())
622 m_curveNURBS.snapto(snap);
623 m_curveNURBS.write(curve_Nurbs, m_contained.getEntity());
625 if(m_curveCatmullRom.isSelected())
627 m_curveCatmullRom.snapto(snap);
628 m_curveCatmullRom.write(curve_CatmullRomSpline, m_contained.getEntity());
632 void evaluateTransform()
634 if(getType() == TRANSFORM_PRIMITIVE)
636 m_contained.translate(getTranslation());
637 m_contained.rotate(getRotation());
641 transformComponents(calculateTransform());
644 void applyTransform()
646 m_contained.revertTransform();
648 m_contained.freezeTransform();
650 typedef MemberCaller<Doom3GroupInstance, &Doom3GroupInstance::applyTransform> ApplyTransformCaller;
652 void selectionChangedComponent(const Selectable& selectable)
654 GlobalSelectionSystem().getObserver(SelectionSystem::eComponent)(selectable);
655 GlobalSelectionSystem().onComponentSelection(*this, selectable);
657 typedef MemberCaller1<Doom3GroupInstance, const Selectable&, &Doom3GroupInstance::selectionChangedComponent> SelectionChangedComponentCaller;
660 class Doom3GroupNode :
661 public scene::Node::Symbiot,
662 public scene::Instantiable,
663 public scene::Cloneable,
664 public scene::Traversable::Observer
668 NodeTypeCastTable m_casts;
672 NodeStaticCast<Doom3GroupNode, scene::Instantiable>::install(m_casts);
673 NodeStaticCast<Doom3GroupNode, scene::Cloneable>::install(m_casts);
674 NodeContainedCast<Doom3GroupNode, scene::Traversable>::install(m_casts);
675 NodeContainedCast<Doom3GroupNode, Snappable>::install(m_casts);
676 NodeContainedCast<Doom3GroupNode, TransformNode>::install(m_casts);
677 NodeContainedCast<Doom3GroupNode, Entity>::install(m_casts);
678 NodeContainedCast<Doom3GroupNode, Nameable>::install(m_casts);
679 NodeContainedCast<Doom3GroupNode, Namespaced>::install(m_casts);
680 NodeContainedCast<Doom3GroupNode, ModelSkin>::install(m_casts);
682 NodeTypeCastTable& get()
690 InstanceSet m_instances;
691 Doom3Group m_contained;
695 m_contained.attach(this);
699 m_contained.detach(this);
703 typedef LazyStatic<TypeCasts> StaticTypeCasts;
705 scene::Traversable& get(NullType<scene::Traversable>)
707 return m_contained.getTraversable();
709 Snappable& get(NullType<Snappable>)
713 TransformNode& get(NullType<TransformNode>)
715 return m_contained.getTransformNode();
717 Entity& get(NullType<Entity>)
719 return m_contained.getEntity();
721 Nameable& get(NullType<Nameable>)
723 return m_contained.getNameable();
725 Namespaced& get(NullType<Namespaced>)
727 return m_contained.getNamespaced();
729 ModelSkin& get(NullType<ModelSkin>)
731 return m_contained.getModelSkin();
734 Doom3GroupNode(EntityClass* eclass) :
735 m_node(this, this, StaticTypeCasts::instance().get()),
736 m_contained(eclass, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSet::BoundsChangedCaller(m_instances), InstanceSetEvaluateTransform<Doom3GroupInstance>::Caller(m_instances))
740 Doom3GroupNode(const Doom3GroupNode& other) :
741 scene::Node::Symbiot(other),
742 scene::Instantiable(other),
743 scene::Cloneable(other),
744 scene::Traversable::Observer(other),
745 m_node(this, this, StaticTypeCasts::instance().get()),
746 m_contained(other.m_contained, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSet::BoundsChangedCaller(m_instances), InstanceSetEvaluateTransform<Doom3GroupInstance>::Caller(m_instances))
764 scene::Node& clone() const
766 return (new Doom3GroupNode(*this))->node();
769 void insert(scene::Node& child)
771 m_instances.insert(child);
773 void erase(scene::Node& child)
775 m_instances.erase(child);
778 scene::Instance* create(const scene::Path& path, scene::Instance* parent)
780 return new Doom3GroupInstance(path, parent, m_contained);
782 void forEachInstance(const scene::Instantiable::Visitor& visitor)
784 m_instances.forEachInstance(visitor);
786 void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
788 m_instances.insert(observer, path, instance);
790 scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
792 return m_instances.erase(observer, path);
796 void Doom3Group_construct()
798 CurveEdit::Type::instance().m_controlsShader = GlobalShaderCache().capture("$POINT");
799 CurveEdit::Type::instance().m_selectedShader = GlobalShaderCache().capture("$SELPOINT");
802 void Doom3Group_destroy()
804 GlobalShaderCache().release("$SELPOINT");
805 GlobalShaderCache().release("$POINT");
808 scene::Node& New_Doom3Group(EntityClass* eclass)
810 return (new Doom3GroupNode(eclass))->node();