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;
86 RotationKey m_rotationKey;
89 ClassnameFilter m_filter;
92 TraversableObserverPairRelay m_traverseObservers;
93 Doom3GroupOrigin m_funcStaticOrigin;
94 RenderablePivot m_renderOrigin;
95 RenderableNamedEntity m_renderName;
96 mutable Vector3 m_name_origin;
100 NURBSCurve m_curveNURBS;
101 SignalHandlerId m_curveNURBSChanged;
102 CatmullRomSpline m_curveCatmullRom;
103 SignalHandlerId m_curveCatmullRomChanged;
105 mutable AABB m_curveBounds;
107 Callback m_transformChanged;
108 Callback m_evaluateTransform;
111 CopiedString m_modelKey;
114 scene::Traversable* m_traversable;
118 default_rotation(m_rotation);
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));
131 m_traverseObservers.attach(m_funcStaticOrigin);
133 m_nameKeys.setKeyIsName(keyIsNameDoom3Doom3Group);
136 m_entity.attach(m_keyObservers);
140 m_entity.detach(m_keyObservers);
151 m_traverseObservers.detach(m_funcStaticOrigin);
156 m_traversable = &m_model.getTraversable();
157 m_model.attach(&m_traverseObservers);
162 m_model.detach(&m_traverseObservers);
164 void attachTraverse()
166 m_traversable = &m_traverse;
167 m_traverse.attach(&m_traverseObservers);
169 void detachTraverse()
172 m_traverse.detach(&m_traverseObservers);
180 void setIsModel(bool newValue)
182 if(newValue && !m_isModel)
187 m_nameKeys.setKeyIsName(Static<KeyIsName>::instance().m_keyIsName);
188 m_model.modelChanged(m_modelKey.c_str());
190 else if(!newValue && m_isModel)
195 m_nameKeys.setKeyIsName(keyIsNameDoom3Doom3Group);
197 m_isModel = newValue;
203 setIsModel(!string_equal(m_modelKey.c_str(), m_name.c_str()));
206 void nameChanged(const char* value)
211 typedef MemberCaller1<Doom3Group, const char*, &Doom3Group::nameChanged> NameChangedCaller;
213 void modelChanged(const char* value)
219 m_model.modelChanged(value);
223 m_model.modelChanged("");
226 typedef MemberCaller1<Doom3Group, const char*, &Doom3Group::modelChanged> ModelChangedCaller;
228 void updateTransform()
230 m_transform.localToParent() = g_matrix4_identity;
233 matrix4_translate_by_vec3(m_transform.localToParent(), m_origin);
234 matrix4_multiply_by_matrix4(m_transform.localToParent(), rotation_toMatrix(m_rotation));
236 m_transformChanged();
239 m_funcStaticOrigin.originChanged();
242 typedef MemberCaller<Doom3Group, &Doom3Group::updateTransform> UpdateTransformCaller;
246 m_origin = m_originKey.m_origin;
249 typedef MemberCaller<Doom3Group, &Doom3Group::originChanged> OriginChangedCaller;
251 void rotationChanged()
253 rotation_assign(m_rotation, m_rotationKey.m_rotation);
256 typedef MemberCaller<Doom3Group, &Doom3Group::rotationChanged> RotationChangedCaller;
262 scene::Node* node = m_model.getNode();
265 Node_modelSkinChanged(*node);
269 typedef MemberCaller<Doom3Group, &Doom3Group::skinChanged> SkinChangedCaller;
272 Doom3Group(EntityClass* eclass, scene::Node& node, const Callback& transformChanged, const Callback& boundsChanged, const Callback& evaluateTransform) :
274 m_originKey(OriginChangedCaller(*this)),
275 m_origin(ORIGINKEY_IDENTITY),
276 m_rotationKey(RotationChangedCaller(*this)),
277 m_filter(m_entity, node),
279 m_nameKeys(m_entity),
280 m_funcStaticOrigin(m_traverse, m_origin),
281 m_renderName(m_named, 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),
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),
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),
316 InstanceCounter m_instanceCounter;
317 void instanceAttach(const scene::Path& path)
319 if(++m_instanceCounter.m_count == 1)
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()));
325 m_funcStaticOrigin.enable();
328 void instanceDetach(const scene::Path& path)
330 if(--m_instanceCounter.m_count == 0)
332 m_funcStaticOrigin.disable();
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();
340 EntityKeyValues& getEntity()
344 const EntityKeyValues& getEntity() const
349 scene::Traversable& getTraversable()
351 return *m_traversable;
353 Namespaced& getNamespaced()
357 Nameable& getNameable()
361 TransformNode& getTransformNode()
365 ModelSkin& getModelSkin()
370 void attach(scene::Traversable::Observer* observer)
372 m_traverseObservers.attach(*observer);
374 void detach(scene::Traversable::Observer* observer)
376 m_traverseObservers.detach(*observer);
379 const AABB& localAABB() const
381 m_curveBounds = m_curveNURBS.m_bounds;
382 aabb_extend_by_aabb_safe(m_curveBounds, m_curveCatmullRom.m_bounds);
383 return m_curveBounds;
386 void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected) const
388 if(isModel() && selected)
390 m_renderOrigin.render(renderer, volume, localToWorld);
393 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly);
394 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eFullMaterials);
396 if(!m_curveNURBS.m_renderCurve.m_vertices.empty())
398 renderer.addRenderable(m_curveNURBS.m_renderCurve, localToWorld);
400 if(!m_curveCatmullRom.m_renderCurve.m_vertices.empty())
402 renderer.addRenderable(m_curveCatmullRom.m_renderCurve, localToWorld);
406 void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected, const AABB& childBounds) const
408 renderSolid(renderer, volume, localToWorld, selected);
412 // draw models as usual
415 // don't draw the name for worldspawn
416 if(!strcmp(m_entity.getEntityClass().name(), "worldspawn"))
419 // place name in the middle of the "children cloud"
420 m_name_origin = childBounds.origin;
423 renderer.addRenderable(m_renderName, localToWorld);
427 void testSelect(Selector& selector, SelectionTest& test, SelectionIntersection& best)
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);
433 void translate(const Vector3& translation)
435 m_origin = origin_translated(m_origin, translation);
437 void rotate(const Quaternion& rotation)
439 rotation_rotate(m_rotation, rotation);
441 void snapto(float snap)
443 m_originKey.m_origin = origin_snapped(m_originKey.m_origin, snap);
444 m_originKey.write(&m_entity);
446 void revertTransform()
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;
453 void freezeTransform()
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);
464 void transformChanged()
467 m_evaluateTransform();
469 m_curveNURBS.curveChanged();
470 m_curveCatmullRom.curveChanged();
472 typedef MemberCaller<Doom3Group, &Doom3Group::transformChanged> TransformChangedCaller;
475 class ControlPointAddBounds
479 ControlPointAddBounds(AABB& bounds) : m_bounds(bounds)
482 void operator()(const Vector3& point) const
484 aabb_extend_by_point_safe(m_bounds, point);
488 class Doom3GroupInstance :
489 public TargetableInstance,
490 public TransformModifier,
492 public SelectionTestable,
493 public ComponentSelectionTestable,
494 public ComponentEditable,
495 public ComponentSnappable
499 InstanceTypeCastTable m_casts;
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);
513 InstanceTypeCastTable& get()
519 Doom3Group& m_contained;
520 CurveEdit m_curveNURBS;
521 CurveEdit m_curveCatmullRom;
522 mutable AABB m_aabb_component;
525 typedef LazyStatic<TypeCasts> StaticTypeCasts;
528 Bounded& get(NullType<Bounded>)
533 STRING_CONSTANT(Name, "Doom3GroupInstance");
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))
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));
546 StaticRenderableConnectionLines::instance().attach(*this);
548 ~Doom3GroupInstance()
550 StaticRenderableConnectionLines::instance().detach(*this);
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());
556 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
558 m_contained.renderSolid(renderer, volume, Instance::localToWorld(), getSelectable().isSelected());
560 m_curveNURBS.renderComponentsSelected(renderer, volume, localToWorld());
561 m_curveCatmullRom.renderComponentsSelected(renderer, volume, localToWorld());
563 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
565 m_contained.renderWireframe(renderer, volume, Instance::localToWorld(), getSelectable().isSelected(), Instance::childBounds());
567 m_curveNURBS.renderComponentsSelected(renderer, volume, localToWorld());
568 m_curveCatmullRom.renderComponentsSelected(renderer, volume, localToWorld());
570 void renderComponents(Renderer& renderer, const VolumeTest& volume) const
572 if(GlobalSelectionSystem().ComponentMode() == SelectionSystem::eVertex)
574 m_curveNURBS.renderComponents(renderer, volume, localToWorld());
575 m_curveCatmullRom.renderComponents(renderer, volume, localToWorld());
579 void testSelect(Selector& selector, SelectionTest& test)
581 test.BeginMesh(localToWorld());
582 SelectionIntersection best;
584 m_contained.testSelect(selector, test, best);
588 Selector_add(selector, getSelectable(), best);
592 bool isSelectedComponents() const
594 return m_curveNURBS.isSelected() || m_curveCatmullRom.isSelected();
596 void setSelectedComponents(bool selected, SelectionSystem::EComponentMode mode)
598 if(mode == SelectionSystem::eVertex)
600 m_curveNURBS.setSelected(selected);
601 m_curveCatmullRom.setSelected(selected);
604 void testSelectComponents(Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode)
606 if(mode == SelectionSystem::eVertex)
608 test.BeginMesh(localToWorld());
609 m_curveNURBS.testSelect(selector, test);
610 m_curveCatmullRom.testSelect(selector, test);
614 void transformComponents(const Matrix4& matrix)
616 if(m_curveNURBS.isSelected())
618 m_curveNURBS.transform(matrix);
620 if(m_curveCatmullRom.isSelected())
622 m_curveCatmullRom.transform(matrix);
626 const AABB& getSelectedComponentsBounds() const
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;
634 void snapComponents(float snap)
636 if(m_curveNURBS.isSelected())
638 m_curveNURBS.snapto(snap);
639 m_curveNURBS.write(curve_Nurbs, m_contained.getEntity());
641 if(m_curveCatmullRom.isSelected())
643 m_curveCatmullRom.snapto(snap);
644 m_curveCatmullRom.write(curve_CatmullRomSpline, m_contained.getEntity());
648 void evaluateTransform()
650 if(getType() == TRANSFORM_PRIMITIVE)
652 m_contained.translate(getTranslation());
653 m_contained.rotate(getRotation());
657 transformComponents(calculateTransform());
660 void applyTransform()
662 m_contained.revertTransform();
664 m_contained.freezeTransform();
666 typedef MemberCaller<Doom3GroupInstance, &Doom3GroupInstance::applyTransform> ApplyTransformCaller;
668 void selectionChangedComponent(const Selectable& selectable)
670 GlobalSelectionSystem().getObserver(SelectionSystem::eComponent)(selectable);
671 GlobalSelectionSystem().onComponentSelection(*this, selectable);
673 typedef MemberCaller1<Doom3GroupInstance, const Selectable&, &Doom3GroupInstance::selectionChangedComponent> SelectionChangedComponentCaller;
676 class Doom3GroupNode :
677 public scene::Node::Symbiot,
678 public scene::Instantiable,
679 public scene::Cloneable,
680 public scene::Traversable::Observer
684 NodeTypeCastTable m_casts;
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);
698 NodeTypeCastTable& get()
706 InstanceSet m_instances;
707 Doom3Group m_contained;
711 m_contained.attach(this);
715 m_contained.detach(this);
719 typedef LazyStatic<TypeCasts> StaticTypeCasts;
721 scene::Traversable& get(NullType<scene::Traversable>)
723 return m_contained.getTraversable();
725 Snappable& get(NullType<Snappable>)
729 TransformNode& get(NullType<TransformNode>)
731 return m_contained.getTransformNode();
733 Entity& get(NullType<Entity>)
735 return m_contained.getEntity();
737 Nameable& get(NullType<Nameable>)
739 return m_contained.getNameable();
741 Namespaced& get(NullType<Namespaced>)
743 return m_contained.getNamespaced();
745 ModelSkin& get(NullType<ModelSkin>)
747 return m_contained.getModelSkin();
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))
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))
780 scene::Node& clone() const
782 return (new Doom3GroupNode(*this))->node();
785 void insert(scene::Node& child)
787 m_instances.insert(child);
789 void erase(scene::Node& child)
791 m_instances.erase(child);
794 scene::Instance* create(const scene::Path& path, scene::Instance* parent)
796 return new Doom3GroupInstance(path, parent, m_contained);
798 void forEachInstance(const scene::Instantiable::Visitor& visitor)
800 m_instances.forEachInstance(visitor);
802 void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
804 m_instances.insert(observer, path, instance);
806 scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
808 return m_instances.erase(observer, path);
812 void Doom3Group_construct()
814 CurveEdit::Type::instance().m_controlsShader = GlobalShaderCache().capture("$POINT");
815 CurveEdit::Type::instance().m_selectedShader = GlobalShaderCache().capture("$SELPOINT");
818 void Doom3Group_destroy()
820 GlobalShaderCache().release("$SELPOINT");
821 GlobalShaderCache().release("$POINT");
824 scene::Node& New_Doom3Group(EntityClass* eclass)
826 return (new Doom3GroupNode(eclass))->node();