Param.h 4.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /******************************************************************************
  2. Use 'Param' to store a custom parameter consisting of name and value.
  3. /******************************************************************************/
  4. enum PARAM_TYPE : Byte // Parameter Type
  5. {
  6. PARAM_BOOL , // boolean
  7. PARAM_INT , // int
  8. PARAM_FLT , // float
  9. PARAM_STR , // string
  10. PARAM_ENUM , // enum
  11. PARAM_VEC2 , // vector 2D
  12. PARAM_VEC , // vector 3D
  13. PARAM_VEC4 , // vector 4D
  14. PARAM_COLOR , // color
  15. PARAM_ID , // ID
  16. PARAM_ID_ARRAY, // ID array
  17. PARAM_NUM , // number of valid parameter types
  18. };
  19. /******************************************************************************/
  20. struct Param // Parameter
  21. {
  22. PARAM_TYPE type ; // parameter type
  23. Enum *enum_type; // pointer to 'Enum' object from 'Enums' cache, this is valid when "type==PARAM_ENUM"
  24. Str name ; // parameter name
  25. struct Value
  26. {
  27. union
  28. {
  29. Bool b ; // bool
  30. Int i ; // int
  31. Flt f ; // float
  32. Vec2 v2; // vector 2D
  33. Vec v ; // vector 3D
  34. Vec4 v4; // vector 4D
  35. Color c ; // color
  36. UID id; // ID
  37. };
  38. Str s; // string
  39. Value() {} // needed because of union
  40. }value; // parameter value
  41. // get
  42. Str asText (Int precision=-6 )C; // "" on fail
  43. Bool asBool ( )C; // false on fail
  44. Int asInt ( )C; // 0 on fail
  45. Flt asFlt ( )C; // 0 on fail
  46. Vec2 asVec2 ( )C; // (0,0) on fail
  47. Vec asVec ( )C; // (0,0,0) on fail
  48. Vec4 asVec4 ( )C; // (0,0,0,0) on fail
  49. Color asColor( )C; // (0,0,0,0) on fail
  50. UID asID ( )C; // UIDZero on fail
  51. UID asID (Int id_index )C; // UIDZero on fail, use this method for PARAM_ID_ARRAY type, 'id_index'=index of the ID in the array
  52. UID asIDMod(Int id_index )C; // UIDZero on fail, use this method for PARAM_ID_ARRAY type, 'id_index'=index of the ID in the array which is converted modulo to always fit in the array range "id_index=Mod(id_index, IDs())"
  53. Int asEnum ( )C; // -1 on fail
  54. T1(TYPE) TYPE asEnum (TYPE invalid_value)C {Int e=asEnum(); return (e<0) ? invalid_value : TYPE(e);} // use this method for custom enum types, 'invalid_value'=enum value to use when desired value was not found
  55. Int IDs ()C; // get number of ID's stored in this parameter, use this method for PARAM_ID_ARRAY type, for PARAM_ID type this will always return 1, and for other types this will always return 0
  56. UInt memUsage()C; // get memory usage
  57. Bool hasID(C UID &id)C; // check if 'id' is in the list of parameter ID's
  58. // set
  59. Param& clearValue( ); // clear value of this parameter without changing the parameter type
  60. Param& setValue( Int i ); // set value of this parameter to 'i' without changing the parameter type
  61. Param& setValue(C Str &s ); // set value of this parameter to 's' without changing the parameter type
  62. Param& setValue(C Param &src); // set value of this parameter to 'src' without changing the parameter type
  63. Param& setType(PARAM_TYPE type, Enum *enum_type=null); // set type to 'type' and automatically convert the parameter value to match the new type
  64. Param& setTypeValue(C Param &src); // set type and value of this parameter to 'src'
  65. Param& setAsIDArray(C MemPtr<UID> &ids, Bool allow_PARAM_ID_type=true); // set type to PARAM_ID_ARRAY and set value to 'ids', 'allow_PARAM_ID_type'=if number of id's is equal to 1 then use PARAM_ID type instead
  66. Param& includeAsIDArray(C MemPtr<UID> &ids, Bool allow_PARAM_ID_type=true); // set type to PARAM_ID_ARRAY and set value to 'ids', 'allow_PARAM_ID_type'=if number of id's is equal to 1 then use PARAM_ID type instead, this method preserves existing ID's and adds 'ids' which aren't present yet
  67. // io
  68. Bool save(File &f, CChar *path=null)C; // save, 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  69. Bool load(File &f, CChar *path=null) ; // load, 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  70. #if EE_PRIVATE
  71. Int arrayIDs( )C; // !! only for PARAM_ID_ARRAY !!
  72. void loadOld (File &f);
  73. void zero ( );
  74. #endif
  75. Param();
  76. };
  77. /******************************************************************************/
  78. Int Compare (C Param &p0, C Param &p1);
  79. Int CompareValue(C Param &p0, C Param &p1);
  80. /******************************************************************************/