You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1535 lines
46KB

  1. /*
  2. OUI - A minimal semi-immediate GUI handling & layouting library
  3. Copyright (c) 2014 Leonard Ritter <leonard.ritter@duangle.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #ifndef _OUI_H_
  21. #define _OUI_H_
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /*
  26. Revision 2 (2014-07-13)
  27. OUI (short for "Open UI", spoken like the french "oui" for "yes") is a
  28. platform agnostic single-header C library for layouting GUI elements and
  29. handling related user input. Together with a set of widget drawing and logic
  30. routines it can be used to build complex user interfaces.
  31. OUI is a semi-immediate GUI. Widget declarations are persistent for the duration
  32. of the setup and evaluation, but do not need to be kept around longer than one
  33. frame.
  34. OUI has no widget types; instead, it provides only one kind of element, "Items",
  35. which can be taylored to the application by the user and expanded with custom
  36. buffers and event handlers to behave as containers, buttons, sliders, radio
  37. buttons, and so on.
  38. OUI also does not draw anything; Instead it provides a set of functions to
  39. iterate and query the layouted items in order to allow client code to render
  40. each widget with its current state using a preferred graphics library.
  41. A basic setup for OUI usage looks like this:
  42. void app_main(...) {
  43. UIcontext *context = uiCreateContext();
  44. uiMakeCurrent(context);
  45. while (app_running()) {
  46. // update position of mouse cursor; the ui can also be updated
  47. // from received events.
  48. uiSetCursor(app_get_mouse_x(), app_get_mouse_y());
  49. // update button state
  50. for (int i = 0; i < 3; ++i)
  51. uiSetButton(i, app_get_button_state(i));
  52. // begin new UI declarations
  53. uiClear();
  54. // - UI setup code goes here -
  55. app_setup_ui();
  56. // layout UI
  57. uiLayout();
  58. // draw UI
  59. app_draw_ui(render_context,0,0,0);
  60. // update states and fire handlers
  61. uiProcess();
  62. }
  63. uiDestroyContext(context);
  64. }
  65. Here's an example setup for a checkbox control:
  66. typedef struct CheckBoxData {
  67. int type;
  68. const char *label;
  69. bool *checked;
  70. } CheckBoxData;
  71. // called when the item is clicked (see checkbox())
  72. void app_checkbox_handler(int item, UIevent event) {
  73. // retrieve custom data (see checkbox())
  74. const CheckBoxData *data = (const CheckBoxData *)uiGetData(item);
  75. // toggle value
  76. *data->checked = !(*data->checked);
  77. }
  78. // creates a checkbox control for a pointer to a boolean and attaches it to
  79. // a parent item.
  80. int checkbox(int parent, UIhandle handle, const char *label, bool *checked) {
  81. // create new ui item
  82. int item = uiItem();
  83. // set persistent handle for item that is used
  84. // to track activity over time
  85. uiSetHandle(item, handle);
  86. // set size of wiget; horizontal size is dynamic, vertical is fixed
  87. uiSetSize(item, 0, APP_WIDGET_HEIGHT);
  88. // attach checkbox handler, set to fire as soon as the left button is
  89. // pressed; UI_BUTTON0_HOT_UP is also a popular alternative.
  90. uiSetHandler(item, app_checkbox_handler, UI_BUTTON0_DOWN);
  91. // store some custom data with the checkbox that we use for rendering
  92. // and value changes.
  93. CheckBoxData *data = (CheckBoxData *)uiAllocData(item, sizeof(CheckBoxData));
  94. // assign a custom typeid to the data so the renderer knows how to
  95. // render this control.
  96. data->type = APP_WIDGET_CHECKBOX;
  97. data->label = label;
  98. data->checked = checked;
  99. // append to parent
  100. uiAppend(parent, item);
  101. return item;
  102. }
  103. A simple recursive drawing routine can look like this:
  104. void app_draw_ui(AppRenderContext *ctx, int item, int x, int y) {
  105. // retrieve custom data and cast it to an int; we assume the first member
  106. // of every widget data item to be an "int type" field.
  107. const int *type = (const int *)uiGetData(item);
  108. // get the widgets relative rectangle and offset by the parents
  109. // absolute position.
  110. UIrect rect = uiGetRect(item);
  111. rect.x += x;
  112. rect.y += y;
  113. // if a type is set, this is a specialized widget
  114. if (type) {
  115. switch(*type) {
  116. default: break;
  117. case APP_WIDGET_LABEL: {
  118. // ...
  119. } break;
  120. case APP_WIDGET_BUTTON: {
  121. // ...
  122. } break;
  123. case APP_WIDGET_CHECKBOX: {
  124. // cast to the full data type
  125. const CheckBoxData *data = (CheckBoxData*)type;
  126. // get the widgets current state
  127. int state = uiGetState(item);
  128. // if the value is set, the state is always active
  129. if (*data->checked)
  130. state = UI_ACTIVE;
  131. // draw the checkbox
  132. app_draw_checkbox(ctx, rect, state, data->label);
  133. } break;
  134. }
  135. }
  136. // iterate through all children and draw
  137. int kid = uiFirstChild(item);
  138. while (kid >= 0) {
  139. app_draw_ui(ctx, kid, rect.x, rect.y);
  140. kid = uiNextSibling(kid);
  141. }
  142. }
  143. See example.cpp in the repository for a full usage example.
  144. */
  145. // you can override this from the outside to pick
  146. // the export level you need
  147. #ifndef OUI_EXPORT
  148. #define OUI_EXPORT
  149. #endif
  150. // limits
  151. // maximum number of items that may be added (must be power of 2)
  152. #define UI_MAX_ITEMS 4096
  153. // maximum size in bytes reserved for storage of application dependent data
  154. // as passed to uiAllocData().
  155. #define UI_MAX_BUFFERSIZE 1048576
  156. // maximum size in bytes of a single data buffer passed to uiAllocData().
  157. #define UI_MAX_DATASIZE 4096
  158. // maximum depth of nested containers
  159. #define UI_MAX_DEPTH 64
  160. // maximum number of buffered input events
  161. #define UI_MAX_INPUT_EVENTS 64
  162. typedef unsigned int UIuint;
  163. // opaque UI context
  164. typedef struct UIcontext UIcontext;
  165. // application defined context handle
  166. typedef unsigned long long UIhandle;
  167. // item states as returned by uiGetState()
  168. typedef enum UIitemState {
  169. // the item is inactive
  170. UI_COLD = 0,
  171. // the item is inactive, but the cursor is hovering over this item
  172. UI_HOT = 1,
  173. // the item is toggled, activated, focused (depends on item kind)
  174. UI_ACTIVE = 2,
  175. // the item is unresponsive
  176. UI_FROZEN = 3,
  177. } UIitemState;
  178. // layout flags
  179. typedef enum UIlayoutFlags {
  180. // anchor to left item or left side of parent
  181. UI_LEFT = 1,
  182. // anchor to top item or top side of parent
  183. UI_TOP = 2,
  184. // anchor to right item or right side of parent
  185. UI_RIGHT = 4,
  186. // anchor to bottom item or bottom side of parent
  187. UI_DOWN = 8,
  188. // anchor to both left and right item or parent borders
  189. UI_HFILL = 5,
  190. // anchor to both top and bottom item or parent borders
  191. UI_VFILL = 10,
  192. // center horizontally, with left margin as offset
  193. UI_HCENTER = 0,
  194. // center vertically, with top margin as offset
  195. UI_VCENTER = 0,
  196. // center in both directions, with left/top margin as offset
  197. UI_CENTER = 0,
  198. // anchor to all four directions
  199. UI_FILL = 15,
  200. } UIlayoutFlags;
  201. // event flags
  202. typedef enum UIevent {
  203. // on button 0 down
  204. UI_BUTTON0_DOWN = 0x0001,
  205. // on button 0 up
  206. // when this event has a handler, uiGetState() will return UI_ACTIVE as
  207. // long as button 0 is down.
  208. UI_BUTTON0_UP = 0x0002,
  209. // on button 0 up while item is hovered
  210. // when this event has a handler, uiGetState() will return UI_ACTIVE
  211. // when the cursor is hovering the items rectangle; this is the
  212. // behavior expected for buttons.
  213. UI_BUTTON0_HOT_UP = 0x0004,
  214. // item is being captured (button 0 constantly pressed);
  215. // when this event has a handler, uiGetState() will return UI_ACTIVE as
  216. // long as button 0 is down.
  217. UI_BUTTON0_CAPTURE = 0x0008,
  218. // on button 2 down (right mouse button, usually triggers context menu)
  219. UI_BUTTON2_DOWN = 0x0010,
  220. // item has received a new child
  221. // this can be used to allow container items to configure child items
  222. // as they appear.
  223. UI_APPEND = 0x0100,
  224. // item is focused and has received a key-down event
  225. // the respective key can be queried using uiGetKey() and uiGetModifier()
  226. UI_KEY_DOWN = 0x0200,
  227. // item is focused and has received a key-up event
  228. // the respective key can be queried using uiGetKey() and uiGetModifier()
  229. UI_KEY_UP = 0x0400,
  230. // item is focused and has received a character event
  231. // the respective character can be queried using uiGetKey()
  232. UI_CHAR = 0x0800,
  233. // item is requested to fill a container with additional items,
  234. // usually menuitems for a context menu.
  235. // the respective container can be queried using uiGetExtendItem()
  236. UI_EXTEND = 0x1000,
  237. } UIevent;
  238. // handler callback; event is one of UI_EVENT_*
  239. typedef void (*UIhandler)(int item, UIevent event);
  240. // for cursor positions, mainly
  241. typedef struct UIvec2 {
  242. union {
  243. int v[2];
  244. struct { int x, y; };
  245. };
  246. } UIvec2;
  247. // layout rectangle
  248. typedef struct UIrect {
  249. union {
  250. int v[4];
  251. struct { int x, y, w, h; };
  252. };
  253. } UIrect;
  254. // unless declared otherwise, all operations have the complexity O(1).
  255. // Context Management
  256. // ------------------
  257. // create a new UI context; call uiMakeCurrent() to make this context the
  258. // current context. The context is managed by the client and must be released
  259. // using uiDestroyContext()
  260. OUI_EXPORT UIcontext *uiCreateContext();
  261. // select an UI context as the current context; a context must always be
  262. // selected before using any of the other UI functions
  263. OUI_EXPORT void uiMakeCurrent(UIcontext *ctx);
  264. // release the memory of an UI context created with uiCreateContext(); if the
  265. // context is the current context, the current context will be set to NULL
  266. OUI_EXPORT void uiDestroyContext(UIcontext *ctx);
  267. // Input Control
  268. // -------------
  269. // sets the current cursor position (usually belonging to a mouse) to the
  270. // screen coordinates at (x,y)
  271. OUI_EXPORT void uiSetCursor(int x, int y);
  272. // returns the current cursor position in screen coordinates as set by
  273. // uiSetCursor()
  274. OUI_EXPORT UIvec2 uiGetCursor();
  275. // returns the offset of the cursor relative to the last call to uiProcess()
  276. OUI_EXPORT UIvec2 uiGetCursorDelta();
  277. // returns the beginning point of a drag operation.
  278. OUI_EXPORT UIvec2 uiGetCursorStart();
  279. // returns the offset of the cursor relative to the beginning point of a drag
  280. // operation.
  281. OUI_EXPORT UIvec2 uiGetCursorStartDelta();
  282. // sets a mouse or gamepad button as pressed/released
  283. // button is in the range 0..63 and maps to an application defined input
  284. // source.
  285. // enabled is 1 for pressed, 0 for released
  286. OUI_EXPORT void uiSetButton(int button, int enabled);
  287. // returns the current state of an application dependent input button
  288. // as set by uiSetButton().
  289. // the function returns 1 if the button has been set to pressed, 0 for released.
  290. OUI_EXPORT int uiGetButton(int button);
  291. // sets a key as down/up; the key can be any application defined keycode
  292. // mod is an application defined set of flags for modifier keys
  293. // enabled is 1 for key down, 0 for key up
  294. // all key events are being buffered until the next call to uiProcess()
  295. OUI_EXPORT void uiSetKey(unsigned int key, unsigned int mod, int enabled);
  296. // sends a single character for text input; the character is usually in the
  297. // unicode range, but can be application defined.
  298. // all char events are being buffered until the next call to uiProcess()
  299. OUI_EXPORT void uiSetChar(unsigned int value);
  300. // Stages
  301. // ------
  302. // clear the item buffer; uiClear() should be called before the first
  303. // UI declaration for this frame to avoid concatenation of the same UI multiple
  304. // times.
  305. // After the call, all previously declared item IDs are invalid, and all
  306. // application dependent context data has been freed.
  307. OUI_EXPORT void uiClear();
  308. // layout all added items starting from the root item 0.
  309. // after calling uiLayout(), no further modifications to the item tree should
  310. // be done until the next call to uiClear().
  311. // It is safe to immediately draw the items after a call to uiLayout().
  312. // this is an O(N) operation for N = number of declared items.
  313. OUI_EXPORT void uiLayout();
  314. // update the internal state according to the current cursor position and
  315. // button states, and call all registered handlers.
  316. // after calling uiProcess(), no further modifications to the item tree should
  317. // be done until the next call to uiClear().
  318. // Items should be drawn before a call to uiProcess()
  319. // this is an O(N) operation for N = number of declared items.
  320. OUI_EXPORT void uiProcess();
  321. // UI Declaration
  322. // --------------
  323. // create a new UI item and return the new items ID.
  324. OUI_EXPORT int uiItem();
  325. // set an items state to frozen; the UI will not recurse into frozen items
  326. // when searching for hot or active items; subsequently, frozen items and
  327. // their child items will not cause mouse event notifications.
  328. // The frozen state is not applied recursively; uiGetState() will report
  329. // UI_COLD for child items. Upon encountering a frozen item, the drawing
  330. // routine needs to handle rendering of child items appropriately.
  331. // see example.cpp for a demonstration.
  332. OUI_EXPORT void uiSetFrozen(int item, int enable);
  333. // set the application-dependent handle of an item.
  334. // handle is an application defined 64-bit handle. If handle is 0, the item
  335. // will not be interactive.
  336. OUI_EXPORT void uiSetHandle(int item, UIhandle handle);
  337. // assigns the items own address as handle; this may cause glitches
  338. // when the order of items changes while theitem is captured
  339. OUI_EXPORT void uiSetSelfHandle(int item);
  340. // allocate space for application-dependent context data and return the pointer
  341. // if successful. If no data has been allocated, a new pointer is returned.
  342. // Otherwise, an assertion is thrown.
  343. // The memory of the pointer is managed by the UI context.
  344. OUI_EXPORT void *uiAllocData(int item, int size);
  345. // set the handler callback for an interactive item.
  346. // flags is a combination of UI_EVENT_* and designates for which events the
  347. // handler should be called.
  348. OUI_EXPORT void uiSetHandler(int item, UIhandler handler, int flags);
  349. // assign an item to a container.
  350. // an item ID of 0 refers to the root item.
  351. // if child is already assigned to a parent, an assertion will be thrown.
  352. // the function returns the child item ID
  353. OUI_EXPORT int uiAppend(int item, int child);
  354. // set the size of the item; a size of 0 indicates the dimension to be
  355. // dynamic; if the size is set, the item can not expand beyond that size.
  356. OUI_EXPORT void uiSetSize(int item, int w, int h);
  357. // set the anchoring behavior of the item to one or multiple UIlayoutFlags
  358. OUI_EXPORT void uiSetLayout(int item, int flags);
  359. // set the left, top, right and bottom margins of an item; when the item is
  360. // anchored to the parent or another item, the margin controls the distance
  361. // from the neighboring element.
  362. OUI_EXPORT void uiSetMargins(int item, int l, int t, int r, int b);
  363. // anchor the item to another sibling within the same container, so that the
  364. // sibling is left to this item.
  365. OUI_EXPORT void uiSetRightTo(int item, int other);
  366. // anchor the item to another sibling within the same container, so that the
  367. // sibling is above this item.
  368. OUI_EXPORT void uiSetBelow(int item, int other);
  369. // anchor the item to another sibling within the same container, so that the
  370. // sibling is right to this item.
  371. OUI_EXPORT void uiSetLeftTo(int item, int other);
  372. // anchor the item to another sibling within the same container, so that the
  373. // sibling is below this item.
  374. OUI_EXPORT void uiSetAbove(int item, int other);
  375. // set item as recipient of all keyboard events; the item must have a handle
  376. // assigned; if item is -1, no item will be focused.
  377. OUI_EXPORT void uiFocus(int item);
  378. // Iteration
  379. // ---------
  380. // returns the first child item of a container item. If the item is not
  381. // a container or does not contain any items, -1 is returned.
  382. // if item is 0, the first child item of the root item will be returned.
  383. OUI_EXPORT int uiFirstChild(int item);
  384. // returns the last child item of a container item. If the item is not
  385. // a container or does not contain any items, -1 is returned.
  386. // if item is 0, the last child item of the root item will be returned.
  387. OUI_EXPORT int uiLastChild(int item);
  388. // returns an items parent container item.
  389. // if item is 0, -1 will be returned.
  390. OUI_EXPORT int uiParent(int item);
  391. // returns an items next sibling in the list of the parent containers children.
  392. // if item is 0 or the item is the last child item, -1 will be returned.
  393. OUI_EXPORT int uiNextSibling(int item);
  394. // returns an items previous sibling in the list of the parent containers
  395. // children.
  396. // if item is 0 or the item is the first child item, -1 will be returned.
  397. OUI_EXPORT int uiPrevSibling(int item);
  398. // Querying
  399. // --------
  400. // return the current state of the item. This state is only valid after
  401. // a call to uiProcess().
  402. // The returned value is one of UI_COLD, UI_HOT, UI_ACTIVE, UI_FROZEN.
  403. OUI_EXPORT UIitemState uiGetState(int item);
  404. // return the application-dependent handle of the item as passed to uiSetHandle().
  405. OUI_EXPORT UIhandle uiGetHandle(int item);
  406. // return the item with the given application-dependent handle as assigned by
  407. // uiSetHandle() or -1 if unsuccessful.
  408. OUI_EXPORT int uiGetItem(UIhandle handle);
  409. // return the item that is currently under the cursor or -1 for none
  410. OUI_EXPORT int uiGetHotItem();
  411. // return the item that is currently focused or -1 for none
  412. OUI_EXPORT int uiGetFocusedItem();
  413. // return the application-dependent context data for an item as passed to
  414. // uiAllocData(). The memory of the pointer is managed by the UI context
  415. // and should not be altered.
  416. OUI_EXPORT const void *uiGetData(int item);
  417. // return the handler callback for an item as passed to uiSetHandler()
  418. OUI_EXPORT UIhandler uiGetHandler(int item);
  419. // return the handler flags for an item as passed to uiSetHandler()
  420. OUI_EXPORT int uiGetHandlerFlags(int item);
  421. // when handling a KEY_DOWN/KEY_UP event: the key that triggered this event
  422. OUI_EXPORT unsigned int uiGetKey();
  423. // when handling a KEY_DOWN/KEY_UP event: the key that triggered this event
  424. OUI_EXPORT unsigned int uiGetModifier();
  425. // when handling an EXTEND event; the container which to add items to
  426. OUI_EXPORT int uiGetExtendItem();
  427. // returns the number of child items a container item contains. If the item
  428. // is not a container or does not contain any items, 0 is returned.
  429. // if item is 0, the child item count of the root item will be returned.
  430. OUI_EXPORT int uiGetChildCount(int item);
  431. // returns an items child index relative to its parent. If the item is the
  432. // first item, the return value is 0; If the item is the last item, the return
  433. // value is equivalent to uiGetChildCount(uiParent(item))-1.
  434. // if item is 0, 0 will be returned.
  435. OUI_EXPORT int uiGetChildId(int item);
  436. // returns the items layout rectangle relative to the parent. If uiGetRect()
  437. // is called before uiLayout(), the values of the returned rectangle are
  438. // undefined.
  439. OUI_EXPORT UIrect uiGetRect(int item);
  440. // returns the items layout rectangle in absolute coordinates. If
  441. // uiGetAbsoluteRect() is called before uiLayout(), the values of the returned
  442. // rectangle are undefined.
  443. // This function has complexity O(N) for N parents
  444. OUI_EXPORT UIrect uiGetAbsoluteRect(int item);
  445. // returns 1 if an items absolute rectangle contains a given coordinate
  446. // otherwise 0
  447. OUI_EXPORT int uiContains(int item, int x, int y);
  448. // when called from an input event handler, returns the active items absolute
  449. // layout rectangle. If uiGetActiveRect() is called outside of a handler,
  450. // the values of the returned rectangle are undefined.
  451. OUI_EXPORT UIrect uiGetActiveRect();
  452. // return the width of the item as set by uiSetSize()
  453. OUI_EXPORT int uiGetWidth(int item);
  454. // return the height of the item as set by uiSetSize()
  455. OUI_EXPORT int uiGetHeight(int item);
  456. // return the anchoring behavior as set by uiSetLayout()
  457. OUI_EXPORT int uiGetLayout(int item);
  458. // return the left margin of the item as set with uiSetMargins()
  459. OUI_EXPORT int uiGetMarginLeft(int item);
  460. // return the top margin of the item as set with uiSetMargins()
  461. OUI_EXPORT int uiGetMarginTop(int item);
  462. // return the right margin of the item as set with uiSetMargins()
  463. OUI_EXPORT int uiGetMarginRight(int item);
  464. // return the bottom margin of the item as set with uiSetMargins()
  465. OUI_EXPORT int uiGetMarginDown(int item);
  466. // return the items anchored sibling as assigned with uiSetRightTo()
  467. // or -1 if not set.
  468. OUI_EXPORT int uiGetRightTo(int item);
  469. // return the items anchored sibling as assigned with uiSetBelow()
  470. // or -1 if not set.
  471. OUI_EXPORT int uiGetBelow(int item);
  472. // return the items anchored sibling as assigned with uiSetLeftTo()
  473. // or -1 if not set.
  474. OUI_EXPORT int uiGetLeftTo(int item);
  475. // return the items anchored sibling as assigned with uiSetAbove()
  476. // or -1 if not set.
  477. OUI_EXPORT int uiGetAbove(int item);
  478. // request from_item to expand its content into parent
  479. OUI_EXPORT void uiExtend(int parent, int from_item);
  480. #ifdef __cplusplus
  481. };
  482. #endif
  483. #endif // _OUI_H_
  484. #ifdef OUI_IMPLEMENTATION
  485. #include <assert.h>
  486. #ifdef _MSC_VER
  487. #pragma warning (disable: 4996) // Switch off security warnings
  488. #pragma warning (disable: 4100) // Switch off unreferenced formal parameter warnings
  489. #ifdef __cplusplus
  490. #define UI_INLINE inline
  491. #else
  492. #define UI_INLINE
  493. #endif
  494. #else
  495. #define UI_INLINE inline
  496. #endif
  497. #define UI_MAX_KIND 16
  498. #define UI_ANY_INPUT (UI_BUTTON0_DOWN \
  499. |UI_BUTTON0_UP \
  500. |UI_BUTTON0_HOT_UP \
  501. |UI_BUTTON0_CAPTURE \
  502. |UI_BUTTON2_DOWN \
  503. |UI_KEY_DOWN \
  504. |UI_KEY_UP \
  505. |UI_CHAR)
  506. typedef struct UIitem {
  507. // declaration independent unique handle (for persistence)
  508. UIhandle handle;
  509. // handler
  510. UIhandler handler;
  511. // container structure
  512. // number of kids
  513. int numkids;
  514. // index of first kid
  515. int firstkid;
  516. // index of last kid
  517. int lastkid;
  518. // child structure
  519. // parent item
  520. int parent;
  521. // index of kid relative to parent
  522. int kidid;
  523. // index of next sibling with same parent
  524. int nextitem;
  525. // index of previous sibling with same parent
  526. int previtem;
  527. // one or multiple of UIlayoutFlags
  528. int layout_flags;
  529. // size
  530. UIvec2 size;
  531. // visited flags for layouting
  532. int visited;
  533. // margin offsets, interpretation depends on flags
  534. int margins[4];
  535. // neighbors to position borders to
  536. int relto[4];
  537. // computed size
  538. UIvec2 computed_size;
  539. // relative rect
  540. UIrect rect;
  541. // attributes
  542. int frozen;
  543. // index of data or -1 for none
  544. int data;
  545. // size of data
  546. int datasize;
  547. // a combination of UIevents
  548. int event_flags;
  549. } UIitem;
  550. typedef enum UIstate {
  551. UI_STATE_IDLE = 0,
  552. UI_STATE_CAPTURE,
  553. } UIstate;
  554. typedef struct UIhandleEntry {
  555. unsigned int key;
  556. int item;
  557. } UIhandleEntry;
  558. typedef struct UIinputEvent {
  559. unsigned int key;
  560. unsigned int mod;
  561. UIevent event;
  562. } UIinputEvent;
  563. struct UIcontext {
  564. // button state in this frame
  565. unsigned long long buttons;
  566. // button state in the previous frame
  567. unsigned long long last_buttons;
  568. // where the cursor was at the beginning of the active state
  569. UIvec2 start_cursor;
  570. // where the cursor was last frame
  571. UIvec2 last_cursor;
  572. // where the cursor is currently
  573. UIvec2 cursor;
  574. UIhandle hot_handle;
  575. UIhandle active_handle;
  576. UIhandle focus_handle;
  577. UIrect hot_rect;
  578. UIrect active_rect;
  579. UIstate state;
  580. int hot_item;
  581. unsigned int active_key;
  582. unsigned int active_modifier;
  583. int extend_item;
  584. int count;
  585. int datasize;
  586. int eventcount;
  587. UIitem items[UI_MAX_ITEMS];
  588. unsigned char data[UI_MAX_BUFFERSIZE];
  589. UIhandleEntry handles[UI_MAX_ITEMS];
  590. UIinputEvent events[UI_MAX_INPUT_EVENTS];
  591. };
  592. UI_INLINE int ui_max(int a, int b) {
  593. return (a>b)?a:b;
  594. }
  595. UI_INLINE int ui_min(int a, int b) {
  596. return (a<b)?a:b;
  597. }
  598. static UIcontext *ui_context = NULL;
  599. UIcontext *uiCreateContext() {
  600. UIcontext *ctx = (UIcontext *)malloc(sizeof(UIcontext));
  601. memset(ctx, 0, sizeof(UIcontext));
  602. return ctx;
  603. }
  604. void uiMakeCurrent(UIcontext *ctx) {
  605. ui_context = ctx;
  606. if (ui_context)
  607. uiClear();
  608. }
  609. void uiDestroyContext(UIcontext *ctx) {
  610. if (ui_context == ctx)
  611. uiMakeCurrent(NULL);
  612. free(ctx);
  613. }
  614. UI_INLINE unsigned int uiHashHandle(UIhandle handle) {
  615. handle = (handle+(handle>>32)) & 0xffffffff;
  616. unsigned int x = (unsigned int)handle;
  617. x += (x>>6)+(x>>19);
  618. x += x<<16;
  619. x ^= x<<3;
  620. x += x>>5;
  621. x ^= x<<2;
  622. x += x>>15;
  623. x ^= x<<10;
  624. return x?x:1; // must not be zero
  625. }
  626. UI_INLINE unsigned int uiHashProbeDistance(unsigned int key, unsigned int slot_index) {
  627. unsigned int pos = key & (UI_MAX_ITEMS-1);
  628. return (slot_index + UI_MAX_ITEMS - pos) & (UI_MAX_ITEMS-1);
  629. }
  630. UI_INLINE UIhandleEntry *uiHashLookupHandle(unsigned int key) {
  631. assert(ui_context);
  632. int pos = key & (UI_MAX_ITEMS-1);
  633. unsigned int dist = 0;
  634. for (;;) {
  635. UIhandleEntry *entry = ui_context->handles + pos;
  636. unsigned int pos_key = entry->key;
  637. if (!pos_key) return NULL;
  638. else if (entry->key == key)
  639. return entry;
  640. else if (dist > uiHashProbeDistance(pos_key, pos))
  641. return NULL;
  642. pos = (pos+1) & (UI_MAX_ITEMS-1);
  643. ++dist;
  644. }
  645. }
  646. int uiGetItem(UIhandle handle) {
  647. unsigned int key = uiHashHandle(handle);
  648. UIhandleEntry *e = uiHashLookupHandle(key);
  649. return e?(e->item):-1;
  650. }
  651. static void uiHashInsertHandle(UIhandle handle, int item) {
  652. unsigned int key = uiHashHandle(handle);
  653. UIhandleEntry *e = uiHashLookupHandle(key);
  654. if (e) { // update
  655. e->item = item;
  656. return;
  657. }
  658. int pos = key & (UI_MAX_ITEMS-1);
  659. unsigned int dist = 0;
  660. for (unsigned int i = 0; i < UI_MAX_ITEMS; ++i) {
  661. int index = (pos + i) & (UI_MAX_ITEMS-1);
  662. unsigned int pos_key = ui_context->handles[index].key;
  663. if (!pos_key) {
  664. ui_context->handles[index].key = key;
  665. ui_context->handles[index].item = item;
  666. break;
  667. } else {
  668. unsigned int probe_distance = uiHashProbeDistance(pos_key, index);
  669. if (dist > probe_distance) {
  670. unsigned int oldkey = ui_context->handles[index].key;
  671. unsigned int olditem = ui_context->handles[index].item;
  672. ui_context->handles[index].key = key;
  673. ui_context->handles[index].item = item;
  674. key = oldkey;
  675. item = olditem;
  676. dist = probe_distance;
  677. }
  678. }
  679. ++dist;
  680. }
  681. }
  682. void uiSetButton(int button, int enabled) {
  683. assert(ui_context);
  684. unsigned long long mask = 1ull<<button;
  685. // set new bit
  686. ui_context->buttons = (enabled)?
  687. (ui_context->buttons | mask):
  688. (ui_context->buttons & ~mask);
  689. }
  690. static void uiAddInputEvent(UIinputEvent event) {
  691. assert(ui_context);
  692. if (ui_context->eventcount == UI_MAX_INPUT_EVENTS) return;
  693. ui_context->events[ui_context->eventcount++] = event;
  694. }
  695. static void uiClearInputEvents() {
  696. assert(ui_context);
  697. ui_context->eventcount = 0;
  698. }
  699. void uiSetKey(unsigned int key, unsigned int mod, int enabled) {
  700. assert(ui_context);
  701. UIinputEvent event = { key, mod, enabled?UI_KEY_DOWN:UI_KEY_UP };
  702. uiAddInputEvent(event);
  703. }
  704. void uiSetChar(unsigned int value) {
  705. assert(ui_context);
  706. UIinputEvent event = { value, 0, UI_CHAR };
  707. uiAddInputEvent(event);
  708. }
  709. int uiGetLastButton(int button) {
  710. assert(ui_context);
  711. return (ui_context->last_buttons & (1ull<<button))?1:0;
  712. }
  713. int uiGetButton(int button) {
  714. assert(ui_context);
  715. return (ui_context->buttons & (1ull<<button))?1:0;
  716. }
  717. int uiButtonPressed(int button) {
  718. assert(ui_context);
  719. return !uiGetLastButton(button) && uiGetButton(button);
  720. }
  721. int uiButtonReleased(int button) {
  722. assert(ui_context);
  723. return uiGetLastButton(button) && !uiGetButton(button);
  724. }
  725. void uiSetCursor(int x, int y) {
  726. assert(ui_context);
  727. ui_context->cursor.x = x;
  728. ui_context->cursor.y = y;
  729. }
  730. UIvec2 uiGetCursor() {
  731. assert(ui_context);
  732. return ui_context->cursor;
  733. }
  734. UIvec2 uiGetCursorStart() {
  735. assert(ui_context);
  736. return ui_context->start_cursor;
  737. }
  738. UIvec2 uiGetCursorDelta() {
  739. assert(ui_context);
  740. UIvec2 result = {{{
  741. ui_context->cursor.x - ui_context->last_cursor.x,
  742. ui_context->cursor.y - ui_context->last_cursor.y
  743. }}};
  744. return result;
  745. }
  746. UIvec2 uiGetCursorStartDelta() {
  747. assert(ui_context);
  748. UIvec2 result = {{{
  749. ui_context->cursor.x - ui_context->start_cursor.x,
  750. ui_context->cursor.y - ui_context->start_cursor.y
  751. }}};
  752. return result;
  753. }
  754. unsigned int uiGetKey() {
  755. assert(ui_context);
  756. return ui_context->active_key;
  757. }
  758. unsigned int uiGetModifier() {
  759. assert(ui_context);
  760. return ui_context->active_modifier;
  761. }
  762. int uiGetExtendItem() {
  763. assert(ui_context);
  764. return ui_context->extend_item;
  765. }
  766. UIitem *uiItemPtr(int item) {
  767. assert(ui_context && (item >= 0) && (item < ui_context->count));
  768. return ui_context->items + item;
  769. }
  770. int uiGetHotItem() {
  771. assert(ui_context);
  772. return ui_context->hot_item;
  773. }
  774. void uiFocus(int item) {
  775. assert(ui_context && (item >= -1) && (item < ui_context->count));
  776. ui_context->focus_handle = (item < 0)?0:uiGetHandle(item);
  777. }
  778. int uiGetFocusedItem() {
  779. assert(ui_context);
  780. return ui_context->focus_handle?uiGetItem(ui_context->focus_handle):-1;
  781. }
  782. void uiClear() {
  783. assert(ui_context);
  784. ui_context->count = 0;
  785. ui_context->datasize = 0;
  786. ui_context->hot_item = -1;
  787. memset(ui_context->handles, 0, sizeof(ui_context->handles));
  788. }
  789. int uiItem() {
  790. assert(ui_context);
  791. assert(ui_context->count < UI_MAX_ITEMS);
  792. int idx = ui_context->count++;
  793. UIitem *item = uiItemPtr(idx);
  794. memset(item, 0, sizeof(UIitem));
  795. item->parent = -1;
  796. item->firstkid = -1;
  797. item->lastkid = -1;
  798. item->nextitem = -1;
  799. item->previtem = -1;
  800. item->data = -1;
  801. for (int i = 0; i < 4; ++i)
  802. item->relto[i] = -1;
  803. return idx;
  804. }
  805. void uiNotifyItem(int item, UIevent event) {
  806. UIitem *pitem = uiItemPtr(item);
  807. if (pitem->handler && (pitem->event_flags & event)) {
  808. pitem->handler(item, event);
  809. }
  810. }
  811. void uiExtend(int parent, int from_item) {
  812. assert(ui_context);
  813. ui_context->extend_item = parent;
  814. uiNotifyItem(from_item, UI_EXTEND);
  815. }
  816. int uiAppend(int item, int child) {
  817. assert(child > 0);
  818. assert(uiParent(child) == -1);
  819. UIitem *pitem = uiItemPtr(child);
  820. UIitem *pparent = uiItemPtr(item);
  821. pitem->parent = item;
  822. pitem->kidid = pparent->numkids++;
  823. if (pparent->lastkid < 0) {
  824. pparent->firstkid = child;
  825. pparent->lastkid = child;
  826. } else {
  827. pitem->previtem = pparent->lastkid;
  828. uiItemPtr(pparent->lastkid)->nextitem = child;
  829. pparent->lastkid = child;
  830. }
  831. uiNotifyItem(item, UI_APPEND);
  832. return child;
  833. }
  834. void uiSetFrozen(int item, int enable) {
  835. UIitem *pitem = uiItemPtr(item);
  836. pitem->frozen = enable;
  837. }
  838. void uiSetSize(int item, int w, int h) {
  839. UIitem *pitem = uiItemPtr(item);
  840. pitem->size.x = w;
  841. pitem->size.y = h;
  842. }
  843. int uiGetWidth(int item) {
  844. return uiItemPtr(item)->size.x;
  845. }
  846. int uiGetHeight(int item) {
  847. return uiItemPtr(item)->size.y;
  848. }
  849. void uiSetLayout(int item, int flags) {
  850. uiItemPtr(item)->layout_flags = flags;
  851. }
  852. int uiGetLayout(int item) {
  853. return uiItemPtr(item)->layout_flags;
  854. }
  855. void uiSetMargins(int item, int l, int t, int r, int b) {
  856. UIitem *pitem = uiItemPtr(item);
  857. pitem->margins[0] = l;
  858. pitem->margins[1] = t;
  859. pitem->margins[2] = r;
  860. pitem->margins[3] = b;
  861. }
  862. int uiGetMarginLeft(int item) {
  863. return uiItemPtr(item)->margins[0];
  864. }
  865. int uiGetMarginTop(int item) {
  866. return uiItemPtr(item)->margins[1];
  867. }
  868. int uiGetMarginRight(int item) {
  869. return uiItemPtr(item)->margins[2];
  870. }
  871. int uiGetMarginDown(int item) {
  872. return uiItemPtr(item)->margins[3];
  873. }
  874. void uiSetRightTo(int item, int other) {
  875. assert((other < 0) || (uiParent(other) == uiParent(item)));
  876. uiItemPtr(item)->relto[0] = other;
  877. }
  878. int uiGetRightTo(int item) {
  879. return uiItemPtr(item)->relto[0];
  880. }
  881. void uiSetBelow(int item, int other) {
  882. assert((other < 0) || (uiParent(other) == uiParent(item)));
  883. uiItemPtr(item)->relto[1] = other;
  884. }
  885. int uiGetBelow(int item) {
  886. return uiItemPtr(item)->relto[1];
  887. }
  888. void uiSetLeftTo(int item, int other) {
  889. assert((other < 0) || (uiParent(other) == uiParent(item)));
  890. uiItemPtr(item)->relto[2] = other;
  891. }
  892. int uiGetLeftTo(int item) {
  893. return uiItemPtr(item)->relto[2];
  894. }
  895. void uiSetAbove(int item, int other) {
  896. assert((other < 0) || (uiParent(other) == uiParent(item)));
  897. uiItemPtr(item)->relto[3] = other;
  898. }
  899. int uiGetAbove(int item) {
  900. return uiItemPtr(item)->relto[3];
  901. }
  902. UI_INLINE void uiComputeChainSize(UIitem *pkid,
  903. int *need_size, int *hard_size, int dim) {
  904. UIitem *pitem = pkid;
  905. int wdim = dim+2;
  906. int size = pitem->rect.v[wdim] + pitem->margins[dim] + pitem->margins[wdim];
  907. *need_size = size;
  908. *hard_size = pitem->size.v[dim]?size:0;
  909. int it = 0;
  910. pitem->visited |= 1<<dim;
  911. // traverse along left neighbors
  912. while ((pitem->layout_flags>>dim) & UI_LEFT) {
  913. if (pitem->relto[dim] < 0) break;
  914. pitem = uiItemPtr(pitem->relto[dim]);
  915. pitem->visited |= 1<<dim;
  916. size = pitem->rect.v[wdim] + pitem->margins[dim] + pitem->margins[wdim];
  917. *need_size = (*need_size) + size;
  918. *hard_size = (*hard_size) + (pitem->size.v[dim]?size:0);
  919. it++;
  920. assert(it<1000000); // infinite loop
  921. }
  922. // traverse along right neighbors
  923. pitem = pkid;
  924. it = 0;
  925. while ((pitem->layout_flags>>dim) & UI_RIGHT) {
  926. if (pitem->relto[wdim] < 0) break;
  927. pitem = uiItemPtr(pitem->relto[wdim]);
  928. pitem->visited |= 1<<dim;
  929. size = pitem->rect.v[wdim] + pitem->margins[dim] + pitem->margins[wdim];
  930. *need_size = (*need_size) + size;
  931. *hard_size = (*hard_size) + (pitem->size.v[dim]?size:0);
  932. it++;
  933. assert(it<1000000); // infinite loop
  934. }
  935. }
  936. UI_INLINE void uiComputeSizeDim(UIitem *pitem, int dim) {
  937. int wdim = dim+2;
  938. int need_size = 0;
  939. int hard_size = 0;
  940. int kid = pitem->firstkid;
  941. while (kid >= 0) {
  942. UIitem *pkid = uiItemPtr(kid);
  943. if (!(pkid->visited & (1<<dim))) {
  944. int ns,hs;
  945. uiComputeChainSize(pkid, &ns, &hs, dim);
  946. need_size = ui_max(need_size, ns);
  947. hard_size = ui_max(hard_size, hs);
  948. }
  949. kid = uiNextSibling(kid);
  950. }
  951. pitem->computed_size.v[dim] = hard_size;
  952. if (pitem->size.v[dim]) {
  953. pitem->rect.v[wdim] = pitem->size.v[dim];
  954. } else {
  955. pitem->rect.v[wdim] = need_size;
  956. }
  957. }
  958. static void uiComputeBestSize(int item, int dim) {
  959. UIitem *pitem = uiItemPtr(item);
  960. pitem->visited = 0;
  961. // children expand the size
  962. int kid = uiFirstChild(item);
  963. while (kid >= 0) {
  964. uiComputeBestSize(kid, dim);
  965. kid = uiNextSibling(kid);
  966. }
  967. uiComputeSizeDim(pitem, dim);
  968. }
  969. static void uiLayoutChildItem(UIitem *pparent, UIitem *pitem,
  970. int *dyncount, int *consumed_space, int dim) {
  971. if (pitem->visited & (4<<dim)) return;
  972. pitem->visited |= (4<<dim);
  973. int wdim = dim+2;
  974. int x = 0;
  975. int s = pparent->rect.v[wdim];
  976. int flags = pitem->layout_flags>>dim;
  977. int hasl = (flags & UI_LEFT) && (pitem->relto[dim] >= 0);
  978. int hasr = (flags & UI_RIGHT) && (pitem->relto[wdim] >= 0);
  979. if ((flags & UI_HFILL) != UI_HFILL) {
  980. *consumed_space = (*consumed_space)
  981. + pitem->rect.v[wdim]
  982. + pitem->margins[wdim]
  983. + pitem->margins[dim];
  984. } else if (!pitem->size.v[dim]) {
  985. *dyncount = (*dyncount)+1;
  986. }
  987. if (hasl) {
  988. UIitem *pl = uiItemPtr(pitem->relto[dim]);
  989. uiLayoutChildItem(pparent, pl, dyncount, consumed_space, dim);
  990. x = pl->rect.v[dim]+pl->rect.v[wdim]+pl->margins[wdim];
  991. s -= x;
  992. }
  993. if (hasr) {
  994. UIitem *pl = uiItemPtr(pitem->relto[wdim]);
  995. uiLayoutChildItem(pparent, pl, dyncount, consumed_space, dim);
  996. s = pl->rect.v[dim]-pl->margins[dim]-x;
  997. }
  998. switch(flags & UI_HFILL) {
  999. default:
  1000. case UI_HCENTER: {
  1001. pitem->rect.v[dim] = x+(s-pitem->rect.v[wdim])/2+pitem->margins[dim];
  1002. } break;
  1003. case UI_LEFT: {
  1004. pitem->rect.v[dim] = x+pitem->margins[dim];
  1005. } break;
  1006. case UI_RIGHT: {
  1007. pitem->rect.v[dim] = x+s-pitem->rect.v[wdim]-pitem->margins[wdim];
  1008. } break;
  1009. case UI_HFILL: {
  1010. if (pitem->size.v[dim]) { // hard maximum size; can't stretch
  1011. if (!hasl)
  1012. pitem->rect.v[dim] = x+pitem->margins[dim];
  1013. else
  1014. pitem->rect.v[dim] = x+s-pitem->rect.v[wdim]-pitem->margins[wdim];
  1015. } else {
  1016. if (1) { //!pitem->rect.v[wdim]) {
  1017. //int width = (pparent->rect.v[wdim] - pparent->computed_size.v[dim]);
  1018. int width = (pparent->rect.v[wdim] - (*consumed_space));
  1019. int space = width / (*dyncount);
  1020. //int rest = width - space*(*dyncount);
  1021. if (!hasl) {
  1022. pitem->rect.v[dim] = x+pitem->margins[dim];
  1023. pitem->rect.v[wdim] = s-pitem->margins[dim]-pitem->margins[wdim];
  1024. } else {
  1025. pitem->rect.v[wdim] = space-pitem->margins[dim]-pitem->margins[wdim];
  1026. pitem->rect.v[dim] = x+s-pitem->rect.v[wdim]-pitem->margins[wdim];
  1027. }
  1028. } else {
  1029. pitem->rect.v[dim] = x+pitem->margins[dim];
  1030. pitem->rect.v[wdim] = s-pitem->margins[dim]-pitem->margins[wdim];
  1031. }
  1032. }
  1033. } break;
  1034. }
  1035. }
  1036. UI_INLINE void uiLayoutItemDim(UIitem *pitem, int dim) {
  1037. int wdim = dim+2;
  1038. int kid = pitem->firstkid;
  1039. int consumed_space = 0;
  1040. int dyncount = 0;
  1041. while (kid >= 0) {
  1042. UIitem *pkid = uiItemPtr(kid);
  1043. uiLayoutChildItem(pitem, pkid, &dyncount, &consumed_space, dim);
  1044. kid = uiNextSibling(kid);
  1045. }
  1046. }
  1047. static void uiLayoutItem(int item, int dim) {
  1048. UIitem *pitem = uiItemPtr(item);
  1049. uiLayoutItemDim(pitem, dim);
  1050. int kid = uiFirstChild(item);
  1051. while (kid >= 0) {
  1052. uiLayoutItem(kid, dim);
  1053. kid = uiNextSibling(kid);
  1054. }
  1055. }
  1056. UIrect uiGetRect(int item) {
  1057. return uiItemPtr(item)->rect;
  1058. }
  1059. UIrect uiGetActiveRect() {
  1060. assert(ui_context);
  1061. return ui_context->active_rect;
  1062. }
  1063. int uiFirstChild(int item) {
  1064. return uiItemPtr(item)->firstkid;
  1065. }
  1066. int uiLastChild(int item) {
  1067. return uiItemPtr(item)->lastkid;
  1068. }
  1069. int uiNextSibling(int item) {
  1070. return uiItemPtr(item)->nextitem;
  1071. }
  1072. int uiPrevSibling(int item) {
  1073. return uiItemPtr(item)->previtem;
  1074. }
  1075. int uiParent(int item) {
  1076. return uiItemPtr(item)->parent;
  1077. }
  1078. const void *uiGetData(int item) {
  1079. UIitem *pitem = uiItemPtr(item);
  1080. if (pitem->data < 0) return NULL;
  1081. return ui_context->data + pitem->data;
  1082. }
  1083. void *uiAllocData(int item, int size) {
  1084. assert((size > 0) && (size < UI_MAX_DATASIZE));
  1085. UIitem *pitem = uiItemPtr(item);
  1086. assert(pitem->data < 0);
  1087. assert((ui_context->datasize+size) <= UI_MAX_BUFFERSIZE);
  1088. pitem->data = ui_context->datasize;
  1089. ui_context->datasize += size;
  1090. return ui_context->data + pitem->data;
  1091. }
  1092. void uiSetHandle(int item, UIhandle handle) {
  1093. uiItemPtr(item)->handle = handle;
  1094. if (handle) {
  1095. uiHashInsertHandle(handle, item);
  1096. }
  1097. }
  1098. void uiSetSelfHandle(int item) {
  1099. UIitem *pitem = uiItemPtr(item);
  1100. pitem->handle = (UIhandle)pitem;
  1101. uiHashInsertHandle((UIhandle)pitem, item);
  1102. }
  1103. UIhandle uiGetHandle(int item) {
  1104. return uiItemPtr(item)->handle;
  1105. }
  1106. void uiSetHandler(int item, UIhandler handler, int flags) {
  1107. UIitem *pitem = uiItemPtr(item);
  1108. pitem->handler = handler;
  1109. pitem->event_flags = flags;
  1110. }
  1111. UIhandler uiGetHandler(int item) {
  1112. return uiItemPtr(item)->handler;
  1113. }
  1114. int uiGetHandlerFlags(int item) {
  1115. return uiItemPtr(item)->event_flags;
  1116. }
  1117. int uiGetChildId(int item) {
  1118. return uiItemPtr(item)->kidid;
  1119. }
  1120. int uiGetChildCount(int item) {
  1121. return uiItemPtr(item)->numkids;
  1122. }
  1123. UIrect uiGetAbsoluteRect(int item) {
  1124. UIrect rect = uiGetRect(item);
  1125. item = uiParent(item);
  1126. while (item >= 0) {
  1127. rect.x += uiItemPtr(item)->rect.x;
  1128. rect.y += uiItemPtr(item)->rect.y;
  1129. item = uiParent(item);
  1130. }
  1131. return rect;
  1132. }
  1133. int uiContains(int item, int x, int y) {
  1134. UIrect rect = uiGetAbsoluteRect(item);
  1135. x -= rect.x;
  1136. y -= rect.y;
  1137. if ((x>=0)
  1138. && (y>=0)
  1139. && (x<rect.w)
  1140. && (y<rect.h)) return 1;
  1141. return 0;
  1142. }
  1143. int uiFindItem(int item, int x, int y, int ox, int oy) {
  1144. UIitem *pitem = uiItemPtr(item);
  1145. if (pitem->frozen) return -1;
  1146. UIrect rect = pitem->rect;
  1147. x -= rect.x;
  1148. y -= rect.y;
  1149. ox += rect.x;
  1150. oy += rect.y;
  1151. if ((x>=0)
  1152. && (y>=0)
  1153. && (x<rect.w)
  1154. && (y<rect.h)) {
  1155. int kid = uiLastChild(item);
  1156. while (kid >= 0) {
  1157. int best_hit = uiFindItem(kid,x,y,ox,oy);
  1158. if (best_hit >= 0) return best_hit;
  1159. kid = uiPrevSibling(kid);
  1160. }
  1161. // click-through if the item has no handler for input events
  1162. if (pitem->event_flags & UI_ANY_INPUT) {
  1163. rect.x = ox;
  1164. rect.y = oy;
  1165. ui_context->hot_rect = rect;
  1166. return item;
  1167. }
  1168. }
  1169. return -1;
  1170. }
  1171. void uiLayout() {
  1172. assert(ui_context);
  1173. if (!ui_context->count) return;
  1174. // compute widths
  1175. uiComputeBestSize(0,0);
  1176. // position root element rect
  1177. uiItemPtr(0)->rect.x = uiItemPtr(0)->margins[0];
  1178. uiLayoutItem(0,0);
  1179. // compute heights
  1180. uiComputeBestSize(0,1);
  1181. // position root element rect
  1182. uiItemPtr(0)->rect.y = uiItemPtr(0)->margins[1];
  1183. uiLayoutItem(0,1);
  1184. // drawing routines may require this to be set already
  1185. int hot = uiFindItem(0,
  1186. ui_context->cursor.x, ui_context->cursor.y, 0, 0);
  1187. ui_context->hot_item = hot;
  1188. }
  1189. void uiProcess() {
  1190. assert(ui_context);
  1191. if (!ui_context->count) {
  1192. uiClearInputEvents();
  1193. return;
  1194. }
  1195. int hot_item = uiGetItem(ui_context->hot_handle);
  1196. int active_item = uiGetItem(ui_context->active_handle);
  1197. int focus_item = uiGetItem(ui_context->focus_handle);
  1198. // send all keyboard events
  1199. if (focus_item >= 0) {
  1200. for (int i = 0; i < ui_context->eventcount; ++i) {
  1201. ui_context->active_key = ui_context->events[i].key;
  1202. ui_context->active_modifier = ui_context->events[i].mod;
  1203. uiNotifyItem(focus_item,
  1204. ui_context->events[i].event);
  1205. }
  1206. } else {
  1207. ui_context->focus_handle = 0;
  1208. }
  1209. uiClearInputEvents();
  1210. int hot = ui_context->hot_item;
  1211. switch(ui_context->state) {
  1212. default:
  1213. case UI_STATE_IDLE: {
  1214. ui_context->start_cursor = ui_context->cursor;
  1215. if (uiGetButton(0)) {
  1216. hot_item = -1;
  1217. active_item = hot;
  1218. ui_context->active_rect = ui_context->hot_rect;
  1219. if (active_item != focus_item) {
  1220. focus_item = -1;
  1221. ui_context->focus_handle = 0;
  1222. }
  1223. if (active_item >= 0) {
  1224. uiNotifyItem(active_item, UI_BUTTON0_DOWN);
  1225. }
  1226. ui_context->state = UI_STATE_CAPTURE;
  1227. } else if (uiGetButton(2) && !uiGetLastButton(2) && (hot >= 0)) {
  1228. hot_item = -1;
  1229. ui_context->active_rect = ui_context->hot_rect;
  1230. uiNotifyItem(hot, UI_BUTTON2_DOWN);
  1231. } else {
  1232. hot_item = hot;
  1233. }
  1234. } break;
  1235. case UI_STATE_CAPTURE: {
  1236. if (!uiGetButton(0)) {
  1237. if (active_item >= 0) {
  1238. uiNotifyItem(active_item, UI_BUTTON0_UP);
  1239. if (active_item == hot) {
  1240. uiNotifyItem(active_item, UI_BUTTON0_HOT_UP);
  1241. }
  1242. }
  1243. active_item = -1;
  1244. ui_context->state = UI_STATE_IDLE;
  1245. } else {
  1246. if (active_item >= 0) {
  1247. uiNotifyItem(active_item, UI_BUTTON0_CAPTURE);
  1248. }
  1249. if (hot == active_item)
  1250. hot_item = hot;
  1251. else
  1252. hot_item = -1;
  1253. }
  1254. } break;
  1255. }
  1256. ui_context->last_cursor = ui_context->cursor;
  1257. ui_context->hot_handle = (hot_item>=0)?
  1258. uiGetHandle(hot_item):0;
  1259. ui_context->active_handle = (active_item>=0)?
  1260. uiGetHandle(active_item):0;
  1261. }
  1262. static int uiIsActive(int item) {
  1263. assert(ui_context);
  1264. return (ui_context->active_handle)&&(uiGetHandle(item) == ui_context->active_handle);
  1265. }
  1266. static int uiIsHot(int item) {
  1267. assert(ui_context);
  1268. return (ui_context->hot_handle)&&(uiGetHandle(item) == ui_context->hot_handle);
  1269. }
  1270. static int uiIsFocused(int item) {
  1271. assert(ui_context);
  1272. return (ui_context->focus_handle)&&(uiGetHandle(item) == ui_context->focus_handle);
  1273. }
  1274. UIitemState uiGetState(int item) {
  1275. UIitem *pitem = uiItemPtr(item);
  1276. if (pitem->frozen) return UI_FROZEN;
  1277. if (uiIsFocused(item)) {
  1278. if (pitem->event_flags & (UI_KEY_DOWN|UI_CHAR|UI_KEY_UP)) return UI_ACTIVE;
  1279. }
  1280. if (uiIsActive(item)) {
  1281. if (pitem->event_flags & (UI_BUTTON0_CAPTURE|UI_BUTTON0_UP)) return UI_ACTIVE;
  1282. if ((pitem->event_flags & UI_BUTTON0_HOT_UP)
  1283. && uiIsHot(item)) return UI_ACTIVE;
  1284. return UI_COLD;
  1285. } else if (uiIsHot(item)) {
  1286. return UI_HOT;
  1287. }
  1288. return UI_COLD;
  1289. }
  1290. #endif // OUI_IMPLEMENTATION