]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/entity/generic.cpp
added string-pooling for shader variable names and entity keys
[xonotic/netradiant.git] / plugins / entity / generic.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 has a fixed size specified in its entity-definition and does not display a model (e.g. info_player_start).
24 ///
25 /// This entity displays an axis-aligned bounding box of the size and colour specified in its entity-definition.
26 /// The "origin" key directly controls the entity's local-to-parent transform.
27 /// An arrow is drawn to visualise the "angle" key.
28
29 #include "cullable.h"
30 #include "renderable.h"
31 #include "editable.h"
32
33 #include "math/frustum.h"
34 #include "selectionlib.h"
35 #include "instancelib.h"
36 #include "transformlib.h"
37 #include "entitylib.h"
38 #include "render.h"
39 #include "eclasslib.h"
40 #include "math/line.h"
41
42 #include "targetable.h"
43 #include "origin.h"
44 #include "angle.h"
45 #include "filters.h"
46 #include "namedentity.h"
47 #include "keyobservers.h"
48 #include "namekeys.h"
49 #include "rotation.h"
50
51 #include "entity.h"
52
53
54 class RenderableArrow : public OpenGLRenderable
55 {
56   const Ray& m_ray;
57
58 public:
59   RenderableArrow(const Ray& ray)
60     : m_ray(ray)
61   {
62   }
63
64   void render(RenderStateFlags state) const
65   {
66     arrow_draw(m_ray.origin, m_ray.direction);
67   }
68 };
69
70 inline void read_aabb(AABB& aabb, const EntityClass& eclass)
71 {
72   aabb = aabb_for_minmax(eclass.mins, eclass.maxs);
73 }
74
75
76 class GenericEntity :
77   public Cullable,
78   public Bounded,
79   public Snappable
80 {
81   EntityKeyValues m_entity;
82   KeyObserverMap m_keyObservers;
83   MatrixTransform m_transform;
84
85   OriginKey m_originKey;
86   Vector3 m_origin;
87   AngleKey m_angleKey;
88   float m_angle;
89
90   ClassnameFilter m_filter;
91   NamedEntity m_named;
92   NameKeys m_nameKeys;
93
94   AABB m_aabb_local;
95   Ray m_ray;
96
97   RenderableArrow m_arrow;
98   RenderableSolidAABB m_aabb_solid;
99   RenderableWireframeAABB m_aabb_wire;
100   RenderableNamedEntity m_renderName;
101
102   Callback m_transformChanged;
103   Callback m_evaluateTransform;
104
105   void construct()
106   {
107     read_aabb(m_aabb_local, m_entity.getEntityClass());
108     m_ray.origin = m_aabb_local.origin;
109     m_ray.direction[0] = 1;
110     m_ray.direction[1] = 0;
111     m_ray.direction[2] = 0;
112
113     m_keyObservers.insert("classname", ClassnameFilter::ClassnameChangedCaller(m_filter));
114     m_keyObservers.insert(Static<KeyIsName>::instance().m_nameKey, NamedEntity::IdentifierChangedCaller(m_named));
115     m_keyObservers.insert("angle", AngleKey::AngleChangedCaller(m_angleKey));
116     m_keyObservers.insert("origin", OriginKey::OriginChangedCaller(m_originKey));
117   }
118
119   void updateTransform()
120   {
121     m_transform.localToParent() = g_matrix4_identity;
122     matrix4_translate_by_vec3(m_transform.localToParent(), m_origin);
123     m_ray.direction = matrix4_transformed_direction(matrix4_rotation_for_z(degrees_to_radians(m_angle)), Vector3(1, 0, 0));
124     m_transformChanged();
125   }
126   typedef MemberCaller<GenericEntity, &GenericEntity::updateTransform> UpdateTransformCaller;
127   void originChanged()
128   {
129     m_origin = m_originKey.m_origin;
130     updateTransform();
131   }
132   typedef MemberCaller<GenericEntity, &GenericEntity::originChanged> OriginChangedCaller;
133   void angleChanged()
134   {
135     m_angle = m_angleKey.m_angle;
136     updateTransform();
137   }
138   typedef MemberCaller<GenericEntity, &GenericEntity::angleChanged> AngleChangedCaller;
139 public:
140
141   GenericEntity(EntityClass* eclass, scene::Node& node, const Callback& transformChanged, const Callback& evaluateTransform) :
142     m_entity(eclass),
143     m_originKey(OriginChangedCaller(*this)),
144     m_origin(ORIGINKEY_IDENTITY),
145     m_angleKey(AngleChangedCaller(*this)),
146     m_angle(ANGLEKEY_IDENTITY),
147     m_filter(m_entity, node),
148     m_named(m_entity),
149     m_nameKeys(m_entity),
150     m_arrow(m_ray),
151     m_aabb_solid(m_aabb_local),
152     m_aabb_wire(m_aabb_local),
153     m_renderName(m_named, g_vector3_identity),
154     m_transformChanged(transformChanged),
155     m_evaluateTransform(evaluateTransform)
156   {
157     construct();
158   }
159   GenericEntity(const GenericEntity& other, scene::Node& node, const Callback& transformChanged, const Callback& evaluateTransform) :
160     m_entity(other.m_entity),
161     m_originKey(OriginChangedCaller(*this)),
162     m_origin(ORIGINKEY_IDENTITY),
163     m_angleKey(AngleChangedCaller(*this)),
164     m_angle(ANGLEKEY_IDENTITY),
165     m_filter(m_entity, node),
166     m_named(m_entity),
167     m_nameKeys(m_entity),
168     m_arrow(m_ray),
169     m_aabb_solid(m_aabb_local),
170     m_aabb_wire(m_aabb_local),
171     m_renderName(m_named, g_vector3_identity),
172     m_transformChanged(transformChanged),
173     m_evaluateTransform(evaluateTransform)
174   {
175     construct();
176   }
177
178   InstanceCounter m_instanceCounter;
179   void instanceAttach(const scene::Path& path)
180   {
181     if(++m_instanceCounter.m_count == 1)
182     {
183       m_filter.instanceAttach();
184       m_entity.instanceAttach(path_find_mapfile(path.begin(), path.end()));
185       m_entity.attach(m_keyObservers);
186     }
187   }
188   void instanceDetach(const scene::Path& path)
189   {
190     if(--m_instanceCounter.m_count == 0)
191     {
192       m_entity.detach(m_keyObservers);
193       m_entity.instanceDetach(path_find_mapfile(path.begin(), path.end()));
194       m_filter.instanceDetach();
195     }
196   }
197
198   EntityKeyValues& getEntity()
199   {
200     return m_entity;
201   }
202   const EntityKeyValues& getEntity() const
203   {
204     return m_entity;
205   }
206
207   Namespaced& getNamespaced()
208   {
209     return m_nameKeys;
210   }
211   Nameable& getNameable()
212   {
213     return m_named;
214   }
215   TransformNode& getTransformNode()
216   {
217     return m_transform;
218   }
219
220   const AABB& localAABB() const
221   {
222     return m_aabb_local;
223   }
224
225   VolumeIntersectionValue intersectVolume(const VolumeTest& volume, const Matrix4& localToWorld) const
226   {
227     return volume.TestAABB(localAABB(), localToWorld);
228   }
229
230   void renderArrow(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
231   {
232     if(g_showAngles)
233     {
234       renderer.addRenderable(m_arrow, localToWorld);
235     }
236   }
237   void renderSolid(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
238   {
239     renderer.SetState(m_entity.getEntityClass().m_state_fill, Renderer::eFullMaterials);
240     renderer.addRenderable(m_aabb_solid, localToWorld);
241     renderArrow(renderer, volume, localToWorld);
242   }
243   void renderWireframe(Renderer& renderer, const VolumeTest& volume, const Matrix4& localToWorld) const
244   {
245     renderer.SetState(m_entity.getEntityClass().m_state_wire, Renderer::eWireframeOnly);
246     renderer.addRenderable(m_aabb_wire, localToWorld);
247     renderArrow(renderer, volume, localToWorld);
248     if(g_showNames)
249     {
250       renderer.addRenderable(m_renderName, localToWorld);
251     }
252   }
253
254
255   void testSelect(Selector& selector, SelectionTest& test, const Matrix4& localToWorld)
256   {
257     test.BeginMesh(localToWorld);
258
259     SelectionIntersection best;
260     aabb_testselect(m_aabb_local, test, best);
261     if(best.valid())
262     {
263       selector.addIntersection(best);
264     }
265   }
266
267   void translate(const Vector3& translation)
268   {
269     m_origin = origin_translated(m_origin, translation);
270   }
271   void rotate(const Quaternion& rotation)
272   {
273     m_angle = angle_rotated(m_angle, rotation);
274   }
275   void snapto(float snap)
276   {
277     m_originKey.m_origin = origin_snapped(m_originKey.m_origin, snap);
278     m_originKey.write(&m_entity);
279   }
280   void revertTransform()
281   {
282     m_origin = m_originKey.m_origin;
283     m_angle = m_angleKey.m_angle;
284   }
285   void freezeTransform()
286   {
287     m_originKey.m_origin = m_origin;
288     m_originKey.write(&m_entity);
289     m_angleKey.m_angle = m_angle;
290     m_angleKey.write(&m_entity);
291   }
292   void transformChanged()
293   {
294     revertTransform();
295     m_evaluateTransform();
296     updateTransform();
297   }
298   typedef MemberCaller<GenericEntity, &GenericEntity::transformChanged> TransformChangedCaller;
299 };
300
301 class GenericEntityInstance :
302   public TargetableInstance,
303   public TransformModifier,
304   public Renderable,
305   public SelectionTestable
306 {
307   class TypeCasts
308   {
309     InstanceTypeCastTable m_casts;
310   public:
311     TypeCasts()
312     {
313       m_casts = TargetableInstance::StaticTypeCasts::instance().get();
314       InstanceContainedCast<GenericEntityInstance, Bounded>::install(m_casts);
315       InstanceContainedCast<GenericEntityInstance, Cullable>::install(m_casts);
316       InstanceStaticCast<GenericEntityInstance, Renderable>::install(m_casts);
317       InstanceStaticCast<GenericEntityInstance, SelectionTestable>::install(m_casts);
318       InstanceStaticCast<GenericEntityInstance, Transformable>::install(m_casts);
319       InstanceIdentityCast<GenericEntityInstance>::install(m_casts);
320     }
321     InstanceTypeCastTable& get()
322     {
323       return m_casts;
324     }
325   };
326
327   GenericEntity& m_contained;
328   mutable AABB m_bounds;
329 public:
330
331   typedef LazyStatic<TypeCasts> StaticTypeCasts;
332
333   Bounded& get(NullType<Bounded>)
334   {
335     return m_contained;
336   }
337   Cullable& get(NullType<Cullable>)
338   {
339     return m_contained;
340   }
341
342   STRING_CONSTANT(Name, "GenericEntityInstance");
343
344   GenericEntityInstance(const scene::Path& path, scene::Instance* parent, GenericEntity& contained) :
345     TargetableInstance(path, parent, this, StaticTypeCasts::instance().get(), contained.getEntity(), *this),
346     TransformModifier(GenericEntity::TransformChangedCaller(contained), ApplyTransformCaller(*this)),
347     m_contained(contained)
348   {
349     m_contained.instanceAttach(Instance::path());
350
351     StaticRenderableConnectionLines::instance().attach(*this);
352   }
353   ~GenericEntityInstance()
354   {
355     StaticRenderableConnectionLines::instance().detach(*this);
356
357     m_contained.instanceDetach(Instance::path());
358   }
359
360   void renderSolid(Renderer& renderer, const VolumeTest& volume) const
361   {
362     m_contained.renderSolid(renderer, volume, Instance::localToWorld());
363   }
364   void renderWireframe(Renderer& renderer, const VolumeTest& volume) const
365   {
366     m_contained.renderWireframe(renderer, volume, Instance::localToWorld());
367   }
368
369   void testSelect(Selector& selector, SelectionTest& test)
370   {
371     m_contained.testSelect(selector, test, Instance::localToWorld());
372   }
373
374   void evaluateTransform()
375   {
376     if(getType() == TRANSFORM_PRIMITIVE)
377     {
378       m_contained.translate(getTranslation());
379       m_contained.rotate(getRotation());
380     }
381   }
382   void applyTransform()
383   {
384     m_contained.revertTransform();
385     evaluateTransform();
386     m_contained.freezeTransform();
387   }
388   typedef MemberCaller<GenericEntityInstance, &GenericEntityInstance::applyTransform> ApplyTransformCaller;
389 };
390
391 class GenericEntityNode :
392   public scene::Node::Symbiot,
393   public scene::Instantiable,
394   public scene::Cloneable
395 {
396   class TypeCasts
397   {
398     NodeTypeCastTable m_casts;
399   public:
400     TypeCasts()
401     {
402       NodeStaticCast<GenericEntityNode, scene::Instantiable>::install(m_casts);
403       NodeStaticCast<GenericEntityNode, scene::Cloneable>::install(m_casts);
404       NodeContainedCast<GenericEntityNode, Snappable>::install(m_casts);
405       NodeContainedCast<GenericEntityNode, TransformNode>::install(m_casts);
406       NodeContainedCast<GenericEntityNode, Entity>::install(m_casts);
407       NodeContainedCast<GenericEntityNode, Nameable>::install(m_casts);
408       NodeContainedCast<GenericEntityNode, Namespaced>::install(m_casts);
409     }
410     NodeTypeCastTable& get()
411     {
412       return m_casts;
413     }
414   };
415
416
417   InstanceSet m_instances;
418
419   scene::Node m_node;
420   GenericEntity m_contained;
421
422 public:
423   typedef LazyStatic<TypeCasts> StaticTypeCasts;
424
425   Snappable& get(NullType<Snappable>)
426   {
427     return m_contained;
428   }
429   TransformNode& get(NullType<TransformNode>)
430   {
431     return m_contained.getTransformNode();
432   }
433   Entity& get(NullType<Entity>)
434   {
435     return m_contained.getEntity();
436   }
437   Nameable& get(NullType<Nameable>)
438   {
439     return m_contained.getNameable();
440   }
441   Namespaced& get(NullType<Namespaced>)
442   {
443     return m_contained.getNamespaced();
444   }
445
446   GenericEntityNode(EntityClass* eclass) :
447     m_node(this, this, StaticTypeCasts::instance().get()),
448     m_contained(eclass, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSetEvaluateTransform<GenericEntityInstance>::Caller(m_instances))
449   {
450   }
451   GenericEntityNode(const GenericEntityNode& other) :
452     scene::Node::Symbiot(other),
453     scene::Instantiable(other),
454     scene::Cloneable(other),
455     m_node(this, this, StaticTypeCasts::instance().get()),
456     m_contained(other.m_contained, m_node, InstanceSet::TransformChangedCaller(m_instances), InstanceSetEvaluateTransform<GenericEntityInstance>::Caller(m_instances))
457   {
458   }
459   void release()
460   {
461     delete this;
462   }
463   scene::Node& node()
464   {
465     return m_node;
466   }
467
468   scene::Node& clone() const
469   {
470     return (new GenericEntityNode(*this))->node();
471   }
472
473   scene::Instance* create(const scene::Path& path, scene::Instance* parent)
474   {
475     return new GenericEntityInstance(path, parent, m_contained);
476   }
477   void forEachInstance(const scene::Instantiable::Visitor& visitor)
478   {
479     m_instances.forEachInstance(visitor);
480   }
481   void insert(scene::Instantiable::Observer* observer, const scene::Path& path, scene::Instance* instance)
482   {
483     m_instances.insert(observer, path, instance);
484   }
485   scene::Instance* erase(scene::Instantiable::Observer* observer, const scene::Path& path)
486   {
487     return m_instances.erase(observer, path);
488   }
489 };
490
491 scene::Node& New_GenericEntity(EntityClass* eclass)
492 {
493   return (new GenericEntityNode(eclass))->node();
494 }