namedentity.h

Go to the documentation of this file.
00001 
00006 /*
00007  Copyright (C) 2001-2006, William Joseph.
00008  All Rights Reserved.
00009 
00010  This file is part of GtkRadiant.
00011 
00012  GtkRadiant is free software; you can redistribute it and/or modify
00013  it under the terms of the GNU General Public License as published by
00014  the Free Software Foundation; either version 2 of the License, or
00015  (at your option) any later version.
00016 
00017  GtkRadiant is distributed in the hope that it will be useful,
00018  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  GNU General Public License for more details.
00021 
00022  You should have received a copy of the GNU General Public License
00023  along with GtkRadiant; if not, write to the Free Software
00024  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00025  */
00026 
00027 #if !defined(INCLUDED_NAMEDENTITY_H)
00028 #define INCLUDED_NAMEDENTITY_H
00029 
00030 #include "entitylib.h"
00031 #include "eclasslib.h"
00032 #include "generic/callback.h"
00033 #include "nameable.h"
00034 
00035 #include <set>
00036 
00037 class NameCallbackSet
00038 {
00039         typedef std::set<NameCallback> NameCallbacks;
00040         NameCallbacks m_callbacks;
00041     public:
00042         void insert (const NameCallback& callback)
00043         {
00044             m_callbacks.insert(callback);
00045         }
00046         void erase (const NameCallback& callback)
00047         {
00048             m_callbacks.erase(callback);
00049         }
00050         void changed (const std::string& name) const
00051         {
00052             for (NameCallbacks::const_iterator i = m_callbacks.begin(); i != m_callbacks.end(); ++i) {
00053                 (*i)(name);
00054             }
00055         }
00056 };
00057 
00058 class NamedEntity
00059 {
00060         EntityKeyValues& m_entity;
00061         NameCallbackSet m_changed;
00062         std::string m_name;
00063     public:
00064         NamedEntity (EntityKeyValues& entity) :
00065             m_entity(entity)
00066         {
00067         }
00068         std::string name () const
00069         {
00070             if (m_name.empty())
00071                 return m_entity.getEntityClass().name();
00072             return m_name;
00073         }
00074         void attach (const NameCallback& callback)
00075         {
00076             m_changed.insert(callback);
00077         }
00078         void detach (const NameCallback& callback)
00079         {
00080             m_changed.erase(callback);
00081         }
00082 
00083         void identifierChanged (const std::string& value)
00084         {
00085             if (value.empty()) {
00086                 m_changed.changed(m_entity.getEntityClass().name());
00087             } else {
00088                 m_changed.changed(value);
00089             }
00090             m_name = value;
00091         }
00092         typedef MemberCaller1<NamedEntity, const std::string&, &NamedEntity::identifierChanged> IdentifierChangedCaller;
00093 };
00094 
00095 class RenderableNamedEntity: public OpenGLRenderable
00096 {
00097         const NamedEntity& m_named;
00098         const Vector3& m_position;
00099     public:
00100         RenderableNamedEntity (const NamedEntity& named, const Vector3& position) :
00101             m_named(named), m_position(position)
00102         {
00103         }
00104 
00108         void render (RenderStateFlags state) const
00109         {
00110             glRasterPos3fv(m_position);
00111             GlobalOpenGL().drawString(m_named.name());
00112         }
00113 };
00114 
00115 #endif

Generated by  doxygen 1.6.2