ngui.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /* ngui: immediate-mode widgets for RGB565 canvas windows. See ngui.h. */
  2. #include "ngui.h"
  3. #include "ucmd_lib.h"
  4. #include "font_8x16.h"
  5. const struct ngui_theme ngui_theme_dark = {
  6. .bg = NGUI_RGB(24, 28, 40),
  7. .panel = NGUI_RGB(38, 43, 58),
  8. .panel_alt = NGUI_RGB(50, 56, 74),
  9. .border = NGUI_RGB(92, 99, 124),
  10. .text = NGUI_RGB(228, 230, 240),
  11. .text_dim = NGUI_RGB(145, 151, 170),
  12. .accent = NGUI_RGB(84, 132, 224),
  13. .selection = NGUI_RGB(54, 92, 166),
  14. .button = NGUI_RGB(54, 61, 80),
  15. .button_hot = NGUI_RGB(70, 81, 108),
  16. .button_down = NGUI_RGB(48, 112, 204),
  17. .icon_folder = NGUI_RGB(238, 198, 88),
  18. .icon_file = NGUI_RGB(204, 210, 226),
  19. .icon_disk = NGUI_RGB(112, 174, 232),
  20. .error = NGUI_RGB(238, 116, 116),
  21. };
  22. static unsigned int slen(const char *s)
  23. {
  24. unsigned int n = 0;
  25. while (s && s[n])
  26. n++;
  27. return n;
  28. }
  29. static void scopy(char *dst, const char *src, unsigned int cap)
  30. {
  31. unsigned int i = 0;
  32. if (!cap)
  33. return;
  34. while (src && src[i] && i + 1 < cap) {
  35. dst[i] = src[i];
  36. i++;
  37. }
  38. dst[i] = '\0';
  39. }
  40. static int rect_empty(const struct ngui_rect *r)
  41. {
  42. return r->w <= 0 || r->h <= 0;
  43. }
  44. static int rect_intersect(const struct ngui_rect *a, const struct ngui_rect *b,
  45. struct ngui_rect *out)
  46. {
  47. int x0 = a->x > b->x ? a->x : b->x;
  48. int y0 = a->y > b->y ? a->y : b->y;
  49. int x1 = a->x + a->w < b->x + b->w ? a->x + a->w : b->x + b->w;
  50. int y1 = a->y + a->h < b->y + b->h ? a->y + a->h : b->y + b->h;
  51. out->x = x0;
  52. out->y = y0;
  53. out->w = x1 - x0;
  54. out->h = y1 - y0;
  55. return !rect_empty(out);
  56. }
  57. static struct ngui_rect rect_make(int x, int y, int w, int h)
  58. {
  59. struct ngui_rect r;
  60. r.x = x;
  61. r.y = y;
  62. r.w = w;
  63. r.h = h;
  64. return r;
  65. }
  66. int ngui_rect_contains(const struct ngui_rect *r, int x, int y)
  67. {
  68. return x >= r->x && y >= r->y && x < r->x + r->w && y < r->y + r->h;
  69. }
  70. void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h)
  71. {
  72. unsigned int i;
  73. for (i = 0; i < sizeof(*ui); i++)
  74. ((unsigned char *)ui)[i] = 0;
  75. ui->px = fb;
  76. ui->w = w;
  77. ui->h = h;
  78. ui->theme = ngui_theme_dark;
  79. ui->nclip = 0;
  80. ui->dirty = 1;
  81. ui->caret_on = 1;
  82. ui->last_blink = u_ticks_ms();
  83. ui->last_mouse_x = -1;
  84. ui->last_mouse_y = -1;
  85. }
  86. void ngui_invalidate(struct ngui *ui)
  87. {
  88. ui->dirty = 1;
  89. }
  90. void ngui_set_focus(struct ngui *ui, unsigned int id)
  91. {
  92. if (ui->focus != id) {
  93. ui->focus = id;
  94. ui->last_blink = u_ticks_ms();
  95. ui->caret_on = 1;
  96. ui->dirty = 1;
  97. }
  98. }
  99. unsigned int ngui_focus(struct ngui *ui)
  100. {
  101. return ui->focus;
  102. }
  103. static void key_push(struct ngui *ui, int k)
  104. {
  105. unsigned int next = (ui->key_head + 1) % 32;
  106. if (next == ui->key_tail)
  107. return;
  108. ui->keys[ui->key_head] = k;
  109. ui->key_head = next;
  110. }
  111. int ngui_key_peek(struct ngui *ui)
  112. {
  113. if (ui->key_tail == ui->key_head)
  114. return 0;
  115. return ui->keys[ui->key_tail];
  116. }
  117. int ngui_key(struct ngui *ui)
  118. {
  119. int k = ngui_key_peek(ui);
  120. if (!k)
  121. return 0;
  122. ui->key_tail = (ui->key_tail + 1) % 32;
  123. return k;
  124. }
  125. int ngui_begin(struct ngui *ui)
  126. {
  127. struct win_info wi;
  128. struct win_pointer wp;
  129. struct win_event ev;
  130. unsigned int now;
  131. int want = ui->dirty;
  132. int i, k;
  133. ui->dirty = 0;
  134. ui->pressed = ui->released = 0;
  135. ui->clicked = 0;
  136. ui->focus_claimed = 0;
  137. ui->hot = 0;
  138. if (u_win_info(&wi) == 0) {
  139. if (wi.width != (unsigned int)ui->w ||
  140. wi.height != (unsigned int)ui->h) {
  141. if (u_win_ack_resize() == 0) {
  142. ui->px = (unsigned short *)wi.fb;
  143. ui->w = (int)wi.width;
  144. ui->h = (int)wi.height;
  145. }
  146. want = 1;
  147. } else if (wi.fb && wi.fb != (void *)ui->px) {
  148. ui->px = (unsigned short *)wi.fb;
  149. }
  150. }
  151. if (u_win_pointer(&wp) == 0) {
  152. ui->mouse_x = wp.x;
  153. ui->mouse_y = wp.y;
  154. ui->buttons = wp.buttons;
  155. ui->inside = wp.inside ? 1 : 0;
  156. ui->focused = wp.focused ? 1 : 0;
  157. } else {
  158. ui->mouse_x = ui->mouse_y = 0;
  159. ui->buttons = 0;
  160. ui->inside = 0;
  161. ui->focused = 0;
  162. }
  163. for (i = 0; i < 16; i++) {
  164. int r = u_win_event(&ev);
  165. if (r != 1)
  166. break;
  167. if (ev.type == WIN_EV_POINTER_DOWN) {
  168. ui->pressed |= 1;
  169. ui->press_x = ev.x;
  170. ui->press_y = ev.y;
  171. ui->clicked = 1;
  172. } else if (ev.type == WIN_EV_POINTER_UP) {
  173. ui->released |= 1;
  174. ui->release_x = ev.x;
  175. ui->release_y = ev.y;
  176. }
  177. }
  178. for (i = 0; i < 32; i++) {
  179. k = u_key_poll();
  180. if (!k)
  181. break;
  182. key_push(ui, k);
  183. want = 1;
  184. }
  185. now = u_ticks_ms();
  186. if (ui->focus) {
  187. if (now - ui->last_blink >= 500) {
  188. ui->last_blink = now;
  189. ui->caret_on = !ui->caret_on;
  190. want = 1;
  191. }
  192. } else {
  193. ui->caret_on = 1;
  194. }
  195. if (ui->inside != ui->last_inside) {
  196. want = 1;
  197. } else if (ui->inside &&
  198. (ui->mouse_x != ui->last_mouse_x ||
  199. ui->mouse_y != ui->last_mouse_y)) {
  200. want = 1;
  201. }
  202. if (ui->focused != ui->last_focused)
  203. want = 1;
  204. ui->last_mouse_x = ui->mouse_x;
  205. ui->last_mouse_y = ui->mouse_y;
  206. ui->last_inside = ui->inside;
  207. ui->last_focused = ui->focused;
  208. if (ui->pressed || ui->released)
  209. want = 1;
  210. return want;
  211. }
  212. void ngui_end_ex(struct ngui *ui, int present)
  213. {
  214. if (present && u_win_present() != 0)
  215. ui->dirty = 1;
  216. if (ui->clicked && !ui->focus_claimed)
  217. ui->focus = 0;
  218. /* Only the release edge ends a press. The sampled button state is not
  219. * synchronized with the event queue: it can read 0 for a frame before
  220. * the release event is delivered. Clearing active on "!buttons" made
  221. * clicks fire only when the two happened to land in the same frame. */
  222. if (ui->released)
  223. ui->active = 0;
  224. ui->pressed = ui->released = 0;
  225. ui->clicked = 0;
  226. ui->focus_claimed = 0;
  227. ui->press_x = ui->press_y = 0;
  228. ui->release_x = ui->release_y = 0;
  229. }
  230. void ngui_end(struct ngui *ui)
  231. {
  232. ngui_end_ex(ui, 1);
  233. }
  234. struct ngui_rect ngui_cut_top(struct ngui_rect *r, int h)
  235. {
  236. struct ngui_rect out = *r;
  237. if (h < 0)
  238. h = 0;
  239. if (h > r->h)
  240. h = r->h;
  241. out.h = h;
  242. r->y += h;
  243. r->h -= h;
  244. return out;
  245. }
  246. struct ngui_rect ngui_cut_bottom(struct ngui_rect *r, int h)
  247. {
  248. struct ngui_rect out = *r;
  249. if (h < 0)
  250. h = 0;
  251. if (h > r->h)
  252. h = r->h;
  253. out.y = r->y + r->h - h;
  254. out.h = h;
  255. r->h -= h;
  256. return out;
  257. }
  258. struct ngui_rect ngui_cut_left(struct ngui_rect *r, int w)
  259. {
  260. struct ngui_rect out = *r;
  261. if (w < 0)
  262. w = 0;
  263. if (w > r->w)
  264. w = r->w;
  265. out.w = w;
  266. r->x += w;
  267. r->w -= w;
  268. return out;
  269. }
  270. struct ngui_rect ngui_cut_right(struct ngui_rect *r, int w)
  271. {
  272. struct ngui_rect out = *r;
  273. if (w < 0)
  274. w = 0;
  275. if (w > r->w)
  276. w = r->w;
  277. out.x = r->x + r->w - w;
  278. out.w = w;
  279. r->w -= w;
  280. return out;
  281. }
  282. void ngui_inset(struct ngui_rect *r, int n)
  283. {
  284. r->x += n;
  285. r->y += n;
  286. r->w -= n * 2;
  287. r->h -= n * 2;
  288. if (r->w < 0)
  289. r->w = 0;
  290. if (r->h < 0)
  291. r->h = 0;
  292. }
  293. void ngui_push_clip(struct ngui *ui, struct ngui_rect r)
  294. {
  295. struct ngui_rect out;
  296. struct ngui_rect canvas;
  297. if (ui->nclip >= 4)
  298. return;
  299. canvas = rect_make(0, 0, ui->w, ui->h);
  300. if (!rect_intersect(&r, &canvas, &out))
  301. out = rect_make(0, 0, 0, 0);
  302. if (ui->nclip > 0) {
  303. struct ngui_rect clipped = out;
  304. if (!rect_intersect(&clipped, &ui->clip[ui->nclip - 1], &out))
  305. out = rect_make(0, 0, 0, 0);
  306. }
  307. ui->clip[ui->nclip++] = out;
  308. }
  309. void ngui_pop_clip(struct ngui *ui)
  310. {
  311. if (ui->nclip > 0)
  312. ui->nclip--;
  313. }
  314. static int pixel_visible(struct ngui *ui, int x, int y)
  315. {
  316. struct ngui_rect *c;
  317. if (x < 0 || y < 0 || x >= ui->w || y >= ui->h)
  318. return 0;
  319. if (ui->nclip <= 0)
  320. return 1;
  321. c = &ui->clip[ui->nclip - 1];
  322. return x >= c->x && y >= c->y &&
  323. x < c->x + c->w && y < c->y + c->h;
  324. }
  325. void ngui_fill_rect(struct ngui *ui, struct ngui_rect r, unsigned short color)
  326. {
  327. struct ngui_rect canvas;
  328. int x, y;
  329. if (rect_empty(&r))
  330. return;
  331. canvas = rect_make(0, 0, ui->w, ui->h);
  332. if (!rect_intersect(&r, &canvas, &r))
  333. return;
  334. if (ui->nclip > 0 &&
  335. !rect_intersect(&r, &ui->clip[ui->nclip - 1], &r))
  336. return;
  337. for (y = r.y; y < r.y + r.h; y++) {
  338. unsigned short *p = ui->px + y * ui->w + r.x;
  339. for (x = 0; x < r.w; x++)
  340. p[x] = color;
  341. }
  342. }
  343. void ngui_frame(struct ngui *ui, struct ngui_rect r, unsigned short color)
  344. {
  345. if (r.w <= 0 || r.h <= 0)
  346. return;
  347. ngui_fill_rect(ui, rect_make(r.x, r.y, r.w, 1), color);
  348. ngui_fill_rect(ui, rect_make(r.x, r.y + r.h - 1, r.w, 1), color);
  349. if (r.h > 2) {
  350. ngui_fill_rect(ui, rect_make(r.x, r.y + 1, 1, r.h - 2), color);
  351. ngui_fill_rect(ui, rect_make(r.x + r.w - 1, r.y + 1, 1,
  352. r.h - 2), color);
  353. }
  354. }
  355. static void ngui_pixel(struct ngui *ui, int x, int y, unsigned short color)
  356. {
  357. if (pixel_visible(ui, x, y))
  358. ui->px[y * ui->w + x] = color;
  359. }
  360. void ngui_line(struct ngui *ui, int x0, int y0, int x1, int y1,
  361. unsigned short color)
  362. {
  363. int dx = x1 - x0;
  364. int dy = y1 - y0;
  365. int sx = dx < 0 ? -1 : 1;
  366. int sy = dy < 0 ? -1 : 1;
  367. int err, e2;
  368. if (dx < 0)
  369. dx = -dx;
  370. if (dy < 0)
  371. dy = -dy;
  372. dy = -dy;
  373. err = dx + dy;
  374. for (;;) {
  375. ngui_pixel(ui, x0, y0, color);
  376. if (x0 == x1 && y0 == y1)
  377. break;
  378. e2 = err * 2;
  379. if (e2 >= dy) {
  380. err += dy;
  381. x0 += sx;
  382. }
  383. if (e2 <= dx) {
  384. err += dx;
  385. y0 += sy;
  386. }
  387. }
  388. }
  389. static void ngui_glyph(struct ngui *ui, int x, int y, unsigned char c,
  390. unsigned short color, int scale, int bold)
  391. {
  392. const unsigned char *g = &font_8x16[(unsigned int)c * FONT_8X16_ROWS];
  393. int row, col, sx, sy;
  394. if (scale < 1)
  395. scale = 1;
  396. for (row = 0; row < FONT_8X16_ROWS; row++) {
  397. unsigned char bits = g[row];
  398. if (!bits)
  399. continue;
  400. for (col = 0; col < 8; col++) {
  401. if (!(bits & (0x80u >> col)))
  402. continue;
  403. for (sy = 0; sy < scale; sy++) {
  404. for (sx = 0; sx < scale; sx++) {
  405. ngui_pixel(ui,
  406. x + col * scale + sx +
  407. (bold && scale == 1 ? 1 : 0),
  408. y + row * scale + sy,
  409. color);
  410. }
  411. }
  412. if (bold && scale > 1)
  413. ngui_pixel(ui, x + col * scale + scale,
  414. y + row * scale, color);
  415. }
  416. }
  417. }
  418. unsigned int ngui_text_width(const char *s)
  419. {
  420. return slen(s) * 8;
  421. }
  422. unsigned int ngui_text_width_n(const char *s, unsigned int n, int scale)
  423. {
  424. (void)s;
  425. if (scale < 1)
  426. scale = 1;
  427. return n * 8u * (unsigned int)scale;
  428. }
  429. void ngui_text_n(struct ngui *ui, int x, int y, const char *s, unsigned int n,
  430. unsigned short color)
  431. {
  432. unsigned int i;
  433. for (i = 0; i < n && s && s[i]; i++)
  434. ngui_glyph(ui, x + (int)i * 8, y, (unsigned char)s[i], color, 1, 0);
  435. }
  436. void ngui_text(struct ngui *ui, int x, int y, const char *s,
  437. unsigned short color)
  438. {
  439. ngui_text_n(ui, x, y, s, slen(s), color);
  440. }
  441. void ngui_text_styled(struct ngui *ui, int x, int y, const char *s,
  442. unsigned int n, unsigned short color, int scale, int bold)
  443. {
  444. unsigned int i;
  445. if (scale < 1)
  446. scale = 1;
  447. for (i = 0; i < n && s && s[i]; i++)
  448. ngui_glyph(ui, x + (int)i * 8 * scale, y, (unsigned char)s[i],
  449. color, scale, bold);
  450. }
  451. /* Draw a string upside down. The whole n-character block is rotated, so the
  452. * first character ends up on the right; used for the mirrored corner index on
  453. * a playing card. */
  454. void ngui_text_rot180(struct ngui *ui, int x, int y, const char *s,
  455. unsigned int n, unsigned short color)
  456. {
  457. unsigned int i;
  458. int row, col;
  459. for (i = 0; i < n && s && s[i]; i++) {
  460. const unsigned char *g =
  461. &font_8x16[(unsigned int)(unsigned char)s[i] *
  462. FONT_8X16_ROWS];
  463. for (row = 0; row < FONT_8X16_ROWS; row++) {
  464. unsigned char bits = g[row];
  465. if (!bits)
  466. continue;
  467. for (col = 0; col < 8; col++) {
  468. if (!(bits & (0x80u >> col)))
  469. continue;
  470. ngui_pixel(ui,
  471. x + (int)(n - 1 - i) * 8 + 7 - col,
  472. y + FONT_8X16_ROWS - 1 - row,
  473. color);
  474. }
  475. }
  476. }
  477. }
  478. void ngui_text_ellipsis(struct ngui *ui, struct ngui_rect r, int x, int y,
  479. const char *s, unsigned short color)
  480. {
  481. unsigned int n = slen(s);
  482. int max = (r.x + r.w - x) / 8;
  483. unsigned int i;
  484. if (max <= 0)
  485. return;
  486. if (n <= (unsigned int)max || max < 4) {
  487. for (i = 0; i < n && (int)i < max; i++)
  488. ngui_glyph(ui, x + (int)i * 8, y,
  489. (unsigned char)s[i], color, 1, 0);
  490. return;
  491. }
  492. for (i = 0; i < (unsigned int)(max - 3); i++)
  493. ngui_glyph(ui, x + (int)i * 8, y, (unsigned char)s[i], color, 1, 0);
  494. ngui_glyph(ui, x + (max - 3) * 8, y, '.', color, 1, 0);
  495. ngui_glyph(ui, x + (max - 2) * 8, y, '.', color, 1, 0);
  496. ngui_glyph(ui, x + (max - 1) * 8, y, '.', color, 1, 0);
  497. }
  498. void ngui_icon(struct ngui *ui, struct ngui_rect r, enum ngui_icon icon,
  499. unsigned short color)
  500. {
  501. int x = r.x + r.w / 2 - 8;
  502. int y = r.y + r.h / 2 - 8;
  503. switch (icon) {
  504. case NGUI_ICON_FOLDER:
  505. ngui_fill_rect(ui, rect_make(x + 1, y + 3, 7, 4), color);
  506. ngui_fill_rect(ui, rect_make(x, y + 6, 16, 9), color);
  507. break;
  508. case NGUI_ICON_FILE:
  509. ngui_frame(ui, rect_make(x + 2, y, 12, 16), color);
  510. ngui_line(ui, x + 10, y, x + 13, y + 3, color);
  511. ngui_fill_rect(ui, rect_make(x + 4, y + 7, 8, 1), color);
  512. ngui_fill_rect(ui, rect_make(x + 4, y + 10, 8, 1), color);
  513. break;
  514. case NGUI_ICON_DISK:
  515. ngui_frame(ui, rect_make(x, y + 2, 16, 12), color);
  516. ngui_fill_rect(ui, rect_make(x + 2, y + 5, 12, 2), color);
  517. ngui_fill_rect(ui, rect_make(x + 11, y + 9, 3, 3), color);
  518. break;
  519. case NGUI_ICON_UP:
  520. ngui_line(ui, x + 8, y + 2, x + 3, y + 8, color);
  521. ngui_line(ui, x + 8, y + 2, x + 13, y + 8, color);
  522. ngui_fill_rect(ui, rect_make(x + 6, y + 7, 5, 7), color);
  523. break;
  524. case NGUI_ICON_HOME:
  525. ngui_line(ui, x + 2, y + 8, x + 8, y + 2, color);
  526. ngui_line(ui, x + 8, y + 2, x + 14, y + 8, color);
  527. ngui_frame(ui, rect_make(x + 3, y + 8, 10, 7), color);
  528. ngui_fill_rect(ui, rect_make(x + 7, y + 11, 3, 4), color);
  529. break;
  530. case NGUI_ICON_REFRESH:
  531. ngui_line(ui, x + 3, y + 8, x + 6, y + 4, color);
  532. ngui_line(ui, x + 6, y + 4, x + 10, y + 3, color);
  533. ngui_line(ui, x + 10, y + 3, x + 13, y + 6, color);
  534. ngui_line(ui, x + 13, y + 6, x + 13, y + 9, color);
  535. ngui_fill_rect(ui, rect_make(x + 11, y + 1, 4, 4), color);
  536. break;
  537. case NGUI_ICON_PLUS:
  538. ngui_fill_rect(ui, rect_make(x + 7, y + 3, 2, 10), color);
  539. ngui_fill_rect(ui, rect_make(x + 3, y + 7, 10, 2), color);
  540. break;
  541. case NGUI_ICON_CHEVRON_LEFT:
  542. ngui_line(ui, x + 10, y + 3, x + 6, y + 8, color);
  543. ngui_line(ui, x + 6, y + 8, x + 10, y + 13, color);
  544. break;
  545. case NGUI_ICON_CHEVRON_RIGHT:
  546. ngui_line(ui, x + 6, y + 3, x + 10, y + 8, color);
  547. ngui_line(ui, x + 10, y + 8, x + 6, y + 13, color);
  548. break;
  549. default:
  550. break;
  551. }
  552. }
  553. int ngui_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  554. const char *label)
  555. {
  556. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  557. int press_hot = ui->pressed &&
  558. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  559. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  560. int fired = 0;
  561. unsigned short bg;
  562. int tw, tx, ty;
  563. if (press_hot)
  564. ui->active = id;
  565. if (ui->pressed && ui->released && press_hot && release_hot)
  566. fired = 1;
  567. if (ui->released && ui->active == id) {
  568. if (release_hot)
  569. fired = 1;
  570. ui->active = 0;
  571. }
  572. if (ui->active == id)
  573. bg = ui->theme.button_down;
  574. else if (hot)
  575. bg = ui->theme.button_hot;
  576. else
  577. bg = ui->theme.button;
  578. ngui_fill_rect(ui, r, bg);
  579. ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
  580. if (label && label[0]) {
  581. tw = (int)ngui_text_width(label);
  582. tx = r.x + (r.w - tw) / 2;
  583. ty = r.y + (r.h - 16) / 2;
  584. ngui_push_clip(ui, r);
  585. ngui_text(ui, tx, ty, label, ui->theme.text);
  586. ngui_pop_clip(ui);
  587. }
  588. if (hot)
  589. ui->hot = id;
  590. return fired;
  591. }
  592. /* One row of a drop-down menu: left-aligned label, optional check mark and
  593. * right-aligned shortcut. Uses the same press/release/active semantics as
  594. * ngui_button so a menu and its caller never disagree about a click. */
  595. int ngui_menu_item(struct ngui *ui, unsigned int id, struct ngui_rect r,
  596. const char *label, const char *shortcut, int checked)
  597. {
  598. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  599. int press_hot = ui->pressed &&
  600. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  601. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  602. int fired = 0;
  603. int selected;
  604. unsigned short bg, fg;
  605. int ty;
  606. if (press_hot)
  607. ui->active = id;
  608. if (ui->pressed && ui->released && press_hot && release_hot)
  609. fired = 1;
  610. if (ui->released && ui->active == id) {
  611. if (release_hot)
  612. fired = 1;
  613. ui->active = 0;
  614. }
  615. selected = (ui->active == id) || hot;
  616. bg = selected ? ui->theme.selection : ui->theme.panel;
  617. fg = selected ? NGUI_RGB(255, 255, 255) : ui->theme.text;
  618. ngui_fill_rect(ui, r, bg);
  619. ty = r.y + (r.h - 16) / 2;
  620. ngui_push_clip(ui, r);
  621. if (checked) {
  622. ngui_line(ui, r.x + 8, ty + 10, r.x + 10, ty + 13, fg);
  623. ngui_line(ui, r.x + 10, ty + 13, r.x + 15, ty + 5, fg);
  624. }
  625. if (label)
  626. ngui_text(ui, r.x + 22, ty, label, fg);
  627. if (shortcut) {
  628. int sw = (int)ngui_text_width(shortcut);
  629. ngui_text(ui, r.x + r.w - 8 - sw, ty, shortcut, fg);
  630. }
  631. ngui_pop_clip(ui);
  632. if (hot)
  633. ui->hot = id;
  634. return fired;
  635. }
  636. int ngui_icon_button(struct ngui *ui, unsigned int id, struct ngui_rect r,
  637. enum ngui_icon icon){
  638. int hot = ui->inside && ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y);
  639. int press_hot = ui->pressed &&
  640. ngui_rect_contains(&r, ui->press_x, ui->press_y);
  641. int release_hot = ngui_rect_contains(&r, ui->release_x, ui->release_y);
  642. int fired = 0;
  643. unsigned short bg;
  644. if (press_hot)
  645. ui->active = id;
  646. if (ui->pressed && ui->released && press_hot && release_hot)
  647. fired = 1;
  648. if (ui->released && ui->active == id) {
  649. if (release_hot)
  650. fired = 1;
  651. ui->active = 0;
  652. }
  653. if (ui->active == id)
  654. bg = ui->theme.button_down;
  655. else if (hot)
  656. bg = ui->theme.button_hot;
  657. else
  658. bg = ui->theme.button;
  659. ngui_fill_rect(ui, r, bg);
  660. ngui_frame(ui, r, hot ? ui->theme.accent : ui->theme.border);
  661. ngui_icon(ui, r, icon, ui->theme.text);
  662. if (hot)
  663. ui->hot = id;
  664. return fired;
  665. }
  666. void ngui_input_set(struct ngui_input *in, const char *s)
  667. {
  668. scopy(in->text, s, NGUI_INPUT_MAX);
  669. in->len = slen(in->text);
  670. in->cursor = in->len;
  671. in->scroll = 0;
  672. in->anchor = in->cursor;
  673. in->dragging = 0;
  674. in->last_click_ms = 0;
  675. }
  676. static int input_word_char(char c)
  677. {
  678. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  679. (c >= '0' && c <= '9') || c == '_';
  680. }
  681. /* Next word start after `pos` (Windows edit-control semantics). */
  682. static unsigned int input_word_next(const struct ngui_input *in,
  683. unsigned int pos)
  684. {
  685. while (pos < in->len && input_word_char(in->text[pos]))
  686. pos++;
  687. while (pos < in->len && !input_word_char(in->text[pos]))
  688. pos++;
  689. return pos;
  690. }
  691. /* Start of the previous word before `pos`. */
  692. static unsigned int input_word_prev(const struct ngui_input *in,
  693. unsigned int pos)
  694. {
  695. while (pos > 0 && !input_word_char(in->text[pos - 1]))
  696. pos--;
  697. while (pos > 0 && input_word_char(in->text[pos - 1]))
  698. pos--;
  699. return pos;
  700. }
  701. /* End of the word to the right of `pos`. A caret inside a word takes the
  702. * rest of that word and its trailing separators; a caret on separators takes
  703. * them plus the next word. */
  704. static unsigned int input_word_delete_end(const struct ngui_input *in,
  705. unsigned int pos)
  706. {
  707. if (pos < in->len && input_word_char(in->text[pos])) {
  708. while (pos < in->len && input_word_char(in->text[pos]))
  709. pos++;
  710. while (pos < in->len && !input_word_char(in->text[pos]))
  711. pos++;
  712. return pos;
  713. }
  714. while (pos < in->len && !input_word_char(in->text[pos]))
  715. pos++;
  716. while (pos < in->len && input_word_char(in->text[pos]))
  717. pos++;
  718. return pos;
  719. }
  720. static void input_sel_bounds(const struct ngui_input *in,
  721. unsigned int *a, unsigned int *b)
  722. {
  723. if (in->anchor < in->cursor) {
  724. *a = in->anchor;
  725. *b = in->cursor;
  726. } else {
  727. *a = in->cursor;
  728. *b = in->anchor;
  729. }
  730. }
  731. /* Remove [a, b) and keep cursor/anchor consistent. Returns 1 when the text
  732. * changed. */
  733. static int input_delete_range(struct ngui_input *in, unsigned int a,
  734. unsigned int b)
  735. {
  736. unsigned int i;
  737. if (a >= b || b > in->len)
  738. return 0;
  739. for (i = b; i < in->len; i++)
  740. in->text[i - (b - a)] = in->text[i];
  741. in->len -= b - a;
  742. in->text[in->len] = '\0';
  743. if (in->cursor >= b)
  744. in->cursor -= b - a;
  745. else if (in->cursor > a)
  746. in->cursor = a;
  747. if (in->anchor >= b)
  748. in->anchor -= b - a;
  749. else if (in->anchor > a)
  750. in->anchor = a;
  751. return 1;
  752. }
  753. static int input_delete_selection(struct ngui_input *in)
  754. {
  755. unsigned int a, b;
  756. input_sel_bounds(in, &a, &b);
  757. return input_delete_range(in, a, b);
  758. }
  759. static void input_insert(struct ngui_input *in, char c)
  760. {
  761. unsigned int i;
  762. input_delete_selection(in);
  763. if (in->len + 1 >= NGUI_INPUT_MAX)
  764. return;
  765. i = in->len;
  766. while (i > in->cursor) {
  767. in->text[i] = in->text[i - 1];
  768. i--;
  769. }
  770. in->text[in->cursor] = c;
  771. in->cursor++;
  772. in->len++;
  773. in->text[in->len] = '\0';
  774. in->anchor = in->cursor;
  775. }
  776. static void input_delete(struct ngui_input *in, int back)
  777. {
  778. unsigned int pos;
  779. if (back) {
  780. if (in->cursor == 0)
  781. return;
  782. pos = in->cursor - 1;
  783. } else {
  784. if (in->cursor >= in->len)
  785. return;
  786. pos = in->cursor;
  787. }
  788. input_delete_range(in, pos, pos + 1);
  789. }
  790. static void input_move_to(struct ngui_input *in, unsigned int pos)
  791. {
  792. if (pos > in->len)
  793. pos = in->len;
  794. in->cursor = pos;
  795. in->anchor = pos;
  796. }
  797. /* Two presses in the same field within this window select all of its text. */
  798. #define NGUI_DOUBLE_CLICK_MS 500
  799. /* Character position under a content-relative x, clamped to the visible
  800. * part of the field. */
  801. static unsigned int input_pos_at(const struct ngui_input *in, int inner_x,
  802. int cols, int x)
  803. {
  804. int col = (x - inner_x) / 8;
  805. unsigned int pos;
  806. if (col < 0)
  807. col = 0;
  808. if (cols > 0 && col > cols)
  809. col = cols;
  810. pos = in->scroll + (unsigned int)col;
  811. if (pos > in->len)
  812. pos = in->len;
  813. return pos;
  814. }
  815. static void input_draw_selection(struct ngui *ui, const struct ngui_input *in,
  816. struct ngui_rect inner, int cols)
  817. {
  818. unsigned int a, b, va, vb;
  819. input_sel_bounds(in, &a, &b);
  820. if (a == b || cols <= 0)
  821. return;
  822. va = a < in->scroll ? in->scroll : a;
  823. vb = b > in->scroll + (unsigned int)cols
  824. ? in->scroll + (unsigned int)cols : b;
  825. if (va < vb)
  826. ngui_fill_rect(ui, rect_make(inner.x + (int)(va - in->scroll) * 8,
  827. inner.y, (int)(vb - va) * 8, 16),
  828. ui->theme.selection);
  829. }
  830. int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
  831. struct ngui_input *in)
  832. {
  833. struct ngui_rect inner;
  834. int focused = (ui->focus == id);
  835. int result = 0;
  836. int cols, k;
  837. int changed = 0;
  838. inner = rect_make(r.x + 4, r.y + (r.h - 16) / 2, r.w - 8, 16);
  839. if (inner.w < 0)
  840. inner.w = 0;
  841. cols = inner.w / 8;
  842. if (cols < 0)
  843. cols = 0;
  844. if (ui->pressed &&
  845. ngui_rect_contains(&r, ui->press_x, ui->press_y)) {
  846. unsigned int now = u_ticks_ms();
  847. int dbl = in->last_click_ms &&
  848. (unsigned int)(now - in->last_click_ms) <=
  849. NGUI_DOUBLE_CLICK_MS;
  850. if (dbl) {
  851. /* Double click: select the whole text and keep it
  852. * selected while the second press is held. */
  853. in->anchor = 0;
  854. in->cursor = in->len;
  855. in->dragging = 0;
  856. } else {
  857. unsigned int pos = input_pos_at(in, inner.x, cols,
  858. ui->press_x);
  859. in->cursor = pos;
  860. in->anchor = pos;
  861. in->dragging = 1;
  862. }
  863. in->last_click_ms = now;
  864. ui->focus = id;
  865. ui->focus_claimed = 1;
  866. ui->last_blink = now;
  867. ui->caret_on = 1;
  868. focused = 1;
  869. } else if (ui->pressed) {
  870. in->dragging = 0;
  871. }
  872. /* Extend the selection while the button that started in this field is
  873. * still held. Sampled coordinates may be outside the canvas. The
  874. * release *event* is authoritative: the sampled button state can lag
  875. * the event queue by a frame. */
  876. if (in->dragging && (ui->buttons & 1) && !ui->released) {
  877. in->cursor = input_pos_at(in, inner.x, cols, ui->mouse_x);
  878. /* Keep redrawing when the pointer leaves the window. */
  879. ngui_invalidate(ui);
  880. }
  881. if (ui->released)
  882. in->dragging = 0;
  883. if (focused) {
  884. while ((k = ngui_key(ui)) != 0) {
  885. if (k == '\n' || k == '\r') {
  886. result |= NGUI_INPUT_SUBMIT;
  887. break;
  888. }
  889. if (k == NGUI_KEY_ESC || k == NGUI_KEY_TAB) {
  890. result |= NGUI_INPUT_CANCEL;
  891. ui->focus = 0;
  892. break;
  893. }
  894. if (k == NGUI_KEY_BACKSPACE || k == 0x7F) {
  895. if (!input_delete_selection(in))
  896. input_delete(in, 1);
  897. changed = 1;
  898. } else if (k == NGUI_KEY_DELETE) {
  899. if (!input_delete_selection(in))
  900. input_delete(in, 0);
  901. changed = 1;
  902. } else if (k == NGUI_KEY_CTRL_BACKSPACE) {
  903. if (input_delete_selection(in))
  904. changed = 1;
  905. else
  906. changed = input_delete_range(in,
  907. input_word_prev(in, in->cursor),
  908. in->cursor);
  909. } else if (k == NGUI_KEY_CTRL_DELETE) {
  910. if (input_delete_selection(in)) {
  911. changed = 1;
  912. } else {
  913. unsigned int a = in->cursor;
  914. unsigned int b = input_word_delete_end(in,
  915. a);
  916. /* At the end of the line, delete the word
  917. * before the caret instead of doing
  918. * nothing. */
  919. if (b == a) {
  920. a = input_word_prev(in, in->cursor);
  921. b = in->cursor;
  922. }
  923. changed = input_delete_range(in, a, b);
  924. }
  925. } else if (k == NGUI_KEY_LEFT) {
  926. input_move_to(in, in->cursor > 0 ? in->cursor - 1 : 0);
  927. } else if (k == NGUI_KEY_RIGHT) {
  928. input_move_to(in, in->cursor < in->len
  929. ? in->cursor + 1 : in->len);
  930. } else if (k == NGUI_KEY_CTRL_LEFT) {
  931. input_move_to(in, input_word_prev(in, in->cursor));
  932. } else if (k == NGUI_KEY_CTRL_RIGHT) {
  933. input_move_to(in, input_word_next(in, in->cursor));
  934. } else if (k == NGUI_KEY_HOME) {
  935. input_move_to(in, 0);
  936. } else if (k == NGUI_KEY_END) {
  937. input_move_to(in, in->len);
  938. } else if (k == 0x01) { /* Ctrl+A: select all */
  939. in->anchor = 0;
  940. in->cursor = in->len;
  941. } else if (k == 0x15) { /* Ctrl+U */
  942. in->len = 0;
  943. in->cursor = 0;
  944. in->anchor = 0;
  945. in->text[0] = '\0';
  946. changed = 1;
  947. } else if (k >= 0x20 && k <= 0x7E) {
  948. input_insert(in, (char)k);
  949. changed = 1;
  950. }
  951. }
  952. }
  953. ngui_fill_rect(ui, r, focused ? ui->theme.panel_alt : ui->theme.panel);
  954. ngui_frame(ui, r, focused ? ui->theme.accent : ui->theme.border);
  955. if (in->scroll > in->len)
  956. in->scroll = in->len;
  957. if (in->cursor < in->scroll)
  958. in->scroll = in->cursor;
  959. if (cols > 0 && in->cursor >= in->scroll + (unsigned int)cols)
  960. in->scroll = in->cursor - (unsigned int)cols + 1;
  961. if (in->scroll > in->len)
  962. in->scroll = in->len;
  963. ngui_push_clip(ui, inner);
  964. if (in->len == 0 && in->placeholder) {
  965. ngui_text(ui, inner.x, inner.y, in->placeholder,
  966. ui->theme.text_dim);
  967. } else {
  968. if (focused)
  969. input_draw_selection(ui, in, inner, cols);
  970. ngui_text(ui, inner.x, inner.y, in->text + in->scroll,
  971. ui->theme.text);
  972. }
  973. if (focused && ui->caret_on) {
  974. int col = (int)in->cursor - (int)in->scroll;
  975. if (col >= 0 && col <= cols)
  976. ngui_fill_rect(ui, rect_make(inner.x + col * 8, inner.y,
  977. 1, 16), ui->theme.accent);
  978. }
  979. ngui_pop_clip(ui);
  980. if (changed)
  981. result |= NGUI_INPUT_CHANGED;
  982. return result;
  983. }
  984. static int scrollable(const struct ngui_scroll *s)
  985. {
  986. return s->content_h > s->view_h && s->view_h > 0;
  987. }
  988. static int scroll_thumb_h(const struct ngui_rect *r,
  989. const struct ngui_scroll *s)
  990. {
  991. int h = 0;
  992. if (s->content_h > 0)
  993. h = r->h * s->view_h / s->content_h;
  994. if (h < 12)
  995. h = 12;
  996. if (h > r->h)
  997. h = r->h;
  998. return h;
  999. }
  1000. static int scroll_thumb_y(const struct ngui_rect *r,
  1001. const struct ngui_scroll *s)
  1002. {
  1003. int th = scroll_thumb_h(r, s);
  1004. int avail = r->h - th;
  1005. int max = s->content_h - s->view_h;
  1006. if (avail <= 0 || max <= 0)
  1007. return r->y;
  1008. return r->y + s->offset * avail / max;
  1009. }
  1010. static void scroll_clamp(struct ngui_scroll *s)
  1011. {
  1012. int max = s->content_h - s->view_h;
  1013. if (max < 0)
  1014. max = 0;
  1015. if (s->offset > max)
  1016. s->offset = max;
  1017. if (s->offset < 0)
  1018. s->offset = 0;
  1019. }
  1020. void ngui_scroll_to(struct ngui_scroll *s, int offset)
  1021. {
  1022. s->offset = offset;
  1023. scroll_clamp(s);
  1024. }
  1025. void ngui_scroll_ensure_visible(struct ngui_scroll *s, int y, int h)
  1026. {
  1027. if (y < s->offset)
  1028. s->offset = y;
  1029. else if (y + h > s->offset + s->view_h)
  1030. s->offset = y + h - s->view_h;
  1031. scroll_clamp(s);
  1032. }
  1033. void ngui_scroll_begin(struct ngui *ui, struct ngui_rect r,
  1034. struct ngui_scroll *s)
  1035. {
  1036. struct ngui_rect bar;
  1037. int scrolls = scrollable(s);
  1038. s->view_h = r.h;
  1039. scroll_clamp(s);
  1040. if (!ui->buttons)
  1041. s->dragging = 0;
  1042. if (scrolls) {
  1043. int th = scroll_thumb_h(&r, s);
  1044. int ty = scroll_thumb_y(&r, s);
  1045. bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
  1046. if (ui->pressed &&
  1047. ngui_rect_contains(&bar, ui->press_x, ui->press_y)) {
  1048. if (ui->press_y >= ty && ui->press_y < ty + th) {
  1049. s->dragging = 1;
  1050. s->drag_dy = ui->press_y - ty;
  1051. } else {
  1052. int rel = ui->press_y - r.y - th / 2;
  1053. int avail = r.h - th;
  1054. int max = s->content_h - s->view_h;
  1055. if (rel < 0)
  1056. rel = 0;
  1057. if (rel > avail)
  1058. rel = avail;
  1059. if (avail > 0)
  1060. s->offset = rel * max / avail;
  1061. s->dragging = 1;
  1062. s->drag_dy = th / 2;
  1063. scroll_clamp(s);
  1064. }
  1065. }
  1066. /* Sampled buttons are authoritative while held: some event paths
  1067. * deliver the press late or coalesce it with the release. Start
  1068. * the drag from the current pointer position as well. */
  1069. if (!s->dragging && (ui->buttons & 1) &&
  1070. ngui_rect_contains(&bar, ui->mouse_x, ui->mouse_y)) {
  1071. if (ui->mouse_y >= ty && ui->mouse_y < ty + th) {
  1072. s->dragging = 1;
  1073. s->drag_dy = ui->mouse_y - ty;
  1074. } else {
  1075. int rel = ui->mouse_y - r.y - th / 2;
  1076. int avail = r.h - th;
  1077. int max = s->content_h - s->view_h;
  1078. if (rel < 0)
  1079. rel = 0;
  1080. if (rel > avail)
  1081. rel = avail;
  1082. if (avail > 0)
  1083. s->offset = rel * max / avail;
  1084. s->dragging = 1;
  1085. s->drag_dy = th / 2;
  1086. scroll_clamp(s);
  1087. }
  1088. }
  1089. if (s->dragging && (ui->buttons & 1)) {
  1090. int rel = ui->mouse_y - r.y - s->drag_dy;
  1091. int avail = r.h - th;
  1092. int max = s->content_h - s->view_h;
  1093. if (rel < 0)
  1094. rel = 0;
  1095. if (rel > avail)
  1096. rel = avail;
  1097. if (avail > 0)
  1098. s->offset = rel * max / avail;
  1099. scroll_clamp(s);
  1100. }
  1101. if (ui->released)
  1102. s->dragging = 0;
  1103. }
  1104. ngui_push_clip(ui, r);
  1105. }
  1106. void ngui_scroll_end(struct ngui *ui, struct ngui_rect r,
  1107. struct ngui_scroll *s)
  1108. {
  1109. ngui_pop_clip(ui);
  1110. if (!scrollable(s))
  1111. return;
  1112. {
  1113. struct ngui_rect bar = rect_make(r.x + r.w - 15, r.y, 14, r.h);
  1114. int th = scroll_thumb_h(&r, s);
  1115. int ty = scroll_thumb_y(&r, s);
  1116. struct ngui_rect thumb = rect_make(bar.x, ty, 14, th);
  1117. ngui_fill_rect(ui, bar, ui->theme.panel_alt);
  1118. ngui_fill_rect(ui, thumb, s->dragging ?
  1119. ui->theme.accent : ui->theme.text_dim);
  1120. }
  1121. }
  1122. int ngui_scroll_keys(struct ngui *ui, struct ngui_scroll *s)
  1123. {
  1124. int k = ngui_key_peek(ui);
  1125. int page;
  1126. if (k != NGUI_KEY_PAGE_UP && k != NGUI_KEY_PAGE_DOWN)
  1127. return 0;
  1128. page = s->view_h - 22;
  1129. if (page < 22)
  1130. page = 22;
  1131. ngui_key(ui);
  1132. if (k == NGUI_KEY_PAGE_UP)
  1133. s->offset -= page;
  1134. else
  1135. s->offset += page;
  1136. scroll_clamp(s);
  1137. return 1;
  1138. }