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
22 #if !defined (INCLUDED_ENTITYLIB_H)
23 #define INCLUDED_ENTITYLIB_H
25 #include "ireference.h"
26 #include "debugging/debugging.h"
31 #include "selectable.h"
33 #include "generic/callback.h"
34 #include "math/vector.h"
35 #include "math/aabb.h"
37 #include "string/pooledstring.h"
38 #include "generic/referencecounted.h"
40 #include "container/container.h"
41 #include "eclasslib.h"
46 inline void arrow_draw(const Vector3& origin, const Vector3& direction_forward, const Vector3& direction_left, const Vector3& direction_up)
48 Vector3 endpoint(vector3_added(origin, vector3_scaled(direction_forward, 32.0)));
50 Vector3 tip1(vector3_added(vector3_added(endpoint, vector3_scaled(direction_forward, -8.0)), vector3_scaled(direction_up, -4.0)));
51 Vector3 tip2(vector3_added(tip1, vector3_scaled(direction_up, 8.0)));
52 Vector3 tip3(vector3_added(vector3_added(endpoint, vector3_scaled(direction_forward, -8.0)), vector3_scaled(direction_left, -4.0)));
53 Vector3 tip4(vector3_added(tip3, vector3_scaled(direction_left, 8.0)));
57 glVertex3fv(vector3_to_array(origin));
58 glVertex3fv(vector3_to_array(endpoint));
60 glVertex3fv(vector3_to_array(endpoint));
61 glVertex3fv(vector3_to_array(tip1));
63 glVertex3fv(vector3_to_array(endpoint));
64 glVertex3fv(vector3_to_array(tip2));
66 glVertex3fv(vector3_to_array(endpoint));
67 glVertex3fv(vector3_to_array(tip3));
69 glVertex3fv(vector3_to_array(endpoint));
70 glVertex3fv(vector3_to_array(tip4));
72 glVertex3fv(vector3_to_array(tip1));
73 glVertex3fv(vector3_to_array(tip3));
75 glVertex3fv(vector3_to_array(tip3));
76 glVertex3fv(vector3_to_array(tip2));
78 glVertex3fv(vector3_to_array(tip2));
79 glVertex3fv(vector3_to_array(tip4));
81 glVertex3fv(vector3_to_array(tip4));
82 glVertex3fv(vector3_to_array(tip1));
87 class SelectionIntersection;
89 inline void aabb_testselect(const AABB& aabb, SelectionTest& test, SelectionIntersection& best)
91 const IndexPointer::index_type indices[24] = {
101 aabb_corners(aabb, points);
102 test.TestQuads(VertexPointer(reinterpret_cast<VertexPointer::pointer>(points), sizeof(Vector3)), IndexPointer(indices, 24), best);
105 inline void aabb_draw_wire(const Vector3 points[8])
107 unsigned int indices[24] = {
108 0, 1, 1, 2, 2, 3, 3, 0,
109 4, 5, 5, 6, 6, 7, 7, 4,
110 0, 4, 1, 5, 2, 6, 3, 7,
113 glVertexPointer(3, GL_FLOAT, 0, points);
114 glDrawElements(GL_LINES, sizeof(indices)/sizeof(indices[0]), GL_UNSIGNED_INT, indices);
117 for(std::size_t i = 0; i < sizeof(indices)/sizeof(indices[0]); ++i)
119 glVertex3fv(points[indices[i]]);
125 inline void aabb_draw_flatshade(const Vector3 points[8])
129 glNormal3fv(vector3_to_array(aabb_normals[0]));
130 glVertex3fv(vector3_to_array(points[2]));
131 glVertex3fv(vector3_to_array(points[1]));
132 glVertex3fv(vector3_to_array(points[5]));
133 glVertex3fv(vector3_to_array(points[6]));
135 glNormal3fv(vector3_to_array(aabb_normals[1]));
136 glVertex3fv(vector3_to_array(points[1]));
137 glVertex3fv(vector3_to_array(points[0]));
138 glVertex3fv(vector3_to_array(points[4]));
139 glVertex3fv(vector3_to_array(points[5]));
141 glNormal3fv(vector3_to_array(aabb_normals[2]));
142 glVertex3fv(vector3_to_array(points[0]));
143 glVertex3fv(vector3_to_array(points[1]));
144 glVertex3fv(vector3_to_array(points[2]));
145 glVertex3fv(vector3_to_array(points[3]));
147 glNormal3fv(vector3_to_array(aabb_normals[3]));
148 glVertex3fv(vector3_to_array(points[0]));
149 glVertex3fv(vector3_to_array(points[3]));
150 glVertex3fv(vector3_to_array(points[7]));
151 glVertex3fv(vector3_to_array(points[4]));
153 glNormal3fv(vector3_to_array(aabb_normals[4]));
154 glVertex3fv(vector3_to_array(points[3]));
155 glVertex3fv(vector3_to_array(points[2]));
156 glVertex3fv(vector3_to_array(points[6]));
157 glVertex3fv(vector3_to_array(points[7]));
159 glNormal3fv(vector3_to_array(aabb_normals[5]));
160 glVertex3fv(vector3_to_array(points[7]));
161 glVertex3fv(vector3_to_array(points[6]));
162 glVertex3fv(vector3_to_array(points[5]));
163 glVertex3fv(vector3_to_array(points[4]));
168 inline void aabb_draw_wire(const AABB& aabb)
171 aabb_corners(aabb, points);
172 aabb_draw_wire(points);
175 inline void aabb_draw_flatshade(const AABB& aabb)
178 aabb_corners(aabb, points);
179 aabb_draw_flatshade(points);
182 inline void aabb_draw_textured(const AABB& aabb)
185 aabb_corners(aabb, points);
189 glNormal3fv(vector3_to_array(aabb_normals[0]));
190 glTexCoord2fv(aabb_texcoord_topleft);
191 glVertex3fv(vector3_to_array(points[2]));
192 glTexCoord2fv(aabb_texcoord_topright);
193 glVertex3fv(vector3_to_array(points[1]));
194 glTexCoord2fv(aabb_texcoord_botright);
195 glVertex3fv(vector3_to_array(points[5]));
196 glTexCoord2fv(aabb_texcoord_botleft);
197 glVertex3fv(vector3_to_array(points[6]));
199 glNormal3fv(vector3_to_array(aabb_normals[1]));
200 glTexCoord2fv(aabb_texcoord_topleft);
201 glVertex3fv(vector3_to_array(points[1]));
202 glTexCoord2fv(aabb_texcoord_topright);
203 glVertex3fv(vector3_to_array(points[0]));
204 glTexCoord2fv(aabb_texcoord_botright);
205 glVertex3fv(vector3_to_array(points[4]));
206 glTexCoord2fv(aabb_texcoord_botleft);
207 glVertex3fv(vector3_to_array(points[5]));
209 glNormal3fv(vector3_to_array(aabb_normals[2]));
210 glTexCoord2fv(aabb_texcoord_topleft);
211 glVertex3fv(vector3_to_array(points[0]));
212 glTexCoord2fv(aabb_texcoord_topright);
213 glVertex3fv(vector3_to_array(points[1]));
214 glTexCoord2fv(aabb_texcoord_botright);
215 glVertex3fv(vector3_to_array(points[2]));
216 glTexCoord2fv(aabb_texcoord_botleft);
217 glVertex3fv(vector3_to_array(points[3]));
219 glNormal3fv(vector3_to_array(aabb_normals[3]));
220 glTexCoord2fv(aabb_texcoord_topleft);
221 glVertex3fv(vector3_to_array(points[0]));
222 glTexCoord2fv(aabb_texcoord_topright);
223 glVertex3fv(vector3_to_array(points[3]));
224 glTexCoord2fv(aabb_texcoord_botright);
225 glVertex3fv(vector3_to_array(points[7]));
226 glTexCoord2fv(aabb_texcoord_botleft);
227 glVertex3fv(vector3_to_array(points[4]));
229 glNormal3fv(vector3_to_array(aabb_normals[4]));
230 glTexCoord2fv(aabb_texcoord_topleft);
231 glVertex3fv(vector3_to_array(points[3]));
232 glTexCoord2fv(aabb_texcoord_topright);
233 glVertex3fv(vector3_to_array(points[2]));
234 glTexCoord2fv(aabb_texcoord_botright);
235 glVertex3fv(vector3_to_array(points[6]));
236 glTexCoord2fv(aabb_texcoord_botleft);
237 glVertex3fv(vector3_to_array(points[7]));
239 glNormal3fv(vector3_to_array(aabb_normals[5]));
240 glTexCoord2fv(aabb_texcoord_topleft);
241 glVertex3fv(vector3_to_array(points[7]));
242 glTexCoord2fv(aabb_texcoord_topright);
243 glVertex3fv(vector3_to_array(points[6]));
244 glTexCoord2fv(aabb_texcoord_botright);
245 glVertex3fv(vector3_to_array(points[5]));
246 glTexCoord2fv(aabb_texcoord_botleft);
247 glVertex3fv(vector3_to_array(points[4]));
252 inline void aabb_draw_solid(const AABB& aabb, RenderStateFlags state)
254 if(state & RENDER_TEXTURE)
256 aabb_draw_textured(aabb);
260 aabb_draw_flatshade(aabb);
264 inline void aabb_draw(const AABB& aabb, RenderStateFlags state)
266 if(state & RENDER_FILL)
268 aabb_draw_solid(aabb, state);
272 aabb_draw_wire(aabb);
276 class RenderableSolidAABB : public OpenGLRenderable
280 RenderableSolidAABB(const AABB& aabb) : m_aabb(aabb)
283 void render(RenderStateFlags state) const
285 aabb_draw_solid(m_aabb, state);
289 class RenderableWireframeAABB : public OpenGLRenderable
293 RenderableWireframeAABB(const AABB& aabb) : m_aabb(aabb)
296 void render(RenderStateFlags state) const
298 aabb_draw_wire(m_aabb);
303 /// \brief A key/value pair of strings.
305 /// - Notifies observers when value changes - value changes to "" on destruction.
306 /// - Provides undo support through the global undo system.
307 class KeyValue : public EntityKeyValue
309 typedef UnsortedSet<KeyObserver> KeyObservers;
311 std::size_t m_refcount;
312 KeyObservers m_observers;
313 CopiedString m_string;
315 ObservedUndoableObject<CopiedString> m_undo;
316 static EntityCreator::KeyValueChangedFunc m_entityKeyValueChanged;
319 KeyValue(const char* string, const char* empty)
320 : m_refcount(0), m_string(string), m_empty(empty), m_undo(m_string, UndoImportCaller(*this))
326 ASSERT_MESSAGE(m_observers.empty(), "KeyValue::~KeyValue: observers still attached");
329 static void setKeyValueChangedFunc(EntityCreator::KeyValueChangedFunc func)
331 m_entityKeyValueChanged = func;
340 if(--m_refcount == 0)
346 void instanceAttach(MapFile* map)
348 m_undo.instanceAttach(map);
350 void instanceDetach(MapFile* map)
352 m_undo.instanceDetach(map);
355 void attach(const KeyObserver& observer)
357 (*m_observers.insert(observer))(c_str());
359 void detach(const KeyObserver& observer)
362 m_observers.erase(observer);
364 const char* c_str() const
366 if(string_empty(m_string.c_str()))
370 return m_string.c_str();
372 void assign(const char* other)
374 if(!string_equal(m_string.c_str(), other))
384 m_entityKeyValueChanged();
385 KeyObservers::reverse_iterator i = m_observers.rbegin();
386 while(i != m_observers.rend())
392 void importState(const CopiedString& string)
398 typedef MemberCaller1<KeyValue, const CopiedString&, &KeyValue::importState> UndoImportCaller;
401 /// \brief An unsorted list of key/value pairs.
403 /// - Notifies observers when a pair is inserted or removed.
404 /// - Provides undo support through the global undo system.
405 /// - New keys are appended to the end of the list.
406 class EntityKeyValues : public Entity
409 typedef KeyValue Value;
411 static StringPool& getPool()
413 return Static<StringPool, KeyContext>::instance();
416 static EntityCreator::KeyValueChangedFunc m_entityKeyValueChanged;
417 static Counter* m_counter;
419 EntityClass* m_eclass;
422 typedef Static<StringPool, KeyContext> KeyPool;
423 typedef PooledString<KeyPool> Key;
424 typedef SmartPointer<KeyValue> KeyValuePtr;
425 typedef UnsortedMap<Key, KeyValuePtr> KeyValues;
426 KeyValues m_keyValues;
428 typedef UnsortedSet<Observer*> Observers;
429 Observers m_observers;
431 ObservedUndoableObject<KeyValues> m_undo;
434 bool m_observerMutex;
436 void notifyInsert(const char* key, Value& value)
438 m_observerMutex = true;
439 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
441 (*i)->insert(key, value);
443 m_observerMutex = false;
445 void notifyErase(const char* key, Value& value)
447 m_observerMutex = true;
448 for(Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
450 (*i)->erase(key, value);
452 m_observerMutex = false;
454 void forEachKeyValue_notifyInsert()
456 for(KeyValues::const_iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i)
458 notifyInsert((*i).first.c_str(), *(*i).second);
461 void forEachKeyValue_notifyErase()
463 for(KeyValues::const_iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i)
465 notifyErase((*i).first.c_str(), *(*i).second);
469 void insert(const char* key, const KeyValuePtr& keyValue)
471 KeyValues::iterator i = m_keyValues.insert(KeyValues::value_type(key, keyValue));
472 notifyInsert(key, *(*i).second);
476 (*i).second->instanceAttach(m_undo.map());
480 void insert(const char* key, const char* value)
482 KeyValues::iterator i = m_keyValues.find(key);
483 if(i != m_keyValues.end())
485 (*i).second->assign(value);
490 insert(key, KeyValuePtr(new KeyValue(value, EntityClass_valueForKey(*m_eclass, key))));
494 void erase(KeyValues::iterator i)
498 (*i).second->instanceDetach(m_undo.map());
502 KeyValuePtr value((*i).second);
503 m_keyValues.erase(i);
504 notifyErase(key.c_str(), *value);
507 void erase(const char* key)
509 KeyValues::iterator i = m_keyValues.find(key);
510 if(i != m_keyValues.end())
520 EntityKeyValues(EntityClass* eclass) :
522 m_undo(m_keyValues, UndoImportCaller(*this)),
524 m_observerMutex(false),
525 m_isContainer(!eclass->fixedsize)
528 EntityKeyValues(const EntityKeyValues& other) :
530 m_eclass(&other.getEntityClass()),
531 m_undo(m_keyValues, UndoImportCaller(*this)),
533 m_observerMutex(false),
534 m_isContainer(other.m_isContainer)
536 for(KeyValues::const_iterator i = other.m_keyValues.begin(); i != other.m_keyValues.end(); ++i)
538 insert((*i).first.c_str(), (*i).second->c_str());
543 for(Observers::iterator i = m_observers.begin(); i != m_observers.end();)
545 // post-increment to allow current element to be removed safely
548 ASSERT_MESSAGE(m_observers.empty(), "EntityKeyValues::~EntityKeyValues: observers still attached");
551 static void setKeyValueChangedFunc(EntityCreator::KeyValueChangedFunc func)
553 m_entityKeyValueChanged = func;
554 KeyValue::setKeyValueChangedFunc(func);
556 static void setCounter(Counter* counter)
561 void importState(const KeyValues& keyValues)
563 for(KeyValues::iterator i = m_keyValues.begin(); i != m_keyValues.end();)
568 for(KeyValues::const_iterator i = keyValues.begin(); i != keyValues.end(); ++i)
570 insert((*i).first.c_str(), (*i).second);
573 m_entityKeyValueChanged();
575 typedef MemberCaller1<EntityKeyValues, const KeyValues&, &EntityKeyValues::importState> UndoImportCaller;
577 void attach(Observer& observer)
579 ASSERT_MESSAGE(!m_observerMutex, "observer cannot be attached during iteration");
580 m_observers.insert(&observer);
581 for(KeyValues::const_iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i)
583 observer.insert((*i).first.c_str(), *(*i).second);
586 void detach(Observer& observer)
588 ASSERT_MESSAGE(!m_observerMutex, "observer cannot be detached during iteration");
589 m_observers.erase(&observer);
590 for(KeyValues::const_iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i)
592 observer.erase((*i).first.c_str(), *(*i).second);
596 void forEachKeyValue_instanceAttach(MapFile* map)
598 for(KeyValues::const_iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i)
600 (*i).second->instanceAttach(map);
603 void forEachKeyValue_instanceDetach(MapFile* map)
605 for(KeyValues::const_iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i)
607 (*i).second->instanceDetach(map);
611 void instanceAttach(MapFile* map)
615 m_counter->increment();
619 forEachKeyValue_instanceAttach(map);
620 m_undo.instanceAttach(map);
622 void instanceDetach(MapFile* map)
626 m_counter->decrement();
629 m_undo.instanceDetach(map);
630 forEachKeyValue_instanceDetach(map);
635 EntityClass& getEntityClass() const
639 void forEachKeyValue(Visitor& visitor) const
641 for(KeyValues::const_iterator i = m_keyValues.begin(); i != m_keyValues.end(); ++i)
643 visitor.visit((*i).first.c_str(), (*i).second->c_str());
646 void setKeyValue(const char* key, const char* value)
649 /*|| string_equal(EntityClass_valueForKey(*m_eclass, key), value)*/) // don't delete values equal to default
657 m_entityKeyValueChanged();
659 const char* getKeyValue(const char* key) const
661 KeyValues::const_iterator i = m_keyValues.find(key);
662 if(i != m_keyValues.end())
664 return (*i).second->c_str();
667 return EntityClass_valueForKey(*m_eclass, key);
670 bool isContainer() const
672 return m_isContainer;
676 /// \brief A Resource reference with a controlled lifetime.
677 /// \brief The resource is released when the ResourceReference is destroyed.
678 class ResourceReference
681 Resource* m_resource;
683 ResourceReference(const char* name)
688 ResourceReference(const ResourceReference& other)
689 : m_name(other.m_name)
693 ResourceReference& operator=(const ResourceReference& other)
695 ResourceReference tmp(other);
706 m_resource = GlobalReferenceCache().capture(m_name.c_str());
710 GlobalReferenceCache().release(m_name.c_str());
713 const char* getName() const
715 return m_name.c_str();
717 void setName(const char* name)
719 ResourceReference tmp(name);
723 void swap(ResourceReference& other)
725 std::swap(m_resource, other.m_resource);
726 std::swap(m_name, other.m_name);
729 void attach(ModuleObserver& observer)
731 m_resource->attach(observer);
733 void detach(ModuleObserver& observer)
735 m_resource->detach(observer);
746 /// \brief Swaps the values of \p self and \p other.
747 /// Overloads std::swap.
748 inline void swap(ResourceReference& self, ResourceReference& other)