celtool/stdparams.h
00001 /* 00002 Crystal Space Entity Layer 00003 Copyright (C) 2003 by Jorrit Tyberghein 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 00020 #ifndef __CEL_CELTOOL_PARAMS__ 00021 #define __CEL_CELTOOL_PARAMS__ 00022 00023 #include "cstypes.h" 00024 #include "csutil/scf.h" 00025 #include "csutil/strhash.h" 00026 #include "csutil/util.h" 00027 #include "csutil/array.h" 00028 #include "csutil/stringarray.h" 00029 #include "behaviourlayer/behave.h" 00030 00031 // The following macros will set 'var' to the required variable and 00032 // 'p_var' will be made to 0 if there is a failure. 00033 #define CEL_FETCH_STRING_PAR(var,params,id) \ 00034 const celData* p_##var = params ? params->GetParameter (id) : 0; \ 00035 const char* var = 0; \ 00036 if (p_##var && p_##var->type == CEL_DATA_STRING) { \ 00037 var = p_##var->value.s->GetData (); \ 00038 } else { p_##var = 0; } 00039 #define CEL_FETCH_VECTOR2_PAR(var,params,id) \ 00040 const celData* p_##var = params ? params->GetParameter (id) : 0; \ 00041 csVector2 var; \ 00042 if (p_##var && p_##var->type == CEL_DATA_VECTOR2) { \ 00043 var.Set (p_##var->value.v.x, p_##var->value.v.y); \ 00044 } else { p_##var = 0; } 00045 #define CEL_FETCH_VECTOR3_PAR(var,params,id) \ 00046 const celData* p_##var = params ? params->GetParameter (id) : 0; \ 00047 csVector3 var; \ 00048 if (p_##var && p_##var->type == CEL_DATA_VECTOR3) { \ 00049 var.Set (p_##var->value.v.x, p_##var->value.v.y, p_##var->value.v.z); \ 00050 } else { p_##var = 0; } 00051 #define CEL_FETCH_VECTOR4_PAR(var,params,id) \ 00052 const celData* p_##var = params ? params->GetParameter (id) : 0; \ 00053 csVector4 var; \ 00054 if (p_##var && p_##var->type == CEL_DATA_VECTOR4) { \ 00055 var.Set (p_##var->value.v.x, p_##var->value.v.y, p_##var->value.v.z, p_##var->value.v.w); \ 00056 } else { p_##var = 0; } 00057 #define CEL_FETCH_COLOR_PAR(var,params,id) \ 00058 const celData* p_##var = params ? params->GetParameter (id) : 0; \ 00059 csColor var; \ 00060 if (p_##var && p_##var->type == CEL_DATA_COLOR) { \ 00061 var.Set (p_##var->value.col.red, p_##var->value.col.green, p_##var->value.col.blue); \ 00062 } else { p_##var = 0; } 00063 #define CEL_FETCH_COLOR4_PAR(var,params,id) \ 00064 const celData* p_##var = params ? params->GetParameter (id) : 0; \ 00065 csColor4 var; \ 00066 if (p_##var && p_##var->type == CEL_DATA_COLOR4) { \ 00067 var.Set (p_##var->value.col.red, p_##var->value.col.green, p_##var->value.col.blue, p_##var->value.col.alpha); \ 00068 } else { p_##var = 0; } 00069 #define CEL_FETCH_FLOAT_PAR(var,params,id) \ 00070 const celData* p_##var = params ? params->GetParameter (id) : 0; \ 00071 float var = 0.0f; \ 00072 if (p_##var) { \ 00073 if (p_##var->type == CEL_DATA_FLOAT) \ 00074 var = p_##var->value.f; \ 00075 else if (p_##var->type == CEL_DATA_LONG) \ 00076 var = float (p_##var->value.l); \ 00077 else p_##var = 0; \ 00078 } 00079 #define CEL_FETCH_LONG_PAR(var,params,id) \ 00080 const celData* p_##var = params ? params->GetParameter (id) : 0; \ 00081 long var = 0; \ 00082 if (p_##var) { \ 00083 if (p_##var->type == CEL_DATA_LONG) \ 00084 var = p_##var->value.l; \ 00085 else if (p_##var->type == CEL_DATA_FLOAT) \ 00086 var = long (p_##var->value.f); \ 00087 else p_##var = 0; \ 00088 } 00089 #define CEL_FETCH_BOOL_PAR(var,params,id) \ 00090 const celData* p_##var = params ? params->GetParameter (id) : 0; \ 00091 bool var = false; \ 00092 if (p_##var) { \ 00093 if (p_##var->type == CEL_DATA_BOOL) \ 00094 var = p_##var->value.bo; \ 00095 else if (p_##var->type == CEL_DATA_LONG) \ 00096 var = ((p_##var->value.l)? true : false); \ 00097 else p_##var = 0; \ 00098 } 00099 #define CEL_FETCH_PCLASS_PAR(var,params,id) \ 00100 const celData* p_##var = params ? params->GetParameter (id) : 0; \ 00101 iCelPropertyClass* var = 0; \ 00102 if (p_##var) { \ 00103 if (p_##var->type == CEL_DATA_PCLASS) \ 00104 var = p_##var->value.pc; \ 00105 } 00106 00110 class celGenericParameterBlock : public scfImplementation1< 00111 celGenericParameterBlock, iCelParameterBlock> 00112 { 00113 private: 00114 size_t count; 00115 csStringID* ids; 00116 celData* data; 00117 char** names; 00118 00119 public: 00120 celGenericParameterBlock (size_t count) : 00121 scfImplementationType (this) 00122 { 00123 celGenericParameterBlock::count = count; 00124 ids = new csStringID[count]; 00125 data = new celData[count]; 00126 names = new char*[count]; 00127 memset (names, 0, sizeof (char*)*count); 00128 } 00129 virtual ~celGenericParameterBlock () 00130 { 00131 delete[] ids; 00132 delete[] data; 00133 size_t i; 00134 for (i = 0 ; i < count ; i++) 00135 delete[] names[i]; 00136 delete[] names; 00137 } 00138 00139 void SetParameterDef (size_t idx, csStringID id, const char* parname) 00140 { 00141 ids[idx] = id; 00142 delete[] names[idx]; 00143 names[idx] = csStrNew (parname); 00144 } 00145 celData& GetParameter (size_t idx) { return data[idx]; } 00146 00147 virtual size_t GetParameterCount () const { return count; } 00148 virtual const char* GetParameter (size_t idx, csStringID& id, 00149 celDataType& t) const 00150 { 00151 if (/*idx < 0 || */idx >= count) 00152 { 00153 id = csInvalidStringID; 00154 t = CEL_DATA_NONE; 00155 return 0; 00156 } 00157 id = ids[idx]; 00158 t = data[idx].type; 00159 return names[idx]; 00160 } 00161 virtual const celData* GetParameter (csStringID id) const 00162 { 00163 size_t i; 00164 for (i = 0 ; i < count ; i++) 00165 if (id == ids[i]) 00166 return &data[i]; 00167 return 0; 00168 } 00169 virtual const celData* GetParameterByIndex (size_t idx) const 00170 { 00171 return (idx >= count) ? 0 : &data[idx]; 00172 } 00173 }; 00174 00178 class celVariableParameterBlock : public scfImplementation1< 00179 celVariableParameterBlock,iCelParameterBlock> 00180 { 00181 private: 00182 csArray<csStringID> ids; 00183 csArray<celData> data; 00184 csStringArray names; 00185 00186 public: 00187 celVariableParameterBlock () : scfImplementationType (this) 00188 { 00189 } 00193 celVariableParameterBlock (iCelParameterBlock* other) : 00194 scfImplementationType (this) 00195 { 00196 if (other != 0) 00197 { 00198 const char* name = 0; 00199 csStringID id; 00200 celDataType type; 00201 for (size_t idx = 0; idx < other->GetParameterCount (); idx++) 00202 { 00203 name = other->GetParameter (idx, id, type); 00204 SetParameterDef (idx, id, name); 00205 data.GetExtend (idx) = *other->GetParameter (id); 00206 } 00207 } 00208 } 00209 virtual ~celVariableParameterBlock () 00210 { 00211 } 00212 00213 void SetParameterDef (size_t idx, csStringID id, const char* parname) 00214 { 00215 ids.GetExtend (idx) = id; 00216 if (idx >= names.GetSize ()) 00217 names.SetSize (idx+1); 00218 names.Put (idx, parname); 00219 } 00220 celData& GetParameter (size_t idx) { return data.GetExtend (idx); } 00221 00222 virtual size_t GetParameterCount () const { return data.GetSize (); } 00223 virtual const char* GetParameter (size_t idx, csStringID& id, 00224 celDataType& t) const 00225 { 00226 if (/*idx < 0 || */idx >= data.GetSize ()) 00227 { 00228 id = csInvalidStringID; 00229 t = CEL_DATA_NONE; 00230 return 0; 00231 } 00232 id = ids[idx]; 00233 t = data[idx].type; 00234 return names[idx]; 00235 } 00236 virtual const celData* GetParameter (csStringID id) const 00237 { 00238 size_t i; 00239 for (i = 0 ; i < data.GetSize () ; i++) 00240 if (id == ids[i]) 00241 return &data[i]; 00242 return 0; 00243 } 00244 virtual const celData* GetParameterByIndex (size_t idx) const 00245 { 00246 return (idx >= data.GetSize ()) ? 0 : &data[idx]; 00247 } 00248 }; 00249 00253 class celOneParameterBlock : public scfImplementation1< 00254 celOneParameterBlock, iCelParameterBlock> 00255 { 00256 private: 00257 csStringID id; 00258 celData data; 00259 csString name; 00260 00261 public: 00262 celOneParameterBlock () : scfImplementationType (this) 00263 { 00264 } 00265 virtual ~celOneParameterBlock () 00266 { 00267 } 00268 00269 void SetParameterDef (csStringID id, const char* parname) 00270 { 00271 celOneParameterBlock::id = id; 00272 name = parname; 00273 } 00274 celData& GetParameter (int) { return data; } 00275 00276 virtual size_t GetParameterCount () const { return 1; } 00277 virtual const char* GetParameter (size_t idx, csStringID& id, 00278 celDataType& t) const 00279 { 00280 if (idx != 0) 00281 { 00282 id = csInvalidStringID; 00283 t = CEL_DATA_NONE; 00284 return 0; 00285 } 00286 id = celOneParameterBlock::id; 00287 t = data.type; 00288 return name; 00289 } 00290 virtual const celData* GetParameter (csStringID id) const 00291 { 00292 if (id != celOneParameterBlock::id) return 0; 00293 return &data; 00294 } 00295 virtual const celData* GetParameterByIndex (size_t idx) const 00296 { 00297 return (idx != 0) ? 0 : &data; 00298 } 00299 }; 00300 00305 class celCombineParameterBlock : public scfImplementation1< 00306 celCombineParameterBlock, iCelParameterBlock> 00307 { 00308 private: 00309 csRef<iCelParameterBlock> b1; 00310 csRef<iCelParameterBlock> b2; 00311 00312 public: 00316 celCombineParameterBlock (iCelParameterBlock* b1, iCelParameterBlock* b2) 00317 : scfImplementationType (this), b1 (b1), b2 (b2) 00318 { 00319 } 00320 virtual ~celCombineParameterBlock () 00321 { 00322 } 00323 void SetParameterBlock1 (iCelParameterBlock* b1) 00324 { 00325 celCombineParameterBlock::b1 = b1; 00326 } 00327 void SetParameterBlock2 (iCelParameterBlock* b2) 00328 { 00329 celCombineParameterBlock::b2 = b2; 00330 } 00331 00332 virtual size_t GetParameterCount () const 00333 { 00334 return b1->GetParameterCount () + (b2 ? b2->GetParameterCount () : 0); 00335 } 00336 virtual const char* GetParameter (size_t idx, csStringID& id, 00337 celDataType& t) const 00338 { 00339 if (idx < b1->GetParameterCount ()) 00340 { 00341 return b1->GetParameter (idx, id, t); 00342 } 00343 else if (b2) 00344 { 00345 return b2->GetParameter (idx-b1->GetParameterCount (), id, t); 00346 } 00347 else 00348 { 00349 return 0; 00350 } 00351 } 00352 virtual const celData* GetParameter (csStringID id) const 00353 { 00354 const celData* data = b1->GetParameter (id); 00355 if (data) return data; 00356 if (!b2) return 0; 00357 return b2->GetParameter (id); 00358 } 00359 virtual const celData* GetParameterByIndex (size_t idx) const 00360 { 00361 if (idx < b1->GetParameterCount ()) 00362 { 00363 return b1->GetParameterByIndex (idx); 00364 } 00365 else if (b2) 00366 { 00367 return b2->GetParameterByIndex (idx-b1->GetParameterCount ()); 00368 } 00369 else 00370 { 00371 return 0; 00372 } 00373 } 00374 }; 00375 00376 #endif // __CEL_CELTOOL_PARAMS__ 00377
Generated for CEL: Crystal Entity Layer 1.4.1 by doxygen 1.7.1