Property.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /******************************************************************************
  2. Use 'Property' to handle manipulating class members using Gui controls.
  3. /******************************************************************************/
  4. enum PROP_MOUSE_EDIT_MODE : Byte // Property Mouse Edit Mode
  5. {
  6. PROP_MOUSE_EDIT_LINEAR, // works by adding/subtracting value
  7. PROP_MOUSE_EDIT_SCALAR, // works by multiplying/dividing value
  8. };
  9. /******************************************************************************/
  10. const_mem_addr struct Property // Class Member Gui Control !! must be stored in constant memory address !!
  11. {
  12. Dbl min_value, max_value; // -Inf..Inf , default=0
  13. Bool min_use , max_use ; // true/false, default=false
  14. PROP_MOUSE_EDIT_MODE mouse_edit_mode ; // default=PROP_MOUSE_EDIT_LINEAR
  15. Flt mouse_edit_speed ; // 0..Inf , default=1.0 (40 for Int's)
  16. Int real_precision ; // default=MAX_INT (auto-detect)
  17. MemberDesc md ;
  18. Text name ;
  19. CheckBox checkbox;
  20. TextLine textline;
  21. Button button ;
  22. ComboBox combobox;
  23. Slider slider ;
  24. // manage
  25. T1(TYPE) Property& create (C Str &name , TYPE &member) {return create(name, MemberDesc(member));}
  26. Property& create (C Str &name , C MemberDesc &md=MemberDesc());
  27. Property& setColor ( ); // set as color type
  28. Property& setFile (C Str &ext=S , C Str &desc=S); // set as file type, 'ext'=list of supported extensions separated with '|' symbol (null for all kinds of extensions), 'desc'=description of supported files types (if specified then it will be used in the window title bar instead of the 1st extension)
  29. Property& setEnum ( ); // set as enum type
  30. Property& setEnum (CChar8 *data[], Int elms ); // set as enum type and automatically set enum values
  31. Property& setEnum (CChar *data[], Int elms ); // set as enum type and automatically set enum values
  32. Property& setSlider( ); // set as slider type
  33. // get / set
  34. Property& desc(C Str &desc); C Str& desc()C; // set/get property description
  35. Property& autoData(Ptr object); Ptr autoData()C {return _auto_data;} // set/get automatic data setting
  36. Property& set( Bool value, SET_MODE mode=SET_DEFAULT); Bool asBool ()C; Bool asBool (CPtr object)C {return md.asBool (object );} // set / get from gui / get from data, false on fail
  37. Property& set( Int value, SET_MODE mode=SET_DEFAULT); Int asInt ()C; Int asInt (CPtr object)C {return md.asInt (object );} // set / get from gui / get from data, 0 on fail
  38. Property& set( Long value, SET_MODE mode=SET_DEFAULT); Long asLong ()C; Long asLong (CPtr object)C {return md.asLong (object );} // set / get from gui / get from data, 0 on fail
  39. Property& set( Flt value, SET_MODE mode=SET_DEFAULT); Flt asFlt ()C; Flt asFlt (CPtr object)C {return md.asFlt (object );} // set / get from gui / get from data, 0 on fail
  40. Property& set( Dbl value, SET_MODE mode=SET_DEFAULT); Dbl asDbl ()C; Dbl asDbl (CPtr object)C {return md.asDbl (object );} // set / get from gui / get from data, 0 on fail
  41. Property& set(C Vec2 &value, SET_MODE mode=SET_DEFAULT); Vec2 asVec2 ()C; Vec2 asVec2 (CPtr object)C {return md.asVec2 (object );} // set / get from gui / get from data, Vec2 (0, 0) on fail
  42. Property& set(C VecD2 &value, SET_MODE mode=SET_DEFAULT); VecD2 asVecD2()C; VecD2 asVecD2(CPtr object)C {return md.asVecD2(object );} // set / get from gui / get from data, VecD2(0, 0) on fail
  43. Property& set(C Str &value, SET_MODE mode=SET_DEFAULT); Str asText ()C; Str asText (CPtr object)C {return md.asText (object, real_precision);} // set / get from gui / get from data, "" on fail
  44. Int asEnum (CPtr object)C {return md.asEnum (object );} // set / get from gui / get from data, -1 on fail
  45. Property& min (Dbl value ) {min_use= true; min_value=value; return T;} // set minimum allowed value
  46. Property& max (Dbl value ) {max_use= true; max_value=value; return T;} // set maximum allowed value
  47. Property& range(Dbl min, Dbl max) {min_use=max_use=true; min_value=min; max_value=max; return T;} // set allowed value range
  48. Property& mouseEditDel ( ); // disable ability of changing the value with a mouse
  49. Property& mouseEditMode (PROP_MOUSE_EDIT_MODE mode ) {mouse_edit_mode =mode ; return T;} // default=PROP_MOUSE_EDIT_LINEAR
  50. Property& mouseEditSpeed(Flt speed) {mouse_edit_speed=speed; return T;} // 0..Inf, default=1.0 (40 for Int's)
  51. Property& precision(Int precision) {real_precision=precision; return T;} // set real value precision
  52. Property& changed(void (*changed)(C Property &prop), void (*pre_changed)(C Property &prop)=null); // set functions called after and before a property value is changed
  53. // get value clamped according to specified limits
  54. Bool clamp(Bool value);
  55. Int clamp(Int value);
  56. UInt clamp(UInt value);
  57. Long clamp(Long value);
  58. ULong clamp(ULong value);
  59. Flt clamp(Flt value);
  60. Dbl clamp(Dbl value);
  61. Bool contains(C GuiObj *child)C; // if property contains 'child' object
  62. // operations
  63. void fromGui( Ptr object); // set data from gui
  64. void toGui(CPtr object); // set gui from data
  65. void toGui( ); // set gui from 'autoData'
  66. Property& hide ( ); // hide property
  67. Property& show ( ); // show property
  68. Property& visible (Bool on); Bool visible()C; // set/get visibility
  69. Property& visibleToggle( ); // toggle visibility
  70. Property& pos (C Vec2 &pos ); // set position
  71. Property& move(C Vec2 &delta); // move by delta
  72. Property& close(); // will hide any visible windows (including Color Picker)
  73. Property& parent(GuiObj &parent); // change parent from current to 'parent'
  74. Property& enabled(Bool enabled); // set if enabled
  75. Rect addTo(GuiObj &parent, C Vec2 &pos, Flt text_width, Flt height, Flt value_width); // instead of calling this method manually, it is recommended to call 'AddProperties'. This method adds self to 'parent' and sets the rectangles of its members. Returns rectangle of this property
  76. ~Property();
  77. Property();
  78. #if !EE_PRIVATE
  79. private:
  80. #endif
  81. STRUCT(Color , GuiCustom)
  82. //{
  83. virtual void update(C GuiPC &gpc);
  84. virtual void draw (C GuiPC &gpc);
  85. };
  86. GUI_OBJ_TYPE _value_type;
  87. VecD2 _mouse_edit_value;
  88. Ptr _auto_data;
  89. Color _color;
  90. Str _win_io_ext, _win_io_ext_desc;
  91. ColorPicker *_cp;
  92. WindowIO *_win_io;
  93. void (*_changed)(C Property &prop), (*_pre_changed)(C Property &prop);
  94. #if EE_PRIVATE
  95. static void MouseEdit(Property &prop);
  96. static void SelectFile(Property &prop);
  97. Int actualPrecision()C; // get actual value precision
  98. #endif
  99. NO_COPY_CONSTRUCTOR(Property);
  100. };
  101. /******************************************************************************/
  102. Rect AddProperties(Memx<Property> &properties, GuiObj &parent, C Vec2 &left_up, Flt property_height, Flt value_width, C TextStylePtr &text_style=null); // add 'properties' properties to 'parent' gui object, 'left_up'=coordinates in parent space where to start adding elements, 'property_height'=height of a single property element, 'value_width'=width of gui elements specified for value presentation, returns rectangle containing the properties, 'ts'=text style used for describing properties (the object is not copied, only the pointer to the object is remembered, therefore it must point to a constant memory address !!)
  103. void SaveProperties(C Memx<Property> &properties, MemPtr<TextNode> nodes, Char space_replacement=' ', Bool handle_same_names=false); // save properties to text nodes, 'space_replacement'=replace spaces with this character, 'handle_same_names'=if handle case when some properties have same names
  104. void LoadProperties( Memx<Property> &properties, C MemPtr<TextNode> &nodes, Char space_replacement=' ', Bool handle_same_names=false); // load properties from text nodes, 'space_replacement'=replace spaces with this character, 'handle_same_names'=if handle case when some properties have same names
  105. void SaveProperties(C Memx<Property> &properties, XmlNode &node, Char space_replacement='\0'); // save properties to xml node, since xml does not support parameters with spaces in their names, all spaces need to be replaced, you can use 'space_replacement' to specify custom character as the replacement
  106. void LoadProperties( Memx<Property> &properties, C XmlNode &node, Char space_replacement='\0'); // load properties from xml node, since xml does not support parameters with spaces in their names, all spaces need to be replaced, you can use 'space_replacement' to specify custom character as the replacement
  107. /******************************************************************************/