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 "csutil/refarr.h" 00030 #include "behaviourlayer/behave.h" 00031 #include "tools/expression.h" 00032 00033 #include "celtool/celtoolextern.h" 00034 00035 struct iObjectRegistry; 00036 struct iDocumentNode; 00037 struct iParameter; 00038 struct iParameterManager; 00039 struct iCelPlLayer; 00040 class celVariableParameterBlock; 00041 00043 struct celParSpec 00044 { 00046 celDataType type; 00047 00049 csStringID id; 00050 00052 csString value; 00053 00054 celParSpec () { } 00055 celParSpec (celDataType type, csStringID id, const char* value) : 00056 type (type), id (id), value (value) { } 00057 }; 00058 00062 class CEL_CELTOOL_EXPORT celParameterTools 00063 { 00064 public: 00073 static csRef<iCelParameterBlock> ParseParams (iObjectRegistry* object_reg, 00074 iDocumentNode* node, ...); 00075 00080 static bool WriteParams (iObjectRegistry* object_reg, 00081 iDocumentNode* node, iCelParameterBlock* params); 00082 00086 static csString GetTypeName (celDataType type); 00087 00091 static celDataType GetType (const char* name); 00092 00097 static bool ToString (const celData& in, csString& out); 00098 00103 static bool ToLong (const celData& in, long& out); 00104 00109 static bool ToBool (const celData& in, bool& out); 00110 00115 static bool ToFloat (const celData& in, float& out); 00116 00121 static bool ToVector2 (const celData& in, csVector2& out); 00122 00127 static bool ToVector3 (const celData& in, csVector3& out); 00128 00133 static bool ToVector4 (const celData& in, csVector4& out); 00134 00139 static bool ToColor (const celData& in, csColor& out); 00140 00145 static bool ToColor4 (const celData& in, csColor4& out); 00146 00161 static bool Convert (const celData& in, celDataType type, celData& out); 00162 00168 static bool ParseParSpecBlock (iObjectRegistry* object_reg, 00169 iDocumentNode* node, csArray<celParSpec>& parameters); 00170 00174 static bool WriteParSpecBlock (iObjectRegistry* object_reg, 00175 iDocumentNode* node, const csArray<celParSpec>& parameters); 00176 00185 static csPtr<celVariableParameterBlock> GetParameterBlock ( 00186 iParameterManager* pm, 00187 iCelParameterBlock* params, 00188 const csArray<celParSpec>& parameters, 00189 csRefArray<iParameter>& dyn_parameters); 00190 00200 static bool FillParameterBlock ( 00201 iCelPlLayer* pl, 00202 iCelParameterBlock* params, 00203 celVariableParameterBlock* act_params, 00204 const csArray<celParSpec>& parameters, 00205 const csRefArray<iParameter>& dyn_parameters); 00206 00210 static csString GetDebugData (const celData* data); 00211 00216 static void Dump (const char* title, 00217 iCelParameterBlock* params, iCelPlLayer* pl = 0); 00218 }; 00219 00220 struct celVariable 00221 { 00222 csStringID id; 00223 celData data; 00224 00225 celVariable () { } 00226 celVariable (csStringID id, const celData& data) : id (id), data (data) { } 00227 }; 00228 00232 class CEL_CELTOOL_EXPORT celVariableParameterBlock : public scfImplementation1< 00233 celVariableParameterBlock,iCelParameterBlock> 00234 { 00235 private: 00236 csArray<celVariable> vars; 00237 00238 public: 00239 celVariableParameterBlock (int capacity = 0) : 00240 scfImplementationType (this), vars (capacity) 00241 { 00242 } 00246 celVariableParameterBlock (iCelParameterBlock* other) : 00247 scfImplementationType (this) 00248 { 00249 if (other != 0) 00250 { 00251 celDataType type; 00252 for (size_t idx = 0; idx < other->GetParameterCount (); idx++) 00253 { 00254 AddParameter (other->GetParameterDef (idx, type), *other->GetParameterByIndex (idx)); 00255 } 00256 } 00257 } 00258 virtual ~celVariableParameterBlock () 00259 { 00260 } 00261 00262 void Clear () 00263 { 00264 vars.Empty (); 00265 } 00266 00270 size_t AddParameter (csStringID id, const celData& data) 00271 { 00272 celVariable var (id, data); 00273 return vars.Push (var); 00274 } 00275 00279 void RemoveParameter (csStringID id) 00280 { 00281 for (size_t i = 0 ; i < vars.GetSize () ; i++) 00282 if (vars[i].id == id) 00283 { 00284 vars.DeleteIndex (i); 00285 return; 00286 } 00287 } 00288 00292 celData& AddParameter (csStringID id) 00293 { 00294 celVariable var; 00295 var.id = id; 00296 size_t idx = vars.Push (var); 00297 return vars[idx].data; 00298 } 00299 00300 void SetParameterDef (size_t idx, csStringID id) 00301 { 00302 vars.GetExtend (idx).id = id; 00303 } 00304 celData& GetParameter (size_t idx) 00305 { 00306 return vars.GetExtend (idx).data; 00307 } 00308 00309 virtual size_t GetParameterCount () const { return vars.GetSize (); } 00310 virtual csStringID GetParameterDef (size_t idx, celDataType& t) const 00311 { 00312 if (/*idx < 0 || */idx >= vars.GetSize ()) 00313 { 00314 t = CEL_DATA_NONE; 00315 return csInvalidStringID; 00316 } 00317 t = vars[idx].data.type; 00318 return vars[idx].id; 00319 } 00320 virtual const celData* GetParameter (csStringID id) const 00321 { 00322 size_t i; 00323 for (i = 0 ; i < vars.GetSize () ; i++) 00324 if (id == vars[i].id) 00325 return &vars[i].data; 00326 return 0; 00327 } 00328 virtual const celData* GetParameterByIndex (size_t idx) const 00329 { 00330 return (idx >= vars.GetSize ()) ? 0 : &vars[idx].data; 00331 } 00332 00333 celData* GetParameter (csStringID id) 00334 { 00335 size_t i; 00336 for (i = 0 ; i < vars.GetSize () ; i++) 00337 if (id == vars[i].id) 00338 return &vars[i].data; 00339 return 0; 00340 } 00341 00346 void Merge (iCelParameterBlock* params); 00347 }; 00348 00353 class CEL_CELTOOL_EXPORT celEntityParameterBlock : public scfImplementation1< 00354 celEntityParameterBlock, iCelParameterBlock> 00355 { 00356 private: 00357 static csStringID thisID; 00358 celData thisData; 00359 iCelEntity* entity; 00360 00361 public: 00362 celEntityParameterBlock (iCelPlLayer* pl, iCelEntity* entity); 00363 virtual ~celEntityParameterBlock () { } 00364 00365 virtual size_t GetParameterCount () const { return 1; } 00366 virtual csStringID GetParameterDef (size_t idx, celDataType& t) const 00367 { 00368 if (idx == 0) { t = CEL_DATA_ENTITY; return thisID; } 00369 return csInvalidStringID; 00370 } 00371 virtual const celData* GetParameter (csStringID id) const 00372 { 00373 if (id == thisID) return &thisData; 00374 return 0; 00375 } 00376 virtual const celData* GetParameterByIndex (size_t idx) const 00377 { 00378 if (idx == 0) return &thisData; 00379 return 0; 00380 } 00381 }; 00382 00386 class CEL_CELTOOL_EXPORT celOneParameterBlock : public scfImplementation1< 00387 celOneParameterBlock, iCelParameterBlock> 00388 { 00389 private: 00390 csStringID id; 00391 celData data; 00392 00393 public: 00394 celOneParameterBlock () : scfImplementationType (this) 00395 { 00396 } 00398 celOneParameterBlock (csStringID id, const char* str) 00399 : scfImplementationType (this) 00400 { 00401 SetParameterDef (id); 00402 GetParameter (0).Set (str); 00403 } 00404 virtual ~celOneParameterBlock () 00405 { 00406 } 00407 00408 void SetParameterDef (csStringID id) 00409 { 00410 celOneParameterBlock::id = id; 00411 } 00412 celData& GetParameter (int) { return data; } 00413 00414 virtual size_t GetParameterCount () const { return 1; } 00415 virtual csStringID GetParameterDef (size_t idx, celDataType& t) const 00416 { 00417 if (idx != 0) 00418 { 00419 t = CEL_DATA_NONE; 00420 return csInvalidStringID; 00421 } 00422 t = data.type; 00423 return celOneParameterBlock::id; 00424 } 00425 virtual const celData* GetParameter (csStringID id) const 00426 { 00427 if (id != celOneParameterBlock::id) return 0; 00428 return &data; 00429 } 00430 virtual const celData* GetParameterByIndex (size_t idx) const 00431 { 00432 return (idx != 0) ? 0 : &data; 00433 } 00434 }; 00435 00440 class CEL_CELTOOL_EXPORT celCombineParameterBlock : public scfImplementation1< 00441 celCombineParameterBlock, iCelParameterBlock> 00442 { 00443 private: 00444 csRef<iCelParameterBlock> b1; 00445 csRef<iCelParameterBlock> b2; 00446 00447 public: 00451 celCombineParameterBlock (iCelParameterBlock* b1, iCelParameterBlock* b2) 00452 : scfImplementationType (this), b1 (b1), b2 (b2) 00453 { 00454 } 00455 virtual ~celCombineParameterBlock () 00456 { 00457 } 00458 void SetParameterBlock1 (iCelParameterBlock* b1) 00459 { 00460 celCombineParameterBlock::b1 = b1; 00461 } 00462 void SetParameterBlock2 (iCelParameterBlock* b2) 00463 { 00464 celCombineParameterBlock::b2 = b2; 00465 } 00466 00467 virtual size_t GetParameterCount () const 00468 { 00469 return b1->GetParameterCount () + (b2 ? b2->GetParameterCount () : 0); 00470 } 00471 virtual csStringID GetParameterDef (size_t idx, celDataType& t) const 00472 { 00473 if (idx < b1->GetParameterCount ()) 00474 { 00475 return b1->GetParameterDef (idx, t); 00476 } 00477 else if (b2) 00478 { 00479 return b2->GetParameterDef (idx-b1->GetParameterCount (), t); 00480 } 00481 else 00482 { 00483 return csInvalidStringID; 00484 } 00485 } 00486 virtual const celData* GetParameter (csStringID id) const 00487 { 00488 const celData* data = b1->GetParameter (id); 00489 if (data) return data; 00490 if (!b2) return 0; 00491 return b2->GetParameter (id); 00492 } 00493 virtual const celData* GetParameterByIndex (size_t idx) const 00494 { 00495 if (idx < b1->GetParameterCount ()) 00496 { 00497 return b1->GetParameterByIndex (idx); 00498 } 00499 else if (b2) 00500 { 00501 return b2->GetParameterByIndex (idx-b1->GetParameterCount ()); 00502 } 00503 else 00504 { 00505 return 0; 00506 } 00507 } 00508 }; 00509 00510 struct celParameterMapping 00511 { 00512 csStringID source; 00513 csStringID dest; 00514 csRef<iCelExpression> expression; 00515 }; 00516 00521 class CEL_CELTOOL_EXPORT celMappedParameterBlock : public celVariableParameterBlock 00522 { 00523 public: 00524 celMappedParameterBlock (iCelEntity* entity, iCelParameterBlock* params, 00525 const csArray<celParameterMapping>& mapping) 00526 { 00527 for (size_t mi = 0 ; mi < mapping.GetSize () ; mi++) 00528 { 00529 const celParameterMapping& m = mapping[mi]; 00530 SetParameterDef (mi, m.dest); 00531 if (m.expression) 00532 m.expression->Execute (entity, GetParameter (mi), params); 00533 else 00534 { 00535 for (size_t i = 0 ; i < params->GetParameterCount () ; i++) 00536 { 00537 celDataType t; 00538 csStringID id = params->GetParameterDef (i, t); 00539 if (id == m.source) 00540 { 00541 GetParameter (mi) = *params->GetParameterByIndex (i); 00542 break; 00543 } 00544 } 00545 } 00546 } 00547 for (size_t i = 0 ; i < params->GetParameterCount () ; i++) 00548 { 00549 celDataType t; 00550 csStringID id = params->GetParameterDef (i, t); 00551 size_t idx = i+mapping.GetSize (); 00552 SetParameterDef (idx, id); 00553 const celData* data = params->GetParameterByIndex (i); 00554 if (data) 00555 GetParameter (idx) = *data; 00556 } 00557 } 00558 virtual ~celMappedParameterBlock () 00559 { 00560 } 00561 }; 00562 00563 #endif // __CEL_CELTOOL_PARAMS__ 00564
Generated for CEL: Crystal Entity Layer 2.1 by doxygen 1.6.1
