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.

1451 lines
41KB

  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. enum {
  152. // maximum number of items that may be added (must be power of 2)
  153. UI_MAX_ITEMS = 4096,
  154. // maximum size in bytes reserved for storage of application dependent data
  155. // as passed to uiAllocData().
  156. UI_MAX_BUFFERSIZE = 1048576,
  157. // maximum size in bytes of a single data buffer passed to uiAllocData().
  158. UI_MAX_DATASIZE = 4096,
  159. // maximum depth of nested containers
  160. UI_MAX_DEPTH = 64,
  161. // maximum number of buffered input events
  162. UI_MAX_INPUT_EVENTS = 64,
  163. // consecutive click threshold in ms
  164. UI_CLICK_THRESHOLD = 250,
  165. };
  166. typedef unsigned int UIuint;
  167. // opaque UI context
  168. typedef struct UIcontext UIcontext;
  169. // item states as returned by uiGetState()
  170. typedef enum UIitemState {
  171. // the item is inactive
  172. UI_COLD = 0,
  173. // the item is inactive, but the cursor is hovering over this item
  174. UI_HOT = 1,
  175. // the item is toggled, activated, focused (depends on item kind)
  176. UI_ACTIVE = 2,
  177. // the item is unresponsive
  178. UI_FROZEN = 3,
  179. } UIitemState;
  180. // layout flags
  181. typedef enum UIlayoutFlags {
  182. // container flags:
  183. // flex-direction (bit 0+1)
  184. // left to right
  185. UI_ROW = 0x002,
  186. // top to bottom
  187. UI_COLUMN = 0x003,
  188. // model (bit 1)
  189. // free layout
  190. UI_LAYOUT = 0x000,
  191. // flex model
  192. UI_FLEX = 0x002,
  193. // flex-wrap (bit 2)
  194. // single-line
  195. UI_NOWRAP = 0x000,
  196. // multi-line, wrap left to right
  197. UI_WRAP = 0x004,
  198. // justify-content (start, end, center, space-between)
  199. // can be implemented by putting flex container in a layout container,
  200. // then using UI_LEFT, UI_RIGHT, UI_HFILL, UI_HCENTER, etc.
  201. // align-items
  202. // can be implemented by putting flex container in a layout container,
  203. // then using UI_TOP, UI_DOWN, UI_VFILL, UI_VCENTER, etc.
  204. // FILL is equivalent to stretch/grow
  205. // align-content (start, end, center, stretch)
  206. // can be implemented by putting flex container in a layout container,
  207. // then using UI_TOP, UI_DOWN, UI_VFILL, UI_VCENTER, etc.
  208. // FILL is equivalent to stretch; space-between is not supported.
  209. // child item flags:
  210. // attachments (bit 5-8)
  211. // fully valid when parent uses UI_LAYOUT model
  212. // partially valid when in UI_FLEX model
  213. // anchor to left item or left side of parent
  214. UI_LEFT = 0x020,
  215. // anchor to top item or top side of parent
  216. UI_TOP = 0x040,
  217. // anchor to right item or right side of parent
  218. UI_RIGHT = 0x080,
  219. // anchor to bottom item or bottom side of parent
  220. UI_DOWN = 0x100,
  221. // anchor to both left and right item or parent borders
  222. UI_HFILL = 0x0a0,
  223. // anchor to both top and bottom item or parent borders
  224. UI_VFILL = 0x140,
  225. // center horizontally, with left margin as offset
  226. UI_HCENTER = 0x000,
  227. // center vertically, with top margin as offset
  228. UI_VCENTER = 0x000,
  229. // center in both directions, with left/top margin as offset
  230. UI_CENTER = 0x000,
  231. // anchor to all four directions
  232. UI_FILL = 0x1e0,
  233. } UIlayoutFlags;
  234. // event flags
  235. typedef enum UIevent {
  236. // on button 0 down
  237. UI_BUTTON0_DOWN = 0x0200,
  238. // on button 0 up
  239. // when this event has a handler, uiGetState() will return UI_ACTIVE as
  240. // long as button 0 is down.
  241. UI_BUTTON0_UP = 0x0400,
  242. // on button 0 up while item is hovered
  243. // when this event has a handler, uiGetState() will return UI_ACTIVE
  244. // when the cursor is hovering the items rectangle; this is the
  245. // behavior expected for buttons.
  246. UI_BUTTON0_HOT_UP = 0x0800,
  247. // item is being captured (button 0 constantly pressed);
  248. // when this event has a handler, uiGetState() will return UI_ACTIVE as
  249. // long as button 0 is down.
  250. UI_BUTTON0_CAPTURE = 0x1000,
  251. // on button 2 down (right mouse button, usually triggers context menu)
  252. UI_BUTTON2_DOWN = 0x2000,
  253. // item has received a scrollwheel event
  254. // the accumulated wheel offset can be queried with uiGetScroll()
  255. UI_SCROLL = 0x4000,
  256. // item is focused and has received a key-down event
  257. // the respective key can be queried using uiGetKey() and uiGetModifier()
  258. UI_KEY_DOWN = 0x8000,
  259. // item is focused and has received a key-up event
  260. // the respective key can be queried using uiGetKey() and uiGetModifier()
  261. UI_KEY_UP = 0x10000,
  262. // item is focused and has received a character event
  263. // the respective character can be queried using uiGetKey()
  264. UI_CHAR = 0x20000,
  265. } UIevent;
  266. // handler callback; event is one of UI_EVENT_*
  267. typedef void (*UIhandler)(int item, UIevent event);
  268. // for cursor positions, mainly
  269. typedef struct UIvec2 {
  270. union {
  271. int v[2];
  272. struct { int x, y; };
  273. };
  274. } UIvec2;
  275. // layout rectangle
  276. typedef struct UIrect {
  277. union {
  278. int v[4];
  279. struct { int x, y, w, h; };
  280. };
  281. } UIrect;
  282. // unless declared otherwise, all operations have the complexity O(1).
  283. // Context Management
  284. // ------------------
  285. // create a new UI context; call uiMakeCurrent() to make this context the
  286. // current context. The context is managed by the client and must be released
  287. // using uiDestroyContext()
  288. OUI_EXPORT UIcontext *uiCreateContext();
  289. // select an UI context as the current context; a context must always be
  290. // selected before using any of the other UI functions
  291. OUI_EXPORT void uiMakeCurrent(UIcontext *ctx);
  292. // release the memory of an UI context created with uiCreateContext(); if the
  293. // context is the current context, the current context will be set to NULL
  294. OUI_EXPORT void uiDestroyContext(UIcontext *ctx);
  295. // Input Control
  296. // -------------
  297. // sets the current cursor position (usually belonging to a mouse) to the
  298. // screen coordinates at (x,y)
  299. OUI_EXPORT void uiSetCursor(int x, int y);
  300. // returns the current cursor position in screen coordinates as set by
  301. // uiSetCursor()
  302. OUI_EXPORT UIvec2 uiGetCursor();
  303. // returns the offset of the cursor relative to the last call to uiProcess()
  304. OUI_EXPORT UIvec2 uiGetCursorDelta();
  305. // returns the beginning point of a drag operation.
  306. OUI_EXPORT UIvec2 uiGetCursorStart();
  307. // returns the offset of the cursor relative to the beginning point of a drag
  308. // operation.
  309. OUI_EXPORT UIvec2 uiGetCursorStartDelta();
  310. // sets a mouse or gamepad button as pressed/released
  311. // button is in the range 0..63 and maps to an application defined input
  312. // source.
  313. // enabled is 1 for pressed, 0 for released
  314. OUI_EXPORT void uiSetButton(int button, int enabled);
  315. // returns the current state of an application dependent input button
  316. // as set by uiSetButton().
  317. // the function returns 1 if the button has been set to pressed, 0 for released.
  318. OUI_EXPORT int uiGetButton(int button);
  319. // returns the number of chained clicks; 1 is a single click,
  320. // 2 is a double click, etc.
  321. OUI_EXPORT int uiGetClicks();
  322. // sets a key as down/up; the key can be any application defined keycode
  323. // mod is an application defined set of flags for modifier keys
  324. // enabled is 1 for key down, 0 for key up
  325. // all key events are being buffered until the next call to uiProcess()
  326. OUI_EXPORT void uiSetKey(unsigned int key, unsigned int mod, int enabled);
  327. // sends a single character for text input; the character is usually in the
  328. // unicode range, but can be application defined.
  329. // all char events are being buffered until the next call to uiProcess()
  330. OUI_EXPORT void uiSetChar(unsigned int value);
  331. // accumulates scroll wheel offsets for the current frame
  332. // all offsets are being accumulated until the next call to uiProcess()
  333. OUI_EXPORT void uiSetScroll(int x, int y);
  334. // returns the currently accumulated scroll wheel offsets for this frame
  335. OUI_EXPORT UIvec2 uiGetScroll();
  336. // Stages
  337. // ------
  338. // clear the item buffer; uiClear() should be called before the first
  339. // UI declaration for this frame to avoid concatenation of the same UI multiple
  340. // times.
  341. // After the call, all previously declared item IDs are invalid, and all
  342. // application dependent context data has been freed.
  343. OUI_EXPORT void uiClear();
  344. // layout all added items starting from the root item 0.
  345. // after calling uiLayout(), no further modifications to the item tree should
  346. // be done until the next call to uiClear().
  347. // It is safe to immediately draw the items after a call to uiLayout().
  348. // this is an O(N) operation for N = number of declared items.
  349. OUI_EXPORT void uiLayout();
  350. // update the current hot item; this only needs to be called if items are kept
  351. // for more than one frame and uiLayout() is not called
  352. OUI_EXPORT void uiUpdateHotItem();
  353. // update the internal state according to the current cursor position and
  354. // button states, and call all registered handlers.
  355. // timestamp is the time in milliseconds relative to the last call to uiProcess()
  356. // and is used to estimate the threshold for double-clicks
  357. // after calling uiProcess(), no further modifications to the item tree should
  358. // be done until the next call to uiClear().
  359. // Items should be drawn before a call to uiProcess()
  360. // this is an O(N) operation for N = number of declared items.
  361. OUI_EXPORT void uiProcess(int timestamp);
  362. // reset the currently stored hot/active etc. handles; this should be called when
  363. // a re-declaration of the UI changes the item indices, to avoid state
  364. // related glitches because item identities have changed.
  365. OUI_EXPORT void uiClearState();
  366. // UI Declaration
  367. // --------------
  368. // create a new UI item and return the new items ID.
  369. OUI_EXPORT int uiItem();
  370. // set an items state to frozen; the UI will not recurse into frozen items
  371. // when searching for hot or active items; subsequently, frozen items and
  372. // their child items will not cause mouse event notifications.
  373. // The frozen state is not applied recursively; uiGetState() will report
  374. // UI_COLD for child items. Upon encountering a frozen item, the drawing
  375. // routine needs to handle rendering of child items appropriately.
  376. // see example.cpp for a demonstration.
  377. OUI_EXPORT void uiSetFrozen(int item, int enable);
  378. // set the application-dependent handle of an item.
  379. // handle is an application defined 64-bit handle. If handle is NULL, the item
  380. // will not be interactive.
  381. OUI_EXPORT void uiSetHandle(int item, void *handle);
  382. // allocate space for application-dependent context data and assign it
  383. // as the handle to the item.
  384. // The memory of the pointer is managed by the UI context and released
  385. // upon the next call to uiClear()
  386. OUI_EXPORT void *uiAllocHandle(int item, int size);
  387. // set the global handler callback for interactive items.
  388. // the handler will be called for each item whose event flags are set using
  389. // uiSetEvents.
  390. OUI_EXPORT void uiSetHandler(UIhandler handler);
  391. // flags is a combination of UI_EVENT_* and designates for which events the
  392. // handler should be called.
  393. OUI_EXPORT void uiSetEvents(int item, int flags);
  394. // assign an item to a container.
  395. // an item ID of 0 refers to the root item.
  396. // if child is already assigned to a parent, an assertion will be thrown.
  397. // the function returns the child item ID
  398. OUI_EXPORT int uiAppend(int item, int child);
  399. // set the size of the item; a size of 0 indicates the dimension to be
  400. // dynamic; if the size is set, the item can not expand beyond that size.
  401. OUI_EXPORT void uiSetSize(int item, int w, int h);
  402. // set the anchoring behavior of the item to one or multiple UIlayoutFlags
  403. OUI_EXPORT void uiSetLayout(int item, int flags);
  404. // set the left, top, right and bottom margins of an item; when the item is
  405. // anchored to the parent or another item, the margin controls the distance
  406. // from the neighboring element.
  407. OUI_EXPORT void uiSetMargins(int item, short l, short t, short r, short b);
  408. // set item as recipient of all keyboard events; the item must have a handle
  409. // assigned; if item is -1, no item will be focused.
  410. OUI_EXPORT void uiFocus(int item);
  411. // Iteration
  412. // ---------
  413. // returns the first child item of a container item. If the item is not
  414. // a container or does not contain any items, -1 is returned.
  415. // if item is 0, the first child item of the root item will be returned.
  416. OUI_EXPORT int uiFirstChild(int item);
  417. // returns the last child item of a container item. If the item is not
  418. // a container or does not contain any items, -1 is returned.
  419. // if item is 0, the last child item of the root item will be returned.
  420. OUI_EXPORT int uiLastChild(int item);
  421. // returns an items next sibling in the list of the parent containers children.
  422. // if item is 0 or the item is the last child item, -1 will be returned.
  423. OUI_EXPORT int uiNextSibling(int item);
  424. // returns an items previous sibling in the list of the parent containers
  425. // children.
  426. // if item is 0 or the item is the first child item, -1 will be returned.
  427. OUI_EXPORT int uiPrevSibling(int item);
  428. // Querying
  429. // --------
  430. // return the total number of allocated items
  431. OUI_EXPORT int uiGetItemCount();
  432. // return the current state of the item. This state is only valid after
  433. // a call to uiProcess().
  434. // The returned value is one of UI_COLD, UI_HOT, UI_ACTIVE, UI_FROZEN.
  435. OUI_EXPORT UIitemState uiGetState(int item);
  436. // return the application-dependent handle of the item as passed to uiSetHandle()
  437. // or uiAllocHandle().
  438. OUI_EXPORT void *uiGetHandle(int item);
  439. // return the item that is currently under the cursor or -1 for none
  440. OUI_EXPORT int uiGetHotItem();
  441. // return the item that is currently focused or -1 for none
  442. OUI_EXPORT int uiGetFocusedItem();
  443. // return the handler callback as passed to uiSetHandler()
  444. OUI_EXPORT UIhandler uiGetHandler();
  445. // return the event flags for an item as passed to uiSetEvents()
  446. OUI_EXPORT int uiGetEvents(int item);
  447. // when handling a KEY_DOWN/KEY_UP event: the key that triggered this event
  448. OUI_EXPORT unsigned int uiGetKey();
  449. // when handling a KEY_DOWN/KEY_UP event: the key that triggered this event
  450. OUI_EXPORT unsigned int uiGetModifier();
  451. // returns the items layout rectangle in absolute coordinates. If
  452. // uiGetRect() is called before uiLayout(), the values of the returned
  453. // rectangle are undefined.
  454. OUI_EXPORT UIrect uiGetRect(int item);
  455. // returns 1 if an items absolute rectangle contains a given coordinate
  456. // otherwise 0
  457. OUI_EXPORT int uiContains(int item, int x, int y);
  458. // return the width of the item as set by uiSetSize()
  459. OUI_EXPORT int uiGetWidth(int item);
  460. // return the height of the item as set by uiSetSize()
  461. OUI_EXPORT int uiGetHeight(int item);
  462. // return the anchoring behavior as set by uiSetLayout()
  463. OUI_EXPORT int uiGetLayout(int item);
  464. // return the left margin of the item as set with uiSetMargins()
  465. OUI_EXPORT short uiGetMarginLeft(int item);
  466. // return the top margin of the item as set with uiSetMargins()
  467. OUI_EXPORT short uiGetMarginTop(int item);
  468. // return the right margin of the item as set with uiSetMargins()
  469. OUI_EXPORT short uiGetMarginRight(int item);
  470. // return the bottom margin of the item as set with uiSetMargins()
  471. OUI_EXPORT short uiGetMarginDown(int item);
  472. #ifdef __cplusplus
  473. };
  474. #endif
  475. #endif // _OUI_H_
  476. #ifdef OUI_IMPLEMENTATION
  477. #include <assert.h>
  478. #ifdef _MSC_VER
  479. #pragma warning (disable: 4996) // Switch off security warnings
  480. #pragma warning (disable: 4100) // Switch off unreferenced formal parameter warnings
  481. #ifdef __cplusplus
  482. #define UI_INLINE inline
  483. #else
  484. #define UI_INLINE
  485. #endif
  486. #else
  487. #define UI_INLINE inline
  488. #endif
  489. #define UI_MAX_KIND 16
  490. #define UI_ANY_BUTTON0_INPUT (UI_BUTTON0_DOWN \
  491. |UI_BUTTON0_UP \
  492. |UI_BUTTON0_HOT_UP \
  493. |UI_BUTTON0_CAPTURE)
  494. #define UI_ANY_BUTTON2_INPUT (UI_BUTTON2_DOWN)
  495. #define UI_ANY_MOUSE_INPUT (UI_ANY_BUTTON0_INPUT \
  496. |UI_ANY_BUTTON2_INPUT)
  497. #define UI_ANY_KEY_INPUT (UI_KEY_DOWN \
  498. |UI_KEY_UP \
  499. |UI_CHAR)
  500. #define UI_ANY_INPUT (UI_ANY_MOUSE_INPUT \
  501. |UI_ANY_KEY_INPUT)
  502. // extra item flags
  503. enum {
  504. // bit 0-8
  505. UI_ITEM_LAYOUT_MASK = 0x0001FF,
  506. // bit 9-18
  507. UI_ITEM_EVENT_MASK = 0x07FE00,
  508. // item is frozen (bit 19)
  509. UI_ITEM_FROZEN = 0x080000,
  510. // item handle is pointer to data (bit 20)
  511. UI_ITEM_DATA = 0x100000,
  512. };
  513. typedef struct UIitem {
  514. // declaration independent unique handle (for persistence)
  515. void *handle;
  516. unsigned int flags;
  517. // container structure
  518. // index of first kid
  519. int firstkid;
  520. // index of last kid
  521. int lastkid;
  522. // child structure
  523. // index of next sibling with same parent
  524. int nextitem;
  525. // index of previous sibling with same parent
  526. int previtem;
  527. // margin offsets, interpretation depends on flags
  528. // after layouting, the first two components are absolute coordinates
  529. short margins[4];
  530. // size
  531. short size[2];
  532. } UIitem;
  533. typedef enum UIstate {
  534. UI_STATE_IDLE = 0,
  535. UI_STATE_CAPTURE,
  536. } UIstate;
  537. typedef struct UIhandleEntry {
  538. unsigned int key;
  539. int item;
  540. } UIhandleEntry;
  541. typedef struct UIinputEvent {
  542. unsigned int key;
  543. unsigned int mod;
  544. UIevent event;
  545. } UIinputEvent;
  546. struct UIcontext {
  547. // handler
  548. UIhandler handler;
  549. // button state in this frame
  550. unsigned long long buttons;
  551. // button state in the previous frame
  552. unsigned long long last_buttons;
  553. // where the cursor was at the beginning of the active state
  554. UIvec2 start_cursor;
  555. // where the cursor was last frame
  556. UIvec2 last_cursor;
  557. // where the cursor is currently
  558. UIvec2 cursor;
  559. // accumulated scroll wheel offsets
  560. UIvec2 scroll;
  561. int active_item;
  562. int focus_item;
  563. int last_hot_item;
  564. int last_click_item;
  565. int hot_item;
  566. UIstate state;
  567. unsigned int active_key;
  568. unsigned int active_modifier;
  569. int event_item;
  570. int last_timestamp;
  571. int last_click_timestamp;
  572. int clicks;
  573. int count;
  574. int datasize;
  575. int eventcount;
  576. UIitem items[UI_MAX_ITEMS];
  577. unsigned char data[UI_MAX_BUFFERSIZE];
  578. UIinputEvent events[UI_MAX_INPUT_EVENTS];
  579. };
  580. UI_INLINE int ui_max(int a, int b) {
  581. return (a>b)?a:b;
  582. }
  583. UI_INLINE int ui_min(int a, int b) {
  584. return (a<b)?a:b;
  585. }
  586. static UIcontext *ui_context = NULL;
  587. UIcontext *uiCreateContext() {
  588. UIcontext *ctx = (UIcontext *)malloc(sizeof(UIcontext));
  589. memset(ctx, 0, sizeof(UIcontext));
  590. UIcontext *oldctx = ui_context;
  591. uiMakeCurrent(ctx);
  592. uiClear();
  593. uiClearState();
  594. uiMakeCurrent(oldctx);
  595. return ctx;
  596. }
  597. void uiMakeCurrent(UIcontext *ctx) {
  598. ui_context = ctx;
  599. }
  600. void uiDestroyContext(UIcontext *ctx) {
  601. if (ui_context == ctx)
  602. uiMakeCurrent(NULL);
  603. free(ctx);
  604. }
  605. void uiSetButton(int button, int enabled) {
  606. assert(ui_context);
  607. unsigned long long mask = 1ull<<button;
  608. // set new bit
  609. ui_context->buttons = (enabled)?
  610. (ui_context->buttons | mask):
  611. (ui_context->buttons & ~mask);
  612. }
  613. static void uiAddInputEvent(UIinputEvent event) {
  614. assert(ui_context);
  615. if (ui_context->eventcount == UI_MAX_INPUT_EVENTS) return;
  616. ui_context->events[ui_context->eventcount++] = event;
  617. }
  618. static void uiClearInputEvents() {
  619. assert(ui_context);
  620. ui_context->eventcount = 0;
  621. ui_context->scroll.x = 0;
  622. ui_context->scroll.y = 0;
  623. }
  624. void uiSetKey(unsigned int key, unsigned int mod, int enabled) {
  625. assert(ui_context);
  626. UIinputEvent event = { key, mod, enabled?UI_KEY_DOWN:UI_KEY_UP };
  627. uiAddInputEvent(event);
  628. }
  629. void uiSetChar(unsigned int value) {
  630. assert(ui_context);
  631. UIinputEvent event = { value, 0, UI_CHAR };
  632. uiAddInputEvent(event);
  633. }
  634. void uiSetScroll(int x, int y) {
  635. assert(ui_context);
  636. ui_context->scroll.x += x;
  637. ui_context->scroll.y += y;
  638. }
  639. UIvec2 uiGetScroll() {
  640. assert(ui_context);
  641. return ui_context->scroll;
  642. }
  643. int uiGetLastButton(int button) {
  644. assert(ui_context);
  645. return (ui_context->last_buttons & (1ull<<button))?1:0;
  646. }
  647. int uiGetButton(int button) {
  648. assert(ui_context);
  649. return (ui_context->buttons & (1ull<<button))?1:0;
  650. }
  651. int uiButtonPressed(int button) {
  652. assert(ui_context);
  653. return !uiGetLastButton(button) && uiGetButton(button);
  654. }
  655. int uiButtonReleased(int button) {
  656. assert(ui_context);
  657. return uiGetLastButton(button) && !uiGetButton(button);
  658. }
  659. void uiSetCursor(int x, int y) {
  660. assert(ui_context);
  661. ui_context->cursor.x = x;
  662. ui_context->cursor.y = y;
  663. }
  664. UIvec2 uiGetCursor() {
  665. assert(ui_context);
  666. return ui_context->cursor;
  667. }
  668. UIvec2 uiGetCursorStart() {
  669. assert(ui_context);
  670. return ui_context->start_cursor;
  671. }
  672. UIvec2 uiGetCursorDelta() {
  673. assert(ui_context);
  674. UIvec2 result = {{{
  675. ui_context->cursor.x - ui_context->last_cursor.x,
  676. ui_context->cursor.y - ui_context->last_cursor.y
  677. }}};
  678. return result;
  679. }
  680. UIvec2 uiGetCursorStartDelta() {
  681. assert(ui_context);
  682. UIvec2 result = {{{
  683. ui_context->cursor.x - ui_context->start_cursor.x,
  684. ui_context->cursor.y - ui_context->start_cursor.y
  685. }}};
  686. return result;
  687. }
  688. unsigned int uiGetKey() {
  689. assert(ui_context);
  690. return ui_context->active_key;
  691. }
  692. unsigned int uiGetModifier() {
  693. assert(ui_context);
  694. return ui_context->active_modifier;
  695. }
  696. // return the total number of allocated items
  697. OUI_EXPORT int uiGetItemCount() {
  698. assert(ui_context);
  699. return ui_context->count;
  700. }
  701. UIitem *uiItemPtr(int item) {
  702. assert(ui_context && (item >= 0) && (item < ui_context->count));
  703. return ui_context->items + item;
  704. }
  705. int uiGetHotItem() {
  706. assert(ui_context);
  707. return ui_context->hot_item;
  708. }
  709. void uiFocus(int item) {
  710. assert(ui_context && (item >= -1) && (item < ui_context->count));
  711. ui_context->focus_item = item;
  712. }
  713. static void uiValidateStateItems() {
  714. assert(ui_context);
  715. if (ui_context->last_hot_item >= ui_context->count)
  716. ui_context->last_hot_item = -1;
  717. if (ui_context->active_item >= ui_context->count)
  718. ui_context->active_item = -1;
  719. if (ui_context->focus_item >= ui_context->count)
  720. ui_context->focus_item = -1;
  721. if (ui_context->last_click_item >= ui_context->count)
  722. ui_context->last_click_item = -1;
  723. }
  724. int uiGetFocusedItem() {
  725. assert(ui_context);
  726. return ui_context->focus_item;
  727. }
  728. void uiClear() {
  729. assert(ui_context);
  730. ui_context->count = 0;
  731. ui_context->datasize = 0;
  732. ui_context->hot_item = -1;
  733. }
  734. void uiClearState() {
  735. assert(ui_context);
  736. ui_context->last_hot_item = -1;
  737. ui_context->active_item = -1;
  738. ui_context->focus_item = -1;
  739. ui_context->last_click_item = -1;
  740. }
  741. int uiItem() {
  742. assert(ui_context);
  743. assert(ui_context->count < UI_MAX_ITEMS);
  744. int idx = ui_context->count++;
  745. UIitem *item = uiItemPtr(idx);
  746. memset(item, 0, sizeof(UIitem));
  747. item->firstkid = -1;
  748. item->lastkid = -1;
  749. item->nextitem = -1;
  750. item->previtem = -1;
  751. return idx;
  752. }
  753. void uiNotifyItem(int item, UIevent event) {
  754. assert(ui_context);
  755. if (!ui_context->handler)
  756. return;
  757. assert((event & UI_ITEM_EVENT_MASK) == event);
  758. ui_context->event_item = item;
  759. UIitem *pitem = uiItemPtr(item);
  760. if (pitem->flags & event) {
  761. ui_context->handler(item, event);
  762. }
  763. }
  764. int uiAppend(int item, int child) {
  765. assert(child > 0);
  766. UIitem *pitem = uiItemPtr(child);
  767. UIitem *pparent = uiItemPtr(item);
  768. if (pparent->lastkid < 0) {
  769. pparent->firstkid = child;
  770. pparent->lastkid = child;
  771. } else {
  772. pitem->previtem = pparent->lastkid;
  773. uiItemPtr(pparent->lastkid)->nextitem = child;
  774. pparent->lastkid = child;
  775. }
  776. return child;
  777. }
  778. void uiSetFrozen(int item, int enable) {
  779. UIitem *pitem = uiItemPtr(item);
  780. if (enable)
  781. pitem->flags |= UI_ITEM_FROZEN;
  782. else
  783. pitem->flags &= ~UI_ITEM_FROZEN;
  784. }
  785. void uiSetSize(int item, int w, int h) {
  786. UIitem *pitem = uiItemPtr(item);
  787. pitem->size[0] = w;
  788. pitem->size[1] = h;
  789. }
  790. int uiGetWidth(int item) {
  791. return uiItemPtr(item)->size[0];
  792. }
  793. int uiGetHeight(int item) {
  794. return uiItemPtr(item)->size[1];
  795. }
  796. void uiSetLayout(int item, int flags) {
  797. uiItemPtr(item)->flags |= flags & UI_ITEM_LAYOUT_MASK;
  798. }
  799. int uiGetLayout(int item) {
  800. return uiItemPtr(item)->flags & UI_ITEM_LAYOUT_MASK;
  801. }
  802. void uiSetMargins(int item, short l, short t, short r, short b) {
  803. UIitem *pitem = uiItemPtr(item);
  804. pitem->margins[0] = l;
  805. pitem->margins[1] = t;
  806. pitem->margins[2] = r;
  807. pitem->margins[3] = b;
  808. }
  809. short uiGetMarginLeft(int item) {
  810. return uiItemPtr(item)->margins[0];
  811. }
  812. short uiGetMarginTop(int item) {
  813. return uiItemPtr(item)->margins[1];
  814. }
  815. short uiGetMarginRight(int item) {
  816. return uiItemPtr(item)->margins[2];
  817. }
  818. short uiGetMarginDown(int item) {
  819. return uiItemPtr(item)->margins[3];
  820. }
  821. // compute bounding box of all items super-imposed
  822. UI_INLINE void uiComputeImposedSizeDim(UIitem *pitem, int dim) {
  823. int wdim = dim+2;
  824. if (pitem->size[dim])
  825. return;
  826. // largest size is required size
  827. short need_size = 0;
  828. int kid = pitem->firstkid;
  829. while (kid >= 0) {
  830. UIitem *pkid = uiItemPtr(kid);
  831. // width = start margin + calculated width + end margin
  832. int kidsize = pkid->margins[dim] + pkid->size[dim] + pkid->margins[wdim];
  833. need_size = ui_max(need_size, kidsize);
  834. kid = uiNextSibling(kid);
  835. }
  836. pitem->size[dim] = need_size;
  837. }
  838. // compute bounding box of all items stacked
  839. UI_INLINE void uiComputeStackedSizeDim(UIitem *pitem, int dim) {
  840. int wdim = dim+2;
  841. if (pitem->size[dim])
  842. return;
  843. short need_size = 0;
  844. int kid = pitem->firstkid;
  845. while (kid >= 0) {
  846. UIitem *pkid = uiItemPtr(kid);
  847. // width += start margin + calculated width + end margin
  848. need_size += pkid->margins[dim] + pkid->size[dim] + pkid->margins[wdim];
  849. kid = uiNextSibling(kid);
  850. }
  851. pitem->size[dim] = need_size;
  852. }
  853. static void uiComputeBestSize(int item, int dim) {
  854. UIitem *pitem = uiItemPtr(item);
  855. // children expand the size
  856. int kid = uiFirstChild(item);
  857. while (kid >= 0) {
  858. uiComputeBestSize(kid, dim);
  859. kid = uiNextSibling(kid);
  860. }
  861. if(pitem->flags & UI_FLEX) {
  862. // flex model
  863. if ((pitem->flags & 1) == (unsigned int)dim) // direction
  864. uiComputeStackedSizeDim(pitem, dim);
  865. else
  866. uiComputeImposedSizeDim(pitem, dim);
  867. } else {
  868. // layout model
  869. uiComputeImposedSizeDim(pitem, dim);
  870. }
  871. }
  872. // stack all items according to their alignment
  873. UI_INLINE void uiLayoutStackedItemDim(UIitem *pitem, int dim) {
  874. int wdim = dim+2;
  875. short space = pitem->size[dim];
  876. short used = 0;
  877. int count = 0;
  878. // first pass: count items that need to be expanded,
  879. // and the space that is used
  880. int kid = pitem->firstkid;
  881. while (kid >= 0) {
  882. UIitem *pkid = uiItemPtr(kid);
  883. int flags = (pkid->flags & UI_ITEM_LAYOUT_MASK) >> dim;
  884. if ((flags & UI_HFILL) == UI_HFILL) { // grow
  885. count++;
  886. used += pkid->margins[dim] + pkid->margins[wdim];
  887. } else {
  888. used += pkid->margins[dim] + pkid->size[dim] + pkid->margins[wdim];
  889. }
  890. kid = uiNextSibling(kid);
  891. }
  892. int extra_space = ui_max(space - used,0);
  893. if (extra_space && count) {
  894. // distribute width among items
  895. float width = (float)extra_space / (float)count;
  896. float x = (float)pitem->margins[dim];
  897. float x1;
  898. // second pass: distribute and rescale
  899. kid = pitem->firstkid;
  900. while (kid >= 0) {
  901. short ix0,ix1;
  902. UIitem *pkid = uiItemPtr(kid);
  903. int flags = (pkid->flags & UI_ITEM_LAYOUT_MASK) >> dim;
  904. x += (float)pkid->margins[dim];
  905. if ((flags & UI_HFILL) == UI_HFILL) { // grow
  906. x1 = x+width;
  907. } else {
  908. x1 = x+(float)pkid->size[dim];
  909. }
  910. ix0 = (short)x;
  911. ix1 = (short)x1;
  912. pkid->margins[dim] = ix0;
  913. pkid->size[dim] = ix1-ix0;
  914. x = x1 + (float)pkid->margins[wdim];
  915. kid = uiNextSibling(kid);
  916. }
  917. } else {
  918. // single pass: just distribute
  919. short x = pitem->margins[dim];
  920. int kid = pitem->firstkid;
  921. while (kid >= 0) {
  922. UIitem *pkid = uiItemPtr(kid);
  923. x += pkid->margins[dim];
  924. pkid->margins[dim] = x;
  925. x += pkid->size[dim] + pkid->margins[wdim];
  926. kid = uiNextSibling(kid);
  927. }
  928. }
  929. }
  930. // superimpose all items according to their alignment
  931. UI_INLINE void uiLayoutImposedItemDim(UIitem *pitem, int dim) {
  932. int wdim = dim+2;
  933. short space = pitem->size[dim];
  934. short offset = pitem->margins[dim];
  935. int kid = pitem->firstkid;
  936. while (kid >= 0) {
  937. UIitem *pkid = uiItemPtr(kid);
  938. int flags = (pkid->flags & UI_ITEM_LAYOUT_MASK) >> dim;
  939. switch(flags & UI_HFILL) {
  940. default: break;
  941. case UI_HCENTER: {
  942. pkid->margins[dim] += (space-pkid->size[dim])/2;
  943. } break;
  944. case UI_RIGHT: {
  945. pkid->margins[dim] = space-pkid->size[dim]-pkid->margins[wdim];
  946. } break;
  947. case UI_HFILL: {
  948. pkid->size[dim] = ui_max(0,space-pkid->margins[dim]-pkid->margins[wdim]);
  949. } break;
  950. }
  951. pkid->margins[dim] += offset;
  952. kid = uiNextSibling(kid);
  953. }
  954. }
  955. static void uiLayoutItem(int item, int dim) {
  956. UIitem *pitem = uiItemPtr(item);
  957. if(pitem->flags & UI_FLEX) {
  958. // flex model
  959. if ((pitem->flags & 1) == (unsigned int)dim) // direction
  960. uiLayoutStackedItemDim(pitem, dim);
  961. else
  962. uiLayoutImposedItemDim(pitem, dim);
  963. } else {
  964. // layout model
  965. uiLayoutImposedItemDim(pitem, dim);
  966. }
  967. int kid = uiFirstChild(item);
  968. while (kid >= 0) {
  969. uiLayoutItem(kid, dim);
  970. kid = uiNextSibling(kid);
  971. }
  972. }
  973. UIrect uiGetRect(int item) {
  974. UIitem *pitem = uiItemPtr(item);
  975. UIrect rc = {{{
  976. pitem->margins[0], pitem->margins[1],
  977. pitem->size[0], pitem->size[1]
  978. }}};
  979. return rc;
  980. }
  981. int uiFirstChild(int item) {
  982. return uiItemPtr(item)->firstkid;
  983. }
  984. int uiLastChild(int item) {
  985. return uiItemPtr(item)->lastkid;
  986. }
  987. int uiNextSibling(int item) {
  988. return uiItemPtr(item)->nextitem;
  989. }
  990. int uiPrevSibling(int item) {
  991. return uiItemPtr(item)->previtem;
  992. }
  993. void *uiAllocHandle(int item, int size) {
  994. assert((size > 0) && (size < UI_MAX_DATASIZE));
  995. UIitem *pitem = uiItemPtr(item);
  996. assert(pitem->handle == NULL);
  997. assert((ui_context->datasize+size) <= UI_MAX_BUFFERSIZE);
  998. pitem->handle = ui_context->data + ui_context->datasize;
  999. pitem->flags |= UI_ITEM_DATA;
  1000. ui_context->datasize += size;
  1001. return pitem->handle;
  1002. }
  1003. void uiSetHandle(int item, void *handle) {
  1004. UIitem *pitem = uiItemPtr(item);
  1005. assert(pitem->handle == NULL);
  1006. pitem->handle = handle;
  1007. }
  1008. void *uiGetHandle(int item) {
  1009. return uiItemPtr(item)->handle;
  1010. }
  1011. void uiSetHandler(UIhandler handler) {
  1012. assert(ui_context);
  1013. ui_context->handler = handler;
  1014. }
  1015. UIhandler uiGetHandler() {
  1016. assert(ui_context);
  1017. return ui_context->handler;
  1018. }
  1019. void uiSetEvents(int item, int flags) {
  1020. UIitem *pitem = uiItemPtr(item);
  1021. pitem->flags &= ~UI_ITEM_EVENT_MASK;
  1022. pitem->flags |= flags & UI_ITEM_EVENT_MASK;
  1023. }
  1024. int uiGetEvents(int item) {
  1025. return uiItemPtr(item)->flags & UI_ITEM_EVENT_MASK;
  1026. }
  1027. int uiContains(int item, int x, int y) {
  1028. UIrect rect = uiGetRect(item);
  1029. x -= rect.x;
  1030. y -= rect.y;
  1031. if ((x>=0)
  1032. && (y>=0)
  1033. && (x<rect.w)
  1034. && (y<rect.h)) return 1;
  1035. return 0;
  1036. }
  1037. int uiFindItemForEvent(int item, UIevent event, int x, int y) {
  1038. UIitem *pitem = uiItemPtr(item);
  1039. if (pitem->flags & UI_ITEM_FROZEN) return -1;
  1040. if (uiContains(item, x, y)) {
  1041. int kid = uiLastChild(item);
  1042. while (kid >= 0) {
  1043. int best_hit = uiFindItemForEvent(kid, event, x, y);
  1044. if (best_hit >= 0) return best_hit;
  1045. kid = uiPrevSibling(kid);
  1046. }
  1047. // click-through if the item has no handler for this event
  1048. if (pitem->flags & event) {
  1049. return item;
  1050. }
  1051. }
  1052. return -1;
  1053. }
  1054. void uiLayout() {
  1055. assert(ui_context);
  1056. if (!ui_context->count) return;
  1057. // compute widths
  1058. uiComputeBestSize(0,0);
  1059. uiLayoutItem(0,0);
  1060. // compute heights
  1061. uiComputeBestSize(0,1);
  1062. uiLayoutItem(0,1);
  1063. uiValidateStateItems();
  1064. // drawing routines may require this to be set already
  1065. uiUpdateHotItem();
  1066. }
  1067. void uiUpdateHotItem() {
  1068. assert(ui_context);
  1069. if (!ui_context->count) return;
  1070. ui_context->hot_item = uiFindItemForEvent(0,
  1071. (UIevent)UI_ANY_MOUSE_INPUT,
  1072. ui_context->cursor.x, ui_context->cursor.y);
  1073. }
  1074. int uiGetClicks() {
  1075. return ui_context->clicks;
  1076. }
  1077. void uiProcess(int timestamp) {
  1078. assert(ui_context);
  1079. if (!ui_context->count) {
  1080. uiClearInputEvents();
  1081. return;
  1082. }
  1083. int hot_item = ui_context->last_hot_item;
  1084. int active_item = ui_context->active_item;
  1085. int focus_item = ui_context->focus_item;
  1086. // send all keyboard events
  1087. if (focus_item >= 0) {
  1088. for (int i = 0; i < ui_context->eventcount; ++i) {
  1089. ui_context->active_key = ui_context->events[i].key;
  1090. ui_context->active_modifier = ui_context->events[i].mod;
  1091. uiNotifyItem(focus_item,
  1092. ui_context->events[i].event);
  1093. }
  1094. } else {
  1095. ui_context->focus_item = -1;
  1096. }
  1097. if (ui_context->scroll.x || ui_context->scroll.y) {
  1098. int scroll_item = uiFindItemForEvent(0, UI_SCROLL,
  1099. ui_context->cursor.x, ui_context->cursor.y);
  1100. if (scroll_item >= 0) {
  1101. uiNotifyItem(scroll_item, UI_SCROLL);
  1102. }
  1103. }
  1104. uiClearInputEvents();
  1105. int hot = ui_context->hot_item;
  1106. switch(ui_context->state) {
  1107. default:
  1108. case UI_STATE_IDLE: {
  1109. ui_context->start_cursor = ui_context->cursor;
  1110. if (uiGetButton(0)) {
  1111. hot_item = -1;
  1112. active_item = hot;
  1113. if (active_item != focus_item) {
  1114. focus_item = -1;
  1115. ui_context->focus_item = -1;
  1116. }
  1117. if (active_item >= 0) {
  1118. if (
  1119. ((timestamp - ui_context->last_click_timestamp) > UI_CLICK_THRESHOLD)
  1120. || (ui_context->last_click_item != active_item)) {
  1121. ui_context->clicks = 0;
  1122. }
  1123. ui_context->clicks++;
  1124. ui_context->last_click_timestamp = timestamp;
  1125. ui_context->last_click_item = active_item;
  1126. uiNotifyItem(active_item, UI_BUTTON0_DOWN);
  1127. }
  1128. ui_context->state = UI_STATE_CAPTURE;
  1129. } else if (uiGetButton(2) && !uiGetLastButton(2)) {
  1130. hot_item = -1;
  1131. hot = uiFindItemForEvent(0, UI_BUTTON2_DOWN,
  1132. ui_context->cursor.x, ui_context->cursor.y);
  1133. if (hot >= 0) {
  1134. uiNotifyItem(hot, UI_BUTTON2_DOWN);
  1135. }
  1136. } else {
  1137. hot_item = hot;
  1138. }
  1139. } break;
  1140. case UI_STATE_CAPTURE: {
  1141. if (!uiGetButton(0)) {
  1142. if (active_item >= 0) {
  1143. uiNotifyItem(active_item, UI_BUTTON0_UP);
  1144. if (active_item == hot) {
  1145. uiNotifyItem(active_item, UI_BUTTON0_HOT_UP);
  1146. }
  1147. }
  1148. active_item = -1;
  1149. ui_context->state = UI_STATE_IDLE;
  1150. } else {
  1151. if (active_item >= 0) {
  1152. uiNotifyItem(active_item, UI_BUTTON0_CAPTURE);
  1153. }
  1154. if (hot == active_item)
  1155. hot_item = hot;
  1156. else
  1157. hot_item = -1;
  1158. }
  1159. } break;
  1160. }
  1161. ui_context->last_cursor = ui_context->cursor;
  1162. ui_context->last_hot_item = hot_item;
  1163. ui_context->active_item = active_item;
  1164. ui_context->last_timestamp = timestamp;
  1165. ui_context->last_buttons = ui_context->buttons;
  1166. }
  1167. static int uiIsActive(int item) {
  1168. assert(ui_context);
  1169. return ui_context->active_item == item;
  1170. }
  1171. static int uiIsHot(int item) {
  1172. assert(ui_context);
  1173. return ui_context->last_hot_item == item;
  1174. }
  1175. static int uiIsFocused(int item) {
  1176. assert(ui_context);
  1177. return ui_context->focus_item == item;
  1178. }
  1179. UIitemState uiGetState(int item) {
  1180. UIitem *pitem = uiItemPtr(item);
  1181. if (pitem->flags & UI_ITEM_FROZEN) return UI_FROZEN;
  1182. if (uiIsFocused(item)) {
  1183. if (pitem->flags & (UI_KEY_DOWN|UI_CHAR|UI_KEY_UP)) return UI_ACTIVE;
  1184. }
  1185. if (uiIsActive(item)) {
  1186. if (pitem->flags & (UI_BUTTON0_CAPTURE|UI_BUTTON0_UP)) return UI_ACTIVE;
  1187. if ((pitem->flags & UI_BUTTON0_HOT_UP)
  1188. && uiIsHot(item)) return UI_ACTIVE;
  1189. return UI_COLD;
  1190. } else if (uiIsHot(item)) {
  1191. return UI_HOT;
  1192. }
  1193. return UI_COLD;
  1194. }
  1195. #endif // OUI_IMPLEMENTATION