origin.h

Go to the documentation of this file.
00001 /*
00002  Copyright (C) 2001-2006, William Joseph.
00003  All Rights Reserved.
00004 
00005  This file is part of GtkRadiant.
00006 
00007  GtkRadiant is free software; you can redistribute it and/or modify
00008  it under the terms of the GNU General Public License as published by
00009  the Free Software Foundation; either version 2 of the License, or
00010  (at your option) any later version.
00011 
00012  GtkRadiant is distributed in the hope that it will be useful,
00013  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  GNU General Public License for more details.
00016 
00017  You should have received a copy of the GNU General Public License
00018  along with GtkRadiant; if not, write to the Free Software
00019  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020  */
00021 
00022 #if !defined(INCLUDED_ORIGIN_H)
00023 #define INCLUDED_ORIGIN_H
00024 
00025 #include "ientity.h"
00026 
00027 #include "math/matrix.h"
00028 #include "generic/callback.h"
00029 #include "stringio.h"
00030 
00031 const Vector3 ORIGINKEY_IDENTITY = Vector3(0, 0, 0);
00032 
00033 inline void default_origin (Vector3& origin)
00034 {
00035     origin = ORIGINKEY_IDENTITY;
00036 }
00037 inline void read_origin (Vector3& origin, const std::string& value)
00038 {
00039     if (!string_parse_vector3(value.c_str(), origin)) {
00040         default_origin(origin);
00041     }
00042 }
00043 inline void write_origin (const Vector3& origin, Entity* entity, const char* key)
00044 {
00045     char value[64];
00046     sprintf(value, "%g %g %g", origin[0], origin[1], origin[2]);
00047     entity->setKeyValue(key, value);
00048 }
00049 
00050 inline Vector3 origin_translated (const Vector3& origin, const Vector3& translation)
00051 {
00052     return matrix4_get_translation_vec3(matrix4_multiplied_by_matrix4(Matrix4::getTranslation(origin),
00053             Matrix4::getTranslation(translation)));
00054 }
00055 
00056 inline Vector3 origin_snapped (const Vector3& origin, float snap)
00057 {
00058     return vector3_snapped(origin, snap);
00059 }
00060 
00061 class OriginKey
00062 {
00063         Callback m_originChanged;
00064     public:
00065         Vector3 m_origin;
00066 
00067         OriginKey (const Callback& originChanged) :
00068             m_originChanged(originChanged), m_origin(ORIGINKEY_IDENTITY)
00069         {
00070         }
00071 
00072         void originChanged (const std::string& value)
00073         {
00074             read_origin(m_origin, value);
00075             m_originChanged();
00076         }
00077         typedef MemberCaller1<OriginKey, const std::string&, &OriginKey::originChanged> OriginChangedCaller;
00078 
00079         void write (Entity* entity) const
00080         {
00081             write_origin(m_origin, entity, "origin");
00082         }
00083 };
00084 
00085 #endif

Generated by  doxygen 1.6.2