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.

1686 lines
50KB

  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. // anchor to left item or left side of parent
  183. UI_LEFT = 0x1,
  184. // anchor to top item or top side of parent
  185. UI_TOP = 0x2,
  186. // anchor to right item or right side of parent
  187. UI_RIGHT = 0x4,
  188. // anchor to bottom item or bottom side of parent
  189. UI_DOWN = 0x8,
  190. // anchor to both left and right item or parent borders
  191. UI_HFILL = 0x5,
  192. // anchor to both top and bottom item or parent borders
  193. UI_VFILL = 0xA,
  194. // center horizontally, with left margin as offset
  195. UI_HCENTER = 0x0,
  196. // center vertically, with top margin as offset
  197. UI_VCENTER = 0x0,
  198. // center in both directions, with left/top margin as offset
  199. UI_CENTER = 0x0,
  200. // anchor to all four directions
  201. UI_FILL = 0xF,
  202. } UIlayoutFlags;
  203. // event flags
  204. typedef enum UIevent {
  205. // on button 0 down
  206. UI_BUTTON0_DOWN = 0x0010,
  207. // on button 0 up
  208. // when this event has a handler, uiGetState() will return UI_ACTIVE as
  209. // long as button 0 is down.
  210. UI_BUTTON0_UP = 0x0020,
  211. // on button 0 up while item is hovered
  212. // when this event has a handler, uiGetState() will return UI_ACTIVE
  213. // when the cursor is hovering the items rectangle; this is the
  214. // behavior expected for buttons.
  215. UI_BUTTON0_HOT_UP = 0x0040,
  216. // item is being captured (button 0 constantly pressed);
  217. // when this event has a handler, uiGetState() will return UI_ACTIVE as
  218. // long as button 0 is down.
  219. UI_BUTTON0_CAPTURE = 0x0080,
  220. // on button 2 down (right mouse button, usually triggers context menu)
  221. UI_BUTTON2_DOWN = 0x0100,
  222. // item has received a scrollwheel event
  223. // the accumulated wheel offset can be queried with uiGetScroll()
  224. UI_SCROLL = 0x0200,
  225. // item is focused and has received a key-down event
  226. // the respective key can be queried using uiGetKey() and uiGetModifier()
  227. UI_KEY_DOWN = 0x0400,
  228. // item is focused and has received a key-up event
  229. // the respective key can be queried using uiGetKey() and uiGetModifier()
  230. UI_KEY_UP = 0x0800,
  231. // item is focused and has received a character event
  232. // the respective character can be queried using uiGetKey()
  233. UI_CHAR = 0x1000,
  234. // if this flag is set, all events will propagate to the parent;
  235. // the original item firing this event can be retrieved using
  236. // uiGetEventItem()
  237. UI_PROPAGATE = 0x2000,
  238. // used if, after computing the horizontal size of the element, the vertical
  239. // size needs adjustment.
  240. // the handler is called after the horizontal layout step, and can make
  241. // modifications to the items height using uiSetSize()
  242. UI_ADJUST_HEIGHT = 0x4000,
  243. } UIevent;
  244. // handler callback; event is one of UI_EVENT_*
  245. typedef void (*UIhandler)(int item, UIevent event);
  246. // for cursor positions, mainly
  247. typedef struct UIvec2 {
  248. union {
  249. int v[2];
  250. struct { int x, y; };
  251. };
  252. } UIvec2;
  253. // layout rectangle
  254. typedef struct UIrect {
  255. union {
  256. int v[4];
  257. struct { int x, y, w, h; };
  258. };
  259. } UIrect;
  260. // unless declared otherwise, all operations have the complexity O(1).
  261. // Context Management
  262. // ------------------
  263. // create a new UI context; call uiMakeCurrent() to make this context the
  264. // current context. The context is managed by the client and must be released
  265. // using uiDestroyContext()
  266. OUI_EXPORT UIcontext *uiCreateContext();
  267. // select an UI context as the current context; a context must always be
  268. // selected before using any of the other UI functions
  269. OUI_EXPORT void uiMakeCurrent(UIcontext *ctx);
  270. // release the memory of an UI context created with uiCreateContext(); if the
  271. // context is the current context, the current context will be set to NULL
  272. OUI_EXPORT void uiDestroyContext(UIcontext *ctx);
  273. // Input Control
  274. // -------------
  275. // sets the current cursor position (usually belonging to a mouse) to the
  276. // screen coordinates at (x,y)
  277. OUI_EXPORT void uiSetCursor(int x, int y);
  278. // returns the current cursor position in screen coordinates as set by
  279. // uiSetCursor()
  280. OUI_EXPORT UIvec2 uiGetCursor();
  281. // returns the offset of the cursor relative to the last call to uiProcess()
  282. OUI_EXPORT UIvec2 uiGetCursorDelta();
  283. // returns the beginning point of a drag operation.
  284. OUI_EXPORT UIvec2 uiGetCursorStart();
  285. // returns the offset of the cursor relative to the beginning point of a drag
  286. // operation.
  287. OUI_EXPORT UIvec2 uiGetCursorStartDelta();
  288. // sets a mouse or gamepad button as pressed/released
  289. // button is in the range 0..63 and maps to an application defined input
  290. // source.
  291. // enabled is 1 for pressed, 0 for released
  292. OUI_EXPORT void uiSetButton(int button, int enabled);
  293. // returns the current state of an application dependent input button
  294. // as set by uiSetButton().
  295. // the function returns 1 if the button has been set to pressed, 0 for released.
  296. OUI_EXPORT int uiGetButton(int button);
  297. // returns the number of chained clicks; 1 is a single click,
  298. // 2 is a double click, etc.
  299. OUI_EXPORT int uiGetClicks();
  300. // sets a key as down/up; the key can be any application defined keycode
  301. // mod is an application defined set of flags for modifier keys
  302. // enabled is 1 for key down, 0 for key up
  303. // all key events are being buffered until the next call to uiProcess()
  304. OUI_EXPORT void uiSetKey(unsigned int key, unsigned int mod, int enabled);
  305. // sends a single character for text input; the character is usually in the
  306. // unicode range, but can be application defined.
  307. // all char events are being buffered until the next call to uiProcess()
  308. OUI_EXPORT void uiSetChar(unsigned int value);
  309. // accumulates scroll wheel offsets for the current frame
  310. // all offsets are being accumulated until the next call to uiProcess()
  311. OUI_EXPORT void uiSetScroll(int x, int y);
  312. // returns the currently accumulated scroll wheel offsets for this frame
  313. OUI_EXPORT UIvec2 uiGetScroll();
  314. // Stages
  315. // ------
  316. // clear the item buffer; uiClear() should be called before the first
  317. // UI declaration for this frame to avoid concatenation of the same UI multiple
  318. // times.
  319. // After the call, all previously declared item IDs are invalid, and all
  320. // application dependent context data has been freed.
  321. OUI_EXPORT void uiClear();
  322. // layout all added items starting from the root item 0.
  323. // after calling uiLayout(), no further modifications to the item tree should
  324. // be done until the next call to uiClear().
  325. // It is safe to immediately draw the items after a call to uiLayout().
  326. // this is an O(N) operation for N = number of declared items.
  327. OUI_EXPORT void uiLayout();
  328. // update the current hot item; this only needs to be called if items are kept
  329. // for more than one frame and uiLayout() is not called
  330. OUI_EXPORT void uiUpdateHotItem();
  331. // update the internal state according to the current cursor position and
  332. // button states, and call all registered handlers.
  333. // timestamp is the time in milliseconds relative to the last call to uiProcess()
  334. // and is used to estimate the threshold for double-clicks
  335. // after calling uiProcess(), no further modifications to the item tree should
  336. // be done until the next call to uiClear().
  337. // Items should be drawn before a call to uiProcess()
  338. // this is an O(N) operation for N = number of declared items.
  339. OUI_EXPORT void uiProcess(int timestamp);
  340. // reset the currently stored hot/active etc. handles; this should be called when
  341. // a redeclaration of the UI changes the handle addresses, to avoid state
  342. // related glitches because item identities have changed. If you're using
  343. // uiAllocHandle() you should definitely make use of this.
  344. OUI_EXPORT void uiClearHandleState();
  345. // UI Declaration
  346. // --------------
  347. // create a new UI item and return the new items ID.
  348. OUI_EXPORT int uiItem();
  349. // set an items state to frozen; the UI will not recurse into frozen items
  350. // when searching for hot or active items; subsequently, frozen items and
  351. // their child items will not cause mouse event notifications.
  352. // The frozen state is not applied recursively; uiGetState() will report
  353. // UI_COLD for child items. Upon encountering a frozen item, the drawing
  354. // routine needs to handle rendering of child items appropriately.
  355. // see example.cpp for a demonstration.
  356. OUI_EXPORT void uiSetFrozen(int item, int enable);
  357. // set the application-dependent handle of an item.
  358. // handle is an application defined 64-bit handle. If handle is NULL, the item
  359. // will not be interactive.
  360. OUI_EXPORT void uiSetHandle(int item, void *handle);
  361. // allocate space for application-dependent context data and assign it
  362. // as the handle to the item.
  363. // The memory of the pointer is managed by the UI context and released
  364. // upon the next call to uiClear()
  365. OUI_EXPORT void *uiAllocHandle(int item, int size);
  366. // set the handler callback for an interactive item.
  367. // flags is a combination of UI_EVENT_* and designates for which events the
  368. // handler should be called.
  369. OUI_EXPORT void uiSetHandler(int item, UIhandler handler, int flags);
  370. // assign an item to a container.
  371. // an item ID of 0 refers to the root item.
  372. // if child is already assigned to a parent, an assertion will be thrown.
  373. // the function returns the child item ID
  374. OUI_EXPORT int uiAppend(int item, int child);
  375. // set the size of the item; a size of 0 indicates the dimension to be
  376. // dynamic; if the size is set, the item can not expand beyond that size.
  377. OUI_EXPORT void uiSetSize(int item, int w, int h);
  378. // set the anchoring behavior of the item to one or multiple UIlayoutFlags
  379. OUI_EXPORT void uiSetLayout(int item, int flags);
  380. // set the left, top, right and bottom margins of an item; when the item is
  381. // anchored to the parent or another item, the margin controls the distance
  382. // from the neighboring element.
  383. OUI_EXPORT void uiSetMargins(int item, int l, int t, int r, int b);
  384. // anchor the item to another sibling within the same container, so that the
  385. // sibling is left to this item.
  386. OUI_EXPORT void uiSetRightTo(int item, int other);
  387. // anchor the item to another sibling within the same container, so that the
  388. // sibling is above this item.
  389. OUI_EXPORT void uiSetBelow(int item, int other);
  390. // anchor the item to another sibling within the same container, so that the
  391. // sibling is right to this item.
  392. OUI_EXPORT void uiSetLeftTo(int item, int other);
  393. // anchor the item to another sibling within the same container, so that the
  394. // sibling is below this item.
  395. OUI_EXPORT void uiSetAbove(int item, int other);
  396. // set item as recipient of all keyboard events; the item must have a handle
  397. // assigned; if item is -1, no item will be focused.
  398. OUI_EXPORT void uiFocus(int item);
  399. // Iteration
  400. // ---------
  401. // returns the first child item of a container item. If the item is not
  402. // a container or does not contain any items, -1 is returned.
  403. // if item is 0, the first child item of the root item will be returned.
  404. OUI_EXPORT int uiFirstChild(int item);
  405. // returns the last child item of a container item. If the item is not
  406. // a container or does not contain any items, -1 is returned.
  407. // if item is 0, the last child item of the root item will be returned.
  408. OUI_EXPORT int uiLastChild(int item);
  409. // returns an items parent container item.
  410. // if item is 0, -1 will be returned.
  411. OUI_EXPORT int uiParent(int item);
  412. // returns an items next sibling in the list of the parent containers children.
  413. // if item is 0 or the item is the last child item, -1 will be returned.
  414. OUI_EXPORT int uiNextSibling(int item);
  415. // returns an items previous sibling in the list of the parent containers
  416. // children.
  417. // if item is 0 or the item is the first child item, -1 will be returned.
  418. OUI_EXPORT int uiPrevSibling(int item);
  419. // Querying
  420. // --------
  421. // return the total number of allocated items
  422. OUI_EXPORT int uiGetItemCount();
  423. // return the current state of the item. This state is only valid after
  424. // a call to uiProcess().
  425. // The returned value is one of UI_COLD, UI_HOT, UI_ACTIVE, UI_FROZEN.
  426. OUI_EXPORT UIitemState uiGetState(int item);
  427. // return the application-dependent handle of the item as passed to uiSetHandle()
  428. // or uiAllocHandle().
  429. OUI_EXPORT void *uiGetHandle(int item);
  430. // return the item with the given application-dependent handle as assigned by
  431. // uiSetHandle() or -1 if unsuccessful.
  432. OUI_EXPORT int uiGetItem(void *handle);
  433. // return the item that is currently under the cursor or -1 for none
  434. OUI_EXPORT int uiGetHotItem();
  435. // return the item that is currently focused or -1 for none
  436. OUI_EXPORT int uiGetFocusedItem();
  437. // return the handler callback for an item as passed to uiSetHandler()
  438. OUI_EXPORT UIhandler uiGetHandler(int item);
  439. // return the handler flags for an item as passed to uiSetHandler()
  440. OUI_EXPORT int uiGetHandlerFlags(int item);
  441. // when handling a KEY_DOWN/KEY_UP event: the key that triggered this event
  442. OUI_EXPORT unsigned int uiGetKey();
  443. // when handling a KEY_DOWN/KEY_UP event: the key that triggered this event
  444. OUI_EXPORT unsigned int uiGetModifier();
  445. // when handling a PROPAGATE event; the original item firing this event
  446. OUI_EXPORT int uiGetEventItem();
  447. // returns the number of child items a container item contains. If the item
  448. // is not a container or does not contain any items, 0 is returned.
  449. // if item is 0, the child item count of the root item will be returned.
  450. OUI_EXPORT int uiGetChildCount(int item);
  451. // returns an items child index relative to its parent. If the item is the
  452. // first item, the return value is 0; If the item is the last item, the return
  453. // value is equivalent to uiGetChildCount(uiParent(item))-1.
  454. // if item is 0, 0 will be returned.
  455. OUI_EXPORT int uiGetChildId(int item);
  456. // returns the items layout rectangle relative to the parent. If uiGetRect()
  457. // is called before uiLayout(), the values of the returned rectangle are
  458. // undefined.
  459. OUI_EXPORT UIrect uiGetRect(int item);
  460. // returns the items layout rectangle in absolute coordinates. If
  461. // uiGetAbsoluteRect() is called before uiLayout(), the values of the returned
  462. // rectangle are undefined.
  463. // This function has complexity O(N) for N parents
  464. OUI_EXPORT UIrect uiGetAbsoluteRect(int item);
  465. // returns 1 if an items absolute rectangle contains a given coordinate
  466. // otherwise 0
  467. OUI_EXPORT int uiContains(int item, int x, int y);
  468. // when called from an input event handler, returns the active items absolute
  469. // layout rectangle. If uiGetActiveRect() is called outside of a handler,
  470. // the values of the returned rectangle are undefined.
  471. OUI_EXPORT UIrect uiGetActiveRect();
  472. // return the width of the item as set by uiSetSize()
  473. OUI_EXPORT int uiGetWidth(int item);
  474. // return the height of the item as set by uiSetSize()
  475. OUI_EXPORT int uiGetHeight(int item);
  476. // return the anchoring behavior as set by uiSetLayout()
  477. OUI_EXPORT int uiGetLayout(int item);
  478. // return the left margin of the item as set with uiSetMargins()
  479. OUI_EXPORT int uiGetMarginLeft(int item);
  480. // return the top margin of the item as set with uiSetMargins()
  481. OUI_EXPORT int uiGetMarginTop(int item);
  482. // return the right margin of the item as set with uiSetMargins()
  483. OUI_EXPORT int uiGetMarginRight(int item);
  484. // return the bottom margin of the item as set with uiSetMargins()
  485. OUI_EXPORT int uiGetMarginDown(int item);
  486. // return the items anchored sibling as assigned with uiSetRightTo()
  487. // or -1 if not set.
  488. OUI_EXPORT int uiGetRightTo(int item);
  489. // return the items anchored sibling as assigned with uiSetBelow()
  490. // or -1 if not set.
  491. OUI_EXPORT int uiGetBelow(int item);
  492. // return the items anchored sibling as assigned with uiSetLeftTo()
  493. // or -1 if not set.
  494. OUI_EXPORT int uiGetLeftTo(int item);
  495. // return the items anchored sibling as assigned with uiSetAbove()
  496. // or -1 if not set.
  497. OUI_EXPORT int uiGetAbove(int item);
  498. #ifdef __cplusplus
  499. };
  500. #endif
  501. #endif // _OUI_H_
  502. #ifdef OUI_IMPLEMENTATION
  503. #include <assert.h>
  504. #ifdef _MSC_VER
  505. #pragma warning (disable: 4996) // Switch off security warnings
  506. #pragma warning (disable: 4100) // Switch off unreferenced formal parameter warnings
  507. #ifdef __cplusplus
  508. #define UI_INLINE inline
  509. #else
  510. #define UI_INLINE
  511. #endif
  512. #else
  513. #define UI_INLINE inline
  514. #endif
  515. #define UI_MAX_KIND 16
  516. #define UI_ANY_BUTTON0_INPUT (UI_BUTTON0_DOWN \
  517. |UI_BUTTON0_UP \
  518. |UI_BUTTON0_HOT_UP \
  519. |UI_BUTTON0_CAPTURE)
  520. #define UI_ANY_BUTTON2_INPUT (UI_BUTTON2_DOWN)
  521. #define UI_ANY_MOUSE_INPUT (UI_ANY_BUTTON0_INPUT \
  522. |UI_ANY_BUTTON2_INPUT)
  523. #define UI_ANY_KEY_INPUT (UI_KEY_DOWN \
  524. |UI_KEY_UP \
  525. |UI_CHAR)
  526. #define UI_ANY_INPUT (UI_ANY_MOUSE_INPUT \
  527. |UI_ANY_KEY_INPUT)
  528. #define UI_ITEM_VISITED_XY_FLAG(X) (1<<(UI_ITEM_VISITED_BITOFS+(X)))
  529. #define UI_ITEM_VISITED_WH_FLAG(X) (4<<(UI_ITEM_VISITED_BITOFS+(X)))
  530. // extra item flags
  531. enum {
  532. UI_ITEM_LAYOUT_MASK = 0x000F,
  533. UI_ITEM_EVENT_MASK = 0xFFF0,
  534. // item is frozen
  535. UI_ITEM_FROZEN = 0x10000,
  536. // item handle is pointer to data
  537. UI_ITEM_DATA = 0x20000,
  538. UI_ITEM_VISITED_BITOFS = 18, // 0x4 0000, 0x8 0000, 0x10 0000, 0x20 0000
  539. UI_ITEM_VISITED_MASK = (UI_ITEM_VISITED_XY_FLAG(0)
  540. | UI_ITEM_VISITED_XY_FLAG(1)
  541. | UI_ITEM_VISITED_WH_FLAG(0)
  542. | UI_ITEM_VISITED_WH_FLAG(1)),
  543. };
  544. typedef struct UIitem {
  545. // declaration independent unique handle (for persistence)
  546. void *handle;
  547. // handler
  548. UIhandler handler;
  549. unsigned int flags;
  550. // container structure
  551. // number of kids
  552. int numkids;
  553. // index of first kid
  554. int firstkid;
  555. // index of last kid
  556. int lastkid;
  557. // child structure
  558. // parent item
  559. int parent;
  560. // index of kid relative to parent
  561. int kidid;
  562. // index of next sibling with same parent
  563. int nextitem;
  564. // index of previous sibling with same parent
  565. int previtem;
  566. // size
  567. UIvec2 size;
  568. // margin offsets, interpretation depends on flags
  569. int margins[4];
  570. // neighbors to position borders to
  571. int relto[4];
  572. // computed size
  573. UIvec2 computed_size;
  574. // relative rect
  575. UIrect rect;
  576. } UIitem;
  577. // 40 bytes
  578. typedef struct UIitem2 {
  579. // declaration independent unique handle (for persistence)
  580. void *handle;
  581. // handler
  582. UIhandler handler;
  583. // flags: unifies: 4 layout bits, 11 event bits, 1 frozen bit, 2 visited bits
  584. // 2 new layout bits: rect w/h is fixed
  585. int flags;
  586. // container structure
  587. // index of first kid
  588. int firstkid;
  589. // index of next sibling with same parent
  590. int nextitem;
  591. // margin offsets, orientation/interpretation depends on layout flags
  592. short margins[2];
  593. // relative / absolute offset
  594. short offset[2];
  595. // measured / fixed size
  596. short size[2];
  597. } UIitem2;
  598. typedef enum UIstate {
  599. UI_STATE_IDLE = 0,
  600. UI_STATE_CAPTURE,
  601. } UIstate;
  602. typedef struct UIhandleEntry {
  603. unsigned int key;
  604. int item;
  605. } UIhandleEntry;
  606. typedef struct UIinputEvent {
  607. unsigned int key;
  608. unsigned int mod;
  609. UIevent event;
  610. } UIinputEvent;
  611. struct UIcontext {
  612. // button state in this frame
  613. unsigned long long buttons;
  614. // button state in the previous frame
  615. unsigned long long last_buttons;
  616. // where the cursor was at the beginning of the active state
  617. UIvec2 start_cursor;
  618. // where the cursor was last frame
  619. UIvec2 last_cursor;
  620. // where the cursor is currently
  621. UIvec2 cursor;
  622. // accumulated scroll wheel offsets
  623. UIvec2 scroll;
  624. void *hot_handle;
  625. void *active_handle;
  626. void *focus_handle;
  627. void *last_click_handle;
  628. UIrect hot_rect;
  629. UIrect active_rect;
  630. UIstate state;
  631. int hot_item;
  632. unsigned int active_key;
  633. unsigned int active_modifier;
  634. int event_item;
  635. int last_timestamp;
  636. int last_click_timestamp;
  637. int clicks;
  638. int count;
  639. int datasize;
  640. int eventcount;
  641. UIitem items[UI_MAX_ITEMS];
  642. unsigned char data[UI_MAX_BUFFERSIZE];
  643. UIhandleEntry handles[UI_MAX_ITEMS];
  644. UIinputEvent events[UI_MAX_INPUT_EVENTS];
  645. };
  646. UI_INLINE int ui_max(int a, int b) {
  647. return (a>b)?a:b;
  648. }
  649. UI_INLINE int ui_min(int a, int b) {
  650. return (a<b)?a:b;
  651. }
  652. static UIcontext *ui_context = NULL;
  653. UIcontext *uiCreateContext() {
  654. UIcontext *ctx = (UIcontext *)malloc(sizeof(UIcontext));
  655. memset(ctx, 0, sizeof(UIcontext));
  656. UIcontext *oldctx = ui_context;
  657. uiMakeCurrent(ctx);
  658. uiClear();
  659. uiMakeCurrent(oldctx);
  660. return ctx;
  661. }
  662. void uiMakeCurrent(UIcontext *ctx) {
  663. ui_context = ctx;
  664. }
  665. void uiDestroyContext(UIcontext *ctx) {
  666. if (ui_context == ctx)
  667. uiMakeCurrent(NULL);
  668. free(ctx);
  669. }
  670. UI_INLINE unsigned int uiHashHandle(void *handle) {
  671. unsigned long long uval = (unsigned long long)handle;
  672. uval = (uval+(uval>>32)) & 0xffffffff;
  673. unsigned int x = (unsigned int)uval;
  674. x += (x>>6)+(x>>19);
  675. x += x<<16;
  676. x ^= x<<3;
  677. x += x>>5;
  678. x ^= x<<2;
  679. x += x>>15;
  680. x ^= x<<10;
  681. return x?x:1; // must not be zero
  682. }
  683. UI_INLINE unsigned int uiHashProbeDistance(unsigned int key, unsigned int slot_index) {
  684. unsigned int pos = key & (UI_MAX_ITEMS-1);
  685. return (slot_index + UI_MAX_ITEMS - pos) & (UI_MAX_ITEMS-1);
  686. }
  687. UI_INLINE UIhandleEntry *uiHashLookupHandle(unsigned int key) {
  688. assert(ui_context);
  689. int pos = key & (UI_MAX_ITEMS-1);
  690. unsigned int dist = 0;
  691. for (;;) {
  692. UIhandleEntry *entry = ui_context->handles + pos;
  693. unsigned int pos_key = entry->key;
  694. if (!pos_key) return NULL;
  695. else if (entry->key == key)
  696. return entry;
  697. else if (dist > uiHashProbeDistance(pos_key, pos))
  698. return NULL;
  699. pos = (pos+1) & (UI_MAX_ITEMS-1);
  700. ++dist;
  701. }
  702. }
  703. int uiGetItem(void *handle) {
  704. unsigned int key = uiHashHandle(handle);
  705. UIhandleEntry *e = uiHashLookupHandle(key);
  706. return e?(e->item):-1;
  707. }
  708. static void uiHashInsertHandle(void *handle, int item) {
  709. unsigned int key = uiHashHandle(handle);
  710. UIhandleEntry *e = uiHashLookupHandle(key);
  711. if (e) { // update
  712. e->item = item;
  713. return;
  714. }
  715. int pos = key & (UI_MAX_ITEMS-1);
  716. unsigned int dist = 0;
  717. for (unsigned int i = 0; i < UI_MAX_ITEMS; ++i) {
  718. int index = (pos + i) & (UI_MAX_ITEMS-1);
  719. unsigned int pos_key = ui_context->handles[index].key;
  720. if (!pos_key) {
  721. ui_context->handles[index].key = key;
  722. ui_context->handles[index].item = item;
  723. break;
  724. } else {
  725. unsigned int probe_distance = uiHashProbeDistance(pos_key, index);
  726. if (dist > probe_distance) {
  727. unsigned int oldkey = ui_context->handles[index].key;
  728. unsigned int olditem = ui_context->handles[index].item;
  729. ui_context->handles[index].key = key;
  730. ui_context->handles[index].item = item;
  731. key = oldkey;
  732. item = olditem;
  733. dist = probe_distance;
  734. }
  735. }
  736. ++dist;
  737. }
  738. }
  739. void uiSetButton(int button, int enabled) {
  740. assert(ui_context);
  741. unsigned long long mask = 1ull<<button;
  742. // set new bit
  743. ui_context->buttons = (enabled)?
  744. (ui_context->buttons | mask):
  745. (ui_context->buttons & ~mask);
  746. }
  747. static void uiAddInputEvent(UIinputEvent event) {
  748. assert(ui_context);
  749. if (ui_context->eventcount == UI_MAX_INPUT_EVENTS) return;
  750. ui_context->events[ui_context->eventcount++] = event;
  751. }
  752. static void uiClearInputEvents() {
  753. assert(ui_context);
  754. ui_context->eventcount = 0;
  755. ui_context->scroll.x = 0;
  756. ui_context->scroll.y = 0;
  757. }
  758. void uiSetKey(unsigned int key, unsigned int mod, int enabled) {
  759. assert(ui_context);
  760. UIinputEvent event = { key, mod, enabled?UI_KEY_DOWN:UI_KEY_UP };
  761. uiAddInputEvent(event);
  762. }
  763. void uiSetChar(unsigned int value) {
  764. assert(ui_context);
  765. UIinputEvent event = { value, 0, UI_CHAR };
  766. uiAddInputEvent(event);
  767. }
  768. void uiSetScroll(int x, int y) {
  769. assert(ui_context);
  770. ui_context->scroll.x += x;
  771. ui_context->scroll.y += y;
  772. }
  773. UIvec2 uiGetScroll() {
  774. assert(ui_context);
  775. return ui_context->scroll;
  776. }
  777. int uiGetLastButton(int button) {
  778. assert(ui_context);
  779. return (ui_context->last_buttons & (1ull<<button))?1:0;
  780. }
  781. int uiGetButton(int button) {
  782. assert(ui_context);
  783. return (ui_context->buttons & (1ull<<button))?1:0;
  784. }
  785. int uiButtonPressed(int button) {
  786. assert(ui_context);
  787. return !uiGetLastButton(button) && uiGetButton(button);
  788. }
  789. int uiButtonReleased(int button) {
  790. assert(ui_context);
  791. return uiGetLastButton(button) && !uiGetButton(button);
  792. }
  793. void uiSetCursor(int x, int y) {
  794. assert(ui_context);
  795. ui_context->cursor.x = x;
  796. ui_context->cursor.y = y;
  797. }
  798. UIvec2 uiGetCursor() {
  799. assert(ui_context);
  800. return ui_context->cursor;
  801. }
  802. UIvec2 uiGetCursorStart() {
  803. assert(ui_context);
  804. return ui_context->start_cursor;
  805. }
  806. UIvec2 uiGetCursorDelta() {
  807. assert(ui_context);
  808. UIvec2 result = {{{
  809. ui_context->cursor.x - ui_context->last_cursor.x,
  810. ui_context->cursor.y - ui_context->last_cursor.y
  811. }}};
  812. return result;
  813. }
  814. UIvec2 uiGetCursorStartDelta() {
  815. assert(ui_context);
  816. UIvec2 result = {{{
  817. ui_context->cursor.x - ui_context->start_cursor.x,
  818. ui_context->cursor.y - ui_context->start_cursor.y
  819. }}};
  820. return result;
  821. }
  822. unsigned int uiGetKey() {
  823. assert(ui_context);
  824. return ui_context->active_key;
  825. }
  826. unsigned int uiGetModifier() {
  827. assert(ui_context);
  828. return ui_context->active_modifier;
  829. }
  830. int uiGetEventItem() {
  831. return ui_context->event_item;
  832. }
  833. // return the total number of allocated items
  834. OUI_EXPORT int uiGetItemCount() {
  835. assert(ui_context);
  836. return ui_context->count;
  837. }
  838. UIitem *uiItemPtr(int item) {
  839. assert(ui_context && (item >= 0) && (item < ui_context->count));
  840. return ui_context->items + item;
  841. }
  842. int uiGetHotItem() {
  843. assert(ui_context);
  844. return ui_context->hot_item;
  845. }
  846. void uiFocus(int item) {
  847. assert(ui_context && (item >= -1) && (item < ui_context->count));
  848. ui_context->focus_handle = (item < 0)?0:uiGetHandle(item);
  849. }
  850. int uiGetFocusedItem() {
  851. assert(ui_context);
  852. return ui_context->focus_handle?uiGetItem(ui_context->focus_handle):-1;
  853. }
  854. void uiClear() {
  855. assert(ui_context);
  856. ui_context->count = 0;
  857. ui_context->datasize = 0;
  858. ui_context->hot_item = -1;
  859. memset(ui_context->handles, 0, sizeof(ui_context->handles));
  860. }
  861. void uiClearHandleState() {
  862. assert(ui_context);
  863. ui_context->hot_handle = NULL;
  864. ui_context->active_handle = NULL;
  865. ui_context->focus_handle = NULL;
  866. ui_context->last_click_handle = NULL;
  867. }
  868. int uiItem() {
  869. assert(ui_context);
  870. assert(ui_context->count < UI_MAX_ITEMS);
  871. int idx = ui_context->count++;
  872. UIitem *item = uiItemPtr(idx);
  873. memset(item, 0, sizeof(UIitem));
  874. item->parent = -1;
  875. item->firstkid = -1;
  876. item->lastkid = -1;
  877. item->nextitem = -1;
  878. item->previtem = -1;
  879. for (int i = 0; i < 4; ++i)
  880. item->relto[i] = -1;
  881. return idx;
  882. }
  883. void uiNotifyAllItems(UIevent event) {
  884. assert(ui_context);
  885. assert((event & UI_ITEM_EVENT_MASK) == event);
  886. for (int i = 0; i < ui_context->count; ++i) {
  887. UIitem *pitem = ui_context->items + i;
  888. if (pitem->handler && (pitem->flags & event)) {
  889. pitem->handler(i, event);
  890. }
  891. }
  892. }
  893. void uiNotifyItem(int item, UIevent event) {
  894. assert(ui_context);
  895. assert((event & UI_ITEM_EVENT_MASK) == event);
  896. ui_context->event_item = item;
  897. while (item >= 0) {
  898. UIitem *pitem = uiItemPtr(item);
  899. if (pitem->handler && (pitem->flags & event)) {
  900. pitem->handler(item, event);
  901. }
  902. if (!(pitem->flags & UI_PROPAGATE))
  903. break;
  904. item = uiParent(item);
  905. }
  906. }
  907. int uiAppend(int item, int child) {
  908. assert(child > 0);
  909. assert(uiParent(child) == -1);
  910. UIitem *pitem = uiItemPtr(child);
  911. UIitem *pparent = uiItemPtr(item);
  912. pitem->parent = item;
  913. pitem->kidid = pparent->numkids++;
  914. if (pparent->lastkid < 0) {
  915. pparent->firstkid = child;
  916. pparent->lastkid = child;
  917. } else {
  918. pitem->previtem = pparent->lastkid;
  919. uiItemPtr(pparent->lastkid)->nextitem = child;
  920. pparent->lastkid = child;
  921. }
  922. return child;
  923. }
  924. void uiSetFrozen(int item, int enable) {
  925. UIitem *pitem = uiItemPtr(item);
  926. if (enable)
  927. pitem->flags |= UI_ITEM_FROZEN;
  928. else
  929. pitem->flags &= ~UI_ITEM_FROZEN;
  930. }
  931. void uiSetSize(int item, int w, int h) {
  932. UIitem *pitem = uiItemPtr(item);
  933. pitem->size.x = w;
  934. pitem->size.y = h;
  935. }
  936. int uiGetWidth(int item) {
  937. return uiItemPtr(item)->size.x;
  938. }
  939. int uiGetHeight(int item) {
  940. return uiItemPtr(item)->size.y;
  941. }
  942. void uiSetLayout(int item, int flags) {
  943. uiItemPtr(item)->flags |= flags & UI_ITEM_LAYOUT_MASK;
  944. }
  945. int uiGetLayout(int item) {
  946. return uiItemPtr(item)->flags & UI_ITEM_LAYOUT_MASK;
  947. }
  948. void uiSetMargins(int item, int l, int t, int r, int b) {
  949. UIitem *pitem = uiItemPtr(item);
  950. pitem->margins[0] = l;
  951. pitem->margins[1] = t;
  952. pitem->margins[2] = r;
  953. pitem->margins[3] = b;
  954. }
  955. int uiGetMarginLeft(int item) {
  956. return uiItemPtr(item)->margins[0];
  957. }
  958. int uiGetMarginTop(int item) {
  959. return uiItemPtr(item)->margins[1];
  960. }
  961. int uiGetMarginRight(int item) {
  962. return uiItemPtr(item)->margins[2];
  963. }
  964. int uiGetMarginDown(int item) {
  965. return uiItemPtr(item)->margins[3];
  966. }
  967. void uiSetRightTo(int item, int other) {
  968. assert((other < 0) || (uiParent(other) == uiParent(item)));
  969. uiItemPtr(item)->relto[0] = other;
  970. }
  971. int uiGetRightTo(int item) {
  972. return uiItemPtr(item)->relto[0];
  973. }
  974. void uiSetBelow(int item, int other) {
  975. assert((other < 0) || (uiParent(other) == uiParent(item)));
  976. uiItemPtr(item)->relto[1] = other;
  977. }
  978. int uiGetBelow(int item) {
  979. return uiItemPtr(item)->relto[1];
  980. }
  981. void uiSetLeftTo(int item, int other) {
  982. assert((other < 0) || (uiParent(other) == uiParent(item)));
  983. uiItemPtr(item)->relto[2] = other;
  984. }
  985. int uiGetLeftTo(int item) {
  986. return uiItemPtr(item)->relto[2];
  987. }
  988. void uiSetAbove(int item, int other) {
  989. assert((other < 0) || (uiParent(other) == uiParent(item)));
  990. uiItemPtr(item)->relto[3] = other;
  991. }
  992. int uiGetAbove(int item) {
  993. return uiItemPtr(item)->relto[3];
  994. }
  995. UI_INLINE void uiComputeChainSize(UIitem *pkid,
  996. int *need_size, int *hard_size, int dim) {
  997. UIitem *pitem = pkid;
  998. int wdim = dim+2;
  999. int size = pitem->rect.v[wdim] + pitem->margins[dim] + pitem->margins[wdim];
  1000. *need_size = size;
  1001. *hard_size = pitem->size.v[dim]?size:0;
  1002. int it = 0;
  1003. pitem->flags |= UI_ITEM_VISITED_XY_FLAG(dim);
  1004. // traverse along left neighbors
  1005. while (((pitem->flags&UI_ITEM_LAYOUT_MASK)>>dim) & UI_LEFT) {
  1006. if (pitem->relto[dim] < 0) break;
  1007. pitem = uiItemPtr(pitem->relto[dim]);
  1008. pitem->flags |= UI_ITEM_VISITED_XY_FLAG(dim);
  1009. size = pitem->rect.v[wdim] + pitem->margins[dim] + pitem->margins[wdim];
  1010. *need_size = (*need_size) + size;
  1011. *hard_size = (*hard_size) + (pitem->size.v[dim]?size:0);
  1012. it++;
  1013. assert(it<1000000); // infinite loop
  1014. }
  1015. // traverse along right neighbors
  1016. pitem = pkid;
  1017. it = 0;
  1018. while (((pitem->flags&UI_ITEM_LAYOUT_MASK)>>dim) & UI_RIGHT) {
  1019. if (pitem->relto[wdim] < 0) break;
  1020. pitem = uiItemPtr(pitem->relto[wdim]);
  1021. pitem->flags |= UI_ITEM_VISITED_XY_FLAG(dim);
  1022. size = pitem->rect.v[wdim] + pitem->margins[dim] + pitem->margins[wdim];
  1023. *need_size = (*need_size) + size;
  1024. *hard_size = (*hard_size) + (pitem->size.v[dim]?size:0);
  1025. it++;
  1026. assert(it<1000000); // infinite loop
  1027. }
  1028. }
  1029. UI_INLINE void uiComputeSizeDim(UIitem *pitem, int dim) {
  1030. int wdim = dim+2;
  1031. int need_size = 0;
  1032. int hard_size = 0;
  1033. int kid = pitem->firstkid;
  1034. while (kid >= 0) {
  1035. UIitem *pkid = uiItemPtr(kid);
  1036. if (!(pkid->flags & UI_ITEM_VISITED_XY_FLAG(dim))) {
  1037. int ns,hs;
  1038. uiComputeChainSize(pkid, &ns, &hs, dim);
  1039. need_size = ui_max(need_size, ns);
  1040. hard_size = ui_max(hard_size, hs);
  1041. }
  1042. kid = uiNextSibling(kid);
  1043. }
  1044. pitem->computed_size.v[dim] = hard_size;
  1045. if (pitem->size.v[dim]) {
  1046. pitem->rect.v[wdim] = pitem->size.v[dim];
  1047. } else {
  1048. pitem->rect.v[wdim] = need_size;
  1049. }
  1050. }
  1051. static void uiComputeBestSize(int item, int dim) {
  1052. UIitem *pitem = uiItemPtr(item);
  1053. pitem->flags &= ~UI_ITEM_VISITED_MASK;
  1054. // children expand the size
  1055. int kid = uiFirstChild(item);
  1056. while (kid >= 0) {
  1057. uiComputeBestSize(kid, dim);
  1058. kid = uiNextSibling(kid);
  1059. }
  1060. uiComputeSizeDim(pitem, dim);
  1061. }
  1062. static void uiLayoutChildItem(UIitem *pparent, UIitem *pitem,
  1063. int *dyncount, int *consumed_space, int dim) {
  1064. if (pitem->flags & UI_ITEM_VISITED_WH_FLAG(dim)) return;
  1065. pitem->flags |= UI_ITEM_VISITED_WH_FLAG(dim);
  1066. int wdim = dim+2;
  1067. int x = 0;
  1068. int s = pparent->rect.v[wdim];
  1069. int flags = (pitem->flags & UI_ITEM_LAYOUT_MASK) >> dim;
  1070. int hasl = (flags & UI_LEFT) && (pitem->relto[dim] >= 0);
  1071. int hasr = (flags & UI_RIGHT) && (pitem->relto[wdim] >= 0);
  1072. if ((flags & UI_HFILL) != UI_HFILL) {
  1073. *consumed_space = (*consumed_space)
  1074. + pitem->rect.v[wdim]
  1075. + pitem->margins[wdim]
  1076. + pitem->margins[dim];
  1077. } else if (!pitem->size.v[dim]) {
  1078. *dyncount = (*dyncount)+1;
  1079. }
  1080. if (hasl) {
  1081. UIitem *pl = uiItemPtr(pitem->relto[dim]);
  1082. uiLayoutChildItem(pparent, pl, dyncount, consumed_space, dim);
  1083. x = pl->rect.v[dim]+pl->rect.v[wdim]+pl->margins[wdim];
  1084. s -= x;
  1085. }
  1086. if (hasr) {
  1087. UIitem *pl = uiItemPtr(pitem->relto[wdim]);
  1088. uiLayoutChildItem(pparent, pl, dyncount, consumed_space, dim);
  1089. s = pl->rect.v[dim]-pl->margins[dim]-x;
  1090. }
  1091. switch(flags & UI_HFILL) {
  1092. default:
  1093. case UI_HCENTER: {
  1094. pitem->rect.v[dim] = x+(s-pitem->rect.v[wdim])/2+pitem->margins[dim];
  1095. } break;
  1096. case UI_LEFT: {
  1097. pitem->rect.v[dim] = x+pitem->margins[dim];
  1098. } break;
  1099. case UI_RIGHT: {
  1100. pitem->rect.v[dim] = x+s-pitem->rect.v[wdim]-pitem->margins[wdim];
  1101. } break;
  1102. case UI_HFILL: {
  1103. if (pitem->size.v[dim]) { // hard maximum size; can't stretch
  1104. if (!hasl)
  1105. pitem->rect.v[dim] = x+pitem->margins[dim];
  1106. else
  1107. pitem->rect.v[dim] = x+s-pitem->rect.v[wdim]-pitem->margins[wdim];
  1108. } else {
  1109. if (1) { //!pitem->rect.v[wdim]) {
  1110. //int width = (pparent->rect.v[wdim] - pparent->computed_size.v[dim]);
  1111. int width = (pparent->rect.v[wdim] - (*consumed_space));
  1112. int space = width / (*dyncount);
  1113. //int rest = width - space*(*dyncount);
  1114. if (!hasl) {
  1115. pitem->rect.v[dim] = x+pitem->margins[dim];
  1116. pitem->rect.v[wdim] = s-pitem->margins[dim]-pitem->margins[wdim];
  1117. } else {
  1118. pitem->rect.v[wdim] = space-pitem->margins[dim]-pitem->margins[wdim];
  1119. pitem->rect.v[dim] = x+s-pitem->rect.v[wdim]-pitem->margins[wdim];
  1120. }
  1121. } else {
  1122. pitem->rect.v[dim] = x+pitem->margins[dim];
  1123. pitem->rect.v[wdim] = s-pitem->margins[dim]-pitem->margins[wdim];
  1124. }
  1125. }
  1126. } break;
  1127. }
  1128. }
  1129. UI_INLINE void uiLayoutItemDim(UIitem *pitem, int dim) {
  1130. //int wdim = dim+2;
  1131. int kid = pitem->firstkid;
  1132. int consumed_space = 0;
  1133. int dyncount = 0;
  1134. while (kid >= 0) {
  1135. UIitem *pkid = uiItemPtr(kid);
  1136. uiLayoutChildItem(pitem, pkid, &dyncount, &consumed_space, dim);
  1137. kid = uiNextSibling(kid);
  1138. }
  1139. }
  1140. static void uiLayoutItem(int item, int dim) {
  1141. UIitem *pitem = uiItemPtr(item);
  1142. uiLayoutItemDim(pitem, dim);
  1143. int kid = uiFirstChild(item);
  1144. while (kid >= 0) {
  1145. uiLayoutItem(kid, dim);
  1146. kid = uiNextSibling(kid);
  1147. }
  1148. }
  1149. UIrect uiGetRect(int item) {
  1150. return uiItemPtr(item)->rect;
  1151. }
  1152. UIrect uiGetActiveRect() {
  1153. assert(ui_context);
  1154. return ui_context->active_rect;
  1155. }
  1156. int uiFirstChild(int item) {
  1157. return uiItemPtr(item)->firstkid;
  1158. }
  1159. int uiLastChild(int item) {
  1160. return uiItemPtr(item)->lastkid;
  1161. }
  1162. int uiNextSibling(int item) {
  1163. return uiItemPtr(item)->nextitem;
  1164. }
  1165. int uiPrevSibling(int item) {
  1166. return uiItemPtr(item)->previtem;
  1167. }
  1168. int uiParent(int item) {
  1169. return uiItemPtr(item)->parent;
  1170. }
  1171. void *uiAllocHandle(int item, int size) {
  1172. assert((size > 0) && (size < UI_MAX_DATASIZE));
  1173. UIitem *pitem = uiItemPtr(item);
  1174. assert(pitem->handle == NULL);
  1175. assert((ui_context->datasize+size) <= UI_MAX_BUFFERSIZE);
  1176. pitem->handle = ui_context->data + ui_context->datasize;
  1177. pitem->flags |= UI_ITEM_DATA;
  1178. ui_context->datasize += size;
  1179. uiHashInsertHandle(pitem->handle, item);
  1180. return pitem->handle;
  1181. }
  1182. void uiSetHandle(int item, void *handle) {
  1183. UIitem *pitem = uiItemPtr(item);
  1184. assert(pitem->handle == NULL);
  1185. pitem->handle = handle;
  1186. if (handle) {
  1187. uiHashInsertHandle(handle, item);
  1188. }
  1189. }
  1190. void *uiGetHandle(int item) {
  1191. return uiItemPtr(item)->handle;
  1192. }
  1193. void uiSetHandler(int item, UIhandler handler, int flags) {
  1194. UIitem *pitem = uiItemPtr(item);
  1195. pitem->handler = handler;
  1196. pitem->flags &= ~UI_ITEM_EVENT_MASK;
  1197. pitem->flags |= flags;
  1198. }
  1199. UIhandler uiGetHandler(int item) {
  1200. return uiItemPtr(item)->handler;
  1201. }
  1202. int uiGetHandlerFlags(int item) {
  1203. return uiItemPtr(item)->flags & UI_ITEM_EVENT_MASK;
  1204. }
  1205. int uiGetChildId(int item) {
  1206. return uiItemPtr(item)->kidid;
  1207. }
  1208. int uiGetChildCount(int item) {
  1209. return uiItemPtr(item)->numkids;
  1210. }
  1211. UIrect uiGetAbsoluteRect(int item) {
  1212. UIrect rect = uiGetRect(item);
  1213. item = uiParent(item);
  1214. while (item >= 0) {
  1215. rect.x += uiItemPtr(item)->rect.x;
  1216. rect.y += uiItemPtr(item)->rect.y;
  1217. item = uiParent(item);
  1218. }
  1219. return rect;
  1220. }
  1221. int uiContains(int item, int x, int y) {
  1222. UIrect rect = uiGetAbsoluteRect(item);
  1223. x -= rect.x;
  1224. y -= rect.y;
  1225. if ((x>=0)
  1226. && (y>=0)
  1227. && (x<rect.w)
  1228. && (y<rect.h)) return 1;
  1229. return 0;
  1230. }
  1231. int uiFindItemForEvent(int item, UIevent event,
  1232. UIrect *hot_rect,
  1233. int x, int y, int ox, int oy) {
  1234. UIitem *pitem = uiItemPtr(item);
  1235. if (pitem->flags & UI_ITEM_FROZEN) return -1;
  1236. UIrect rect = pitem->rect;
  1237. x -= rect.x;
  1238. y -= rect.y;
  1239. ox += rect.x;
  1240. oy += rect.y;
  1241. if ((x>=0)
  1242. && (y>=0)
  1243. && (x<rect.w)
  1244. && (y<rect.h)) {
  1245. int kid = uiLastChild(item);
  1246. while (kid >= 0) {
  1247. int best_hit = uiFindItemForEvent(kid,
  1248. event,hot_rect,x,y,ox,oy);
  1249. if (best_hit >= 0) return best_hit;
  1250. kid = uiPrevSibling(kid);
  1251. }
  1252. // click-through if the item has no handler for this event
  1253. if (pitem->flags & event) {
  1254. rect.x = ox;
  1255. rect.y = oy;
  1256. if (hot_rect)
  1257. *hot_rect = rect;
  1258. return item;
  1259. }
  1260. }
  1261. return -1;
  1262. }
  1263. int uiFindItem(int item, int x, int y, int ox, int oy) {
  1264. return uiFindItemForEvent(item, (UIevent)UI_ANY_MOUSE_INPUT,
  1265. &ui_context->hot_rect, x, y, ox, oy);
  1266. }
  1267. void uiLayout() {
  1268. assert(ui_context);
  1269. if (!ui_context->count) return;
  1270. // compute widths
  1271. uiComputeBestSize(0,0);
  1272. // position root element rect
  1273. uiItemPtr(0)->rect.x = uiItemPtr(0)->margins[0];
  1274. uiLayoutItem(0,0);
  1275. // give items a chance to adjust their height
  1276. uiNotifyAllItems(UI_ADJUST_HEIGHT);
  1277. // compute heights
  1278. uiComputeBestSize(0,1);
  1279. // position root element rect
  1280. uiItemPtr(0)->rect.y = uiItemPtr(0)->margins[1];
  1281. uiLayoutItem(0,1);
  1282. // drawing routines may require this to be set already
  1283. uiUpdateHotItem();
  1284. }
  1285. void uiUpdateHotItem() {
  1286. assert(ui_context);
  1287. if (!ui_context->count) return;
  1288. ui_context->hot_item = uiFindItem(0,
  1289. ui_context->cursor.x, ui_context->cursor.y, 0, 0);
  1290. }
  1291. int uiGetClicks() {
  1292. return ui_context->clicks;
  1293. }
  1294. void uiProcess(int timestamp) {
  1295. assert(ui_context);
  1296. if (!ui_context->count) {
  1297. uiClearInputEvents();
  1298. return;
  1299. }
  1300. int hot_item = uiGetItem(ui_context->hot_handle);
  1301. int active_item = uiGetItem(ui_context->active_handle);
  1302. int focus_item = uiGetItem(ui_context->focus_handle);
  1303. // send all keyboard events
  1304. if (focus_item >= 0) {
  1305. for (int i = 0; i < ui_context->eventcount; ++i) {
  1306. ui_context->active_key = ui_context->events[i].key;
  1307. ui_context->active_modifier = ui_context->events[i].mod;
  1308. uiNotifyItem(focus_item,
  1309. ui_context->events[i].event);
  1310. }
  1311. } else {
  1312. ui_context->focus_handle = 0;
  1313. }
  1314. if (ui_context->scroll.x || ui_context->scroll.y) {
  1315. int scroll_item = uiFindItemForEvent(0, UI_SCROLL, NULL,
  1316. ui_context->cursor.x, ui_context->cursor.y, 0, 0);
  1317. if (scroll_item >= 0) {
  1318. uiNotifyItem(scroll_item, UI_SCROLL);
  1319. }
  1320. }
  1321. uiClearInputEvents();
  1322. int hot = ui_context->hot_item;
  1323. switch(ui_context->state) {
  1324. default:
  1325. case UI_STATE_IDLE: {
  1326. ui_context->start_cursor = ui_context->cursor;
  1327. if (uiGetButton(0)) {
  1328. hot_item = -1;
  1329. active_item = hot;
  1330. ui_context->active_rect = ui_context->hot_rect;
  1331. if (active_item != focus_item) {
  1332. focus_item = -1;
  1333. ui_context->focus_handle = 0;
  1334. }
  1335. if (active_item >= 0) {
  1336. void *active_handle = uiGetHandle(active_item);
  1337. if (
  1338. ((timestamp - ui_context->last_click_timestamp) > UI_CLICK_THRESHOLD)
  1339. || (ui_context->last_click_handle != active_handle)) {
  1340. ui_context->clicks = 0;
  1341. }
  1342. ui_context->clicks++;
  1343. ui_context->last_click_timestamp = timestamp;
  1344. ui_context->last_click_handle = active_handle;
  1345. uiNotifyItem(active_item, UI_BUTTON0_DOWN);
  1346. }
  1347. ui_context->state = UI_STATE_CAPTURE;
  1348. } else if (uiGetButton(2) && !uiGetLastButton(2)) {
  1349. hot_item = -1;
  1350. hot = uiFindItemForEvent(0, UI_BUTTON2_DOWN,
  1351. &ui_context->active_rect,
  1352. ui_context->cursor.x, ui_context->cursor.y, 0, 0);
  1353. if (hot >= 0) {
  1354. uiNotifyItem(hot, UI_BUTTON2_DOWN);
  1355. }
  1356. } else {
  1357. hot_item = hot;
  1358. }
  1359. } break;
  1360. case UI_STATE_CAPTURE: {
  1361. if (!uiGetButton(0)) {
  1362. if (active_item >= 0) {
  1363. uiNotifyItem(active_item, UI_BUTTON0_UP);
  1364. if (active_item == hot) {
  1365. uiNotifyItem(active_item, UI_BUTTON0_HOT_UP);
  1366. }
  1367. }
  1368. active_item = -1;
  1369. ui_context->state = UI_STATE_IDLE;
  1370. } else {
  1371. if (active_item >= 0) {
  1372. uiNotifyItem(active_item, UI_BUTTON0_CAPTURE);
  1373. }
  1374. if (hot == active_item)
  1375. hot_item = hot;
  1376. else
  1377. hot_item = -1;
  1378. }
  1379. } break;
  1380. }
  1381. ui_context->last_cursor = ui_context->cursor;
  1382. ui_context->hot_handle = (hot_item>=0)?
  1383. uiGetHandle(hot_item):0;
  1384. ui_context->active_handle = (active_item>=0)?
  1385. uiGetHandle(active_item):0;
  1386. ui_context->last_timestamp = timestamp;
  1387. ui_context->last_buttons = ui_context->buttons;
  1388. }
  1389. static int uiIsActive(int item) {
  1390. assert(ui_context);
  1391. return (ui_context->active_handle)&&(uiGetHandle(item) == ui_context->active_handle);
  1392. }
  1393. static int uiIsHot(int item) {
  1394. assert(ui_context);
  1395. return (ui_context->hot_handle)&&(uiGetHandle(item) == ui_context->hot_handle);
  1396. }
  1397. static int uiIsFocused(int item) {
  1398. assert(ui_context);
  1399. return (ui_context->focus_handle)&&(uiGetHandle(item) == ui_context->focus_handle);
  1400. }
  1401. UIitemState uiGetState(int item) {
  1402. UIitem *pitem = uiItemPtr(item);
  1403. if (pitem->flags & UI_ITEM_FROZEN) return UI_FROZEN;
  1404. if (uiIsFocused(item)) {
  1405. if (pitem->flags & (UI_KEY_DOWN|UI_CHAR|UI_KEY_UP)) return UI_ACTIVE;
  1406. }
  1407. if (uiIsActive(item)) {
  1408. if (pitem->flags & (UI_BUTTON0_CAPTURE|UI_BUTTON0_UP)) return UI_ACTIVE;
  1409. if ((pitem->flags & UI_BUTTON0_HOT_UP)
  1410. && uiIsHot(item)) return UI_ACTIVE;
  1411. return UI_COLD;
  1412. } else if (uiIsHot(item)) {
  1413. return UI_HOT;
  1414. }
  1415. return UI_COLD;
  1416. }
  1417. #endif // OUI_IMPLEMENTATION