origin.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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