scale.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_SCALE_H)
00023 #define INCLUDED_SCALE_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 SCALEKEY_IDENTITY = Vector3(1, 1, 1);
00032
00033 inline void default_scale (Vector3& scale)
00034 {
00035 scale = SCALEKEY_IDENTITY;
00036 }
00037 inline void read_scale (Vector3& scalevec, const char* value)
00038 {
00039 float scale;
00040 if (!string_parse_float(value, scale) || scale == 0) {
00041 default_scale(scalevec);
00042 } else {
00043 scalevec = Vector3(scale, scale, scale);
00044 }
00045 }
00046 inline void read_scalevec (Vector3& scale, const char* value)
00047 {
00048 if (!string_parse_vector3(value, scale) || scale[0] == 0 || scale[1] == 0 || scale[2] == 0)
00049 default_scale(scale);
00050 }
00051 inline void write_scale (const Vector3& scale, Entity* entity)
00052 {
00053 if (scale[0] == 1 && scale[1] == 1 && scale[2] == 1) {
00054 entity->setKeyValue("modelscale_vec", "");
00055 } else {
00056 char value[64];
00057
00058 sprintf(value, "%g %g %g", scale[0], scale[1], scale[2]);
00059 entity->setKeyValue("modelscale_vec", value);
00060 }
00061 }
00062
00063 inline Vector3 scale_scaled (const Vector3& scale, const Vector3& scaling)
00064 {
00065 return matrix4_get_scale_vec3(matrix4_multiplied_by_matrix4(matrix4_scale_for_vec3(scale), matrix4_scale_for_vec3(
00066 scaling)));
00067 }
00068
00069 class ScaleKey
00070 {
00071 Callback m_scaleChanged;
00072 public:
00073 Vector3 m_scale;
00074
00075 ScaleKey (const Callback& scaleChanged) :
00076 m_scaleChanged(scaleChanged), m_scale(SCALEKEY_IDENTITY)
00077 {
00078 }
00079
00080 void scaleChanged (const std::string& value)
00081 {
00082 read_scalevec(m_scale, value.c_str());
00083 m_scaleChanged();
00084 }
00085 typedef MemberCaller1<ScaleKey, const std::string&, &ScaleKey::scaleChanged> ScaleChangedCaller;
00086
00087 void write (Entity* entity) const
00088 {
00089 write_scale(m_scale, entity);
00090 }
00091 };
00092
00093 #endif