]> de.git.xonotic.org Git - voretournament/voretournament.git/blob - misc/mediasource/netradiant-src/plugins/entity/namedentity.h
Move all other sources in a separate subfolder
[voretournament/voretournament.git] / misc / mediasource / netradiant-src / plugins / entity / namedentity.h
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 #if !defined(INCLUDED_NAMEDENTITY_H)
23 #define INCLUDED_NAMEDENTITY_H
24
25 #include "entitylib.h"
26 #include "eclasslib.h"
27 #include "generic/callback.h"
28 #include "nameable.h"
29
30 #include <set>
31
32 class NameCallbackSet
33 {
34   typedef std::set<NameCallback> NameCallbacks;
35   NameCallbacks m_callbacks;
36 public:
37   void insert(const NameCallback& callback)
38   {
39     m_callbacks.insert(callback);
40   }
41   void erase(const NameCallback& callback)
42   {
43     m_callbacks.erase(callback);
44   }
45   void changed(const char* name) const
46   {
47     for(NameCallbacks::const_iterator i = m_callbacks.begin(); i != m_callbacks.end(); ++i)
48     {
49       (*i)(name);
50     }
51   }
52 };
53
54 class NamedEntity : public Nameable
55 {
56   EntityKeyValues& m_entity;
57   NameCallbackSet m_changed;
58   CopiedString m_name;
59 public:
60   NamedEntity(EntityKeyValues& entity) : m_entity(entity)
61   {
62   }
63   const char* name() const
64   {
65     if(string_empty(m_name.c_str()))
66     {
67       return m_entity.getEntityClass().name();
68     }
69     return m_name.c_str();
70   }
71   void attach(const NameCallback& callback)
72   {
73     m_changed.insert(callback);
74   }
75   void detach(const NameCallback& callback)
76   {
77     m_changed.erase(callback);
78   }
79
80   void identifierChanged(const char* value)
81   {
82     if(string_empty(value))
83     {
84       m_changed.changed(m_entity.getEntityClass().name());
85     }
86     else
87     {
88       m_changed.changed(value);
89     }
90     m_name = value;
91   }
92   typedef MemberCaller1<NamedEntity, const char*, &NamedEntity::identifierChanged> IdentifierChangedCaller;
93 };
94
95 class RenderableNamedEntity : public OpenGLRenderable
96 {
97   const NamedEntity& m_named;
98   const Vector3& m_position;
99 public:
100   RenderableNamedEntity(const NamedEntity& named, const Vector3& position)
101     : m_named(named), m_position(position)
102   {
103   }
104   void render(RenderStateFlags state) const
105   {
106     glRasterPos3fv(vector3_to_array(m_position));
107     GlobalOpenGL().drawString(m_named.name());
108   }
109 };
110
111
112
113 #endif