00001 /* -*- C++ -*- */ 00002 00003 /* COPYRIGHT 00004 * 00005 * This file is part of the Visual Effects Engine - VEE 00006 * 00007 * Read the "VEE-LICENSE" file for the license. 00008 * 00009 * Authors & Copyright: 00010 * 00011 * Tommi Ilmonen, Tuukka Heikura, Marko Myllymaa and 00012 * Janne Kontkanen 2001-2004 00013 * 00014 * Additional copyrights: Tekes 2003-2004 00015 * 00016 * firstname.lastname@hut.fi 00017 * 00018 */ 00019 00020 #ifndef VEE_VARIABLE_H 00021 #define VEE_VARIABLE_H 00022 00023 #include <vee_color.h> 00024 #include <vee_vector3.h> 00025 #include <vee_matrix3.h> 00026 00027 /* Keep in mind that these need to be copied to "vee.i" (scripting 00028 language wrappers. */ 00029 enum VEE_VariableType { 00030 VEE_VAR_INVALID, 00031 VEE_VAR_INT, 00032 VEE_VAR_INT_PTR, 00033 VEE_VAR_FLOAT, 00034 VEE_VAR_FLOAT_PTR, 00035 VEE_VAR_RGBA, 00036 VEE_VAR_VECTOR3, 00037 VEE_VAR_VECTOR3_PTR, 00038 VEE_VAR_MATRIX3, 00039 VEE_VAR_SIZEOF 00040 }; 00041 00042 struct __VEE_VarF { float v[5]; }; 00043 // struct __VEE_VarFP { float v; float *vp; }; 00044 00046 union __VEE_Variable { 00047 00048 int m_int; 00049 const int *m_intPtr; 00050 float m_float; 00051 const float *m_floatPtr; 00052 __VEE_VarF m_floats; 00053 const VEE_Vector3 *m_vector3Ptr; 00054 const VEE_Matrix3 *m_matrix3; 00055 // void *m_ptr; 00056 }; 00057 00061 class VEE_Variable 00062 { 00063 public: 00064 VEE_Variable() : m_type(VEE_VAR_INVALID) {} 00065 ~VEE_Variable() {} 00066 00067 // Returns true if the value can be converted to a number 00068 bool isNumber() const 00069 { return m_type >= VEE_VAR_INT && m_type <= VEE_VAR_FLOAT_PTR; } 00070 00072 VEE_VariableType type() const { return m_type; } 00073 bool isValid() const { return m_type != VEE_VAR_INVALID; } 00074 00075 inline float getFloat(float def = 0.0) const; 00076 inline const float * getFloatPtr() const; 00077 inline int getInt(int def = 0) const; 00078 inline const int * getIntPtr() const; 00079 00080 const VEE_Rgba & getRgba(const VEE_Rgba &def = VEE_Rgba::White) 00081 { return m_type == VEE_VAR_RGBA ? 00082 * (const VEE_Rgba * ) & m_val.m_floats : def; } 00083 00084 inline const VEE_Vector3 & getVector3(const VEE_Vector3 &def = VEE_Vector3ZERO) const; 00085 00086 const VEE_Matrix3 & getMatrix3(const VEE_Matrix3 &def = VEE_Matrix3IDENTITY) 00087 { return m_type == VEE_VAR_MATRIX3 ? *m_val.m_matrix3 : def; } 00088 00089 void setFloat(float v) { m_type = VEE_VAR_FLOAT; m_val.m_float = v; } 00090 void setFloatPtr(const float *v) 00091 { m_type = VEE_VAR_FLOAT_PTR; m_val.m_floatPtr = v; } 00092 void setInt(int v) { m_type = VEE_VAR_INT; m_val.m_int = v; } 00093 void setIntPtr(const int *v) 00094 { m_type = VEE_VAR_INT_PTR; m_val.m_intPtr = v; } 00095 void setRgba(const VEE_Rgba &v) 00096 { m_type = VEE_VAR_RGBA; * ((VEE_Rgba *) & m_val.m_floats) = v; } 00097 void setVector3(const VEE_Vector3 &v) 00098 { m_type = VEE_VAR_VECTOR3; * ((VEE_Vector3 *) & m_val.m_floats) = v; } 00099 void setVector3Ptr(const VEE_Vector3 *v) 00100 { m_type = VEE_VAR_VECTOR3_PTR; m_val.m_vector3Ptr = v; } 00105 void setMatrix3(const VEE_Matrix3 &v) 00106 { m_type = VEE_VAR_MATRIX3; m_val.m_matrix3 = &v; } 00107 00108 VEE_Variable & operator = (float v) { setFloat(v); return *this; } 00109 VEE_Variable & operator = (const float *v) { setFloatPtr(v); return *this; } 00110 VEE_Variable & operator = (int v) { setInt(v); return *this; } 00111 VEE_Variable & operator = (const int *v) { setIntPtr(v); return *this; } 00112 VEE_Variable & operator = (const VEE_Vector3 &v) 00113 { setVector3(v); return *this; } 00114 VEE_Variable & operator = (const VEE_Vector3 *v) 00115 { setVector3Ptr(v); return *this; } 00116 VEE_Variable & operator = (const VEE_Rgba &v) 00117 { setRgba(v); return *this; } 00118 VEE_Variable & operator = (const VEE_Matrix3 &v) 00119 { setMatrix3(v); return *this; } 00120 00121 protected: 00122 VEE_VariableType m_type; 00123 __VEE_Variable m_val; 00124 }; 00125 00126 inline float VEE_Variable::getFloat(float def) const 00127 { 00128 if(m_type == VEE_VAR_FLOAT) 00129 return m_val.m_float; 00130 else if(m_type == VEE_VAR_FLOAT_PTR) 00131 return * m_val.m_floatPtr; 00132 else if(m_type == VEE_VAR_INT) 00133 return m_val.m_int; 00134 else if(m_type == VEE_VAR_INT_PTR) 00135 return * m_val.m_intPtr; 00136 return def; 00137 } 00138 00139 inline const float * VEE_Variable::getFloatPtr() const 00140 { 00141 if(m_type == VEE_VAR_FLOAT_PTR) 00142 return m_val.m_floatPtr; 00143 return 0; 00144 } 00145 00146 inline int VEE_Variable::getInt(int def) const 00147 { 00148 if(m_type == VEE_VAR_INT) 00149 return m_val.m_int; 00150 else if(m_type == VEE_VAR_INT_PTR) 00151 return * m_val.m_intPtr; 00152 else if(m_type == VEE_VAR_FLOAT) 00153 return (int) m_val.m_float; 00154 else if(m_type == VEE_VAR_FLOAT_PTR) 00155 return (int) * m_val.m_floatPtr; 00156 return def; 00157 } 00158 00159 inline const int * VEE_Variable::getIntPtr() const 00160 { 00161 if(m_type == VEE_VAR_INT_PTR) 00162 return m_val.m_intPtr; 00163 return 0; 00164 } 00165 00166 inline const VEE_Vector3 & VEE_Variable::getVector3(const VEE_Vector3 &def) const 00167 { 00168 if(m_type == VEE_VAR_VECTOR3) 00169 return * (const VEE_Vector3 *) & m_val.m_floats; 00170 else if(m_type == VEE_VAR_VECTOR3_PTR) 00171 return * m_val.m_vector3Ptr; 00172 return def; 00173 } 00174 00175 00176 #endif