]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/group.cpp
dd3a7f5a6e2eb7622d9239d3ecd05e0cee7c52da
[xonotic/netradiant.git] / plugins / entity / group.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 does not have a fixed size specified in its entity-definition (except misc_model).
24 ///
25 /// This entity behaves as a group, i.e. it contains brushes.
26
27 #include "cullable.h"
28 #include "renderable.h"
29 #include "editable.h"
30
31 #include "selectionlib.h"
32 #include "instancelib.h"
33 #include "transformlib.h"
34 #include "traverselib.h"
35 #include "entitylib.h"
36 #include "render.h"
37 #include "eclasslib.h"
38
39 #include "targetable.h"
40 #include "origin.h"
41 #include "angles.h"
42 #include "scale.h"
43 #include "filters.h"
44 #include "namedentity.h"
45 #include "keyobservers.h"
46 #include "namekeys.h"
47
48 #include "entity.h"
49
50 class Group
51 {
52   EntityKeyValues m_entity;
53   KeyObserverMap m_keyObservers;
54   MatrixTransform m_transform;
55   TraversableNodeSet m_traverse;
56
57   ClassnameFilter m_filter;
58   NamedEntity m_named;
59   NameKeys m_nameKeys;
60
61   RenderableNamedEntity m_renderName;
62
63   Callback m_transformChanged;
64
65   void construct()
66   {
67     m_keyObservers.insert("classname", ClassnameFilter::ClassnameChangedCaller(m_filter));
68     m_keyObservers.insert(Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller(m_named));
69   }
70  
71 public:
72   Group(EntityClass* eclass, scene::Node& node, const Callback& transformChanged) :
73     m_entity(eclass),
74     m_filter(m_entity, node),
75     m_named(m_entity),
76     m_nameKeys(m_entity),
77     m_renderName(m_named, g_vector3_identity),
78     m_transformChanged(transformChanged)
79   {
80     construct();
81   }
82   Group(const Group& other, scene::Node& node, const Callback& transformChanged) :
83     m_entity(other.m_entity),
84     m_filter(m_entity, node),
85     m_named(m_entity),
86     m_nameKeys(m_entity),
87     m_renderName(m_named, g_vector3_identity),
88     m_transformChanged(transformChanged)
89   {
90     construct();
91   }
92
93   InstanceCounter m_instanceCounter;
94   void instanceAttach(const scene::Path& path)
95   {
96     if(++m_instanceCounter.m_count == 1)
97     {
98       m_filter.instanceAttach();
99       m_entity.instanceAttach(path_find_mapfile(path.begin(), path.end()));
100       m_traverse.instanceAttach(path_find_mapfile(path.begin(), path.end()));
101       m_entity.attach(m_keyObservers);
102     }
103   }
104   void instanceDetach(const scene::Path& path)
105   {
106     if(--m_instanceCounter.m_count == 0)
107     {
108       m_entity.detach(m_keyObservers);
109       m_traverse.instanceDetach(path_find_mapfile(path.begin(), path.end()));
110       m_entity.instanceDetach(path_find_mapfile(path.begin(), path.end()));
111       m_filter.instanceDetach();
112     }
113   }
114
115   EntityKeyValues& getEntity()
116   {
117     return m_entity;
118   }
119   const EntityKeyValues& getEntity() const
120   {
121     return m_entity;
122   }
123
124   scene::Traversable& getTraversable()
125   {
126     return m_traverse;
127   }
128   Namespaced& getNamespaced()
129   {
130     return m_nameKeys;
131   }
132   Nameable& getNameable()
133   {
134     return m_named;
135   }
136   TransformNode& getTransformNode()
137   {
138     return m_transform;
139   }
140
141   void attach(scene::Traversable::Observer* observer)
142   {
143     m_traverse.attach(observer);
144   }
145   void detach(scene::Traversable::Observer* observer)
146   {
147     m_traverse.detach(observer);
148   }
149
150   void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
151   {
152     renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly);
153   }
154   void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
155   {
156     renderSolid(renderer, volume, localToWorld);
157
158     if(g_showNames)
159     {
160       renderer.addRenderable(m_renderName, m_transform.localToParent());
161     }
162   }
163 };
164
165 #if 0
166 class TransformableSetTranslation
167 {
168   Translation m_value;
169 public:
170   TransformableSetTranslation(const Translation& value) : m_value(value)
171   {
172   }
173   void operator()(Transformable& transformable) const
174   {
175     transformable.setTranslation(m_value);
176   }
177 };
178
179 class TransformableSetRotation
180 {
181   Rotation m_value;
182 public:
183   TransformableSetRotation(const Rotation& value) : m_value(value)
184   {
185   }
186   void operator()(Transformable& transformable) const
187   {
188     transformable.setRotation(m_value);
189   }
190 };
191
192 class TransformableSetScale
193 {
194   Scale m_value;
195 public:
196   TransformableSetScale(const Scale& value) : m_value(value)
197   {
198   }
199   void operator()(Transformable& transformable) const
200   {
201     transformable.setScale(m_value);
202   }
203 };
204
205 class TransformableSetType
206 {
207   TransformModifierType m_value;
208 public:
209   TransformableSetType(const TransformModifierType& value) : m_value(value)
210   {
211   }
212   void operator()(Transformable& transformable) const
213   {
214     transformable.setType(m_value);
215   }
216 };
217
218 class TransformableFreezeTransform
219 {
220   TransformModifierType m_value;
221 public:
222   void operator()(Transformable& transformable) const
223   {
224     transformable.freezeTransform();
225   }
226 };
227
228 template<typename Functor>
229 inline void Scene_forEachChildTransformable(const Functor& functor, const scene::Path& path)
230 {
231   GlobalSceneGraph().traverse_subgraph(ChildInstanceWalker< InstanceApply<Transformable, Functor> >(functor), path);
232 }
233 #endif
234
235 class GroupInstance :
236   public TargetableInstance,
237 #if 0
238   public Transformable,
239 #endif
240   public Renderable
241 {
242   class TypeCasts
243   {
244     InstanceTypeCastTable m_casts;
245   public:
246     TypeCasts()
247     {
248       m_casts = TargetableInstance::StaticTypeCasts::instance().get();
249       InstanceStaticCast<GroupInstance, Renderable>::install(m_casts);
250 #if 0
251       InstanceStaticCast<GroupInstance, Transformable>::install(m_casts);
252 #endif
253     }
254     InstanceTypeCastTable& get()
255     {
256       return m_casts;
257     }
258   };
259
260   Group& m_contained;
261 public:
262   typedef LazyStatic<TypeCasts> StaticTypeCasts;
263
264   GroupInstance(const scene::Path& path, scene::Instance* parent, Group& group) :
265     TargetableInstance(path, parent, this, StaticTypeCasts::instance().get(), group.getEntity(), *this),
266     m_contained(group)
267   {
268     m_contained.instanceAttach(Instance::path());
269     StaticRenderableConnectionLines::instance().attach(*this);
270   }
271   ~GroupInstance()
272   {
273     StaticRenderableConnectionLines::instance().detach(*this);
274     m_contained.instanceDetach(Instance::path());
275   }
276   void renderSolid(Renderer& renderer, const VolumeTest& volume) const
277   {
278     m_contained.renderSolid(renderer, volume, Instance::localToWorld());
279   }
280   void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
281   {
282     m_contained.renderWireframe(renderer, volume, Instance::localToWorld());
283   }
284
285 #if 0
286   void setType(TransformModifierType type)
287   {
288     Scene_forEachChildTransformable(TransformableSetType(type), Instance::path());
289   }
290   void setTranslation(const Translation& value)
291   {
292     Scene_forEachChildTransformable(TransformableSetTranslation(value), Instance::path());
293   }
294   void setRotation(const Rotation& value)
295   {
296     Scene_forEachChildTransformable(TransformableSetRotation(value), Instance::path());
297   }
298   void setScale(const Scale& value)
299   {
300     Scene_forEachChildTransformable(TransformableSetScale(value), Instance::path());
301   }
302   void freezeTransform()
303   {
304     Scene_forEachChildTransformable(TransformableFreezeTransform(), Instance::path());
305   }
306
307   void evaluateTransform()
308   {
309   }
310 #endif
311 };
312
313 class GroupNode :
314   public scene::Node::Symbiot,
315   public scene::Instantiable,
316   public scene::Cloneable,
317   public scene::Traversable::Observer
318 {
319   class TypeCasts
320   {
321     NodeTypeCastTable m_casts;
322   public:
323     TypeCasts()
324     {
325       NodeStaticCast<GroupNode, scene::Instantiable>::install(m_casts);
326       NodeStaticCast<GroupNode, scene::Cloneable>::install(m_casts);
327       NodeContainedCast<GroupNode, scene::Traversable>::install(m_casts);
328       NodeContainedCast<GroupNode, TransformNode>::install(m_casts);
329       NodeContainedCast<GroupNode, Entity>::install(m_casts);
330       NodeContainedCast<GroupNode, Nameable>::install(m_casts);
331       NodeContainedCast<GroupNode, Namespaced>::install(m_casts);
332     }
333     NodeTypeCastTable& get()
334     {
335       return m_casts;
336     }
337   };
338
339
340   scene::Node m_node;
341   InstanceSet m_instances;
342   Group m_contained;
343
344   void construct()
345   {
346     m_contained.attach(this);
347   }
348   void destroy()
349   {
350     m_contained.detach(this);
351   }
352
353 public:
354
355   typedef LazyStatic<TypeCasts> StaticTypeCasts;
356
357   scene::Traversable& get(NullType<scene::Traversable>)
358   {
359     return m_contained.getTraversable();
360   }
361   TransformNode& get(NullType<TransformNode>)
362   {
363     return m_contained.getTransformNode();
364   }
365   Entity& get(NullType<Entity>)
366   {
367     return m_contained.getEntity();
368   }
369   Nameable& get(NullType<Nameable>)
370   {
371     return m_contained.getNameable();
372   }
373   Namespaced& get(NullType<Namespaced>)
374   {
375     return m_contained.getNamespaced();
376   }
377
378   GroupNode(EntityClass* eclass) :
379     m_node(this, this, StaticTypeCasts::instance().get()),
380     m_contained(eclass, m_node, InstanceSet::TransformChangedCaller(m_instances))
381   {
382     construct();
383   }
384   GroupNode(const GroupNode& other) :
385     scene::Node::Symbiot(other),
386     scene::Instantiable(other),
387     scene::Cloneable(other),
388     scene::Traversable::Observer(other),
389     m_node(this, this, StaticTypeCasts::instance().get()),
390     m_contained(other.m_contained, m_node, InstanceSet::TransformChangedCaller(m_instances))
391   {
392     construct();
393   }
394   ~GroupNode()
395   {
396     destroy();
397   }
398
399   void release()
400   {
401     delete this;
402   }
403   scene::Node& node()
404   {
405     return m_node;
406   }
407
408   scene::Node& clone() const
409   {
410     return (new GroupNode(*this))->node();
411   }
412
413   void insert(scene::Node& child)
414   {
415     m_instances.insert(child);
416   }
417   void erase(scene::Node& child)
418   {
419     m_instances.erase(child);
420   }
421
422   scene::Instance* create(const scene::Path& path, scene::Instance* parent)
423   {
424     return new GroupInstance(path, parent, m_contained);
425   }
426   void forEachInstance(const scene::Instantiable::Visitor& visitor)
427   {
428     m_instances.forEachInstance(visitor);
429   }
430   void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
431   {
432     m_instances.insert(observer, path, instance);
433   }
434   scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
435   {
436     return m_instances.erase(observer, path);
437   }
438 };
439
440 scene::Node& New_Group(EntityClass* eclass)
441 {
442   return (new GroupNode(eclass))->node();
443 }