2#include "nuklear_internal.h"
10nk_drag_behavior(nk_flags *state,
const struct nk_input *in,
14 int left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
15 int left_mouse_click_in_cursor = in &&
16 nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, drag, nk_true);
18 nk_widget_state_reset(state);
19 if (nk_input_is_mouse_hovering_rect(in, drag))
22 if (left_mouse_down && left_mouse_click_in_cursor) {
24 pixels = in->mouse.delta.x;
25 delta = pixels * inc_per_pixel;
26 switch (variant->kind) {
29 variant->value.i = variant->value.i + (int)delta;
30 variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
32 case NK_PROPERTY_FLOAT:
33 variant->value.f = variant->value.f + (float)delta;
34 variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
36 case NK_PROPERTY_DOUBLE:
37 variant->value.d = variant->value.d + (double)delta;
38 variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
45 else if (nk_input_is_mouse_prev_hovering_rect(in, drag))
49nk_property_behavior(nk_flags *ws,
const struct nk_input *in,
54 nk_widget_state_reset(ws);
55 if (in && *state == NK_PROPERTY_DEFAULT) {
56 if (nk_button_behavior(ws, edit, in, NK_BUTTON_DEFAULT))
57 *state = NK_PROPERTY_EDIT;
58 else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, label, nk_true))
59 *state = NK_PROPERTY_DRAG;
60 else if (nk_input_is_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, empty, nk_true))
61 *state = NK_PROPERTY_DRAG;
63 if (*state == NK_PROPERTY_DRAG) {
64 nk_drag_behavior(ws, in, property, variant, inc_per_pixel);
70 const struct nk_rect *bounds,
const struct nk_rect *label, nk_flags state,
71 const char *name,
int len,
const struct nk_user_font *font)
78 background = &style->active;
79 text.text = style->label_active;
81 background = &style->hover;
82 text.text = style->label_hover;
84 background = &style->normal;
85 text.text = style->label_normal;
88 text.text = nk_rgb_factor(text.text, style->color_factor);
91 switch(background->type) {
92 case NK_STYLE_ITEM_IMAGE:
93 text.background = nk_rgba(0, 0, 0, 0);
94 nk_draw_image(out, *bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
96 case NK_STYLE_ITEM_NINE_SLICE:
97 text.background = nk_rgba(0, 0, 0, 0);
98 nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
100 case NK_STYLE_ITEM_COLOR:
101 text.background = background->data.color;
102 nk_fill_rect(out, *bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor));
103 nk_stroke_rect(out, *bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor));
109 if (name && name[0] !=
'#') {
110 nk_widget_text(out, *label, name, len, &text, NK_TEXT_CENTERED, font);
117 switch (variant->kind) {
119 case NK_PROPERTY_INT:
120 variant->value.i = nk_strtoi(buffer, 0);
121 variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
123 case NK_PROPERTY_FLOAT:
124 nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
125 variant->value.f = nk_strtof(buffer, 0);
126 variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
128 case NK_PROPERTY_DOUBLE:
129 nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
130 variant->value.d = NK_STRTOD(buffer, 0);
131 variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
136nk_do_property(nk_flags *ws,
139 float inc_per_pixel,
char *buffer,
int *len,
140 int *state,
int *cursor,
int *select_begin,
int *select_end,
142 enum nk_property_filter filter,
struct nk_input *in,
144 enum nk_button_behavior behavior)
146 const nk_plugin_filter filters[] = {
151 int num_len = 0, name_len = 0;
152 char string[NK_MAX_NUMBER_BUFFER];
167 left.x =
property.x + style->border + style->padding.x;
168 left.y =
property.y + style->border +
property.h/2.0f - left.h/2;
171 if (name && name[0] !=
'#') {
172 name_len = nk_strlen(name);
174 size = font->
width(font->userdata, font->
height, name, name_len);
175 label.x = left.x + left.w + style->padding.x;
176 label.w = (float)size + 2 * style->padding.x;
177 label.y =
property.y + style->border + style->padding.y;
178 label.h =
property.h - (2 * style->border + 2 * style->padding.y);
184 right.x =
property.x +
property.w - (right.w + style->padding.x);
187 if (*state == NK_PROPERTY_EDIT) {
188 size = font->
width(font->userdata, font->
height, buffer, *len);
189 size += style->edit.cursor_size;
193 switch (variant->kind) {
195 case NK_PROPERTY_INT:
196 nk_itoa(
string, variant->value.i);
197 num_len = nk_strlen(
string);
199 case NK_PROPERTY_FLOAT:
200 NK_DTOA(
string, (
double)variant->value.f);
201 num_len = nk_string_float_limit(
string, NK_MAX_FLOAT_PRECISION);
203 case NK_PROPERTY_DOUBLE:
204 NK_DTOA(
string, variant->value.d);
205 num_len = nk_string_float_limit(
string, NK_MAX_FLOAT_PRECISION);
208 size = font->
width(font->userdata, font->
height,
string, num_len);
213 edit.w = (float)size + 2 * style->padding.x;
214 edit.w = NK_MIN(edit.w, right.x - (label.x + label.w));
215 edit.x = right.x - (edit.w + style->padding.x);
216 edit.y =
property.y + style->border;
217 edit.h =
property.h - (2 * style->border);
220 empty.w = edit.x - (label.x + label.w);
221 empty.x = label.x + label.w;
222 empty.y =
property.y;
223 empty.h =
property.h;
226 old = (*state == NK_PROPERTY_EDIT);
227 nk_property_behavior(ws, in, property, label, edit, empty, state, variant, inc_per_pixel);
230 if (style->draw_begin) style->draw_begin(out, style->userdata);
231 nk_draw_property(out, style, &property, &label, *ws, name, name_len, font);
232 if (style->draw_end) style->draw_end(out, style->userdata);
235 if (nk_do_button_symbol(ws, out, left, style->sym_left, behavior, &style->dec_button, in, font)) {
236 switch (variant->kind) {
238 case NK_PROPERTY_INT:
239 variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i - variant->step.i, variant->max_value.i);
break;
240 case NK_PROPERTY_FLOAT:
241 variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f - variant->step.f, variant->max_value.f);
break;
242 case NK_PROPERTY_DOUBLE:
243 variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d - variant->step.d, variant->max_value.d);
break;
247 if (nk_do_button_symbol(ws, out, right, style->sym_right, behavior, &style->inc_button, in, font)) {
248 switch (variant->kind) {
250 case NK_PROPERTY_INT:
251 variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i + variant->step.i, variant->max_value.i);
break;
252 case NK_PROPERTY_FLOAT:
253 variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f + variant->step.f, variant->max_value.f);
break;
254 case NK_PROPERTY_DOUBLE:
255 variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d + variant->step.d, variant->max_value.d);
break;
258 if (!old && (*state == NK_PROPERTY_EDIT)) {
260 NK_MEMCPY(buffer, dst, (nk_size)*length);
261 *cursor = nk_utf_len(buffer, *length);
266 }
else active = (*state == NK_PROPERTY_EDIT);
269 nk_textedit_clear_state(text_edit, NK_TEXT_EDIT_SINGLE_LINE, filters[filter]);
270 text_edit->active = (
unsigned char)active;
271 text_edit->string.len = *length;
272 text_edit->cursor = NK_CLAMP(0, *cursor, *length);
273 text_edit->select_start = NK_CLAMP(0,*select_begin, *length);
274 text_edit->select_end = NK_CLAMP(0,*select_end, *length);
275 text_edit->string.buffer.
allocated = (nk_size)*length;
276 text_edit->string.buffer.
memory.size = NK_MAX_NUMBER_BUFFER;
277 text_edit->string.buffer.
memory.ptr = dst;
278 text_edit->string.buffer.
size = NK_MAX_NUMBER_BUFFER;
279 text_edit->mode = NK_TEXT_EDIT_MODE_INSERT;
280 nk_do_edit(ws, out, edit, (
int)NK_EDIT_FIELD|(
int)NK_EDIT_AUTO_SELECT,
281 filters[filter], text_edit, &style->edit, (*state == NK_PROPERTY_EDIT) ? in: 0, font);
283 *length = text_edit->string.len;
284 *cursor = text_edit->cursor;
285 *select_begin = text_edit->select_start;
286 *select_end = text_edit->select_end;
287 if (text_edit->active && nk_input_is_key_pressed(in, NK_KEY_ENTER))
288 text_edit->active = nk_false;
290 if (active && !text_edit->active) {
292 *state = NK_PROPERTY_DEFAULT;
293 nk_property_save(variant, buffer, *len);
297nk_property_variant_int(int value, int min_value, int max_value, int step)
300 result.kind = NK_PROPERTY_INT;
301 result.value.i = value;
302 result.min_value.i = min_value;
303 result.max_value.i = max_value;
304 result.step.i = step;
308nk_property_variant_float(float value, float min_value, float max_value, float step)
311 result.kind = NK_PROPERTY_FLOAT;
312 result.value.f = value;
313 result.min_value.f = min_value;
314 result.max_value.f = max_value;
315 result.step.f = step;
319nk_property_variant_double(double value, double min_value, double max_value,
323 result.kind = NK_PROPERTY_DOUBLE;
324 result.value.d = value;
325 result.min_value.d = min_value;
326 result.max_value.d = max_value;
327 result.step.d = step;
332 float inc_per_pixel,
const enum nk_property_filter filter)
348 int *select_begin = 0;
353 char dummy_buffer[NK_MAX_NUMBER_BUFFER];
354 int dummy_state = NK_PROPERTY_DEFAULT;
355 int dummy_length = 0;
356 int dummy_cursor = 0;
357 int dummy_select_begin = 0;
358 int dummy_select_end = 0;
361 NK_ASSERT(ctx->current);
362 NK_ASSERT(ctx->current->layout);
363 if (!ctx || !ctx->current || !ctx->current->layout)
367 layout = win->layout;
369 s = nk_widget(&bounds, ctx);
373 if (name[0] ==
'#') {
374 hash = nk_murmur_hash(name, (
int)nk_strlen(name), win->property.seq++);
376 }
else hash = nk_murmur_hash(name, (
int)nk_strlen(name), 42);
379 if (win->property.prev_state == NK_PROPERTY_EDIT && hash == win->property.prev_name) {
380 nk_property_save(variant, win->property.prev_buffer, win->property.prev_length);
381 win->property.prev_state = NK_PROPERTY_DEFAULT;
385 hot = win->property.active && hash == win->property.name;
387 buffer = win->property.buffer;
388 len = &win->property.length;
389 cursor = &win->property.cursor;
390 state = &win->property.state;
391 select_begin = &win->property.select_start;
392 select_end = &win->property.select_end;
394 buffer = dummy_buffer;
396 cursor = &dummy_cursor;
397 state = &dummy_state;
398 select_begin = &dummy_select_begin;
399 select_end = &dummy_select_end;
404 prev_state = win->property.state;
408 nk_do_property(&ctx->last_widget_state, &win->buffer, bounds, name,
409 variant, inc_per_pixel, buffer, len, state, cursor, select_begin,
410 select_end, &style->property, filter, in, style->font, &ctx->
text_edit,
411 ctx->button_behavior);
413 if (in && *state != NK_PROPERTY_DEFAULT && !hot) {
415 if (win->property.active ) {
416 win->property.prev_state = prev_state;
417 win->property.prev_name = win->property.name;
418 win->property.prev_length = win->property.length;
419 NK_MEMCPY(win->property.prev_buffer, win->property.buffer, win->property.length);
422 win->property.active = 1;
423 NK_MEMCPY(win->property.buffer, buffer, (nk_size)*len);
424 win->property.length = *len;
425 win->property.cursor = *cursor;
426 win->property.state = *state;
427 win->property.name = hash;
428 win->property.select_start = *select_begin;
429 win->property.select_end = *select_end;
430 win->edit.active = nk_true;
431 if (*state == NK_PROPERTY_DRAG) {
432 ctx->input.mouse.grab = nk_true;
433 ctx->input.mouse.grabbed = nk_true;
437 if (*state == NK_PROPERTY_DEFAULT && old_state != NK_PROPERTY_DEFAULT) {
438 if (old_state == NK_PROPERTY_DRAG) {
439 ctx->input.mouse.grab = nk_false;
440 ctx->input.mouse.grabbed = nk_false;
441 ctx->input.mouse.ungrab = nk_true;
443 win->property.select_start = 0;
444 win->property.select_end = 0;
445 win->property.active = 0;
446 win->edit.active = nk_false;
450nk_property_int(
struct nk_context *ctx,
const char *name,
451 int min,
int *val,
int max,
int step,
float inc_per_pixel)
458 if (!ctx || !ctx->current || !name || !val)
return;
459 variant = nk_property_variant_int(*val, min, max, step);
460 nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
461 *val = variant.value.i;
465 float min,
float *val,
float max,
float step,
float inc_per_pixel)
472 if (!ctx || !ctx->current || !name || !val)
return;
473 variant = nk_property_variant_float(*val, min, max, step);
474 nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
475 *val = variant.value.f;
479 double min,
double *val,
double max,
double step,
float inc_per_pixel)
486 if (!ctx || !ctx->current || !name || !val)
return;
487 variant = nk_property_variant_double(*val, min, max, step);
488 nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
489 *val = variant.value.d;
493 int max,
int step,
float inc_per_pixel)
499 if (!ctx || !ctx->current || !name)
return val;
500 variant = nk_property_variant_int(val, min, max, step);
501 nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_INT);
502 val = variant.value.i;
507 float val,
float max,
float step,
float inc_per_pixel)
513 if (!ctx || !ctx->current || !name)
return val;
514 variant = nk_property_variant_float(val, min, max, step);
515 nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
516 val = variant.value.f;
521 double val,
double max,
double step,
float inc_per_pixel)
527 if (!ctx || !ctx->current || !name)
return val;
528 variant = nk_property_variant_double(val, min, max, step);
529 nk_property(ctx, name, &variant, inc_per_pixel, NK_FILTER_FLOAT);
530 val = variant.value.d;
main API and documentation file
NK_API float nk_propertyf(struct nk_context *, const char *name, float min, float val, float max, float step, float inc_per_pixel)
NK_API void nk_property_float(struct nk_context *, const char *name, float min, float *val, float max, float step, float inc_per_pixel)
@ NK_WINDOW_ROM
sets window widgets into a read only mode and does not allow input changes
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_property_double(struct nk_context *, const char *name, double min, double *val, double max, double step, float inc_per_pixel)
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
@ NK_WIDGET_STATE_ACTIVED
!< widget is being hovered
@ NK_WIDGET_STATE_ENTERED
!< widget is neither active nor hovered
@ NK_WIDGET_STATE_HOVER
!< widget has been hovered on the current frame
@ NK_WIDGET_STATE_ACTIVE
!< widget is being hovered
@ NK_WIDGET_STATE_HOVERED
!< widget is from this frame on not hovered anymore
@ NK_WIDGET_DISABLED
The widget is manually disabled and acts like NK_WIDGET_ROM.
@ NK_WIDGET_ROM
The widget is partially visible and cannot be updated.
NK_API double nk_propertyd(struct nk_context *, const char *name, double min, double val, double max, double step, float inc_per_pixel)
NK_API int nk_propertyi(struct nk_context *, const char *name, int min, int val, int max, int step, float inc_per_pixel)
struct nk_memory memory
!< memory management type
nk_size size
!< number of allocation calls
nk_size allocated
!< growing factor for dynamic memory management
struct nk_text_edit text_edit
text editor objects are quite big because of an internal undo/redo stack.
nk_text_width_f width
!< max height of the font
float height
!< user provided font handle