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_widget.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ===============================================================
5 *
6 * WIDGET
7 *
8 * ===============================================================*/
9NK_API struct nk_rect
10nk_widget_bounds(const struct nk_context *ctx)
11{
12 struct nk_rect bounds;
13 NK_ASSERT(ctx);
14 NK_ASSERT(ctx->current);
15 if (!ctx || !ctx->current)
16 return nk_rect(0,0,0,0);
17 nk_layout_peek(&bounds, ctx);
18 return bounds;
19}
20NK_API struct nk_vec2
21nk_widget_position(const struct nk_context *ctx)
22{
23 struct nk_rect bounds;
24 NK_ASSERT(ctx);
25 NK_ASSERT(ctx->current);
26 if (!ctx || !ctx->current)
27 return nk_vec2(0,0);
28
29 nk_layout_peek(&bounds, ctx);
30 return nk_vec2(bounds.x, bounds.y);
31}
32NK_API struct nk_vec2
33nk_widget_size(const struct nk_context *ctx)
34{
35 struct nk_rect bounds;
36 NK_ASSERT(ctx);
37 NK_ASSERT(ctx->current);
38 if (!ctx || !ctx->current)
39 return nk_vec2(0,0);
40
41 nk_layout_peek(&bounds, ctx);
42 return nk_vec2(bounds.w, bounds.h);
43}
44NK_API float
45nk_widget_width(const struct nk_context *ctx)
46{
47 struct nk_rect bounds;
48 NK_ASSERT(ctx);
49 NK_ASSERT(ctx->current);
50 if (!ctx || !ctx->current)
51 return 0;
52
53 nk_layout_peek(&bounds, ctx);
54 return bounds.w;
55}
56NK_API float
57nk_widget_height(const struct nk_context *ctx)
58{
59 struct nk_rect bounds;
60 NK_ASSERT(ctx);
61 NK_ASSERT(ctx->current);
62 if (!ctx || !ctx->current)
63 return 0;
64
65 nk_layout_peek(&bounds, ctx);
66 return bounds.h;
67}
68NK_API nk_bool
69nk_widget_is_hovered(const struct nk_context *ctx)
70{
71 struct nk_rect c, v;
72 struct nk_rect bounds;
73 NK_ASSERT(ctx);
74 NK_ASSERT(ctx->current);
75 if (!ctx || !ctx->current || ctx->active != ctx->current)
76 return 0;
77
78 c = ctx->current->layout->clip;
79 c.x = (float)((int)c.x);
80 c.y = (float)((int)c.y);
81 c.w = (float)((int)c.w);
82 c.h = (float)((int)c.h);
83
84 nk_layout_peek(&bounds, ctx);
85 nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
86 if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
87 return 0;
88 return nk_input_is_mouse_hovering_rect(&ctx->input, bounds);
89}
90NK_API nk_bool
91nk_widget_is_mouse_clicked(const struct nk_context *ctx, enum nk_buttons btn)
92{
93 struct nk_rect c, v;
94 struct nk_rect bounds;
95 NK_ASSERT(ctx);
96 NK_ASSERT(ctx->current);
97 if (!ctx || !ctx->current || ctx->active != ctx->current)
98 return 0;
99
100 c = ctx->current->layout->clip;
101 c.x = (float)((int)c.x);
102 c.y = (float)((int)c.y);
103 c.w = (float)((int)c.w);
104 c.h = (float)((int)c.h);
105
106 nk_layout_peek(&bounds, ctx);
107 nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
108 if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
109 return 0;
110 return nk_input_mouse_clicked(&ctx->input, btn, bounds);
111}
112NK_API nk_bool
113nk_widget_has_mouse_click_down(const struct nk_context *ctx, enum nk_buttons btn, nk_bool down)
114{
115 struct nk_rect c, v;
116 struct nk_rect bounds;
117 NK_ASSERT(ctx);
118 NK_ASSERT(ctx->current);
119 if (!ctx || !ctx->current || ctx->active != ctx->current)
120 return 0;
121
122 c = ctx->current->layout->clip;
123 c.x = (float)((int)c.x);
124 c.y = (float)((int)c.y);
125 c.w = (float)((int)c.w);
126 c.h = (float)((int)c.h);
127
128 nk_layout_peek(&bounds, ctx);
129 nk_unify(&v, &c, bounds.x, bounds.y, bounds.x + bounds.w, bounds.y + bounds.h);
130 if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds.x, bounds.y, bounds.w, bounds.h))
131 return 0;
132 return nk_input_has_mouse_click_down_in_rect(&ctx->input, btn, bounds, down);
133}
134NK_API enum nk_widget_layout_states
135nk_widget(struct nk_rect *bounds, const struct nk_context *ctx)
136{
137 struct nk_rect c, v;
138 struct nk_window *win;
139 struct nk_panel *layout;
140 const struct nk_input *in;
141
142 NK_ASSERT(ctx);
143 NK_ASSERT(ctx->current);
144 NK_ASSERT(ctx->current->layout);
145 if (!ctx || !ctx->current || !ctx->current->layout)
146 return NK_WIDGET_INVALID;
147
148 /* allocate space and check if the widget needs to be updated and drawn */
149 nk_panel_alloc_space(bounds, ctx);
150 win = ctx->current;
151 layout = win->layout;
152 in = &ctx->input;
153 c = layout->clip;
154
155 /* if one of these triggers you forgot to add an `if` condition around either
156 a window, group, popup, combobox or contextual menu `begin` and `end` block.
157 Example:
158 if (nk_begin(...) {...} nk_end(...); or
159 if (nk_group_begin(...) { nk_group_end(...);} */
160 NK_ASSERT(!(layout->flags & NK_WINDOW_MINIMIZED));
161 NK_ASSERT(!(layout->flags & NK_WINDOW_HIDDEN));
162 NK_ASSERT(!(layout->flags & NK_WINDOW_CLOSED));
163
164 /* need to convert to int here to remove floating point errors */
165 bounds->x = (float)((int)bounds->x);
166 bounds->y = (float)((int)bounds->y);
167 bounds->w = (float)((int)bounds->w);
168 bounds->h = (float)((int)bounds->h);
169
170 c.x = (float)((int)c.x);
171 c.y = (float)((int)c.y);
172 c.w = (float)((int)c.w);
173 c.h = (float)((int)c.h);
174
175 nk_unify(&v, &c, bounds->x, bounds->y, bounds->x + bounds->w, bounds->y + bounds->h);
176 if (!NK_INTERSECT(c.x, c.y, c.w, c.h, bounds->x, bounds->y, bounds->w, bounds->h))
177 return NK_WIDGET_INVALID;
178 if (win->widgets_disabled)
179 return NK_WIDGET_DISABLED;
180 if (!NK_INBOX(in->mouse.pos.x, in->mouse.pos.y, v.x, v.y, v.w, v.h))
181 return NK_WIDGET_ROM;
182 return NK_WIDGET_VALID;
183}
184NK_API enum nk_widget_layout_states
185nk_widget_fitting(struct nk_rect *bounds, const struct nk_context *ctx,
186 struct nk_vec2 item_padding)
187{
188 /* update the bounds to stand without padding */
189 enum nk_widget_layout_states state;
190 NK_UNUSED(item_padding);
191
192 NK_ASSERT(ctx);
193 NK_ASSERT(ctx->current);
194 NK_ASSERT(ctx->current->layout);
195 if (!ctx || !ctx->current || !ctx->current->layout)
196 return NK_WIDGET_INVALID;
197
198 state = nk_widget(bounds, ctx);
199 return state;
200}
201NK_API void
202nk_spacing(struct nk_context *ctx, int cols)
203{
204 struct nk_window *win;
205 struct nk_panel *layout;
206 struct nk_rect none;
207 int i, index, rows;
208
209 NK_ASSERT(ctx);
210 NK_ASSERT(ctx->current);
211 NK_ASSERT(ctx->current->layout);
212 if (!ctx || !ctx->current || !ctx->current->layout)
213 return;
214
215 /* spacing over row boundaries */
216 win = ctx->current;
217 layout = win->layout;
218 index = (layout->row.index + cols) % layout->row.columns;
219 rows = (layout->row.index + cols) / layout->row.columns;
220 if (rows) {
221 for (i = 0; i < rows; ++i)
222 nk_panel_alloc_row(ctx, win);
223 cols = index;
224 }
225 /* non table layout need to allocate space */
226 if (layout->row.type != NK_LAYOUT_DYNAMIC_FIXED &&
227 layout->row.type != NK_LAYOUT_STATIC_FIXED) {
228 for (i = 0; i < cols; ++i)
229 nk_panel_alloc_space(&none, ctx);
230 } layout->row.index = index;
231}
232NK_API void
233nk_widget_disable_begin(struct nk_context* ctx)
234{
235 struct nk_window* win;
236 struct nk_style* style;
237
238 NK_ASSERT(ctx);
239 NK_ASSERT(ctx->current);
240
241 if (!ctx || !ctx->current)
242 return;
243
244 win = ctx->current;
245 style = &ctx->style;
246
247 win->widgets_disabled = nk_true;
248
249 style->button.color_factor_text = style->button.disabled_factor;
250 style->button.color_factor_background = style->button.disabled_factor;
251 style->chart.color_factor = style->chart.disabled_factor;
252 style->checkbox.color_factor = style->checkbox.disabled_factor;
253 style->combo.color_factor = style->combo.disabled_factor;
254 style->combo.button.color_factor_text = style->combo.button.disabled_factor;
255 style->combo.button.color_factor_background = style->combo.button.disabled_factor;
256 style->contextual_button.color_factor_text = style->contextual_button.disabled_factor;
257 style->contextual_button.color_factor_background = style->contextual_button.disabled_factor;
258 style->edit.color_factor = style->edit.disabled_factor;
259 style->edit.scrollbar.color_factor = style->edit.scrollbar.disabled_factor;
260 style->menu_button.color_factor_text = style->menu_button.disabled_factor;
261 style->menu_button.color_factor_background = style->menu_button.disabled_factor;
262 style->option.color_factor = style->option.disabled_factor;
263 style->progress.color_factor = style->progress.disabled_factor;
264 style->property.color_factor = style->property.disabled_factor;
265 style->property.inc_button.color_factor_text = style->property.inc_button.disabled_factor;
266 style->property.inc_button.color_factor_background = style->property.inc_button.disabled_factor;
267 style->property.dec_button.color_factor_text = style->property.dec_button.disabled_factor;
268 style->property.dec_button.color_factor_background = style->property.dec_button.disabled_factor;
269 style->property.edit.color_factor = style->property.edit.disabled_factor;
270 style->scrollh.color_factor = style->scrollh.disabled_factor;
271 style->scrollh.inc_button.color_factor_text = style->scrollh.inc_button.disabled_factor;
272 style->scrollh.inc_button.color_factor_background = style->scrollh.inc_button.disabled_factor;
273 style->scrollh.dec_button.color_factor_text = style->scrollh.dec_button.disabled_factor;
274 style->scrollh.dec_button.color_factor_background = style->scrollh.dec_button.disabled_factor;
275 style->scrollv.color_factor = style->scrollv.disabled_factor;
276 style->scrollv.inc_button.color_factor_text = style->scrollv.inc_button.disabled_factor;
277 style->scrollv.inc_button.color_factor_background = style->scrollv.inc_button.disabled_factor;
278 style->scrollv.dec_button.color_factor_text = style->scrollv.dec_button.disabled_factor;
279 style->scrollv.dec_button.color_factor_background = style->scrollv.dec_button.disabled_factor;
280 style->selectable.color_factor = style->selectable.disabled_factor;
281 style->slider.color_factor = style->slider.disabled_factor;
282 style->slider.inc_button.color_factor_text = style->slider.inc_button.disabled_factor;
283 style->slider.inc_button.color_factor_background = style->slider.inc_button.disabled_factor;
284 style->slider.dec_button.color_factor_text = style->slider.dec_button.disabled_factor;
285 style->slider.dec_button.color_factor_background = style->slider.dec_button.disabled_factor;
286 style->tab.color_factor = style->tab.disabled_factor;
287 style->tab.node_maximize_button.color_factor_text = style->tab.node_maximize_button.disabled_factor;
288 style->tab.node_minimize_button.color_factor_text = style->tab.node_minimize_button.disabled_factor;
289 style->tab.tab_maximize_button.color_factor_text = style->tab.tab_maximize_button.disabled_factor;
290 style->tab.tab_maximize_button.color_factor_background = style->tab.tab_maximize_button.disabled_factor;
291 style->tab.tab_minimize_button.color_factor_text = style->tab.tab_minimize_button.disabled_factor;
292 style->tab.tab_minimize_button.color_factor_background = style->tab.tab_minimize_button.disabled_factor;
293 style->text.color_factor = style->text.disabled_factor;
294}
295NK_API void
296nk_widget_disable_end(struct nk_context* ctx)
297{
298 struct nk_window* win;
299 struct nk_style* style;
300
301 NK_ASSERT(ctx);
302 NK_ASSERT(ctx->current);
303
304 if (!ctx || !ctx->current)
305 return;
306
307 win = ctx->current;
308 style = &ctx->style;
309
310 win->widgets_disabled = nk_false;
311
312 style->button.color_factor_text = 1.0f;
313 style->button.color_factor_background = 1.0f;
314 style->chart.color_factor = 1.0f;
315 style->checkbox.color_factor = 1.0f;
316 style->combo.color_factor = 1.0f;
317 style->combo.button.color_factor_text = 1.0f;
318 style->combo.button.color_factor_background = 1.0f;
319 style->contextual_button.color_factor_text = 1.0f;
320 style->contextual_button.color_factor_background = 1.0f;
321 style->edit.color_factor = 1.0f;
322 style->edit.scrollbar.color_factor = 1.0f;
323 style->menu_button.color_factor_text = 1.0f;
324 style->menu_button.color_factor_background = 1.0f;
325 style->option.color_factor = 1.0f;
326 style->progress.color_factor = 1.0f;
327 style->property.color_factor = 1.0f;
328 style->property.inc_button.color_factor_text = 1.0f;
329 style->property.inc_button.color_factor_background = 1.0f;
330 style->property.dec_button.color_factor_text = 1.0f;
331 style->property.dec_button.color_factor_background = 1.0f;
332 style->property.edit.color_factor = 1.0f;
333 style->scrollh.color_factor = 1.0f;
334 style->scrollh.inc_button.color_factor_text = 1.0f;
335 style->scrollh.inc_button.color_factor_background = 1.0f;
336 style->scrollh.dec_button.color_factor_text = 1.0f;
337 style->scrollh.dec_button.color_factor_background = 1.0f;
338 style->scrollv.color_factor = 1.0f;
339 style->scrollv.inc_button.color_factor_text = 1.0f;
340 style->scrollv.inc_button.color_factor_background = 1.0f;
341 style->scrollv.dec_button.color_factor_text = 1.0f;
342 style->scrollv.dec_button.color_factor_background = 1.0f;
343 style->selectable.color_factor = 1.0f;
344 style->slider.color_factor = 1.0f;
345 style->slider.inc_button.color_factor_text = 1.0f;
346 style->slider.inc_button.color_factor_background = 1.0f;
347 style->slider.dec_button.color_factor_text = 1.0f;
348 style->slider.dec_button.color_factor_background = 1.0f;
349 style->tab.color_factor = 1.0f;
350 style->tab.node_maximize_button.color_factor_text = 1.0f;
351 style->tab.node_minimize_button.color_factor_text = 1.0f;
352 style->tab.tab_maximize_button.color_factor_text = 1.0f;
353 style->tab.tab_maximize_button.color_factor_background = 1.0f;
354 style->tab.tab_minimize_button.color_factor_text = 1.0f;
355 style->tab.tab_minimize_button.color_factor_background = 1.0f;
356 style->text.color_factor = 1.0f;
357}
main API and documentation file
@ NK_WINDOW_CLOSED
Directly closes and frees the window at the end of the frame.
Definition nuklear.h:5495
@ NK_WINDOW_MINIMIZED
marks the window as minimized
Definition nuklear.h:5496
@ NK_WINDOW_HIDDEN
Hides window and stops any window interaction and drawing.
Definition nuklear.h:5494
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
@ NK_WIDGET_VALID
The widget is completely inside the window and can be updated and drawn.
Definition nuklear.h:3083
@ NK_WIDGET_INVALID
The widget cannot be seen and is completely out of view.
Definition nuklear.h:3082