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 CatmullRomSpline m_curveCatmullRom;
101 mutable AABB m_curveBounds;
103 Callback m_transformChanged;
104 Callback m_evaluateTransform;
107 CopiedString m_modelKey;
110 scene::Traversable* m_traversable;
114 default_rotation(m_rotation);
116 m_keyObservers.insert("classname", ClassnameFilter::ClassnameChangedCaller(m_filter));
117 m_keyObservers.insert(Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller(m_named));
118 m_keyObservers.insert("model", Doom3Group::ModelChangedCaller(*this));
119 m_keyObservers.insert("origin", OriginKey::OriginChangedCaller(m_originKey));
120 m_keyObservers.insert("angle", RotationKey::AngleChangedCaller(m_rotationKey));
121 m_keyObservers.insert("rotation", RotationKey::RotationChangedCaller(m_rotationKey));
122 m_keyObservers.insert("name", NameChangedCaller(*this));
123 m_keyObservers.insert(curve_Nurbs, NURBSCurve::CurveChangedCaller(m_curveNURBS));
124 m_keyObservers.insert(curve_CatmullRomSpline, CatmullRomSpline::CurveChangedCaller(m_curveCatmullRom));
125 m_keyObservers.insert("skin", ModelSkinKey::SkinChangedCaller(m_skin));
127 m_traverseObservers.attach(m_funcStaticOrigin);
129 m_nameKeys.setKeyIsName(keyIsNameDoom3Doom3Group);
132 m_entity.attach(m_keyObservers);
136 m_entity.detach(m_keyObservers);
147 m_traverseObservers.detach(m_funcStaticOrigin);
152 m_traversable = &m_model.getTraversable();
153 m_model.attach(&m_traverseObservers);
158 m_model.detach(&m_traverseObservers);
160 void attachTraverse()
162 m_traversable = &m_traverse;
163 m_traverse.attach(&m_traverseObservers);
165 void detachTraverse()
168 m_traverse.detach(&m_traverseObservers);
176 void setIsModel(bool newValue)
178 if(newValue && !m_isModel)
183 m_nameKeys.setKeyIsName(Static<KeyIsName>::instance().m_keyIsName);
184 m_model.modelChanged(m_modelKey.c_str());
186 else if(!newValue && m_isModel)
191 m_nameKeys.setKeyIsName(keyIsNameDoom3Doom3Group);
193 m_isModel = newValue;
199 setIsModel(!string_empty(m_modelKey.c_str()) && !string_equal(m_modelKey.c_str(), m_name.c_str()));
202 void nameChanged(const char* value)
207 typedef MemberCaller1<Doom3Group, const char*, &Doom3Group::nameChanged> NameChangedCaller;
209 void modelChanged(const char* value)
215 m_model.modelChanged(value);
219 m_model.modelChanged("");
222 typedef MemberCaller1<Doom3Group, const char*, &Doom3Group::modelChanged> ModelChangedCaller;
224 void updateTransform()
226 m_transform.localToParent() = g_matrix4_identity;
229 matrix4_translate_by_vec3(m_transform.localToParent(), m_originKey.m_origin);
230 matrix4_multiply_by_matrix4(m_transform.localToParent(), rotation_toMatrix(m_rotationKey.m_rotation));
232 m_transformChanged();
235 m_funcStaticOrigin.originChanged();
238 typedef MemberCaller<Doom3Group, &Doom3Group::updateTransform> UpdateTransformCaller;
242 m_origin = m_originKey.m_origin;
245 typedef MemberCaller<Doom3Group, &Doom3Group::originChanged> OriginChangedCaller;
247 void rotationChanged()
249 rotation_assign(m_rotation, m_rotationKey.m_rotation);
252 typedef MemberCaller<Doom3Group, &Doom3Group::rotationChanged> RotationChangedCaller;
258 scene::Node* node = m_model.getNode();
261 Node_modelSkinChanged(*node);
265 typedef MemberCaller<Doom3Group, &Doom3Group::skinChanged> SkinChangedCaller;
268 Doom3Group(EntityClass* eclass, scene::Node& node, const Callback& transformChanged, const Callback& boundsChanged, const Callback& evaluateTransform) :
270 m_originKey(OriginChangedCaller(*this)),
271 m_origin(ORIGINKEY_IDENTITY),
272 m_rotationKey(RotationChangedCaller(*this)),
273 m_filter(m_entity, node),
275 m_nameKeys(m_entity),
276 m_funcStaticOrigin(m_traverse, m_origin),
277 m_renderName(m_named, g_vector3_identity),
278 m_skin(SkinChangedCaller(*this)),
279 m_curveNURBS(boundsChanged),
280 m_curveCatmullRom(boundsChanged),
281 m_transformChanged(transformChanged),
282 m_evaluateTransform(evaluateTransform),
287 Doom3Group(const Doom3Group& other, scene::Node& node, const Callback& transformChanged, const Callback& boundsChanged, const Callback& evaluateTransform) :
288 m_entity(other.m_entity),
289 m_originKey(OriginChangedCaller(*this)),
290 m_origin(ORIGINKEY_IDENTITY),
291 m_rotationKey(RotationChangedCaller(*this)),
292 m_filter(m_entity, node),
294 m_nameKeys(m_entity),
295 m_funcStaticOrigin(m_traverse, m_origin),
296 m_renderName(m_named, g_vector3_identity),
297 m_skin(SkinChangedCaller(*this)),
298 m_curveNURBS(boundsChanged),
299 m_curveCatmullRom(boundsChanged),
300 m_transformChanged(transformChanged),
301 m_evaluateTransform(evaluateTransform),
311 InstanceCounter m_instanceCounter;
312 void instanceAttach(const scene::Path& path)
314 if(++m_instanceCounter.m_count == 1)
316 m_filter.instanceAttach();
317 m_entity.instanceAttach(path_find_mapfile(path.begin(), path.end()));
318 m_traverse.instanceAttach(path_find_mapfile(path.begin(), path.end()));
320 m_funcStaticOrigin.enable();
323 void instanceDetach(const scene::Path& path)
325 if(--m_instanceCounter.m_count == 0)
327 m_funcStaticOrigin.disable();
329 m_traverse.instanceDetach(path_find_mapfile(path.begin(), path.end()));
330 m_entity.instanceDetach(path_find_mapfile(path.begin(), path.end()));
331 m_filter.instanceDetach();
335 EntityKeyValues& getEntity()
339 const EntityKeyValues& getEntity() const
344 scene::Traversable& getTraversable()
346 return *m_traversable;
348 Namespaced& getNamespaced()
352 Nameable& getNameable()
356 TransformNode& getTransformNode()
360 ModelSkin& getModelSkin()
365 void attach(scene::Traversable::Observer* observer)
367 m_traverseObservers.attach(*observer);
369 void detach(scene::Traversable::Observer* observer)
371 m_traverseObservers.detach(*observer);
374 const AABB& localAABB() const
376 m_curveBounds = m_curveNURBS.m_bounds;
377 aabb_extend_by_aabb_safe(m_curveBounds, m_curveCatmullRom.m_bounds);
378 return m_curveBounds;
381 void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected) const
383 if(isModel() && selected)
385 m_renderOrigin.render(renderer, volume, localToWorld);
388 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly);
389 renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eFullMaterials);
391 if(!m_curveNURBS.m_renderCurve.m_vertices.empty())
393 renderer.addRenderable(m_curveNURBS.m_renderCurve, localToWorld);
395 if(!m_curveCatmullRom.m_renderCurve.m_vertices.empty())
397 renderer.addRenderable(m_curveCatmullRom.m_renderCurve, localToWorld);
400 void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld, bool selected) const
402 renderSolid(renderer, volume, localToWorld, selected);
403 if(g_showNames && isModel())
405 renderer.addRenderable(m_renderName, localToWorld);
409 void testSelect(Selector& selector, SelectionTest& test, SelectionIntersection& best)
411 PointVertexArray_testSelect(&m_curveNURBS.m_renderCurve.m_vertices[0], m_curveNURBS.m_renderCurve.m_vertices.size(), test, best);
412 PointVertexArray_testSelect(&m_curveCatmullRom.m_renderCurve.m_vertices[0], m_curveCatmullRom.m_renderCurve.m_vertices.size(), test, best);
415 void translate(const Vector3& translation)
417 m_origin = origin_translated(m_origin, translation);
419 void rotate(const Quaternion& rotation)
421 rotation_rotate(m_rotation, rotation);
423 void snapto(float snap)
425 m_originKey.m_origin = origin_snapped(m_originKey.m_origin, snap);
426 m_originKey.write(&m_entity);
428 void revertTransform()
430 m_origin = m_originKey.m_origin;
431 rotation_assign(m_rotation, m_rotationKey.m_rotation);
432 m_curveNURBS.m_controlPointsTransformed = m_curveNURBS.m_controlPoints;
433 m_curveCatmullRom.m_controlPointsTransformed = m_curveCatmullRom.m_controlPoints;
435 void freezeTransform()
437 m_originKey.m_origin = m_origin;
438 m_originKey.write(&m_entity);
439 rotation_assign(m_rotationKey.m_rotation, m_rotation);
440 m_rotationKey.write(&m_entity);
441 m_curveNURBS.m_controlPoints = m_curveNURBS.m_controlPointsTransformed;
442 ControlPoints_write(m_curveNURBS.m_controlPoints, curve_Nurbs, m_entity);
443 m_curveCatmullRom.m_controlPoints = m_curveCatmullRom.m_controlPointsTransformed;
444 ControlPoints_write(m_curveCatmullRom.m_controlPoints, curve_CatmullRomSpline, m_entity);
446 void transformChanged()
449 m_evaluateTransform();
451 m_curveNURBS.curveChanged();
452 m_curveCatmullRom.curveChanged();
454 typedef MemberCaller<Doom3Group, &Doom3Group::transformChanged> TransformChangedCaller;
457 class ControlPointAddBounds
461 ControlPointAddBounds(AABB& bounds) : m_bounds(bounds)
464 void operator()(const Vector3& point) const
466 aabb_extend_by_point_safe(m_bounds, point);
470 class Doom3GroupInstance :
471 public TargetableInstance,
472 public TransformModifier,
474 public SelectionTestable,
475 public ComponentSelectionTestable,
476 public ComponentEditable,
477 public ComponentSnappable
481 InstanceTypeCastTable m_casts;
485 m_casts = TargetableInstance::StaticTypeCasts::instance().get();
486 InstanceContainedCast<Doom3GroupInstance, Bounded>::install(m_casts);
487 InstanceStaticCast<Doom3GroupInstance, Renderable>::install(m_casts);
488 InstanceStaticCast<Doom3GroupInstance, SelectionTestable>::install(m_casts);
489 InstanceStaticCast<Doom3GroupInstance, ComponentSelectionTestable>::install(m_casts);
490 InstanceStaticCast<Doom3GroupInstance, ComponentEditable>::install(m_casts);
491 InstanceStaticCast<Doom3GroupInstance, ComponentSnappable>::install(m_casts);
492 InstanceStaticCast<Doom3GroupInstance, Transformable>::install(m_casts);
493 InstanceIdentityCast<Doom3GroupInstance>::install(m_casts);
495 InstanceTypeCastTable& get()
501 Doom3Group& m_contained;
502 CurveEdit m_curveNURBS;
503 CurveEdit m_curveCatmullRom;
504 mutable AABB m_aabb_component;
507 typedef LazyStatic<TypeCasts> StaticTypeCasts;
510 Bounded& get(NullType<Bounded>)
515 STRING_CONSTANT(Name, "Doom3GroupInstance");
517 Doom3GroupInstance(const scene::Path& path, scene::Instance* parent, Doom3Group& contained) :
518 TargetableInstance(path, parent, this, StaticTypeCasts::instance().get(), contained.getEntity(), *this),
519 TransformModifier(Doom3Group::TransformChangedCaller(contained), ApplyTransformCaller(*this)),
520 m_contained(contained),
521 m_curveNURBS(m_contained.m_curveNURBS.m_controlPointsTransformed, SelectionChangedComponentCaller(*this)),
522 m_curveCatmullRom(m_contained.m_curveCatmullRom.m_controlPointsTransformed, SelectionChangedComponentCaller(*this))
524 m_contained.instanceAttach(Instance::path());
525 m_contained.m_curveNURBS.attach(CurveEdit::CurveChangedCaller(m_curveNURBS));
526 m_contained.m_curveCatmullRom.attach(CurveEdit::CurveChangedCaller(m_curveCatmullRom));
528 StaticRenderableConnectionLines::instance().attach(*this);
530 ~Doom3GroupInstance()
532 StaticRenderableConnectionLines::instance().detach(*this);
534 m_contained.m_curveCatmullRom.detach(CurveEdit::CurveChangedCaller(m_curveCatmullRom));
535 m_contained.m_curveNURBS.detach(CurveEdit::CurveChangedCaller(m_curveNURBS));
536 m_contained.instanceDetach(Instance::path());
538 void renderSolid(Renderer& renderer, const VolumeTest& volume) const
540 m_contained.renderSolid(renderer, volume, Instance::localToWorld(), getSelectable().isSelected());
542 m_curveNURBS.renderComponentsSelected(renderer, volume, localToWorld());
543 m_curveCatmullRom.renderComponentsSelected(renderer, volume, localToWorld());
545 void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
547 m_contained.renderWireframe(renderer, volume, Instance::localToWorld(), getSelectable().isSelected());
549 m_curveNURBS.renderComponentsSelected(renderer, volume, localToWorld());
550 m_curveCatmullRom.renderComponentsSelected(renderer, volume, localToWorld());
552 void renderComponents(Renderer& renderer, const VolumeTest& volume) const
554 if(GlobalSelectionSystem().ComponentMode() == SelectionSystem::eVertex)
556 m_curveNURBS.renderComponents(renderer, volume, localToWorld());
557 m_curveCatmullRom.renderComponents(renderer, volume, localToWorld());
561 void testSelect(Selector& selector, SelectionTest& test)
563 test.BeginMesh(localToWorld());
564 SelectionIntersection best;
566 m_contained.testSelect(selector, test, best);
570 Selector_add(selector, getSelectable(), best);
574 bool isSelectedComponents() const
576 return m_curveNURBS.isSelected() || m_curveCatmullRom.isSelected();
578 void setSelectedComponents(bool selected, SelectionSystem::EComponentMode mode)
580 if(mode == SelectionSystem::eVertex)
582 m_curveNURBS.setSelected(selected);
583 m_curveCatmullRom.setSelected(selected);
586 void testSelectComponents(Selector& selector, SelectionTest& test, SelectionSystem::EComponentMode mode)
588 if(mode == SelectionSystem::eVertex)
590 test.BeginMesh(localToWorld());
591 m_curveNURBS.testSelect(selector, test);
592 m_curveCatmullRom.testSelect(selector, test);
596 void transformComponents(const Matrix4& matrix)
598 if(m_curveNURBS.isSelected())
600 m_curveNURBS.transform(matrix);
602 if(m_curveCatmullRom.isSelected())
604 m_curveCatmullRom.transform(matrix);
608 const AABB& getSelectedComponentsBounds() const
610 m_aabb_component = AABB();
611 m_curveNURBS.forEachSelected(ControlPointAddBounds(m_aabb_component));
612 m_curveCatmullRom.forEachSelected(ControlPointAddBounds(m_aabb_component));
613 return m_aabb_component;
616 void snapComponents(float snap)
618 if(m_curveNURBS.isSelected())
620 m_curveNURBS.snapto(snap);
621 m_curveNURBS.write(curve_Nurbs, m_contained.getEntity());
623 if(m_curveCatmullRom.isSelected())
625 m_curveCatmullRom.snapto(snap);
626 m_curveCatmullRom.write(curve_CatmullRomSpline, m_contained.getEntity());
630 void evaluateTransform()
632 if(getType() == TRANSFORM_PRIMITIVE)
634 m_contained.translate(getTranslation());
635 m_contained.rotate(getRotation());
639 transformComponents(calculateTransform());
642 void applyTransform()
644 m_contained.revertTransform();
646 m_contained.freezeTransform();
648 typedef MemberCaller<Doom3GroupInstance, &Doom3GroupInstance::applyTransform> ApplyTransformCaller;
650 void selectionChangedComponent(const Selectable& selectable)
652 GlobalSelectionSystem().getObserver(SelectionSystem::eComponent)(selectable);
653 GlobalSelectionSystem().onComponentSelection(*this, selectable);
655 typedef MemberCaller1<Doom3GroupInstance, const Selectable&, &Doom3GroupInstance::selectionChangedComponent> SelectionChangedComponentCaller;
658 class Doom3GroupNode :
659 public scene::Node::Symbiot,
660 public scene::Instantiable,
661 public scene::Cloneable,
662 public scene::Traversable::Observer
666 NodeTypeCastTable m_casts;
670 NodeStaticCast<Doom3GroupNode, scene::Instantiable>::install(m_casts);
671 NodeStaticCast<Doom3GroupNode, scene::Cloneable>::install(m_casts);
672 NodeContainedCast<Doom3GroupNode, scene::Traversable>::install(m_casts);
673 NodeContainedCast<Doom3GroupNode, Snappable>::install(m_casts);
674 NodeContainedCast<Doom3GroupNode, TransformNode>::install(m_casts);
675 NodeContainedCast<Doom3GroupNode, Entity>::install(m_casts);
676 NodeContainedCast<Doom3GroupNode, Nameable>::install(m_casts);
677 NodeContainedCast<Doom3GroupNode, Namespaced>::install(m_casts);
678 NodeContainedCast<Doom3GroupNode, ModelSkin>::install(m_casts);
680 NodeTypeCastTable& get()
688 InstanceSet m_instances;
689 Doom3Group m_contained;
693 m_contained.attach(this);
697 m_contained.detach(this);
701 typedef LazyStatic<TypeCasts> StaticTypeCasts;
703 scene::Traversable& get(NullType<scene::Traversable>)
705 return m_contained.getTraversable();
707 Snappable& get(NullType<Snappable>)
711 TransformNode& get(NullType<TransformNode>)
713 return m_contained.getTransformNode();
715 Entity& get(NullType<Entity>)
717 return m_contained.getEntity();
719 Nameable& get(NullType<Nameable>)
721 return m_contained.getNameable();
723 Namespaced& get(NullType<Namespaced>)
725 return m_contained.getNamespaced();
727 ModelSkin& get(NullType<ModelSkin>)
729 return m_contained.getModelSkin();
732 Doom3GroupNode(EntityClass* eclass) :
733 m_node(this, this, StaticTypeCasts::instance().get()),
734 m_contained(eclass, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSet::BoundsChangedCaller(m_instances), InstanceSetEvaluateTransform<Doom3GroupInstance>::Caller(m_instances))
738 Doom3GroupNode(const Doom3GroupNode& other) :
739 scene::Node::Symbiot(other),
740 scene::Instantiable(other),
741 scene::Cloneable(other),
742 scene::Traversable::Observer(other),
743 m_node(this, this, StaticTypeCasts::instance().get()),
744 m_contained(other.m_contained, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSet::BoundsChangedCaller(m_instances), InstanceSetEvaluateTransform<Doom3GroupInstance>::Caller(m_instances))
762 scene::Node& clone() const
764 return (new Doom3GroupNode(*this))->node();
767 void insert(scene::Node& child)
769 m_instances.insert(child);
771 void erase(scene::Node& child)
773 m_instances.erase(child);
776 scene::Instance* create(const scene::Path& path, scene::Instance* parent)
778 return new Doom3GroupInstance(path, parent, m_contained);
780 void forEachInstance(const scene::Instantiable::Visitor& visitor)
782 m_instances.forEachInstance(visitor);
784 void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
786 m_instances.insert(observer, path, instance);
788 scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
790 return m_instances.erase(observer, path);
794 void Doom3Group_construct()
796 CurveEdit::Type::instance().m_controlsShader = GlobalShaderCache().capture("$POINT");
797 CurveEdit::Type::instance().m_selectedShader = GlobalShaderCache().capture("$SELPOINT");
800 void Doom3Group_destroy()
802 GlobalShaderCache().release("$SELPOINT");
803 GlobalShaderCache().release("$POINT");
806 scene::Node& New_Doom3Group(EntityClass* eclass)
808 return (new Doom3GroupNode(eclass))->node();