forked from vurtun/nuklear
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnuklear_toggle.c
320 lines (295 loc) · 10.5 KB
/
nuklear_toggle.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "nuklear.h"
#include "nuklear_internal.h"
/* ===============================================================
*
* TOGGLE
*
* ===============================================================*/
NK_LIB int
nk_toggle_behavior(const struct nk_input *in, struct nk_rect select,
nk_flags *state, int active)
{
nk_widget_state_reset(state);
if (nk_button_behavior(state, select, in, NK_BUTTON_DEFAULT)) {
*state = NK_WIDGET_STATE_ACTIVE;
active = !active;
}
if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, select))
*state |= NK_WIDGET_STATE_ENTERED;
else if (nk_input_is_mouse_prev_hovering_rect(in, select))
*state |= NK_WIDGET_STATE_LEFT;
return active;
}
NK_LIB void
nk_draw_checkbox(struct nk_command_buffer *out,
nk_flags state, const struct nk_style_toggle *style, int active,
const struct nk_rect *label, const struct nk_rect *selector,
const struct nk_rect *cursors, const char *string, int len,
const struct nk_user_font *font)
{
const struct nk_style_item *background;
const struct nk_style_item *cursor;
struct nk_text text;
/* select correct colors/images */
if (state & NK_WIDGET_STATE_HOVER) {
background = &style->hover;
cursor = &style->cursor_hover;
text.text = style->text_hover;
} else if (state & NK_WIDGET_STATE_ACTIVED) {
background = &style->hover;
cursor = &style->cursor_hover;
text.text = style->text_active;
} else {
background = &style->normal;
cursor = &style->cursor_normal;
text.text = style->text_normal;
}
/* draw background and cursor */
if (background->type == NK_STYLE_ITEM_COLOR) {
nk_fill_rect(out, *selector, 0, style->border_color);
nk_fill_rect(out, nk_shrink_rect(*selector, style->border), 0, background->data.color);
} else nk_draw_image(out, *selector, &background->data.image, nk_white);
if (active) {
if (cursor->type == NK_STYLE_ITEM_IMAGE)
nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
else nk_fill_rect(out, *cursors, 0, cursor->data.color);
}
text.padding.x = 0;
text.padding.y = 0;
text.background = style->text_background;
nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
}
NK_LIB void
nk_draw_option(struct nk_command_buffer *out,
nk_flags state, const struct nk_style_toggle *style, int active,
const struct nk_rect *label, const struct nk_rect *selector,
const struct nk_rect *cursors, const char *string, int len,
const struct nk_user_font *font)
{
const struct nk_style_item *background;
const struct nk_style_item *cursor;
struct nk_text text;
/* select correct colors/images */
if (state & NK_WIDGET_STATE_HOVER) {
background = &style->hover;
cursor = &style->cursor_hover;
text.text = style->text_hover;
} else if (state & NK_WIDGET_STATE_ACTIVED) {
background = &style->hover;
cursor = &style->cursor_hover;
text.text = style->text_active;
} else {
background = &style->normal;
cursor = &style->cursor_normal;
text.text = style->text_normal;
}
/* draw background and cursor */
if (background->type == NK_STYLE_ITEM_COLOR) {
nk_fill_circle(out, *selector, style->border_color);
nk_fill_circle(out, nk_shrink_rect(*selector, style->border), background->data.color);
} else nk_draw_image(out, *selector, &background->data.image, nk_white);
if (active) {
if (cursor->type == NK_STYLE_ITEM_IMAGE)
nk_draw_image(out, *cursors, &cursor->data.image, nk_white);
else nk_fill_circle(out, *cursors, cursor->data.color);
}
text.padding.x = 0;
text.padding.y = 0;
text.background = style->text_background;
nk_widget_text(out, *label, string, len, &text, NK_TEXT_LEFT, font);
}
NK_LIB int
nk_do_toggle(nk_flags *state,
struct nk_command_buffer *out, struct nk_rect r,
int *active, const char *str, int len, enum nk_toggle_type type,
const struct nk_style_toggle *style, const struct nk_input *in,
const struct nk_user_font *font)
{
int was_active;
struct nk_rect bounds;
struct nk_rect select;
struct nk_rect cursor;
struct nk_rect label;
NK_ASSERT(style);
NK_ASSERT(out);
NK_ASSERT(font);
if (!out || !style || !font || !active)
return 0;
r.w = NK_MAX(r.w, font->height + 2 * style->padding.x);
r.h = NK_MAX(r.h, font->height + 2 * style->padding.y);
/* add additional touch padding for touch screen devices */
bounds.x = r.x - style->touch_padding.x;
bounds.y = r.y - style->touch_padding.y;
bounds.w = r.w + 2 * style->touch_padding.x;
bounds.h = r.h + 2 * style->touch_padding.y;
/* calculate the selector space */
select.w = font->height;
select.h = select.w;
select.y = r.y + r.h/2.0f - select.h/2.0f;
select.x = r.x;
/* calculate the bounds of the cursor inside the selector */
cursor.x = select.x + style->padding.x + style->border;
cursor.y = select.y + style->padding.y + style->border;
cursor.w = select.w - (2 * style->padding.x + 2 * style->border);
cursor.h = select.h - (2 * style->padding.y + 2 * style->border);
/* label behind the selector */
label.x = select.x + select.w + style->spacing;
label.y = select.y;
label.w = NK_MAX(r.x + r.w, label.x) - label.x;
label.h = select.w;
/* update selector */
was_active = *active;
*active = nk_toggle_behavior(in, bounds, state, *active);
/* draw selector */
if (style->draw_begin)
style->draw_begin(out, style->userdata);
if (type == NK_TOGGLE_CHECK) {
nk_draw_checkbox(out, *state, style, *active, &label, &select, &cursor, str, len, font);
} else {
nk_draw_option(out, *state, style, *active, &label, &select, &cursor, str, len, font);
}
if (style->draw_end)
style->draw_end(out, style->userdata);
return (was_active != *active);
}
/*----------------------------------------------------------------
*
* CHECKBOX
*
* --------------------------------------------------------------*/
NK_API int
nk_check_text(struct nk_context *ctx, const char *text, int len, int active)
{
struct nk_window *win;
struct nk_panel *layout;
const struct nk_input *in;
const struct nk_style *style;
struct nk_rect bounds;
enum nk_widget_layout_states state;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout)
return active;
win = ctx->current;
style = &ctx->style;
layout = win->layout;
state = nk_widget(&bounds, ctx);
if (!state) return active;
in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &active,
text, len, NK_TOGGLE_CHECK, &style->checkbox, in, style->font);
return active;
}
NK_API unsigned int
nk_check_flags_text(struct nk_context *ctx, const char *text, int len,
unsigned int flags, unsigned int value)
{
int old_active;
NK_ASSERT(ctx);
NK_ASSERT(text);
if (!ctx || !text) return flags;
old_active = (int)((flags & value) & value);
if (nk_check_text(ctx, text, len, old_active))
flags |= value;
else flags &= ~value;
return flags;
}
NK_API int
nk_checkbox_text(struct nk_context *ctx, const char *text, int len, int *active)
{
int old_val;
NK_ASSERT(ctx);
NK_ASSERT(text);
NK_ASSERT(active);
if (!ctx || !text || !active) return 0;
old_val = *active;
*active = nk_check_text(ctx, text, len, *active);
return old_val != *active;
}
NK_API int
nk_checkbox_flags_text(struct nk_context *ctx, const char *text, int len,
unsigned int *flags, unsigned int value)
{
int active;
NK_ASSERT(ctx);
NK_ASSERT(text);
NK_ASSERT(flags);
if (!ctx || !text || !flags) return 0;
active = (int)((*flags & value) & value);
if (nk_checkbox_text(ctx, text, len, &active)) {
if (active) *flags |= value;
else *flags &= ~value;
return 1;
}
return 0;
}
NK_API int nk_check_label(struct nk_context *ctx, const char *label, int active)
{
return nk_check_text(ctx, label, nk_strlen(label), active);
}
NK_API unsigned int nk_check_flags_label(struct nk_context *ctx, const char *label,
unsigned int flags, unsigned int value)
{
return nk_check_flags_text(ctx, label, nk_strlen(label), flags, value);
}
NK_API int nk_checkbox_label(struct nk_context *ctx, const char *label, int *active)
{
return nk_checkbox_text(ctx, label, nk_strlen(label), active);
}
NK_API int nk_checkbox_flags_label(struct nk_context *ctx, const char *label,
unsigned int *flags, unsigned int value)
{
return nk_checkbox_flags_text(ctx, label, nk_strlen(label), flags, value);
}
/*----------------------------------------------------------------
*
* OPTION
*
* --------------------------------------------------------------*/
NK_API int
nk_option_text(struct nk_context *ctx, const char *text, int len, int is_active)
{
struct nk_window *win;
struct nk_panel *layout;
const struct nk_input *in;
const struct nk_style *style;
struct nk_rect bounds;
enum nk_widget_layout_states state;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout)
return is_active;
win = ctx->current;
style = &ctx->style;
layout = win->layout;
state = nk_widget(&bounds, ctx);
if (!state) return (int)state;
in = (state == NK_WIDGET_ROM || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
nk_do_toggle(&ctx->last_widget_state, &win->buffer, bounds, &is_active,
text, len, NK_TOGGLE_OPTION, &style->option, in, style->font);
return is_active;
}
NK_API int
nk_radio_text(struct nk_context *ctx, const char *text, int len, int *active)
{
int old_value;
NK_ASSERT(ctx);
NK_ASSERT(text);
NK_ASSERT(active);
if (!ctx || !text || !active) return 0;
old_value = *active;
*active = nk_option_text(ctx, text, len, old_value);
return old_value != *active;
}
NK_API int
nk_option_label(struct nk_context *ctx, const char *label, int active)
{
return nk_option_text(ctx, label, nk_strlen(label), active);
}
NK_API int
nk_radio_label(struct nk_context *ctx, const char *label, int *active)
{
return nk_radio_text(ctx, label, nk_strlen(label), active);
}