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.

817 lines
23KB

  1. /*
  2. OUI - A minimal 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 _UI_H_
  21. #define _UI_H_
  22. /*
  23. OUI (spoken like the french "oui" for "yes") is a single-header library for
  24. layouting GUI elements and handling their user input.
  25. OUI has no widget types; instead, it provides only one kind of element, "Items",
  26. which can be expanded to behave as containers, buttons, sliders, radio
  27. buttons, and so on.
  28. Together with a set of widget drawing routines it can be used to build flowing
  29. user interfaces; the intended use is for bootstrap situations where only basic
  30. UI services are needed.
  31. */
  32. // item states as returned by uiGetState()
  33. // the item is inactive
  34. #define UI_COLD 0x0000
  35. // the item is inactive, but the cursor is hovering over this item
  36. #define UI_HOT 0x0001
  37. // the item is toggled or activated (depends on item kind)
  38. #define UI_ACTIVE 0x0002
  39. // the item is unresponsive
  40. #define UI_FROZEN 0x0003
  41. // maximum number of items that may be added
  42. #define UI_MAX_ITEMS 4096
  43. // maximum size in bytes reserved for storage of application dependent data
  44. // as passed to uiAllocData().
  45. #define UI_MAX_BUFFERSIZE 1048576
  46. // maximum size in bytes of a single data buffer passed to uiAllocData().
  47. #define UI_MAX_DATASIZE 4096
  48. // maximum depth of nested containers
  49. #define UI_MAX_DEPTH 64
  50. typedef unsigned int UIuint;
  51. // opaque UI context
  52. typedef struct UIcontext UIcontext;
  53. // application defined context handle
  54. typedef unsigned long long UIhandle;
  55. // layout flags
  56. typedef enum UIlayoutFlags {
  57. UI_LEFT = 1,
  58. UI_TOP = 2,
  59. UI_RIGHT = 4,
  60. UI_DOWN = 8,
  61. UI_HFILL = 5,
  62. UI_VFILL = 10,
  63. UI_HCENTER = 0,
  64. UI_VCENTER = 0,
  65. UI_CENTER = 0,
  66. UI_FILL = 15,
  67. } UIlayoutFlags;
  68. // event flags
  69. typedef enum UIevent {
  70. // on button 0 down
  71. UI_BUTTON0_DOWN = 0x01,
  72. // on button 0 up
  73. UI_BUTTON0_UP = 0x02,
  74. // on button 0 up while item is hovered
  75. UI_BUTTON0_HOT_UP = 0x04,
  76. // item is being captured (button 0 constantly pressed)
  77. UI_BUTTON0_CAPTURE = 0x08,
  78. } UIevent;
  79. // handler callback; event is one of UI_EVENT_*
  80. typedef void (*UIhandler)(int item, UIevent event);
  81. // for cursor positions, mainly
  82. typedef struct UIvec2 {
  83. union {
  84. int v[2];
  85. struct { int x, y; };
  86. };
  87. } UIvec2;
  88. // layout rectangle
  89. typedef struct UIrect {
  90. union {
  91. int v[4];
  92. struct { int x, y, w, h; };
  93. };
  94. } UIrect;
  95. // unless declared otherwise, all operations have the complexity O(1).
  96. // create a new UI context; call uiMakeCurrent() to make this context the
  97. // current context.
  98. UIcontext *uiCreateContext();
  99. // select an UI context as the current context; a context must always be
  100. // selected before using any of the other UI functions
  101. void uiMakeCurrent(UIcontext *ctx);
  102. // release the memory of an UI context created with uiCreateContext(); if the
  103. // context is the current context, the current context will be set to NULL
  104. void uiDestroyContext(UIcontext *ctx);
  105. // sets a mouse or gamepad button as pressed/released
  106. // button is in the range 0..63 and maps to an application defined input
  107. // source.
  108. // enabled is 1 for pressed, 0 for released
  109. void uiSetButton(int button, int enabled);
  110. // returns the current state of an application dependent input button
  111. // as set by uiSetButton().
  112. // the function returns 1 if the button has been set to pressed, 0 for released.
  113. int uiGetButton(int button);
  114. // sets the current cursor position (usually belonging to a mouse) to the
  115. // screen coordinates at (x,y)
  116. void uiSetCursor(int x, int y);
  117. // returns the current cursor position in screen coordinates as set by
  118. // uiSetCursor()
  119. UIvec2 uiGetCursor();
  120. // returns the offset of the cursor relative to the last call to uiProcess()
  121. UIvec2 uiGetCursorDelta();
  122. // returns the offset of the cursor relative to the beginning point of a drag
  123. // operation.
  124. UIvec2 uiGetCursorStartDelta();
  125. // clear the item buffer; uiClear() should be called before each UI declaration
  126. // to avoid concatenation of the same UI multiple times.
  127. // After the call, all previously declared item IDs are invalid, and all
  128. // application dependent context data has been freed.
  129. void uiClear();
  130. // create a new UI item and return the new items ID.
  131. int uiItem();
  132. // assign an item to a container.
  133. // parent is the item ID of the containing item; an item ID of 0 refers to the
  134. // root item.
  135. // if item is already assigned to a parent, an assertion will be thrown.
  136. void uiSetParent(int item, int parent);
  137. // layout all added items and update the internal state according to the
  138. // current cursor position and button states.
  139. // It is safe to immediately draw the items after a call to uiProcess().
  140. // this is an O(N) operation for N = number of declared items.
  141. void uiProcess();
  142. // returns the number of child items a container item contains. If the item
  143. // is not a container or does not contain any items, 0 is returned.
  144. // if item is 0, the child item count of the root item will be returned.
  145. int uiGetChildCount(int item);
  146. // returns the first child item of a container item. If the item is not
  147. // a container or does not contain any items, -1 is returned.
  148. // if item is 0, the first child item of the root item will be returned.
  149. int uiFirstChild(int item);
  150. // returns the last child item of a container item. If the item is not
  151. // a container or does not contain any items, -1 is returned.
  152. // if item is 0, the last child item of the root item will be returned.
  153. int uiLastChild(int item);
  154. // returns an items parent container item.
  155. // if item is 0, -1 will be returned.
  156. int uiParent(int item);
  157. // returns an items child index relative to its parent. If the item is the
  158. // first item, the return value is 0; If the item is the last item, the return
  159. // value is equivalent to uiGetChildCount(uiParent(item))-1.
  160. // if item is 0, 0 will be returned.
  161. int uiGetChildId(int item);
  162. // returns an items next sibling in the list of the parent containers children.
  163. // if item is 0 or the item is the last child item, -1 will be returned.
  164. int uiNextSibling(int item);
  165. void uiSetSize(int item, int w, int h);
  166. void uiSetLayout(int item, int flags);
  167. void uiSetMargins(int item, int t, int r, int b, int l);
  168. void uiSetRelativeTo(int item, int titem, int ritem, int bitem, int litem);
  169. // returns the items layout rectangle relative to the parent. If uiGetRect()
  170. // is called before uiProcess(), the values of the returned rectangle are
  171. // undefined.
  172. UIrect uiGetRect(int item);
  173. // allocate space for application-dependent context data and return the pointer
  174. // if successful. If no data has been allocated, a new pointer is returned.
  175. // Otherwise, an assertion is thrown.
  176. // The memory of the pointer is managed by the UI context.
  177. void *uiAllocData(int item, int size);
  178. // return the application-dependent context data for an item as passed to
  179. // uiAllocData(). The memory of the pointer is managed by the UI context
  180. // and must not be altered.
  181. const void *uiGetData(int item);
  182. // set the application-dependent handle of an item.
  183. // handle is an application defined 64-bit handle. If handle is 0, the item
  184. // will not be interactive.
  185. void uiSetHandle(int item, UIhandle handle);
  186. // return the application-dependent handle of the item as passed to uiSetHandle().
  187. UIhandle uiGetHandle(int item);
  188. // return the current state of the item. This state is only valid after
  189. // a call to uiProcess().
  190. // The returned value is one of UI_COLD, UI_HOT, UI_ACTIVE.
  191. int uiGetState(int item);
  192. // set the handler callback for an interactive item.
  193. // flags is a combination of UI_EVENT_* and designates for which events the
  194. // handler should be called.
  195. void uiSetHandler(int item, UIhandler handler, int flags);
  196. // return the handler callback for an item as passed to uiSetHandler()
  197. UIhandler uiGetHandler(int item);
  198. // return the handler flags for an item as passed to uiSetHandler()
  199. int uiGetHandlerFlags(int item);
  200. #endif // _UI_H_
  201. #define UI_IMPLEMENTATION
  202. #ifdef UI_IMPLEMENTATION
  203. #include <assert.h>
  204. #ifdef _MSC_VER
  205. #pragma warning (disable: 4996) // Switch off security warnings
  206. #pragma warning (disable: 4100) // Switch off unreferenced formal parameter warnings
  207. #ifdef __cplusplus
  208. #define UI_INLINE inline
  209. #else
  210. #define UI_INLINE
  211. #endif
  212. #else
  213. #define UI_INLINE inline
  214. #endif
  215. #define UI_MAX_KIND 16
  216. typedef struct UIitem {
  217. // declaration independent unique handle (for persistence)
  218. UIhandle handle;
  219. // handler
  220. UIhandler handler;
  221. // container structure
  222. // number of kids
  223. int numkids;
  224. // index of first kid
  225. int firstkid;
  226. // index of last kid
  227. int lastkid;
  228. // child structure
  229. // parent item
  230. int parent;
  231. // index of kid relative to parent
  232. int kidid;
  233. // index of next sibling with same parent
  234. int nextitem;
  235. // one or multiple of UIlayoutFlags
  236. int layout_flags;
  237. // size
  238. UIvec2 size;
  239. int visited;
  240. // margin offsets, interpretation depends on flags
  241. int margins[4];
  242. // neighbors to position borders to
  243. int relto[4];
  244. // relative rect
  245. UIrect rect;
  246. // attributes
  247. // index of data or -1 for none
  248. int data;
  249. // size of data
  250. int datasize;
  251. // a combination of UIevents
  252. int event_flags;
  253. } UIitem;
  254. typedef enum UIstate {
  255. UI_STATE_IDLE = 0,
  256. UI_STATE_CAPTURE,
  257. } UIstate;
  258. struct UIcontext {
  259. // button state in this frame
  260. unsigned long long buttons;
  261. // button state in the previous frame
  262. unsigned long long last_buttons;
  263. // where the cursor was at the beginning of the active state
  264. UIvec2 start_cursor;
  265. // where the cursor was last frame
  266. UIvec2 last_cursor;
  267. // where the cursor is currently
  268. UIvec2 cursor;
  269. UIhandle hot_handle;
  270. UIhandle active_handle;
  271. int hot_item;
  272. int active_item;
  273. UIstate state;
  274. int count;
  275. UIitem items[UI_MAX_ITEMS];
  276. int datasize;
  277. unsigned char data[UI_MAX_BUFFERSIZE];
  278. };
  279. UI_INLINE int ui_max(int a, int b) {
  280. return (a>b)?a:b;
  281. }
  282. UI_INLINE int ui_min(int a, int b) {
  283. return (a<b)?a:b;
  284. }
  285. static UIcontext *ui_context = NULL;
  286. UIcontext *uiCreateContext() {
  287. UIcontext *ctx = (UIcontext *)malloc(sizeof(UIcontext));
  288. memset(ctx, 0, sizeof(UIcontext));
  289. return ctx;
  290. }
  291. void uiMakeCurrent(UIcontext *ctx) {
  292. ui_context = ctx;
  293. if (ui_context)
  294. uiClear();
  295. }
  296. void uiDestroyContext(UIcontext *ctx) {
  297. if (ui_context == ctx)
  298. uiMakeCurrent(NULL);
  299. free(ctx);
  300. }
  301. void uiSetButton(int button, int enabled) {
  302. assert(ui_context);
  303. unsigned long long mask = 1ull<<button;
  304. // set new bit
  305. ui_context->buttons = (enabled)?
  306. (ui_context->buttons | mask):
  307. (ui_context->buttons & ~mask);
  308. }
  309. int uiGetLastButton(int button) {
  310. assert(ui_context);
  311. return (ui_context->last_buttons & (1ull<<button))?1:0;
  312. }
  313. int uiGetButton(int button) {
  314. assert(ui_context);
  315. return (ui_context->buttons & (1ull<<button))?1:0;
  316. }
  317. int uiButtonPressed(int button) {
  318. assert(ui_context);
  319. return !uiGetLastButton(button) && uiGetButton(button);
  320. }
  321. int uiButtonReleased(int button) {
  322. assert(ui_context);
  323. return uiGetLastButton(button) && !uiGetButton(button);
  324. }
  325. void uiSetCursor(int x, int y) {
  326. assert(ui_context);
  327. ui_context->cursor.x = x;
  328. ui_context->cursor.y = y;
  329. }
  330. UIvec2 uiGetCursor() {
  331. assert(ui_context);
  332. return ui_context->cursor;
  333. }
  334. UIvec2 uiGetCursorDelta() {
  335. assert(ui_context);
  336. UIvec2 result = {{{
  337. ui_context->cursor.x - ui_context->last_cursor.x,
  338. ui_context->cursor.y - ui_context->last_cursor.y
  339. }}};
  340. return result;
  341. }
  342. UIvec2 uiGetCursorStartDelta() {
  343. assert(ui_context);
  344. UIvec2 result = {{{
  345. ui_context->cursor.x - ui_context->start_cursor.x,
  346. ui_context->cursor.y - ui_context->start_cursor.y
  347. }}};
  348. return result;
  349. }
  350. UIitem *uiItemPtr(int item) {
  351. assert(ui_context && (item >= 0) && (item < ui_context->count));
  352. return ui_context->items + item;
  353. }
  354. void uiClear() {
  355. assert(ui_context);
  356. ui_context->count = 1;
  357. ui_context->datasize = 0;
  358. ui_context->hot_item = -1;
  359. ui_context->active_item = -1;
  360. // init root object
  361. UIitem *item = ui_context->items;
  362. memset(item, 0, sizeof(UIitem));
  363. item->parent = -1;
  364. item->firstkid = -1;
  365. item->lastkid = -1;
  366. item->nextitem = -1;
  367. item->data = -1;
  368. }
  369. int uiItem() {
  370. assert(ui_context && (ui_context->count < UI_MAX_ITEMS));
  371. int idx = ui_context->count++;
  372. UIitem *item = uiItemPtr(idx);
  373. memset(item, 0, sizeof(UIitem));
  374. item->parent = -1;
  375. item->firstkid = -1;
  376. item->lastkid = -1;
  377. item->nextitem = -1;
  378. item->data = -1;
  379. for (int i = 0; i < 4; ++i)
  380. item->relto[i] = -1;
  381. return idx;
  382. }
  383. void uiSetParent(int item, int parent) {
  384. assert(item > 0);
  385. assert(uiParent(item) == -1);
  386. UIitem *pitem = uiItemPtr(item);
  387. UIitem *pparent = uiItemPtr(parent);
  388. pitem->parent = parent;
  389. pitem->kidid = pparent->numkids++;
  390. if (pparent->lastkid < 0) {
  391. pparent->firstkid = item;
  392. pparent->lastkid = item;
  393. } else {
  394. uiItemPtr(pparent->lastkid)->nextitem = item;
  395. pparent->lastkid = item;
  396. }
  397. }
  398. void uiSetSize(int item, int w, int h) {
  399. UIitem *pitem = uiItemPtr(item);
  400. pitem->size.x = w;
  401. pitem->size.y = h;
  402. }
  403. void uiSetLayout(int item, int flags) {
  404. UIitem *pitem = uiItemPtr(item);
  405. pitem->layout_flags = flags;
  406. }
  407. void uiSetMargins(int item, int l, int t, int r, int b) {
  408. UIitem *pitem = uiItemPtr(item);
  409. pitem->margins[0] = l;
  410. pitem->margins[1] = t;
  411. pitem->margins[2] = r;
  412. pitem->margins[3] = b;
  413. }
  414. void uiSetRelativeTo(int item, int litem, int titem, int ritem, int bitem) {
  415. UIitem *pitem = uiItemPtr(item);
  416. pitem->relto[0] = litem;
  417. pitem->relto[1] = titem;
  418. pitem->relto[2] = ritem;
  419. pitem->relto[3] = bitem;
  420. }
  421. UI_INLINE int uiComputeChainSize(UIitem *pkid, int dim) {
  422. UIitem *pitem = pkid;
  423. int wdim = dim+2;
  424. int size = pitem->rect.v[wdim];
  425. int it = 0;
  426. pitem->visited |= 1<<dim;
  427. // traverse along left neighbors
  428. while ((pitem->layout_flags>>dim) & UI_LEFT) {
  429. size += pitem->margins[dim];
  430. if (pitem->relto[dim] < 0) break;
  431. pitem = uiItemPtr(pitem->relto[dim]);
  432. pitem->visited |= 1<<dim;
  433. size += pitem->rect.v[wdim];
  434. it++;
  435. assert(it<1000000); // infinite loop
  436. }
  437. // traverse along right neighbors
  438. pitem = pkid;
  439. it = 0;
  440. while ((pitem->layout_flags>>dim) & UI_RIGHT) {
  441. size += pitem->margins[wdim];
  442. if (pitem->relto[wdim] < 0) break;
  443. pitem = uiItemPtr(pitem->relto[wdim]);
  444. pitem->visited |= 1<<dim;
  445. size += pitem->rect.v[wdim];
  446. it++;
  447. assert(it<1000000); // infinite loop
  448. }
  449. return size;
  450. }
  451. UI_INLINE void uiComputeSizeDim(UIitem *pitem, int dim) {
  452. int wdim = dim+2;
  453. if (pitem->size.v[dim]) {
  454. pitem->rect.v[wdim] = pitem->size.v[dim];
  455. } else {
  456. int size = 0;
  457. int kid = pitem->firstkid;
  458. while (kid > 0) {
  459. UIitem *pkid = uiItemPtr(kid);
  460. if (!(pkid->visited & (1<<dim))) {
  461. size = ui_max(size, uiComputeChainSize(pkid, dim));
  462. }
  463. kid = uiNextSibling(kid);
  464. }
  465. pitem->rect.v[wdim] = size;
  466. }
  467. }
  468. static void uiComputeBestSize(int item) {
  469. UIitem *pitem = uiItemPtr(item);
  470. pitem->visited = 0;
  471. // children expand the size
  472. int kid = uiFirstChild(item);
  473. while (kid > 0) {
  474. uiComputeBestSize(kid);
  475. kid = uiNextSibling(kid);
  476. }
  477. uiComputeSizeDim(pitem, 0);
  478. uiComputeSizeDim(pitem, 1);
  479. }
  480. static void uiLayoutChildItem(UIitem *pparent, UIitem *pitem, int dim) {
  481. if (pitem->visited & (4<<dim)) return;
  482. pitem->visited |= (4<<dim);
  483. int wdim = dim+2;
  484. int wl = 0;
  485. int wr = pparent->rect.v[wdim];
  486. int flags = pitem->layout_flags>>dim;
  487. if ((flags & UI_LEFT) && (pitem->relto[dim] > 0)) {
  488. UIitem *pl = uiItemPtr(pitem->relto[dim]);
  489. uiLayoutChildItem(pparent, pl, dim);
  490. wl = pl->rect.v[dim]+pl->rect.v[wdim];
  491. wr -= wl;
  492. }
  493. if ((flags & UI_RIGHT) && (pitem->relto[wdim] > 0)) {
  494. UIitem *pl = uiItemPtr(pitem->relto[wdim]);
  495. uiLayoutChildItem(pparent, pl, dim);
  496. wr = pl->rect.v[dim]-wl;
  497. }
  498. switch(flags & UI_HFILL) {
  499. default:
  500. case UI_HCENTER: {
  501. pitem->rect.v[dim] = wl+(wr-pitem->rect.v[wdim])/2+pitem->margins[dim];
  502. } break;
  503. case UI_LEFT: {
  504. pitem->rect.v[dim] = wl+pitem->margins[dim];
  505. } break;
  506. case UI_RIGHT: {
  507. pitem->rect.v[dim] = wl+wr-pitem->rect.v[wdim]-pitem->margins[wdim];
  508. } break;
  509. case UI_HFILL: {
  510. pitem->rect.v[dim] = wl+pitem->margins[dim];
  511. pitem->rect.v[wdim] = wr-pitem->margins[dim]-pitem->margins[wdim];
  512. } break;
  513. }
  514. }
  515. UI_INLINE void uiLayoutItemDim(UIitem *pitem, int dim) {
  516. int kid = pitem->firstkid;
  517. while (kid > 0) {
  518. UIitem *pkid = uiItemPtr(kid);
  519. uiLayoutChildItem(pitem, pkid, dim);
  520. kid = uiNextSibling(kid);
  521. }
  522. }
  523. static void uiLayoutItem(int item) {
  524. UIitem *pitem = uiItemPtr(item);
  525. uiLayoutItemDim(pitem, 0);
  526. uiLayoutItemDim(pitem, 1);
  527. int kid = uiFirstChild(item);
  528. while (kid > 0) {
  529. uiLayoutItem(kid);
  530. kid = uiNextSibling(kid);
  531. }
  532. }
  533. UIrect uiGetRect(int item) {
  534. return uiItemPtr(item)->rect;
  535. }
  536. int uiFirstChild(int item) {
  537. return uiItemPtr(item)->firstkid;
  538. }
  539. int uiLastChild(int item) {
  540. return uiItemPtr(item)->lastkid;
  541. }
  542. int uiNextSibling(int item) {
  543. return uiItemPtr(item)->nextitem;
  544. }
  545. int uiParent(int item) {
  546. return uiItemPtr(item)->parent;
  547. }
  548. const void *uiGetData(int item) {
  549. UIitem *pitem = uiItemPtr(item);
  550. if (pitem->data < 0) return NULL;
  551. return ui_context->data + pitem->data;
  552. }
  553. void *uiAllocData(int item, int size) {
  554. assert((size > 0) && (size < UI_MAX_DATASIZE));
  555. UIitem *pitem = uiItemPtr(item);
  556. assert(pitem->data < 0);
  557. assert((ui_context->datasize+size) <= UI_MAX_BUFFERSIZE);
  558. pitem->data = ui_context->datasize;
  559. ui_context->datasize += size;
  560. return ui_context->data + pitem->data;
  561. }
  562. void uiSetHandle(int item, UIhandle handle) {
  563. uiItemPtr(item)->handle = handle;
  564. if (handle) {
  565. if (handle == ui_context->hot_handle)
  566. ui_context->hot_item = item;
  567. if (handle == ui_context->active_handle)
  568. ui_context->active_item = item;
  569. }
  570. }
  571. UIhandle uiGetHandle(int item) {
  572. return uiItemPtr(item)->handle;
  573. }
  574. void uiSetHandler(int item, UIhandler handler, int flags) {
  575. UIitem *pitem = uiItemPtr(item);
  576. pitem->handler = handler;
  577. pitem->event_flags = flags;
  578. }
  579. UIhandler uiGetHandler(int item) {
  580. return uiItemPtr(item)->handler;
  581. }
  582. int uiGetHandlerFlags(int item) {
  583. return uiItemPtr(item)->event_flags;
  584. }
  585. int uiGetChildId(int item) {
  586. return uiItemPtr(item)->kidid;
  587. }
  588. int uiGetChildCount(int item) {
  589. return uiItemPtr(item)->numkids;
  590. }
  591. int uiFindItem(int item, int x, int y) {
  592. UIrect rect = uiGetRect(item);
  593. x -= rect.x;
  594. y -= rect.y;
  595. if ((x>=0)
  596. && (y>=0)
  597. && (x<rect.w)
  598. && (y<rect.h)) {
  599. int kid = uiFirstChild(item);
  600. while (kid > 0) {
  601. int best_hit = uiFindItem(kid,x,y);
  602. if (best_hit >= 0) return best_hit;
  603. kid = uiNextSibling(kid);
  604. }
  605. return item;
  606. }
  607. return -1;
  608. }
  609. void uiNotifyItem(int item, UIevent event) {
  610. UIitem *pitem = uiItemPtr(item);
  611. if (pitem->handler && (pitem->event_flags & event)) {
  612. pitem->handler(item, event);
  613. }
  614. }
  615. void uiProcess() {
  616. uiComputeBestSize(0);
  617. // position root element rect
  618. uiItemPtr(0)->rect.x = uiItemPtr(0)->margins[0];
  619. uiItemPtr(0)->rect.y = uiItemPtr(0)->margins[1];
  620. uiLayoutItem(0);
  621. int hot = uiFindItem(0, ui_context->cursor.x, ui_context->cursor.y);
  622. switch(ui_context->state) {
  623. default:
  624. case UI_STATE_IDLE: {
  625. if (uiGetButton(0)) {
  626. ui_context->start_cursor = ui_context->cursor;
  627. ui_context->last_cursor = ui_context->cursor;
  628. ui_context->hot_item = -1;
  629. ui_context->active_item = hot;
  630. if (ui_context->active_item >= 0) {
  631. uiNotifyItem(ui_context->active_item, UI_BUTTON0_DOWN);
  632. }
  633. ui_context->state = UI_STATE_CAPTURE;
  634. } else {
  635. ui_context->hot_item = hot;
  636. }
  637. } break;
  638. case UI_STATE_CAPTURE: {
  639. if (!uiGetButton(0)) {
  640. if (ui_context->active_item >= 0) {
  641. uiNotifyItem(ui_context->active_item, UI_BUTTON0_UP);
  642. if (ui_context->active_item == hot) {
  643. uiNotifyItem(ui_context->active_item, UI_BUTTON0_HOT_UP);
  644. }
  645. }
  646. ui_context->active_item = -1;
  647. ui_context->state = UI_STATE_IDLE;
  648. } else {
  649. if (ui_context->active_item >= 0) {
  650. uiNotifyItem(ui_context->active_item, UI_BUTTON0_CAPTURE);
  651. }
  652. if (hot == ui_context->active_item)
  653. ui_context->hot_item = hot;
  654. else
  655. ui_context->hot_item = -1;
  656. }
  657. } break;
  658. }
  659. ui_context->last_cursor = ui_context->cursor;
  660. ui_context->hot_handle = (ui_context->hot_item>=0)?
  661. uiGetHandle(ui_context->hot_item):0;
  662. ui_context->active_handle = (ui_context->active_item>=0)?
  663. uiGetHandle(ui_context->active_item):0;
  664. }
  665. int uiIsActive(int item) {
  666. assert(ui_context);
  667. return ui_context->active_item == item;
  668. }
  669. int uiIsHot(int item) {
  670. assert(ui_context);
  671. return ui_context->hot_item == item;
  672. }
  673. int uiGetState(int item) {
  674. UIitem *pitem = uiItemPtr(item);
  675. if (uiIsActive(item)) {
  676. if (pitem->event_flags & (UI_BUTTON0_CAPTURE|UI_BUTTON0_UP)) return UI_ACTIVE;
  677. if ((pitem->event_flags & UI_BUTTON0_HOT_UP)
  678. && uiIsHot(item)) return UI_ACTIVE;
  679. return UI_COLD;
  680. } else if (uiIsHot(item)) {
  681. return UI_HOT;
  682. }
  683. return UI_COLD;
  684. }
  685. #endif // UI_IMPLEMENTATION