-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathnuklear_widget.c
357 lines (326 loc) · 13.4 KB
/
nuklear_widget.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "nuklear.h"
#include "nuklear_internal.h"
/* ===============================================================
*
* WIDGET
*
* ===============================================================*/
NK_API struct nk_rect
nk_widget_bounds(const struct nk_context *ctx)
{
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current)
return nk_rect(0,0,0,0);
nk_layout_peek(&bounds, ctx);
return bounds;
}
NK_API struct nk_vec2
nk_widget_position(const struct nk_context *ctx)
{
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current)
return nk_vec2(0,0);
nk_layout_peek(&bounds, ctx);
return nk_vec2(bounds.x, bounds.y);
}
NK_API struct nk_vec2
nk_widget_size(const struct nk_context *ctx)
{
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current)
return nk_vec2(0,0);
nk_layout_peek(&bounds, ctx);
return nk_vec2(bounds.w, bounds.h);
}
NK_API float
nk_widget_width(const struct nk_context *ctx)
{
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current)
return 0;
nk_layout_peek(&bounds, ctx);
return bounds.w;
}
NK_API float
nk_widget_height(const struct nk_context *ctx)
{
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current)
return 0;
nk_layout_peek(&bounds, ctx);
return bounds.h;
}
NK_API nk_bool
nk_widget_is_hovered(const struct nk_context *ctx)
{
struct nk_rect c, v;
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current || ctx->active != ctx->current)
return 0;
c = ctx->current->layout->clip;
c.x = (float)((int)c.x);
c.y = (float)((int)c.y);
c.w = (float)((int)c.w);
c.h = (float)((int)c.h);
nk_layout_peek(&bounds, ctx);
nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
return 0;
return nk_input_is_mouse_hovering_rect(&ctx->input, bounds);
}
NK_API nk_bool
nk_widget_is_mouse_clicked(const struct nk_context *ctx, enum nk_buttons btn)
{
struct nk_rect c, v;
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current || ctx->active != ctx->current)
return 0;
c = ctx->current->layout->clip;
c.x = (float)((int)c.x);
c.y = (float)((int)c.y);
c.w = (float)((int)c.w);
c.h = (float)((int)c.h);
nk_layout_peek(&bounds, ctx);
nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
return 0;
return nk_input_mouse_clicked(&ctx->input, btn, bounds);
}
NK_API nk_bool
nk_widget_has_mouse_click_down(const struct nk_context *ctx, enum nk_buttons btn, nk_bool down)
{
struct nk_rect c, v;
struct nk_rect bounds;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current || ctx->active != ctx->current)
return 0;
c = ctx->current->layout->clip;
c.x = (float)((int)c.x);
c.y = (float)((int)c.y);
c.w = (float)((int)c.w);
c.h = (float)((int)c.h);
nk_layout_peek(&bounds, ctx);
nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
return 0;
return nk_input_has_mouse_click_down_in_rect(&ctx->input, btn, bounds, down);
}
NK_API enum nk_widget_layout_states
nk_widget(struct nk_rect *bounds, const struct nk_context *ctx)
{
struct nk_rect c, v;
struct nk_window *win;
struct nk_panel *layout;
const struct nk_input *in;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout)
return NK_WIDGET_INVALID;
/* allocate space and check if the widget needs to be updated and drawn */
nk_panel_alloc_space(bounds, ctx);
win = ctx->current;
layout = win->layout;
in = &ctx->input;
c = layout->clip;
/* if one of these triggers you forgot to add an `if` condition around either
a window, group, popup, combobox or contextual menu `begin` and `end` block.
Example:
if (nk_begin(...) {...} nk_end(...); or
if (nk_group_begin(...) { nk_group_end(...);} */
NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
/* need to convert to int here to remove floating point errors */
bounds->x = (float)((int)bounds->x);
bounds->y = (float)((int)bounds->y);
bounds->w = (float)((int)bounds->w);
bounds->h = (float)((int)bounds->h);
c.x = (float)((int)c.x);
c.y = (float)((int)c.y);
c.w = (float)((int)c.w);
c.h = (float)((int)c.h);
nk_unify(&v, &c, bounds->x, bounds->y, bounds->x + bounds->w, bounds->y + bounds->h);
if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds->x, bounds->y, bounds->w, bounds->h))
return NK_WIDGET_INVALID;
if (win->widgets_disabled)
return NK_WIDGET_DISABLED;
if (!NK_INBOX(in->mouse.pos.x, in->mouse.pos.y, v.x, v.y, v.w, v.h))
return NK_WIDGET_ROM;
return NK_WIDGET_VALID;
}
NK_API enum nk_widget_layout_states
nk_widget_fitting(struct nk_rect *bounds, const struct nk_context *ctx,
struct nk_vec2 item_padding)
{
/* update the bounds to stand without padding */
enum nk_widget_layout_states state;
NK_UNUSED(item_padding);
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout)
return NK_WIDGET_INVALID;
state = nk_widget(bounds, ctx);
return state;
}
NK_API void
nk_spacing(struct nk_context *ctx, int cols)
{
struct nk_window *win;
struct nk_panel *layout;
struct nk_rect none;
int i, index, rows;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
NK_ASSERT(ctx->current->layout);
if (!ctx || !ctx->current || !ctx->current->layout)
return;
/* spacing over row boundaries */
win = ctx->current;
layout = win->layout;
index = (layout->row.index + cols) % layout->row.columns;
rows = (layout->row.index + cols) / layout->row.columns;
if (rows) {
for (i = 0; i < rows; ++i)
nk_panel_alloc_row(ctx, win);
cols = index;
}
/* non table layout need to allocate space */
if (layout->row.type != NK_LAYOUT_DYNAMIC_FIXED &&
layout->row.type != NK_LAYOUT_STATIC_FIXED) {
for (i = 0; i < cols; ++i)
nk_panel_alloc_space(&none, ctx);
} layout->row.index = index;
}
NK_API void
nk_widget_disable_begin(struct nk_context* ctx)
{
struct nk_window* win;
struct nk_style* style;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current)
return;
win = ctx->current;
style = &ctx->style;
win->widgets_disabled = nk_true;
style->button.color_factor_text = style->button.disabled_factor;
style->button.color_factor_background = style->button.disabled_factor;
style->chart.color_factor = style->chart.disabled_factor;
style->checkbox.color_factor = style->checkbox.disabled_factor;
style->combo.color_factor = style->combo.disabled_factor;
style->combo.button.color_factor_text = style->combo.button.disabled_factor;
style->combo.button.color_factor_background = style->combo.button.disabled_factor;
style->contextual_button.color_factor_text = style->contextual_button.disabled_factor;
style->contextual_button.color_factor_background = style->contextual_button.disabled_factor;
style->edit.color_factor = style->edit.disabled_factor;
style->edit.scrollbar.color_factor = style->edit.scrollbar.disabled_factor;
style->menu_button.color_factor_text = style->menu_button.disabled_factor;
style->menu_button.color_factor_background = style->menu_button.disabled_factor;
style->option.color_factor = style->option.disabled_factor;
style->progress.color_factor = style->progress.disabled_factor;
style->property.color_factor = style->property.disabled_factor;
style->property.inc_button.color_factor_text = style->property.inc_button.disabled_factor;
style->property.inc_button.color_factor_background = style->property.inc_button.disabled_factor;
style->property.dec_button.color_factor_text = style->property.dec_button.disabled_factor;
style->property.dec_button.color_factor_background = style->property.dec_button.disabled_factor;
style->property.edit.color_factor = style->property.edit.disabled_factor;
style->scrollh.color_factor = style->scrollh.disabled_factor;
style->scrollh.inc_button.color_factor_text = style->scrollh.inc_button.disabled_factor;
style->scrollh.inc_button.color_factor_background = style->scrollh.inc_button.disabled_factor;
style->scrollh.dec_button.color_factor_text = style->scrollh.dec_button.disabled_factor;
style->scrollh.dec_button.color_factor_background = style->scrollh.dec_button.disabled_factor;
style->scrollv.color_factor = style->scrollv.disabled_factor;
style->scrollv.inc_button.color_factor_text = style->scrollv.inc_button.disabled_factor;
style->scrollv.inc_button.color_factor_background = style->scrollv.inc_button.disabled_factor;
style->scrollv.dec_button.color_factor_text = style->scrollv.dec_button.disabled_factor;
style->scrollv.dec_button.color_factor_background = style->scrollv.dec_button.disabled_factor;
style->selectable.color_factor = style->selectable.disabled_factor;
style->slider.color_factor = style->slider.disabled_factor;
style->slider.inc_button.color_factor_text = style->slider.inc_button.disabled_factor;
style->slider.inc_button.color_factor_background = style->slider.inc_button.disabled_factor;
style->slider.dec_button.color_factor_text = style->slider.dec_button.disabled_factor;
style->slider.dec_button.color_factor_background = style->slider.dec_button.disabled_factor;
style->tab.color_factor = style->tab.disabled_factor;
style->tab.node_maximize_button.color_factor_text = style->tab.node_maximize_button.disabled_factor;
style->tab.node_minimize_button.color_factor_text = style->tab.node_minimize_button.disabled_factor;
style->tab.tab_maximize_button.color_factor_text = style->tab.tab_maximize_button.disabled_factor;
style->tab.tab_maximize_button.color_factor_background = style->tab.tab_maximize_button.disabled_factor;
style->tab.tab_minimize_button.color_factor_text = style->tab.tab_minimize_button.disabled_factor;
style->tab.tab_minimize_button.color_factor_background = style->tab.tab_minimize_button.disabled_factor;
style->text.color_factor = style->text.disabled_factor;
}
NK_API void
nk_widget_disable_end(struct nk_context* ctx)
{
struct nk_window* win;
struct nk_style* style;
NK_ASSERT(ctx);
NK_ASSERT(ctx->current);
if (!ctx || !ctx->current)
return;
win = ctx->current;
style = &ctx->style;
win->widgets_disabled = nk_false;
style->button.color_factor_text = 1.0f;
style->button.color_factor_background = 1.0f;
style->chart.color_factor = 1.0f;
style->checkbox.color_factor = 1.0f;
style->combo.color_factor = 1.0f;
style->combo.button.color_factor_text = 1.0f;
style->combo.button.color_factor_background = 1.0f;
style->contextual_button.color_factor_text = 1.0f;
style->contextual_button.color_factor_background = 1.0f;
style->edit.color_factor = 1.0f;
style->edit.scrollbar.color_factor = 1.0f;
style->menu_button.color_factor_text = 1.0f;
style->menu_button.color_factor_background = 1.0f;
style->option.color_factor = 1.0f;
style->progress.color_factor = 1.0f;
style->property.color_factor = 1.0f;
style->property.inc_button.color_factor_text = 1.0f;
style->property.inc_button.color_factor_background = 1.0f;
style->property.dec_button.color_factor_text = 1.0f;
style->property.dec_button.color_factor_background = 1.0f;
style->property.edit.color_factor = 1.0f;
style->scrollh.color_factor = 1.0f;
style->scrollh.inc_button.color_factor_text = 1.0f;
style->scrollh.inc_button.color_factor_background = 1.0f;
style->scrollh.dec_button.color_factor_text = 1.0f;
style->scrollh.dec_button.color_factor_background = 1.0f;
style->scrollv.color_factor = 1.0f;
style->scrollv.inc_button.color_factor_text = 1.0f;
style->scrollv.inc_button.color_factor_background = 1.0f;
style->scrollv.dec_button.color_factor_text = 1.0f;
style->scrollv.dec_button.color_factor_background = 1.0f;
style->selectable.color_factor = 1.0f;
style->slider.color_factor = 1.0f;
style->slider.inc_button.color_factor_text = 1.0f;
style->slider.inc_button.color_factor_background = 1.0f;
style->slider.dec_button.color_factor_text = 1.0f;
style->slider.dec_button.color_factor_background = 1.0f;
style->tab.color_factor = 1.0f;
style->tab.node_maximize_button.color_factor_text = 1.0f;
style->tab.node_minimize_button.color_factor_text = 1.0f;
style->tab.tab_maximize_button.color_factor_text = 1.0f;
style->tab.tab_maximize_button.color_factor_background = 1.0f;
style->tab.tab_minimize_button.color_factor_text = 1.0f;
style->tab.tab_minimize_button.color_factor_background = 1.0f;
style->text.color_factor = 1.0f;
}