]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/group.cpp
1d078c11847beb3e9e60376cf6319c338fd3043d
[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 #if 0
158     if(g_showNames)
159     {
160       renderer.addRenderable(m_renderName, m_transform.localToParent());
161     }
162 #endif
163   }
164 };
165
166 #if 0
167 class TransformableSetTranslation
168 {
169   Translation m_value;
170 public:
171   TransformableSetTranslation(const Translation& value) : m_value(value)
172   {
173   }
174   void operator()(Transformable& transformable) const
175   {
176     transformable.setTranslation(m_value);
177   }
178 };
179
180 class TransformableSetRotation
181 {
182   Rotation m_value;
183 public:
184   TransformableSetRotation(const Rotation& value) : m_value(value)
185   {
186   }
187   void operator()(Transformable& transformable) const
188   {
189     transformable.setRotation(m_value);
190   }
191 };
192
193 class TransformableSetScale
194 {
195   Scale m_value;
196 public:
197   TransformableSetScale(const Scale& value) : m_value(value)
198   {
199   }
200   void operator()(Transformable& transformable) const
201   {
202     transformable.setScale(m_value);
203   }
204 };
205
206 class TransformableSetType
207 {
208   TransformModifierType m_value;
209 public:
210   TransformableSetType(const TransformModifierType& value) : m_value(value)
211   {
212   }
213   void operator()(Transformable& transformable) const
214   {
215     transformable.setType(m_value);
216   }
217 };
218
219 class TransformableFreezeTransform
220 {
221   TransformModifierType m_value;
222 public:
223   void operator()(Transformable& transformable) const
224   {
225     transformable.freezeTransform();
226   }
227 };
228
229 template<typename Functor>
230 inline void Scene_forEachChildTransformable(const Functor& functor, const scene::Path& path)
231 {
232   GlobalSceneGraph().traverse_subgraph(ChildInstanceWalker< InstanceApply<Transformable, Functor> >(functor), path);
233 }
234 #endif
235
236 class GroupInstance :
237   public TargetableInstance,
238 #if 0
239   public Transformable,
240 #endif
241   public Renderable
242 {
243   class TypeCasts
244   {
245     InstanceTypeCastTable m_casts;
246   public:
247     TypeCasts()
248     {
249       m_casts = TargetableInstance::StaticTypeCasts::instance().get();
250       InstanceStaticCast<GroupInstance, Renderable>::install(m_casts);
251 #if 0
252       InstanceStaticCast<GroupInstance, Transformable>::install(m_casts);
253 #endif
254     }
255     InstanceTypeCastTable& get()
256     {
257       return m_casts;
258     }
259   };
260
261   Group& m_contained;
262 public:
263   typedef LazyStatic<TypeCasts> StaticTypeCasts;
264
265   GroupInstance(const scene::Path& path, scene::Instance* parent, Group& group) :
266     TargetableInstance(path, parent, this, StaticTypeCasts::instance().get(), group.getEntity(), *this),
267     m_contained(group)
268   {
269     m_contained.instanceAttach(Instance::path());
270     StaticRenderableConnectionLines::instance().attach(*this);
271   }
272   ~GroupInstance()
273   {
274     StaticRenderableConnectionLines::instance().detach(*this);
275     m_contained.instanceDetach(Instance::path());
276   }
277   void renderSolid(Renderer& renderer, const VolumeTest& volume) const
278   {
279     m_contained.renderSolid(renderer, volume, Instance::localToWorld());
280   }
281   void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
282   {
283     m_contained.renderWireframe(renderer, volume, Instance::localToWorld());
284   }
285
286 #if 0
287   void setType(TransformModifierType type)
288   {
289     Scene_forEachChildTransformable(TransformableSetType(type), Instance::path());
290   }
291   void setTranslation(const Translation& value)
292   {
293     Scene_forEachChildTransformable(TransformableSetTranslation(value), Instance::path());
294   }
295   void setRotation(const Rotation& value)
296   {
297     Scene_forEachChildTransformable(TransformableSetRotation(value), Instance::path());
298   }
299   void setScale(const Scale& value)
300   {
301     Scene_forEachChildTransformable(TransformableSetScale(value), Instance::path());
302   }
303   void freezeTransform()
304   {
305     Scene_forEachChildTransformable(TransformableFreezeTransform(), Instance::path());
306   }
307
308   void evaluateTransform()
309   {
310   }
311 #endif
312 };
313
314 class GroupNode :
315   public scene::Node::Symbiot,
316   public scene::Instantiable,
317   public scene::Cloneable,
318   public scene::Traversable::Observer
319 {
320   class TypeCasts
321   {
322     NodeTypeCastTable m_casts;
323   public:
324     TypeCasts()
325     {
326       NodeStaticCast<GroupNode, scene::Instantiable>::install(m_casts);
327       NodeStaticCast<GroupNode, scene::Cloneable>::install(m_casts);
328       NodeContainedCast<GroupNode, scene::Traversable>::install(m_casts);
329       NodeContainedCast<GroupNode, TransformNode>::install(m_casts);
330       NodeContainedCast<GroupNode, Entity>::install(m_casts);
331       NodeContainedCast<GroupNode, Nameable>::install(m_casts);
332       NodeContainedCast<GroupNode, Namespaced>::install(m_casts);
333     }
334     NodeTypeCastTable& get()
335     {
336       return m_casts;
337     }
338   };
339
340
341   scene::Node m_node;
342   InstanceSet m_instances;
343   Group m_contained;
344
345   void construct()
346   {
347     m_contained.attach(this);
348   }
349   void destroy()
350   {
351     m_contained.detach(this);
352   }
353
354 public:
355
356   typedef LazyStatic<TypeCasts> StaticTypeCasts;
357
358   scene::Traversable& get(NullType<scene::Traversable>)
359   {
360     return m_contained.getTraversable();
361   }
362   TransformNode& get(NullType<TransformNode>)
363   {
364     return m_contained.getTransformNode();
365   }
366   Entity& get(NullType<Entity>)
367   {
368     return m_contained.getEntity();
369   }
370   Nameable& get(NullType<Nameable>)
371   {
372     return m_contained.getNameable();
373   }
374   Namespaced& get(NullType<Namespaced>)
375   {
376     return m_contained.getNamespaced();
377   }
378
379   GroupNode(EntityClass* eclass) :
380     m_node(this, this, StaticTypeCasts::instance().get()),
381     m_contained(eclass, m_node, InstanceSet::TransformChangedCaller(m_instances))
382   {
383     construct();
384   }
385   GroupNode(const GroupNode& other) :
386     scene::Node::Symbiot(other),
387     scene::Instantiable(other),
388     scene::Cloneable(other),
389     scene::Traversable::Observer(other),
390     m_node(this, this, StaticTypeCasts::instance().get()),
391     m_contained(other.m_contained, m_node, InstanceSet::TransformChangedCaller(m_instances))
392   {
393     construct();
394   }
395   ~GroupNode()
396   {
397     destroy();
398   }
399
400   void release()
401   {
402     delete this;
403   }
404   scene::Node& node()
405   {
406     return m_node;
407   }
408
409   scene::Node& clone() const
410   {
411     return (new GroupNode(*this))->node();
412   }
413
414   void insert(scene::Node& child)
415   {
416     m_instances.insert(child);
417   }
418   void erase(scene::Node& child)
419   {
420     m_instances.erase(child);
421   }
422
423   scene::Instance* create(const scene::Path& path, scene::Instance* parent)
424   {
425     return new GroupInstance(path, parent, m_contained);
426   }
427   void forEachInstance(const scene::Instantiable::Visitor& visitor)
428   {
429     m_instances.forEachInstance(visitor);
430   }
431   void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
432   {
433     m_instances.insert(observer, path, instance);
434   }
435   scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
436   {
437     return m_instances.erase(observer, path);
438   }
439 };
440
441 scene::Node& New_Group(EntityClass* eclass)
442 {
443   return (new GroupNode(eclass))->node();
444 }