CEL

Public API Reference

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 #include "tools/expression.h"
00031 
00032 // The following macros will set 'var' to the required variable and
00033 // 'p_var' will be made to 0 if there is a failure.
00034 #define CEL_FETCH_STRING_PAR(var,params,id) \
00035   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00036   const char* var = 0; \
00037   if (p_##var && p_##var->type == CEL_DATA_STRING) { \
00038     var = p_##var->value.s->GetData (); \
00039   } else { p_##var = 0; }
00040 #define CEL_FETCH_VECTOR2_PAR(var,params,id) \
00041   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00042   csVector2 var; \
00043   if (p_##var && p_##var->type == CEL_DATA_VECTOR2) { \
00044     var.Set (p_##var->value.v.x, p_##var->value.v.y); \
00045   } else { p_##var = 0; }
00046 #define CEL_FETCH_VECTOR3_PAR(var,params,id) \
00047   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00048   csVector3 var; \
00049   if (p_##var && p_##var->type == CEL_DATA_VECTOR3) { \
00050     var.Set (p_##var->value.v.x, p_##var->value.v.y, p_##var->value.v.z); \
00051   } else { p_##var = 0; }
00052 #define CEL_FETCH_VECTOR4_PAR(var,params,id) \
00053   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00054   csVector4 var; \
00055   if (p_##var && p_##var->type == CEL_DATA_VECTOR4) { \
00056     var.Set (p_##var->value.v.x, p_##var->value.v.y, p_##var->value.v.z, p_##var->value.v.w); \
00057   } else { p_##var = 0; }
00058 #define CEL_FETCH_COLOR_PAR(var,params,id) \
00059   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00060   csColor var; \
00061   if (p_##var && p_##var->type == CEL_DATA_COLOR) { \
00062     var.Set (p_##var->value.col.red, p_##var->value.col.green, p_##var->value.col.blue); \
00063   } else { p_##var = 0; }
00064 #define CEL_FETCH_COLOR4_PAR(var,params,id) \
00065   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00066   csColor4 var; \
00067   if (p_##var && p_##var->type == CEL_DATA_COLOR4) { \
00068     var.Set (p_##var->value.col.red, p_##var->value.col.green, p_##var->value.col.blue, p_##var->value.col.alpha); \
00069   } else { p_##var = 0; }
00070 #define CEL_FETCH_FLOAT_PAR(var,params,id) \
00071   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00072   float var = 0.0f; \
00073   if (p_##var) { \
00074     if (p_##var->type == CEL_DATA_FLOAT) \
00075       var = p_##var->value.f; \
00076     else if (p_##var->type == CEL_DATA_LONG) \
00077       var = float (p_##var->value.l); \
00078     else p_##var = 0; \
00079   }
00080 #define CEL_FETCH_LONG_PAR(var,params,id) \
00081   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00082   long var = 0; \
00083   if (p_##var) { \
00084     if (p_##var->type == CEL_DATA_LONG) \
00085       var = p_##var->value.l; \
00086     else if (p_##var->type == CEL_DATA_FLOAT) \
00087       var = long (p_##var->value.f); \
00088     else p_##var = 0; \
00089   }
00090 #define CEL_FETCH_BOOL_PAR(var,params,id) \
00091   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00092   bool var = false; \
00093   if (p_##var) { \
00094     if (p_##var->type == CEL_DATA_BOOL) \
00095       var = p_##var->value.bo; \
00096     else if (p_##var->type == CEL_DATA_LONG) \
00097     var =  ((p_##var->value.l)? true : false); \
00098     else p_##var = 0; \
00099   }
00100 #define CEL_FETCH_PCLASS_PAR(var,params,id) \
00101   const celData* p_##var = params ? params->GetParameter (id) : 0; \
00102   iCelPropertyClass* var = 0; \
00103   if (p_##var) { \
00104     if (p_##var->type == CEL_DATA_PCLASS) \
00105       var = p_##var->value.pc; \
00106   }
00107 
00111 class celGenericParameterBlock : public scfImplementation1<
00112         celGenericParameterBlock, iCelParameterBlock>
00113 {
00114 private:
00115   size_t count;
00116   csStringID* ids;
00117   celData* data;
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   }
00127   virtual ~celGenericParameterBlock ()
00128   {
00129     delete[] ids;
00130     delete[] data;
00131   }
00132 
00133   void SetParameterDef (size_t idx, csStringID id)
00134   {
00135     ids[idx] = id;
00136   }
00137   celData& GetParameter (size_t idx) { return data[idx]; }
00138 
00139   virtual size_t GetParameterCount () const { return count; }
00140   virtual csStringID GetParameterDef (size_t idx, celDataType& t) const
00141   {
00142     if (/*idx < 0 || */idx >= count)
00143     {
00144       t = CEL_DATA_NONE;
00145       return csInvalidStringID;
00146     }
00147     t = data[idx].type;
00148     return ids[idx];
00149   }
00150   virtual const celData* GetParameter (csStringID id) const
00151   {
00152     size_t i;
00153     for (i = 0 ; i < count ; i++)
00154       if (id == ids[i])
00155         return &data[i];
00156     return 0;
00157   }
00158   virtual const celData* GetParameterByIndex (size_t idx) const
00159   {
00160     return (idx >= count) ? 0 : &data[idx];
00161   }
00162 };
00163 
00167 class celVariableParameterBlock : public scfImplementation1<
00168         celVariableParameterBlock,iCelParameterBlock>
00169 {
00170 private:
00171   csArray<csStringID> ids;
00172   csArray<celData> data;
00173 
00174 public:
00175   celVariableParameterBlock () : scfImplementationType (this)
00176   {
00177   }
00181   celVariableParameterBlock (iCelParameterBlock* other) :
00182     scfImplementationType (this)
00183   {
00184     if (other != 0)
00185     {
00186       csStringID id;
00187       celDataType type;
00188       for (size_t idx = 0; idx < other->GetParameterCount (); idx++)
00189       {
00190         id = other->GetParameterDef (idx, type);
00191         SetParameterDef (idx, id);
00192         data.GetExtend (idx) = *other->GetParameter (id);
00193       }
00194     }
00195   }
00196   virtual ~celVariableParameterBlock ()
00197   {
00198   }
00199 
00200   void SetParameterDef (size_t idx, csStringID id)
00201   {
00202     ids.GetExtend (idx) = id;
00203   }
00204   celData& GetParameter (size_t idx) { return data.GetExtend (idx); }
00205 
00206   virtual size_t GetParameterCount () const { return data.GetSize (); }
00207   virtual csStringID GetParameterDef (size_t idx, celDataType& t) const
00208   {
00209     if (/*idx < 0 || */idx >= data.GetSize ())
00210     {
00211       t = CEL_DATA_NONE;
00212       return csInvalidStringID;
00213     }
00214     t = data[idx].type;
00215     return ids[idx];
00216   }
00217   virtual const celData* GetParameter (csStringID id) const
00218   {
00219     size_t i;
00220     for (i = 0 ; i < data.GetSize () ; i++)
00221       if (id == ids[i])
00222         return &data[i];
00223     return 0;
00224   }
00225   virtual const celData* GetParameterByIndex (size_t idx) const
00226   {
00227     return (idx >= data.GetSize ()) ? 0 : &data[idx];
00228   }
00229 };
00230 
00234 class celOneParameterBlock : public scfImplementation1<
00235         celOneParameterBlock, iCelParameterBlock>
00236 {
00237 private:
00238   csStringID id;
00239   celData data;
00240 
00241 public:
00242   celOneParameterBlock () : scfImplementationType (this)
00243   {
00244   }
00246   celOneParameterBlock (csStringID id, const char* str)
00247     : scfImplementationType (this)
00248   {
00249     SetParameterDef (id);
00250     GetParameter (0).Set (str);
00251   }
00252   virtual ~celOneParameterBlock ()
00253   {
00254   }
00255 
00256   void SetParameterDef (csStringID id)
00257   {
00258     celOneParameterBlock::id = id;
00259   }
00260   celData& GetParameter (int) { return data; }
00261 
00262   virtual size_t GetParameterCount () const { return 1; }
00263   virtual csStringID GetParameterDef (size_t idx, celDataType& t) const
00264   {
00265     if (idx != 0)
00266     {
00267       t = CEL_DATA_NONE;
00268       return csInvalidStringID;
00269     }
00270     t = data.type;
00271     return celOneParameterBlock::id;
00272   }
00273   virtual const celData* GetParameter (csStringID id) const
00274   {
00275     if (id != celOneParameterBlock::id) return 0;
00276     return &data;
00277   }
00278   virtual const celData* GetParameterByIndex (size_t idx) const
00279   {
00280     return (idx != 0) ? 0 : &data;
00281   }
00282 };
00283 
00288 class celCombineParameterBlock : public scfImplementation1<
00289         celCombineParameterBlock, iCelParameterBlock>
00290 {
00291 private:
00292   csRef<iCelParameterBlock> b1;
00293   csRef<iCelParameterBlock> b2;
00294 
00295 public:
00299   celCombineParameterBlock (iCelParameterBlock* b1, iCelParameterBlock* b2)
00300     : scfImplementationType (this), b1 (b1), b2 (b2)
00301   {
00302   }
00303   virtual ~celCombineParameterBlock ()
00304   {
00305   }
00306   void SetParameterBlock1 (iCelParameterBlock* b1)
00307   {
00308     celCombineParameterBlock::b1 = b1;
00309   }
00310   void SetParameterBlock2 (iCelParameterBlock* b2)
00311   {
00312     celCombineParameterBlock::b2 = b2;
00313   }
00314 
00315   virtual size_t GetParameterCount () const
00316   {
00317     return b1->GetParameterCount () + (b2 ? b2->GetParameterCount () : 0);
00318   }
00319   virtual csStringID GetParameterDef (size_t idx, celDataType& t) const
00320   {
00321     if (idx < b1->GetParameterCount ())
00322     {
00323       return b1->GetParameterDef (idx, t);
00324     }
00325     else if (b2)
00326     {
00327       return b2->GetParameterDef (idx-b1->GetParameterCount (), t);
00328     }
00329     else
00330     {
00331       return csInvalidStringID;
00332     }
00333   }
00334   virtual const celData* GetParameter (csStringID id) const
00335   {
00336     const celData* data = b1->GetParameter (id);
00337     if (data) return data;
00338     if (!b2) return 0;
00339     return b2->GetParameter (id);
00340   }
00341   virtual const celData* GetParameterByIndex (size_t idx) const
00342   {
00343     if (idx < b1->GetParameterCount ())
00344     {
00345       return b1->GetParameterByIndex (idx);
00346     }
00347     else if (b2)
00348     {
00349       return b2->GetParameterByIndex (idx-b1->GetParameterCount ());
00350     }
00351     else
00352     {
00353       return 0;
00354     }
00355   }
00356 };
00357 
00358 struct celParameterMapping
00359 {
00360   csStringID source;
00361   csStringID dest;
00362   csRef<iCelExpression> expression;
00363 };
00364 
00369 class celMappedParameterBlock : public celVariableParameterBlock
00370 {
00371 public:
00372   celMappedParameterBlock (iCelEntity* entity, iCelParameterBlock* params,
00373       const csArray<celParameterMapping>& mapping)
00374   {
00375     for (size_t mi = 0 ; mi < mapping.GetSize () ; mi++)
00376     {
00377       const celParameterMapping& m = mapping[mi];
00378       SetParameterDef (mi, m.dest);
00379       if (m.expression)
00380         m.expression->Execute (entity, GetParameter (mi), params);
00381       else
00382       {
00383         for (size_t i = 0 ; i < params->GetParameterCount () ; i++)
00384         {
00385           celDataType t;
00386           csStringID id = params->GetParameterDef (i, t);
00387           if (id == m.source)
00388           {
00389             GetParameter (mi) = *params->GetParameterByIndex (i);
00390             break;
00391           }
00392         }
00393       }
00394     }
00395     for (size_t i = 0 ; i < params->GetParameterCount () ; i++)
00396     {
00397       celDataType t;
00398       csStringID id = params->GetParameterDef (i, t);
00399       size_t idx = i+mapping.GetSize ();
00400       SetParameterDef (idx, id);
00401       const celData* data = params->GetParameterByIndex (i);
00402       if (data)
00403         GetParameter (idx) = *data;
00404     }
00405   }
00406   virtual ~celMappedParameterBlock ()
00407   {
00408   }
00409 };
00410 
00411 #endif // __CEL_CELTOOL_PARAMS__
00412 

Generated for CEL: Crystal Entity Layer 2.0 by doxygen 1.7.6.1