Nuklear
This is a minimal-state, immediate-mode graphical user interface toolkit written in ANSI C and licensed under public domain. It was designed as a simple embeddable user interface for application and does not have any dependencies, a default render backend or OS window/input handling but instead provides a highly modular, library-based approach, with simple input state for input and draw commands describing primitive shapes as output. So instead of providing a layered library that tries to abstract over a number of platform and render backends, it focuses only on the actual UI.
 
Loading...
Searching...
No Matches
nuklear_button.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ==============================================================
5 *
6 * BUTTON
7 *
8 * ===============================================================*/
9NK_LIB void
10nk_draw_symbol(struct nk_command_buffer *out, enum nk_symbol_type type,
11 struct nk_rect content, struct nk_color background, struct nk_color foreground,
12 float border_width, const struct nk_user_font *font)
13{
14 switch (type) {
15 case NK_SYMBOL_X:
16 case NK_SYMBOL_UNDERSCORE:
17 case NK_SYMBOL_PLUS:
18 case NK_SYMBOL_MINUS: {
19 /* single character text symbol */
20 const char *X = (type == NK_SYMBOL_X) ? "x":
21 (type == NK_SYMBOL_UNDERSCORE) ? "_":
22 (type == NK_SYMBOL_PLUS) ? "+": "-";
23 struct nk_text text;
24 text.padding = nk_vec2(0,0);
25 text.background = background;
26 text.text = foreground;
27 nk_widget_text(out, content, X, 1, &text, NK_TEXT_CENTERED, font);
28 } break;
29 case NK_SYMBOL_CIRCLE_SOLID:
30 case NK_SYMBOL_CIRCLE_OUTLINE:
31 case NK_SYMBOL_RECT_SOLID:
32 case NK_SYMBOL_RECT_OUTLINE: {
33 /* simple empty/filled shapes */
34 if (type == NK_SYMBOL_RECT_SOLID || type == NK_SYMBOL_RECT_OUTLINE) {
35 nk_fill_rect(out, content, 0, foreground);
36 if (type == NK_SYMBOL_RECT_OUTLINE)
37 nk_fill_rect(out, nk_shrink_rect(content, border_width), 0, background);
38 } else {
39 nk_fill_circle(out, content, foreground);
40 if (type == NK_SYMBOL_CIRCLE_OUTLINE)
41 nk_fill_circle(out, nk_shrink_rect(content, 1), background);
42 }
43 } break;
44 case NK_SYMBOL_TRIANGLE_UP:
45 case NK_SYMBOL_TRIANGLE_DOWN:
46 case NK_SYMBOL_TRIANGLE_LEFT:
47 case NK_SYMBOL_TRIANGLE_RIGHT: {
48 enum nk_heading heading;
49 struct nk_vec2 points[3];
50 heading = (type == NK_SYMBOL_TRIANGLE_RIGHT) ? NK_RIGHT :
51 (type == NK_SYMBOL_TRIANGLE_LEFT) ? NK_LEFT:
52 (type == NK_SYMBOL_TRIANGLE_UP) ? NK_UP: NK_DOWN;
53 nk_triangle_from_direction(points, content, 0, 0, heading);
54 nk_fill_triangle(out, points[0].x, points[0].y, points[1].x, points[1].y,
55 points[2].x, points[2].y, foreground);
56 } break;
57 case NK_SYMBOL_TRIANGLE_UP_OUTLINE:
58 case NK_SYMBOL_TRIANGLE_DOWN_OUTLINE:
59 case NK_SYMBOL_TRIANGLE_LEFT_OUTLINE:
60 case NK_SYMBOL_TRIANGLE_RIGHT_OUTLINE: {
61 enum nk_heading heading;
62 struct nk_vec2 points[3];
63 heading = (type == NK_SYMBOL_TRIANGLE_RIGHT_OUTLINE) ? NK_RIGHT :
64 (type == NK_SYMBOL_TRIANGLE_LEFT_OUTLINE) ? NK_LEFT:
65 (type == NK_SYMBOL_TRIANGLE_UP_OUTLINE) ? NK_UP: NK_DOWN;
66 nk_triangle_from_direction(points, content, 0, 0, heading);
67 nk_stroke_triangle(out, points[0].x, points[0].y, points[1].x, points[1].y,
68 points[2].x, points[2].y, border_width, foreground);
69 } break;
70 default:
71 case NK_SYMBOL_NONE:
72 case NK_SYMBOL_MAX: break;
73 }
74}
75NK_LIB nk_bool
76nk_button_behavior(nk_flags *state, struct nk_rect r,
77 const struct nk_input *i, enum nk_button_behavior behavior)
78{
79 int ret = 0;
80 nk_widget_state_reset(state);
81 if (!i) return 0;
82 if (nk_input_is_mouse_hovering_rect(i, r)) {
84 if (nk_input_is_mouse_down(i, NK_BUTTON_LEFT))
86 if (nk_input_has_mouse_click_in_button_rect(i, NK_BUTTON_LEFT, r)) {
87 ret = (behavior != NK_BUTTON_DEFAULT) ?
88 nk_input_is_mouse_down(i, NK_BUTTON_LEFT):
89#ifdef NK_BUTTON_TRIGGER_ON_RELEASE
90 nk_input_is_mouse_released(i, NK_BUTTON_LEFT);
91#else
92 nk_input_is_mouse_pressed(i, NK_BUTTON_LEFT);
93#endif
94 }
95 }
96 if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(i, r))
98 else if (nk_input_is_mouse_prev_hovering_rect(i, r))
99 *state |= NK_WIDGET_STATE_LEFT;
100 return ret;
101}
102NK_LIB const struct nk_style_item*
103nk_draw_button(struct nk_command_buffer *out,
104 const struct nk_rect *bounds, nk_flags state,
105 const struct nk_style_button *style)
106{
107 const struct nk_style_item *background;
108 if (state & NK_WIDGET_STATE_HOVER)
109 background = &style->hover;
110 else if (state & NK_WIDGET_STATE_ACTIVED)
111 background = &style->active;
112 else background = &style->normal;
113
114 switch (background->type) {
115 case NK_STYLE_ITEM_IMAGE:
116 nk_draw_image(out, *bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor_background));
117 break;
118 case NK_STYLE_ITEM_NINE_SLICE:
119 nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor_background));
120 break;
121 case NK_STYLE_ITEM_COLOR:
122 nk_fill_rect(out, *bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor_background));
123 nk_stroke_rect(out, *bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor_background));
124 break;
125 }
126 return background;
127}
128NK_LIB nk_bool
129nk_do_button(nk_flags *state, struct nk_command_buffer *out, struct nk_rect r,
130 const struct nk_style_button *style, const struct nk_input *in,
131 enum nk_button_behavior behavior, struct nk_rect *content)
132{
133 struct nk_rect bounds;
134 NK_ASSERT(style);
135 NK_ASSERT(state);
136 NK_ASSERT(out);
137 if (!out || !style)
138 return nk_false;
139
140 /* calculate button content space */
141 content->x = r.x + style->padding.x + style->border + style->rounding;
142 content->y = r.y + style->padding.y + style->border + style->rounding;
143 content->w = r.w - (2 * (style->padding.x + style->border + style->rounding));
144 content->h = r.h - (2 * (style->padding.y + style->border + style->rounding));
145
146 /* execute button behavior */
147 bounds.x = r.x - style->touch_padding.x;
148 bounds.y = r.y - style->touch_padding.y;
149 bounds.w = r.w + 2 * style->touch_padding.x;
150 bounds.h = r.h + 2 * style->touch_padding.y;
151 return nk_button_behavior(state, bounds, in, behavior);
152}
153NK_LIB void
154nk_draw_button_text(struct nk_command_buffer *out,
155 const struct nk_rect *bounds, const struct nk_rect *content, nk_flags state,
156 const struct nk_style_button *style, const char *txt, int len,
157 nk_flags text_alignment, const struct nk_user_font *font)
158{
159 struct nk_text text;
160 const struct nk_style_item *background;
161 background = nk_draw_button(out, bounds, state, style);
162
163 /* select correct colors/images */
164 if (background->type == NK_STYLE_ITEM_COLOR)
165 text.background = background->data.color;
166 else text.background = style->text_background;
167 if (state & NK_WIDGET_STATE_HOVER)
168 text.text = style->text_hover;
169 else if (state & NK_WIDGET_STATE_ACTIVED)
170 text.text = style->text_active;
171 else text.text = style->text_normal;
172
173 text.text = nk_rgb_factor(text.text, style->color_factor_text);
174
175 text.padding = nk_vec2(0,0);
176 nk_widget_text(out, *content, txt, len, &text, text_alignment, font);
177}
178NK_LIB nk_bool
179nk_do_button_text(nk_flags *state,
180 struct nk_command_buffer *out, struct nk_rect bounds,
181 const char *string, int len, nk_flags align, enum nk_button_behavior behavior,
182 const struct nk_style_button *style, const struct nk_input *in,
183 const struct nk_user_font *font)
184{
185 struct nk_rect content;
186 int ret = nk_false;
187
188 NK_ASSERT(state);
189 NK_ASSERT(style);
190 NK_ASSERT(out);
191 NK_ASSERT(string);
192 NK_ASSERT(font);
193 if (!out || !style || !font || !string)
194 return nk_false;
195
196 ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
197 if (style->draw_begin) style->draw_begin(out, style->userdata);
198 nk_draw_button_text(out, &bounds, &content, *state, style, string, len, align, font);
199 if (style->draw_end) style->draw_end(out, style->userdata);
200 return ret;
201}
202NK_LIB void
203nk_draw_button_symbol(struct nk_command_buffer *out,
204 const struct nk_rect *bounds, const struct nk_rect *content,
205 nk_flags state, const struct nk_style_button *style,
206 enum nk_symbol_type type, const struct nk_user_font *font)
207{
208 struct nk_color sym, bg;
209 const struct nk_style_item *background;
210
211 /* select correct colors/images */
212 background = nk_draw_button(out, bounds, state, style);
213 if (background->type == NK_STYLE_ITEM_COLOR)
214 bg = background->data.color;
215 else bg = style->text_background;
216
217 if (state & NK_WIDGET_STATE_HOVER)
218 sym = style->text_hover;
219 else if (state & NK_WIDGET_STATE_ACTIVED)
220 sym = style->text_active;
221 else sym = style->text_normal;
222
223 sym = nk_rgb_factor(sym, style->color_factor_text);
224 nk_draw_symbol(out, type, *content, bg, sym, 1, font);
225}
226NK_LIB nk_bool
227nk_do_button_symbol(nk_flags *state,
228 struct nk_command_buffer *out, struct nk_rect bounds,
229 enum nk_symbol_type symbol, enum nk_button_behavior behavior,
230 const struct nk_style_button *style, const struct nk_input *in,
231 const struct nk_user_font *font)
232{
233 int ret;
234 struct nk_rect content;
235
236 NK_ASSERT(state);
237 NK_ASSERT(style);
238 NK_ASSERT(font);
239 NK_ASSERT(out);
240 if (!out || !style || !font || !state)
241 return nk_false;
242
243 ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
244 if (style->draw_begin) style->draw_begin(out, style->userdata);
245 nk_draw_button_symbol(out, &bounds, &content, *state, style, symbol, font);
246 if (style->draw_end) style->draw_end(out, style->userdata);
247 return ret;
248}
249NK_LIB void
250nk_draw_button_image(struct nk_command_buffer *out,
251 const struct nk_rect *bounds, const struct nk_rect *content,
252 nk_flags state, const struct nk_style_button *style, const struct nk_image *img)
253{
254 nk_draw_button(out, bounds, state, style);
255 nk_draw_image(out, *content, img, nk_rgb_factor(nk_white, style->color_factor_background));
256}
257NK_LIB nk_bool
258nk_do_button_image(nk_flags *state,
259 struct nk_command_buffer *out, struct nk_rect bounds,
260 struct nk_image img, enum nk_button_behavior b,
261 const struct nk_style_button *style, const struct nk_input *in)
262{
263 int ret;
264 struct nk_rect content;
265
266 NK_ASSERT(state);
267 NK_ASSERT(style);
268 NK_ASSERT(out);
269 if (!out || !style || !state)
270 return nk_false;
271
272 ret = nk_do_button(state, out, bounds, style, in, b, &content);
273 content.x += style->image_padding.x;
274 content.y += style->image_padding.y;
275 content.w -= 2 * style->image_padding.x;
276 content.h -= 2 * style->image_padding.y;
277
278 if (style->draw_begin) style->draw_begin(out, style->userdata);
279 nk_draw_button_image(out, &bounds, &content, *state, style, &img);
280 if (style->draw_end) style->draw_end(out, style->userdata);
281 return ret;
282}
283NK_LIB void
284nk_draw_button_text_symbol(struct nk_command_buffer *out,
285 const struct nk_rect *bounds, const struct nk_rect *label,
286 const struct nk_rect *symbol, nk_flags state, const struct nk_style_button *style,
287 const char *str, int len, enum nk_symbol_type type,
288 const struct nk_user_font *font)
289{
290 struct nk_color sym;
291 struct nk_text text;
292 const struct nk_style_item *background;
293
294 /* select correct background colors/images */
295 background = nk_draw_button(out, bounds, state, style);
296 if (background->type == NK_STYLE_ITEM_COLOR)
297 text.background = background->data.color;
298 else text.background = style->text_background;
299
300 /* select correct text colors */
301 if (state & NK_WIDGET_STATE_HOVER) {
302 sym = style->text_hover;
303 text.text = style->text_hover;
304 } else if (state & NK_WIDGET_STATE_ACTIVED) {
305 sym = style->text_active;
306 text.text = style->text_active;
307 } else {
308 sym = style->text_normal;
309 text.text = style->text_normal;
310 }
311
312 sym = nk_rgb_factor(sym, style->color_factor_text);
313 text.text = nk_rgb_factor(text.text, style->color_factor_text);
314 text.padding = nk_vec2(0,0);
315 nk_draw_symbol(out, type, *symbol, style->text_background, sym, 0, font);
316 nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
317}
318NK_LIB nk_bool
319nk_do_button_text_symbol(nk_flags *state,
320 struct nk_command_buffer *out, struct nk_rect bounds,
321 enum nk_symbol_type symbol, const char *str, int len, nk_flags align,
322 enum nk_button_behavior behavior, const struct nk_style_button *style,
323 const struct nk_user_font *font, const struct nk_input *in)
324{
325 int ret;
326 struct nk_rect tri = {0,0,0,0};
327 struct nk_rect content;
328
329 NK_ASSERT(style);
330 NK_ASSERT(out);
331 NK_ASSERT(font);
332 if (!out || !style || !font)
333 return nk_false;
334
335 ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
336 tri.y = content.y + (content.h/2) - font->height/2;
337 tri.w = font->height; tri.h = font->height;
338 if (align & NK_TEXT_ALIGN_LEFT) {
339 tri.x = (content.x + content.w) - (2 * style->padding.x + tri.w);
340 tri.x = NK_MAX(tri.x, 0);
341 } else tri.x = content.x + 2 * style->padding.x;
342
343 /* draw button */
344 if (style->draw_begin) style->draw_begin(out, style->userdata);
345 nk_draw_button_text_symbol(out, &bounds, &content, &tri,
346 *state, style, str, len, symbol, font);
347 if (style->draw_end) style->draw_end(out, style->userdata);
348 return ret;
349}
350NK_LIB void
351nk_draw_button_text_image(struct nk_command_buffer *out,
352 const struct nk_rect *bounds, const struct nk_rect *label,
353 const struct nk_rect *image, nk_flags state, const struct nk_style_button *style,
354 const char *str, int len, const struct nk_user_font *font,
355 const struct nk_image *img)
356{
357 struct nk_text text;
358 const struct nk_style_item *background;
359 background = nk_draw_button(out, bounds, state, style);
360
361 /* select correct colors */
362 if (background->type == NK_STYLE_ITEM_COLOR)
363 text.background = background->data.color;
364 else text.background = style->text_background;
365 if (state & NK_WIDGET_STATE_HOVER)
366 text.text = style->text_hover;
367 else if (state & NK_WIDGET_STATE_ACTIVED)
368 text.text = style->text_active;
369 else text.text = style->text_normal;
370
371 text.text = nk_rgb_factor(text.text, style->color_factor_text);
372 text.padding = nk_vec2(0, 0);
373 nk_widget_text(out, *label, str, len, &text, NK_TEXT_CENTERED, font);
374 nk_draw_image(out, *image, img, nk_rgb_factor(nk_white, style->color_factor_background));
375}
376NK_LIB nk_bool
377nk_do_button_text_image(nk_flags *state,
378 struct nk_command_buffer *out, struct nk_rect bounds,
379 struct nk_image img, const char* str, int len, nk_flags align,
380 enum nk_button_behavior behavior, const struct nk_style_button *style,
381 const struct nk_user_font *font, const struct nk_input *in)
382{
383 int ret;
384 struct nk_rect icon;
385 struct nk_rect content;
386
387 NK_ASSERT(style);
388 NK_ASSERT(state);
389 NK_ASSERT(font);
390 NK_ASSERT(out);
391 if (!out || !font || !style || !str)
392 return nk_false;
393
394 ret = nk_do_button(state, out, bounds, style, in, behavior, &content);
395 icon.y = bounds.y + style->padding.y;
396 icon.w = icon.h = bounds.h - 2 * style->padding.y;
397 if (align & NK_TEXT_ALIGN_LEFT) {
398 icon.x = (bounds.x + bounds.w) - (2 * style->padding.x + icon.w);
399 icon.x = NK_MAX(icon.x, 0);
400 } else icon.x = bounds.x + 2 * style->padding.x;
401
402 icon.x += style->image_padding.x;
403 icon.y += style->image_padding.y;
404 icon.w -= 2 * style->image_padding.x;
405 icon.h -= 2 * style->image_padding.y;
406
407 if (style->draw_begin) style->draw_begin(out, style->userdata);
408 nk_draw_button_text_image(out, &bounds, &content, &icon, *state, style, str, len, font, &img);
409 if (style->draw_end) style->draw_end(out, style->userdata);
410 return ret;
411}
412NK_API void
413nk_button_set_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
414{
415 NK_ASSERT(ctx);
416 if (!ctx) return;
417 ctx->button_behavior = behavior;
418}
419NK_API nk_bool
420nk_button_push_behavior(struct nk_context *ctx, enum nk_button_behavior behavior)
421{
422 struct nk_config_stack_button_behavior *button_stack;
423 struct nk_config_stack_button_behavior_element *element;
424
425 NK_ASSERT(ctx);
426 if (!ctx) return 0;
427
428 button_stack = &ctx->stacks.button_behaviors;
429 NK_ASSERT(button_stack->head < (int)NK_LEN(button_stack->elements));
430 if (button_stack->head >= (int)NK_LEN(button_stack->elements))
431 return 0;
432
433 element = &button_stack->elements[button_stack->head++];
434 element->address = &ctx->button_behavior;
435 element->old_value = ctx->button_behavior;
436 ctx->button_behavior = behavior;
437 return 1;
438}
439NK_API nk_bool
440nk_button_pop_behavior(struct nk_context *ctx)
441{
442 struct nk_config_stack_button_behavior *button_stack;
443 struct nk_config_stack_button_behavior_element *element;
444
445 NK_ASSERT(ctx);
446 if (!ctx) return 0;
447
448 button_stack = &ctx->stacks.button_behaviors;
449 NK_ASSERT(button_stack->head > 0);
450 if (button_stack->head < 1)
451 return 0;
452
453 element = &button_stack->elements[--button_stack->head];
454 *element->address = element->old_value;
455 return 1;
456}
457NK_API nk_bool
458nk_button_text_styled(struct nk_context *ctx,
459 const struct nk_style_button *style, const char *title, int len)
460{
461 struct nk_window *win;
462 struct nk_panel *layout;
463 const struct nk_input *in;
464
465 struct nk_rect bounds;
466 enum nk_widget_layout_states state;
467
468 NK_ASSERT(ctx);
469 NK_ASSERT(style);
470 NK_ASSERT(ctx->current);
471 NK_ASSERT(ctx->current->layout);
472 if (!style || !ctx || !ctx->current || !ctx->current->layout) return 0;
473
474 win = ctx->current;
475 layout = win->layout;
476 state = nk_widget(&bounds, ctx);
477
478 if (!state) return 0;
479 in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
480 return nk_do_button_text(&ctx->last_widget_state, &win->buffer, bounds,
481 title, len, style->text_alignment, ctx->button_behavior,
482 style, in, ctx->style.font);
483}
484NK_API nk_bool
485nk_button_text(struct nk_context *ctx, const char *title, int len)
486{
487 NK_ASSERT(ctx);
488 if (!ctx) return 0;
489 return nk_button_text_styled(ctx, &ctx->style.button, title, len);
490}
491NK_API nk_bool nk_button_label_styled(struct nk_context *ctx,
492 const struct nk_style_button *style, const char *title)
493{
494 return nk_button_text_styled(ctx, style, title, nk_strlen(title));
495}
496NK_API nk_bool nk_button_label(struct nk_context *ctx, const char *title)
497{
498 return nk_button_text(ctx, title, nk_strlen(title));
499}
500NK_API nk_bool
501nk_button_color(struct nk_context *ctx, struct nk_color color)
502{
503 struct nk_window *win;
504 struct nk_panel *layout;
505 const struct nk_input *in;
506 struct nk_style_button button;
507
508 int ret = 0;
509 struct nk_rect bounds;
510 struct nk_rect content;
511 enum nk_widget_layout_states state;
512
513 NK_ASSERT(ctx);
514 NK_ASSERT(ctx->current);
515 NK_ASSERT(ctx->current->layout);
516 if (!ctx || !ctx->current || !ctx->current->layout)
517 return 0;
518
519 win = ctx->current;
520 layout = win->layout;
521
522 state = nk_widget(&bounds, ctx);
523 if (!state) return 0;
524 in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
525
526 button = ctx->style.button;
527 button.normal = nk_style_item_color(color);
528 button.hover = nk_style_item_color(color);
529 button.active = nk_style_item_color(color);
530 ret = nk_do_button(&ctx->last_widget_state, &win->buffer, bounds,
531 &button, in, ctx->button_behavior, &content);
532 nk_draw_button(&win->buffer, &bounds, ctx->last_widget_state, &button);
533 return ret;
534}
535NK_API nk_bool
536nk_button_symbol_styled(struct nk_context *ctx,
537 const struct nk_style_button *style, enum nk_symbol_type symbol)
538{
539 struct nk_window *win;
540 struct nk_panel *layout;
541 const struct nk_input *in;
542
543 struct nk_rect bounds;
544 enum nk_widget_layout_states state;
545
546 NK_ASSERT(ctx);
547 NK_ASSERT(ctx->current);
548 NK_ASSERT(ctx->current->layout);
549 if (!ctx || !ctx->current || !ctx->current->layout)
550 return 0;
551
552 win = ctx->current;
553 layout = win->layout;
554 state = nk_widget(&bounds, ctx);
555 if (!state) return 0;
556 in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
557 return nk_do_button_symbol(&ctx->last_widget_state, &win->buffer, bounds,
558 symbol, ctx->button_behavior, style, in, ctx->style.font);
559}
560NK_API nk_bool
561nk_button_symbol(struct nk_context *ctx, enum nk_symbol_type symbol)
562{
563 NK_ASSERT(ctx);
564 if (!ctx) return 0;
565 return nk_button_symbol_styled(ctx, &ctx->style.button, symbol);
566}
567NK_API nk_bool
568nk_button_image_styled(struct nk_context *ctx, const struct nk_style_button *style,
569 struct nk_image img)
570{
571 struct nk_window *win;
572 struct nk_panel *layout;
573 const struct nk_input *in;
574
575 struct nk_rect bounds;
576 enum nk_widget_layout_states state;
577
578 NK_ASSERT(ctx);
579 NK_ASSERT(ctx->current);
580 NK_ASSERT(ctx->current->layout);
581 if (!ctx || !ctx->current || !ctx->current->layout)
582 return 0;
583
584 win = ctx->current;
585 layout = win->layout;
586
587 state = nk_widget(&bounds, ctx);
588 if (!state) return 0;
589 in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
590 return nk_do_button_image(&ctx->last_widget_state, &win->buffer, bounds,
591 img, ctx->button_behavior, style, in);
592}
593NK_API nk_bool
594nk_button_image(struct nk_context *ctx, struct nk_image img)
595{
596 NK_ASSERT(ctx);
597 if (!ctx) return 0;
598 return nk_button_image_styled(ctx, &ctx->style.button, img);
599}
600NK_API nk_bool
601nk_button_symbol_text_styled(struct nk_context *ctx,
602 const struct nk_style_button *style, enum nk_symbol_type symbol,
603 const char *text, int len, nk_flags align)
604{
605 struct nk_window *win;
606 struct nk_panel *layout;
607 const struct nk_input *in;
608
609 struct nk_rect bounds;
610 enum nk_widget_layout_states state;
611
612 NK_ASSERT(ctx);
613 NK_ASSERT(ctx->current);
614 NK_ASSERT(ctx->current->layout);
615 if (!ctx || !ctx->current || !ctx->current->layout)
616 return 0;
617
618 win = ctx->current;
619 layout = win->layout;
620
621 state = nk_widget(&bounds, ctx);
622 if (!state) return 0;
623 in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
624 return nk_do_button_text_symbol(&ctx->last_widget_state, &win->buffer, bounds,
625 symbol, text, len, align, ctx->button_behavior,
626 style, ctx->style.font, in);
627}
628NK_API nk_bool
629nk_button_symbol_text(struct nk_context *ctx, enum nk_symbol_type symbol,
630 const char* text, int len, nk_flags align)
631{
632 NK_ASSERT(ctx);
633 if (!ctx) return 0;
634 return nk_button_symbol_text_styled(ctx, &ctx->style.button, symbol, text, len, align);
635}
636NK_API nk_bool nk_button_symbol_label(struct nk_context *ctx, enum nk_symbol_type symbol,
637 const char *label, nk_flags align)
638{
639 return nk_button_symbol_text(ctx, symbol, label, nk_strlen(label), align);
640}
641NK_API nk_bool nk_button_symbol_label_styled(struct nk_context *ctx,
642 const struct nk_style_button *style, enum nk_symbol_type symbol,
643 const char *title, nk_flags align)
644{
645 return nk_button_symbol_text_styled(ctx, style, symbol, title, nk_strlen(title), align);
646}
647NK_API nk_bool
648nk_button_image_text_styled(struct nk_context *ctx,
649 const struct nk_style_button *style, struct nk_image img, const char *text,
650 int len, nk_flags align)
651{
652 struct nk_window *win;
653 struct nk_panel *layout;
654 const struct nk_input *in;
655
656 struct nk_rect bounds;
657 enum nk_widget_layout_states state;
658
659 NK_ASSERT(ctx);
660 NK_ASSERT(ctx->current);
661 NK_ASSERT(ctx->current->layout);
662 if (!ctx || !ctx->current || !ctx->current->layout)
663 return 0;
664
665 win = ctx->current;
666 layout = win->layout;
667
668 state = nk_widget(&bounds, ctx);
669 if (!state) return 0;
670 in = (state == NK_WIDGET_ROM || state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
671 return nk_do_button_text_image(&ctx->last_widget_state, &win->buffer,
672 bounds, img, text, len, align, ctx->button_behavior,
673 style, ctx->style.font, in);
674}
675NK_API nk_bool
676nk_button_image_text(struct nk_context *ctx, struct nk_image img,
677 const char *text, int len, nk_flags align)
678{
679 return nk_button_image_text_styled(ctx, &ctx->style.button,img, text, len, align);
680}
681NK_API nk_bool nk_button_image_label(struct nk_context *ctx, struct nk_image img,
682 const char *label, nk_flags align)
683{
684 return nk_button_image_text(ctx, img, label, nk_strlen(label), align);
685}
686NK_API nk_bool nk_button_image_label_styled(struct nk_context *ctx,
687 const struct nk_style_button *style, struct nk_image img,
688 const char *label, nk_flags text_alignment)
689{
690 return nk_button_image_text_styled(ctx, style, img, label, nk_strlen(label), text_alignment);
691}
692
main API and documentation file
@ NK_WINDOW_ROM
sets window widgets into a read only mode and does not allow input changes
Definition nuklear.h:5492
NK_API void nk_draw_image(struct nk_command_buffer *, struct nk_rect, const struct nk_image *, struct nk_color)
misc
NK_API void nk_fill_rect(struct nk_command_buffer *, struct nk_rect, float rounding, struct nk_color)
filled shades
@ NK_WIDGET_STATE_LEFT
!< widget is currently activated
Definition nuklear.h:3093
@ NK_WIDGET_STATE_ACTIVED
!< widget is being hovered
Definition nuklear.h:3092
@ NK_WIDGET_STATE_ENTERED
!< widget is neither active nor hovered
Definition nuklear.h:3090
@ NK_WIDGET_STATE_HOVER
!< widget has been hovered on the current frame
Definition nuklear.h:3091
@ NK_WIDGET_STATE_ACTIVE
!< widget is being hovered
Definition nuklear.h:3095
@ NK_WIDGET_STATE_HOVERED
!< widget is from this frame on not hovered anymore
Definition nuklear.h:3094
nk_widget_layout_states
Definition nuklear.h:3081
@ NK_WIDGET_DISABLED
The widget is manually disabled and acts like NK_WIDGET_ROM.
Definition nuklear.h:3085
@ NK_WIDGET_ROM
The widget is partially visible and cannot be updated.
Definition nuklear.h:3084
float height
!< user provided font handle
Definition nuklear.h:4008