nuklear_toggle.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include "nuklear.h"
  2. #include "nuklear_internal.h"
  3. /* ===============================================================
  4. *
  5. * TOGGLE
  6. *
  7. * ===============================================================*/
  8. NK_LIB nk_bool
  9. nk_toggle_behavior(const struct nk_input *in, struct nk_rect select,
  10. nk_flags *state, nk_bool active)
  11. {
  12. nk_widget_state_reset(state);
  13. if (nk_button_behavior(state, select, in, NK_BUTTON_DEFAULT)) {
  14. *state = NK_WIDGET_STATE_ACTIVE;
  15. active = !active;
  16. }
  17. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, select))
  18. *state |= NK_WIDGET_STATE_ENTERED;
  19. else if (nk_input_is_mouse_prev_hovering_rect(in, select))
  20. *state |= NK_WIDGET_STATE_LEFT;
  21. return active;
  22. }
  23. NK_LIB void
  24. nk_draw_checkbox(struct nk_command_buffer *out,
  25. nk_flags state, const struct nk_style_toggle *style, nk_bool active,
  26. const struct nk_rect *label, const struct nk_rect *selector,
  27. const struct nk_rect *cursors, const char *string, int len,
  28. const struct nk_user_font *font, nk_flags text_alignment)
  29. {
  30. const struct nk_style_item *background;
  31. const struct nk_style_item *cursor;
  32. struct nk_text text;
  33. /* select correct colors/images */
  34. if (state & NK_WIDGET_STATE_HOVER) {
  35. background = &style->hover;
  36. cursor = &style->cursor_hover;
  37. text.text = style->text_hover;
  38. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  39. background = &style->hover;
  40. cursor = &style->cursor_hover;
  41. text.text = style->text_active;
  42. } else {
  43. background = &style->normal;
  44. cursor = &style->cursor_normal;
  45. text.text = style->text_normal;
  46. }
  47. text.text = nk_rgb_factor(text.text, style->color_factor);
  48. text.padding.x = 0;
  49. text.padding.y = 0;
  50. text.background = style->text_background;
  51. nk_widget_text(out, *label, string, len, &text, text_alignment, font);
  52. /* draw background and cursor */
  53. if (background->type == NK_STYLE_ITEM_COLOR) {
  54. nk_fill_rect(out, *selector, 0, nk_rgb_factor(style->border_color, style->color_factor));
  55. nk_fill_rect(out, nk_shrink_rect(*selector, style->border), 0, nk_rgb_factor(background->data.color, style->color_factor));
  56. } else nk_draw_image(out, *selector, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
  57. if (active) {
  58. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  59. nk_draw_image(out, *cursors, &cursor->data.image, nk_rgb_factor(nk_white, style->color_factor));
  60. else nk_fill_rect(out, *cursors, 0, cursor->data.color);
  61. }
  62. }
  63. NK_LIB void
  64. nk_draw_option(struct nk_command_buffer *out,
  65. nk_flags state, const struct nk_style_toggle *style, nk_bool active,
  66. const struct nk_rect *label, const struct nk_rect *selector,
  67. const struct nk_rect *cursors, const char *string, int len,
  68. const struct nk_user_font *font, nk_flags text_alignment)
  69. {
  70. const struct nk_style_item *background;
  71. const struct nk_style_item *cursor;
  72. struct nk_text text;
  73. /* select correct colors/images */
  74. if (state & NK_WIDGET_STATE_HOVER) {
  75. background = &style->hover;
  76. cursor = &style->cursor_hover;
  77. text.text = style->text_hover;
  78. } else if (state & NK_WIDGET_STATE_ACTIVED) {
  79. background = &style->hover;
  80. cursor = &style->cursor_hover;
  81. text.text = style->text_active;
  82. } else {
  83. background = &style->normal;
  84. cursor = &style->cursor_normal;
  85. text.text = style->text_normal;
  86. }
  87. text.text = nk_rgb_factor(text.text, style->color_factor);
  88. text.padding.x = 0;
  89. text.padding.y = 0;
  90. text.background = style->text_background;
  91. nk_widget_text(out, *label, string, len, &text, text_alignment, font);
  92. /* draw background and cursor */
  93. if (background->type == NK_STYLE_ITEM_COLOR) {
  94. nk_fill_circle(out, *selector, nk_rgb_factor(style->border_color, style->color_factor));
  95. nk_fill_circle(out, nk_shrink_rect(*selector, style->border), nk_rgb_factor(background->data.color, style->color_factor));
  96. } else nk_draw_image(out, *selector, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
  97. if (active) {
  98. if (cursor->type == NK_STYLE_ITEM_IMAGE)
  99. nk_draw_image(out, *cursors, &cursor->data.image, nk_rgb_factor(nk_white, style->color_factor));
  100. else nk_fill_circle(out, *cursors, cursor->data.color);
  101. }
  102. }
  103. NK_LIB nk_bool
  104. nk_do_toggle(nk_flags *state,
  105. struct nk_command_buffer *out, struct nk_rect r,
  106. nk_bool *active, const char *str, int len, enum nk_toggle_type type,
  107. const struct nk_style_toggle *style, const struct nk_input *in,
  108. const struct nk_user_font *font, nk_flags widget_alignment, nk_flags text_alignment)
  109. {
  110. int was_active;
  111. struct nk_rect bounds;
  112. struct nk_rect select;
  113. struct nk_rect cursor;
  114. struct nk_rect label;
  115. NK_ASSERT(style);
  116. NK_ASSERT(out);
  117. NK_ASSERT(font);
  118. if (!out || !style || !font || !active)
  119. return 0;
  120. r.w = NK_MAX(r.w, font->height + 2 * style->padding.x);
  121. r.h = NK_MAX(r.h, font->height + 2 * style->padding.y);
  122. /* add additional touch padding for touch screen devices */
  123. bounds.x = r.x - style->touch_padding.x;
  124. bounds.y = r.y - style->touch_padding.y;
  125. bounds.w = r.w + 2 * style->touch_padding.x;
  126. bounds.h = r.h + 2 * style->touch_padding.y;
  127. /* calculate the selector space */
  128. select.w = font->height;
  129. select.h = select.w;
  130. if (widget_alignment & NK_WIDGET_ALIGN_RIGHT) {
  131. select.x = r.x + r.w - font->height;
  132. /* label in front of the selector */
  133. label.x = r.x;
  134. label.w = r.w - select.w - style->spacing * 2;
  135. } else if (widget_alignment & NK_WIDGET_ALIGN_CENTERED) {
  136. select.x = r.x + (r.w - select.w) / 2;
  137. /* label in front of selector */
  138. label.x = r.x;
  139. label.w = (r.w - select.w - style->spacing * 2) / 2;
  140. } else { /* Default: NK_WIDGET_ALIGN_LEFT */
  141. select.x = r.x;
  142. /* label behind the selector */
  143. label.x = select.x + select.w + style->spacing;
  144. label.w = NK_MAX(r.x + r.w, label.x) - label.x;
  145. }
  146. if (widget_alignment & NK_WIDGET_ALIGN_TOP) {
  147. select.y = r.y;
  148. } else if (widget_alignment & NK_WIDGET_ALIGN_BOTTOM) {
  149. select.y = r.y + r.h - select.h - 2 * style->padding.y;
  150. } else { /* Default: NK_WIDGET_ALIGN_MIDDLE */
  151. select.y = r.y + r.h/2.0f - select.h/2.0f;
  152. }
  153. label.y = select.y;
  154. label.h = select.w;
  155. /* calculate the bounds of the cursor inside the selector */
  156. cursor.x = select.x + style->padding.x + style->border;
  157. cursor.y = select.y + style->padding.y + style->border;
  158. cursor.w = select.w - (2 * style->padding.x + 2 * style->border);
  159. cursor.h = select.h - (2 * style->padding.y + 2 * style->border);
  160. /* update selector */
  161. was_active = *active;
  162. *active = nk_toggle_behavior(in, bounds, state, *active);
  163. /* draw selector */
  164. if (style->draw_begin)
  165. style->draw_begin(out, style->userdata);
  166. if (type == NK_TOGGLE_CHECK) {
  167. nk_draw_checkbox(out, *state, style, *active, &label, &select, &cursor, str, len, font, text_alignment);
  168. } else {
  169. nk_draw_option(out, *state, style, *active, &label, &select, &cursor, str, len, font, text_alignment);
  170. }
  171. if (style->draw_end)
  172. style->draw_end(out, style->userdata);
  173. return (was_active != *active);
  174. }
  175. /*----------------------------------------------------------------
  176. *
  177. * CHECKBOX
  178. *
  179. * --------------------------------------------------------------*/
  180. NK_API nk_bool
  181. nk_check_text(struct nk_context *ctx, const char *text, int len, nk_bool active)
  182. {
  183. struct nk_window *win;
  184. struct nk_panel *layout;
  185. const struct nk_input *in;
  186. const struct nk_style *style;
  187. struct nk_rect bounds;
  188. enum nk_widget_layout_states state;
  189. NK_ASSERT(ctx);
  190. NK_ASSERT(ctx->current);
  191. NK_ASSERT(ctx->current->layout);
  192. if (!ctx || !ctx->current || !ctx->current->layout)
  193. return active;
  194. win = ctx->current;
  195. style = &ctx->style;
  196. layout = win->layout;
  197. state = nk_widget(&bounds, ctx);
  198. if (!state) return active;
  199. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  200. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &active,
  201. text, len, NK_TOGGLE_CHECK, &style->checkbox, in, style->font, NK_WIDGET_LEFT, NK_TEXT_LEFT);
  202. return active;
  203. }
  204. NK_API nk_bool
  205. nk_check_text_align(struct nk_context *ctx, const char *text, int len, nk_bool active, nk_flags widget_alignment, nk_flags text_alignment)
  206. {
  207. struct nk_window *win;
  208. struct nk_panel *layout;
  209. const struct nk_input *in;
  210. const struct nk_style *style;
  211. struct nk_rect bounds;
  212. enum nk_widget_layout_states state;
  213. NK_ASSERT(ctx);
  214. NK_ASSERT(ctx->current);
  215. NK_ASSERT(ctx->current->layout);
  216. if (!ctx || !ctx->current || !ctx->current->layout)
  217. return active;
  218. win = ctx->current;
  219. style = &ctx->style;
  220. layout = win->layout;
  221. state = nk_widget(&bounds, ctx);
  222. if (!state) return active;
  223. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  224. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &active,
  225. text, len, NK_TOGGLE_CHECK, &style->checkbox, in, style->font, widget_alignment, text_alignment);
  226. return active;
  227. }
  228. NK_API unsigned int
  229. nk_check_flags_text(struct nk_context *ctx, const char *text, int len,
  230. unsigned int flags, unsigned int value)
  231. {
  232. int old_active;
  233. NK_ASSERT(ctx);
  234. NK_ASSERT(text);
  235. if (!ctx || !text) return flags;
  236. old_active = (int)((flags & value) & value);
  237. if (nk_check_text(ctx, text, len, old_active))
  238. flags |= value;
  239. else flags &= ~value;
  240. return flags;
  241. }
  242. NK_API nk_bool
  243. nk_checkbox_text(struct nk_context *ctx, const char *text, int len, nk_bool *active)
  244. {
  245. int old_val;
  246. NK_ASSERT(ctx);
  247. NK_ASSERT(text);
  248. NK_ASSERT(active);
  249. if (!ctx || !text || !active) return 0;
  250. old_val = *active;
  251. *active = nk_check_text(ctx, text, len, *active);
  252. return old_val != *active;
  253. }
  254. NK_API nk_bool
  255. nk_checkbox_text_align(struct nk_context *ctx, const char *text, int len, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment)
  256. {
  257. int old_val;
  258. NK_ASSERT(ctx);
  259. NK_ASSERT(text);
  260. NK_ASSERT(active);
  261. if (!ctx || !text || !active) return 0;
  262. old_val = *active;
  263. *active = nk_check_text_align(ctx, text, len, *active, widget_alignment, text_alignment);
  264. return old_val != *active;
  265. }
  266. NK_API nk_bool
  267. nk_checkbox_flags_text(struct nk_context *ctx, const char *text, int len,
  268. unsigned int *flags, unsigned int value)
  269. {
  270. nk_bool active;
  271. NK_ASSERT(ctx);
  272. NK_ASSERT(text);
  273. NK_ASSERT(flags);
  274. if (!ctx || !text || !flags) return 0;
  275. active = (int)((*flags & value) & value);
  276. if (nk_checkbox_text(ctx, text, len, &active)) {
  277. if (active) *flags |= value;
  278. else *flags &= ~value;
  279. return 1;
  280. }
  281. return 0;
  282. }
  283. NK_API nk_bool nk_check_label(struct nk_context *ctx, const char *label, nk_bool active)
  284. {
  285. return nk_check_text(ctx, label, nk_strlen(label), active);
  286. }
  287. NK_API unsigned int nk_check_flags_label(struct nk_context *ctx, const char *label,
  288. unsigned int flags, unsigned int value)
  289. {
  290. return nk_check_flags_text(ctx, label, nk_strlen(label), flags, value);
  291. }
  292. NK_API nk_bool nk_checkbox_label(struct nk_context *ctx, const char *label, nk_bool *active)
  293. {
  294. return nk_checkbox_text(ctx, label, nk_strlen(label), active);
  295. }
  296. NK_API nk_bool nk_checkbox_label_align(struct nk_context *ctx, const char *label, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment)
  297. {
  298. return nk_checkbox_text_align(ctx, label, nk_strlen(label), active, widget_alignment, text_alignment);
  299. }
  300. NK_API nk_bool nk_checkbox_flags_label(struct nk_context *ctx, const char *label,
  301. unsigned int *flags, unsigned int value)
  302. {
  303. return nk_checkbox_flags_text(ctx, label, nk_strlen(label), flags, value);
  304. }
  305. /*----------------------------------------------------------------
  306. *
  307. * OPTION
  308. *
  309. * --------------------------------------------------------------*/
  310. NK_API nk_bool
  311. nk_option_text(struct nk_context *ctx, const char *text, int len, nk_bool is_active)
  312. {
  313. struct nk_window *win;
  314. struct nk_panel *layout;
  315. const struct nk_input *in;
  316. const struct nk_style *style;
  317. struct nk_rect bounds;
  318. enum nk_widget_layout_states state;
  319. NK_ASSERT(ctx);
  320. NK_ASSERT(ctx->current);
  321. NK_ASSERT(ctx->current->layout);
  322. if (!ctx || !ctx->current || !ctx->current->layout)
  323. return is_active;
  324. win = ctx->current;
  325. style = &ctx->style;
  326. layout = win->layout;
  327. state = nk_widget(&bounds, ctx);
  328. if (!state) return (int)state;
  329. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  330. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &is_active,
  331. text, len, NK_TOGGLE_OPTION, &style->option, in, style->font, NK_WIDGET_LEFT, NK_TEXT_LEFT);
  332. return is_active;
  333. }
  334. NK_API nk_bool
  335. nk_option_text_align(struct nk_context *ctx, const char *text, int len, nk_bool is_active, nk_flags widget_alignment, nk_flags text_alignment)
  336. {
  337. struct nk_window *win;
  338. struct nk_panel *layout;
  339. const struct nk_input *in;
  340. const struct nk_style *style;
  341. struct nk_rect bounds;
  342. enum nk_widget_layout_states state;
  343. NK_ASSERT(ctx);
  344. NK_ASSERT(ctx->current);
  345. NK_ASSERT(ctx->current->layout);
  346. if (!ctx || !ctx->current || !ctx->current->layout)
  347. return is_active;
  348. win = ctx->current;
  349. style = &ctx->style;
  350. layout = win->layout;
  351. state = nk_widget(&bounds, ctx);
  352. if (!state) return (int)state;
  353. in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
  354. nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &is_active,
  355. text, len, NK_TOGGLE_OPTION, &style->option, in, style->font, widget_alignment, text_alignment);
  356. return is_active;
  357. }
  358. NK_API nk_bool
  359. nk_radio_text(struct nk_context *ctx, const char *text, int len, nk_bool *active)
  360. {
  361. int old_value;
  362. NK_ASSERT(ctx);
  363. NK_ASSERT(text);
  364. NK_ASSERT(active);
  365. if (!ctx || !text || !active) return 0;
  366. old_value = *active;
  367. *active = nk_option_text(ctx, text, len, old_value);
  368. return old_value != *active;
  369. }
  370. NK_API nk_bool
  371. nk_radio_text_align(struct nk_context *ctx, const char *text, int len, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment)
  372. {
  373. int old_value;
  374. NK_ASSERT(ctx);
  375. NK_ASSERT(text);
  376. NK_ASSERT(active);
  377. if (!ctx || !text || !active) return 0;
  378. old_value = *active;
  379. *active = nk_option_text_align(ctx, text, len, old_value, widget_alignment, text_alignment);
  380. return old_value != *active;
  381. }
  382. NK_API nk_bool
  383. nk_option_label(struct nk_context *ctx, const char *label, nk_bool active)
  384. {
  385. return nk_option_text(ctx, label, nk_strlen(label), active);
  386. }
  387. NK_API nk_bool
  388. nk_option_label_align(struct nk_context *ctx, const char *label, nk_bool active, nk_flags widget_alignment, nk_flags text_alignment)
  389. {
  390. return nk_option_text_align(ctx, label, nk_strlen(label), active, widget_alignment, text_alignment);
  391. }
  392. NK_API nk_bool
  393. nk_radio_label(struct nk_context *ctx, const char *label, nk_bool *active)
  394. {
  395. return nk_radio_text(ctx, label, nk_strlen(label), active);
  396. }
  397. NK_API nk_bool
  398. nk_radio_label_align(struct nk_context *ctx, const char *label, nk_bool *active, nk_flags widget_alignment, nk_flags text_alignment)
  399. {
  400. return nk_radio_text_align(ctx, label, nk_strlen(label), active, widget_alignment, text_alignment);
  401. }