Gui Object.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /******************************************************************************/
  2. enum GUI_OBJ_TYPE : Byte // Gui Object Type
  3. {
  4. GO_NONE = 0, // none
  5. GO_BUTTON = 1, // Button
  6. GO_CHECKBOX= 2, // CheckBox
  7. GO_COMBOBOX= 3, // ComboBox
  8. GO_CUSTOM = 5, // Custom
  9. GO_DESKTOP = 6, // Desktop
  10. GO_IMAGE = 7, // Image
  11. GO_LIST = 8, // List
  12. GO_MENU = 4, // Menu
  13. GO_MENU_BAR= 9, // MenuBar
  14. GO_PROGRESS=10, // ProgressBar
  15. GO_REGION =11, // Region
  16. GO_SLIDEBAR=12, // SlideBar
  17. GO_SLIDER =13, // Slider
  18. GO_TAB =20, // Tab
  19. GO_TABS =14, // Tabs
  20. GO_TEXT =15, // Text
  21. GO_TEXTBOX =16, // TextBox
  22. GO_TEXTLINE=17, // TextLine
  23. GO_VIEWPORT=18, // Viewport
  24. GO_WINDOW =19, // Window
  25. GO_NUM =21, // number of Gui Object Types
  26. };
  27. enum GUI_BASE_LEVEL
  28. {
  29. GBL_DEFAULT,
  30. GBL_WINDOW ,
  31. GBL_MENU ,
  32. };
  33. enum SET_MODE
  34. {
  35. SET_DEFAULT, // default set mode, callback is called, sound is played
  36. QUIET , // quiet set mode, callback is NOT called, sound is NOT played
  37. NO_SOUND , // no sound set mode, callback is called, sound is NOT played
  38. };
  39. /******************************************************************************/
  40. struct GuiPC // Gui Parent->Child Relation
  41. {
  42. Bool visible , // if parent is visible
  43. enabled ; // if parent is enabled
  44. Vec2 offset ; // screen offset which should be applied when drawing
  45. Rect clip , // clipping rectangle which should be used for D.clip method before any drawing
  46. client_rect; // parent client rectangle in screen space
  47. Bool hidden ()C {return !visible;} // if parent is hidden
  48. Bool disabled()C {return !enabled;} // if parent is disabled
  49. GuiPC() {}
  50. GuiPC( Desktop &desktop);
  51. GuiPC(C GuiPC &old, Region &region );
  52. GuiPC(C GuiPC &old, Window &window );
  53. #if EE_PRIVATE
  54. GuiPC(C GuiPC &old, Bool visible, Bool enabled);
  55. GuiPC(C GuiPC &old, Menu &menu );
  56. GuiPC(C GuiPC &old, _List &list );
  57. #endif
  58. };
  59. /******************************************************************************/
  60. const_mem_addr struct GuiObj // Gui Object interface inherited by all Gui Object classes (Button, CheckBox, Window, ..) !! must be stored in constant memory address !!
  61. {
  62. Ptr user; // user data pointer
  63. // manage
  64. virtual GuiObj& del (); // manually delete
  65. GuiObj& create(C GuiObj &src); // create from 'src', this method will succeed only if objects are of the same type
  66. // children
  67. void operator+=(GuiObj &child ); // add child
  68. void operator-=(GuiObj &child ); // remove child
  69. void operator+=(GuiObjs &children); // add children
  70. void operator-=(GuiObjs &children); // remove children
  71. GuiObj* child (Int i ); // get i-th child
  72. Int childNum ( ); // get number of children
  73. // operations
  74. Bool moveUp ( )C; // move object up in parent's children hierarchy, returns true if object was moved and false it not
  75. Bool moveDown ( )C; // move object down in parent's children hierarchy, returns true if object was moved and false it not
  76. GuiObj& moveToTop ( ) ; // move object to top in parent's children hierarchy
  77. GuiObj& moveToBottom( ) ; // move object to bottom in parent's children hierarchy
  78. GuiObj& moveAbove (C GuiObj &obj) ; // move object above 'obj' in parent's children hierarchy, this call will be ignored if objects have different parents
  79. GuiObj& moveBelow (C GuiObj &obj) ; // move object below 'obj' in parent's children hierarchy, this call will be ignored if objects have different parents
  80. #if EE_PRIVATE
  81. GuiObj& windowsToTop() ; // move all parent windows to top (including self)
  82. GuiObj& validateLevel() ; // place object in correct order in the parents child list
  83. Bool partiallyOccludedInSameLevel()C; // if object is at least partially occluded by any other object in the same hierarchy level
  84. Bool enabledFull()C; // if object and all of its parents are enabled
  85. Bool disabledFull()C; // if object or any of its parents are disabled
  86. #endif
  87. GuiObj& kbSet ( ); // set keyborard focus
  88. GuiObj& kbClear ( ); // clear keyborard focus
  89. virtual GuiObj& hide ( ); // hide object
  90. virtual GuiObj& show ( ); // show object
  91. virtual GuiObj& activate ( ); // activate object (show & set focus), this does not activate any hidden parents of the object
  92. virtual GuiObj& deactivate ( ); // deactivate object ( remove focus)
  93. GuiObj& visible (Bool on) {if(on ) show(); else hide(); return T;} // set visibility
  94. GuiObj& visibleActivate (Bool on) {if(on ){if(hidden())activate();}else hide(); return T;} // set visibility and activate if was hidden
  95. GuiObj& visibleToggle ( ) {if(hidden()) show(); else hide(); return T;} // toggle visibility
  96. GuiObj& visibleToggleActivate( ) {if(hidden()) activate(); else hide(); return T;} // toggle visibility and activate if was hidden
  97. virtual GuiObj& setText ( ); // set text, this is called automatically when application language changes due to 'App.lang', you can override this method and set new text for this object (or its children) based on new 'App.lang', don't forget to call 'super.setText'
  98. // set / get
  99. Bool is ()C {return _type!=GO_NONE;} // get if created
  100. GUI_OBJ_TYPE type ()C {return _type ;} // get object type
  101. CChar * typeName ()C; // get object type name
  102. GuiObj* parent ()C {return _parent ;} // get object parent
  103. Bool visibleFull()C; // get if visible and all parents are also visible
  104. Bool visible ()C {return _visible ;} // get if visible
  105. GuiObj& hidden ( Bool hidden ); Bool hidden ()C {return !_visible ;} // set/get if hidden
  106. virtual GuiObj& enabled( Bool enabled); Bool enabled ()C {return !_disabled ;} // set/get if enabled
  107. virtual GuiObj& disabled( Bool disabled); Bool disabled ()C {return _disabled ;} // set/get if disabled
  108. virtual GuiObj& desc (C Str &desc ); C Str& desc ()C {return _desc ;} // set/get description
  109. virtual GuiObj& rect (C Rect &rect ); C Rect& rect ()C {return _rect ;} // set/get rectangle
  110. virtual GuiObj& size (C Vec2 &size ); Vec2 size ()C {return _rect.size ();} // set/get size
  111. virtual GuiObj& pos (C Vec2 &pos ); Vec2 pos ()C {return _rect.lu ();} // set/get top left position
  112. virtual GuiObj& posRU (C Vec2 &pos ); Vec2 posRU ()C {return _rect.ru ();} // set/get top right position
  113. virtual GuiObj& posLD (C Vec2 &pos ); Vec2 posLD ()C {return _rect.ld ();} // set/get bottom left position
  114. virtual GuiObj& posL (C Vec2 &pos ); Vec2 posL ()C {return _rect.left ();} // set/get left position
  115. virtual GuiObj& posR (C Vec2 &pos ); Vec2 posR ()C {return _rect.right ();} // set/get right position
  116. virtual GuiObj& posD (C Vec2 &pos ); Vec2 posD ()C {return _rect.down ();} // set/get bottom position
  117. virtual GuiObj& posU (C Vec2 &pos ); Vec2 posU ()C {return _rect.up ();} // set/get top position
  118. virtual GuiObj& posC (C Vec2 &pos ); Vec2 posC ()C {return _rect.center();} // set/get center position
  119. Vec2 screenPos ()C; // get absolute position on the screen
  120. Rect screenRect ()C; // get absolute rectangle on the screen
  121. virtual GuiObj& move (C Vec2 &delta ); // move by delta
  122. GuiObj& resize (C Vec2 &delta ); // resize by delta
  123. GuiObj& baseLevel( Int level ); Int baseLevel ()C {return _base_level ;} // set/get base level
  124. // helpers
  125. Bool contains (C GuiObj *child)C; // if object contains 'child'
  126. Int compareLevel(C GuiObj &obj )C; // compare level between this object and 'obj' in parent's children hierarchy, <0 value is returned if this object is below 'obj', 0 value is returned if they share the same level or have different parents, >0 value is returned if this object is above 'obj'
  127. Bool above (C GuiObj &obj )C {return compareLevel(obj)>0;} // if this object is above 'obj'
  128. Bool below (C GuiObj &obj )C {return compareLevel(obj)<0;} // if this object is below 'obj'
  129. // convert
  130. Button & asButton () {return ( Button &)T;} C Button & asButton ()C {return ( Button &)T;} // return as Button (you may use this only if type()==GO_BUTTON )
  131. CheckBox & asCheckBox() {return ( CheckBox &)T;} C CheckBox & asCheckBox()C {return ( CheckBox &)T;} // return as CheckBox (you may use this only if type()==GO_CHECKBOX)
  132. ComboBox & asComboBox() {return ( ComboBox &)T;} C ComboBox & asComboBox()C {return ( ComboBox &)T;} // return as ComboBox (you may use this only if type()==GO_COMBOBOX)
  133. GuiCustom& asCustom () {return ( GuiCustom&)T;} C GuiCustom& asCustom ()C {return ( GuiCustom&)T;} // return as Custom (you may use this only if type()==GO_CUSTOM )
  134. Desktop & asDesktop () {return ( Desktop &)T;} C Desktop & asDesktop ()C {return ( Desktop &)T;} // return as Desktop (you may use this only if type()==GO_DESKTOP )
  135. GuiImage & asImage () {return ( GuiImage &)T;} C GuiImage & asImage ()C {return ( GuiImage &)T;} // return as Image (you may use this only if type()==GO_IMAGE )
  136. _List & asList () {return (_List &)T;} C _List & asList ()C {return (_List &)T;} // return as List (you may use this only if type()==GO_LIST )
  137. Menu & asMenu () {return ( Menu &)T;} C Menu & asMenu ()C {return ( Menu &)T;} // return as Menu (you may use this only if type()==GO_MENU )
  138. MenuBar & asMenuBar () {return ( MenuBar &)T;} C MenuBar & asMenuBar ()C {return ( MenuBar &)T;} // return as MenuBar (you may use this only if type()==GO_MENU_BAR)
  139. Progress & asProgress() {return ( Progress &)T;} C Progress & asProgress()C {return ( Progress &)T;} // return as Progress (you may use this only if type()==GO_PROGRESS)
  140. Region & asRegion () {return ( Region &)T;} C Region & asRegion ()C {return ( Region &)T;} // return as Region (you may use this only if type()==GO_REGION )
  141. SlideBar & asSlideBar() {return ( SlideBar &)T;} C SlideBar & asSlideBar()C {return ( SlideBar &)T;} // return as SlideBar (you may use this only if type()==GO_SLIDEBAR)
  142. Slider & asSlider () {return ( Slider &)T;} C Slider & asSlider ()C {return ( Slider &)T;} // return as Slider (you may use this only if type()==GO_SLIDER )
  143. Tab & asTab () {return ( Tab &)T;} C Tab & asTab ()C {return ( Tab &)T;} // return as Tab (you may use this only if type()==GO_TAB )
  144. Tabs & asTabs () {return ( Tabs &)T;} C Tabs & asTabs ()C {return ( Tabs &)T;} // return as Tabs (you may use this only if type()==GO_TABS )
  145. Text & asText () {return ( Text &)T;} C Text & asText ()C {return ( Text &)T;} // return as Text (you may use this only if type()==GO_TEXT )
  146. TextBox & asTextBox () {return ( TextBox &)T;} C TextBox & asTextBox ()C {return ( TextBox &)T;} // return as TextBox (you may use this only if type()==GO_TEXTBOX )
  147. TextLine & asTextLine() {return ( TextLine &)T;} C TextLine & asTextLine()C {return ( TextLine &)T;} // return as TextLine (you may use this only if type()==GO_TEXTLINE)
  148. Viewport & asViewport() {return ( Viewport &)T;} C Viewport & asViewport()C {return ( Viewport &)T;} // return as Viewport (you may use this only if type()==GO_VIEWPORT)
  149. Window & asWindow () {return ( Window &)T;} C Window & asWindow ()C {return ( Window &)T;} // return as Window (you may use this only if type()==GO_WINDOW )
  150. // main
  151. virtual GuiObj* test (C GuiPC &gpc, C Vec2 &pos, GuiObj* &mouse_wheel); // test if 'pos' screen position intersects with the object, by returning pointer to object or its children upon intersection and null in case no intersection, 'mouse_wheel' may be modified upon intersection either to the object or its children or null
  152. virtual void update(C GuiPC &gpc) {} // update object
  153. virtual void draw (C GuiPC &gpc) {} // draw object
  154. // IO
  155. virtual Bool save(File &f, CChar *path=null)C; // save to file, 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  156. virtual Bool load(File &f, CChar *path=null) ; // load from file, 'path'=path at which resource is located (this is needed so that the sub-resources can be accessed with relative path), false on fail
  157. #if EE_PRIVATE
  158. void zero();
  159. void copyParams(C GuiObj &src); // copy common parameters
  160. Bool is (GUI_OBJ_TYPE type)C; // if object is of 'type' type, this method is "null safe"
  161. GuiObj* last (GUI_OBJ_TYPE type) ; // get last parent of 'type'
  162. GuiObj* first (GUI_OBJ_TYPE type) ; // get first parent of 'type'
  163. GuiObj* firstNon (GUI_OBJ_TYPE type) ; // get first parent of non 'type'
  164. GuiObj* firstContainer ( ) ; // get first parent which is a container
  165. GuiObj* firstKbParent ( ) ; // get first parent which is a kb storage
  166. Region* firstScrollableRegion( ) ; // get first parent which is a scrollable region
  167. Int parents ( )C; // get how many parents this object belongs to
  168. Vec2 clientOffset()C; // get client offset (from position to client position)
  169. Vec2 clientSize ()C; // get client size
  170. Rect localClientRect ()C; // get client rectangle in local space - this is with having Vec2(0, 0) as top left corner, except Desktop which has Vec2(-D.w(), D.h())
  171. Vec2 screenClientPos ()C; // get client position in screen space
  172. Rect screenClientRect ()C; // get client rectangle in screen space
  173. Bool kbCatch()C; // if object can catch keyboard focus
  174. GuiObjChildren* children(); // get children container (null if none)
  175. void notifyChildrenOfClientRectChange(C Rect *old_client, C Rect *new_client);
  176. void notifyParentOfRectChange (C Rect &old_rect , Bool old_visible);
  177. #endif
  178. virtual ~GuiObj() {del();} // set virtual destructor so 'Delete' can be used together with extended classes
  179. GuiObj();
  180. #if !EE_PRIVATE
  181. private:
  182. #endif
  183. Bool _visible, _disabled, _updated;
  184. SByte _base_level;
  185. GUI_OBJ_TYPE _type;
  186. GuiObj *_parent;
  187. Rect _rect;
  188. Str _desc;
  189. virtual void parentClientRectChanged(C Rect *old_client, C Rect *new_client ) {} // called when client rectangle of the parent has changed, 'old_client'=old client rectangle (can be null if old parent is null), 'new_client'=new client rectangle (can be null if new parent is null)
  190. virtual void childRectChanged(C Rect *old_rect , C Rect *new_rect , GuiObj &child) {} // called when rectangle of a child has changed
  191. virtual GuiObj* owner()C {return _parent;}
  192. NO_COPY_CONSTRUCTOR(GuiObj);
  193. };
  194. /******************************************************************************/
  195. struct GuiObjChildren
  196. {
  197. struct Child
  198. {
  199. GuiObj *go;
  200. };
  201. Bool changed;
  202. GuiObj *kb;
  203. Memc<Child> children;
  204. void del();
  205. #if EE_PRIVATE
  206. GuiObj* operator[](Int i) {return children[i].go;}
  207. C GuiObj* operator[](Int i)C {return children[i].go;}
  208. Bool find (C GuiObj &child, Int &index)C;
  209. Bool find (C GuiObj &child_a, C GuiObj &child_b, Int &index_a, Int &index_b)C;
  210. Bool remove ( GuiObj &child);
  211. Child* add ( GuiObj &child, GuiObj &parent);
  212. void validateLevel(C GuiObj &child);
  213. Int compareLevel(C GuiObj &child_a, C GuiObj &child_b)C;
  214. Bool partiallyOccludedInSameLevel(C GuiObj &child);
  215. Bool moveUp (C GuiObj &child);
  216. Bool moveDown (C GuiObj &child);
  217. void moveToTop (C GuiObj &child);
  218. void moveToBottom(C GuiObj &child);
  219. void moveAbove (C GuiObj &child_a, C GuiObj &child_b);
  220. void moveBelow (C GuiObj &child_a, C GuiObj &child_b);
  221. Bool Switch (C GuiObj &go , Bool next=true);
  222. GuiObj* test (C GuiPC &gpc, C Vec2 &pos, GuiObj* &mouse_wheel);
  223. void update(C GuiPC &gpc);
  224. void draw (C GuiPC &gpc);
  225. #endif
  226. ~GuiObjChildren() {del();}
  227. GuiObjChildren() {changed=false; kb=null;}
  228. NO_COPY_CONSTRUCTOR(GuiObjChildren);
  229. };
  230. inline Int Elms(C GuiObjChildren &children) {return children.children.elms();}
  231. /******************************************************************************/
  232. CChar* GuiObjTypeName(GUI_OBJ_TYPE type); // get Gui Object Type Name from 'type' GUI_OBJ_TYPE
  233. #if EE_PRIVATE
  234. inline Flt GuiMaxX(C Rect &rect) {return rect.max.x;} // max horizontal extents
  235. inline Flt GuiMaxY(C Rect &rect) {return -rect.min.y;} // max vertical extents
  236. #endif
  237. /******************************************************************************/