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.

1635 lines
49KB

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