2#include "nuklear_internal.h"
10nk_slider_behavior(nk_flags *state,
struct nk_rect *logical_cursor,
12 struct nk_rect bounds,
float slider_min,
float slider_max,
float slider_value,
13 float slider_step,
float slider_steps)
16 int left_mouse_click_in_cursor;
19 nk_widget_state_reset(state);
20 left_mouse_down = in && in->mouse.buttons[NK_BUTTON_LEFT].down;
21 left_mouse_click_in_cursor = in && nk_input_has_mouse_click_down_in_rect(in,
22 NK_BUTTON_LEFT, *visual_cursor, nk_true);
24 if (left_mouse_down && left_mouse_click_in_cursor) {
26 const float d = in->mouse.pos.x - (visual_cursor->x+visual_cursor->w*0.5f);
27 const float pxstep = bounds.w / slider_steps;
31 if (NK_ABS(d) >= pxstep) {
32 const float steps = (float)((
int)(NK_ABS(d) / pxstep));
33 slider_value += (d > 0) ? (slider_step*steps) : -(slider_step*steps);
34 slider_value = NK_CLAMP(slider_min, slider_value, slider_max);
35 ratio = (slider_value - slider_min)/slider_step;
36 logical_cursor->x = bounds.x + (logical_cursor->w * ratio);
37 in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = logical_cursor->x;
42 if (nk_input_is_mouse_hovering_rect(in, bounds))
45 !nk_input_is_mouse_prev_hovering_rect(in, bounds))
47 else if (nk_input_is_mouse_prev_hovering_rect(in, bounds))
54 const struct nk_rect *visual_cursor,
float min,
float value,
float max)
69 background = &style->active;
70 bar_color = style->bar_active;
71 cursor = &style->cursor_active;
73 background = &style->hover;
74 bar_color = style->bar_hover;
75 cursor = &style->cursor_hover;
77 background = &style->normal;
78 bar_color = style->bar_normal;
79 cursor = &style->cursor_normal;
84 bar.y = (visual_cursor->y + visual_cursor->h/2) - bounds->h/12;
89 fill.w = (visual_cursor->x + (visual_cursor->w/2.0f)) - bar.x;
95 switch(background->type) {
96 case NK_STYLE_ITEM_IMAGE:
97 nk_draw_image(out, *bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
99 case NK_STYLE_ITEM_NINE_SLICE:
100 nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
102 case NK_STYLE_ITEM_COLOR:
103 nk_fill_rect(out, *bounds, style->rounding, nk_rgb_factor(background->data.color, style->color_factor));
104 nk_stroke_rect(out, *bounds, style->rounding, style->border, nk_rgb_factor(style->border_color, style->color_factor));
109 nk_fill_rect(out, bar, style->rounding, nk_rgb_factor(bar_color, style->color_factor));
110 nk_fill_rect(out, fill, style->rounding, nk_rgb_factor(style->bar_filled, style->color_factor));
113 if (cursor->type == NK_STYLE_ITEM_IMAGE)
114 nk_draw_image(out, *visual_cursor, &cursor->data.image, nk_rgb_factor(nk_white, style->color_factor));
116 nk_fill_circle(out, *visual_cursor, nk_rgb_factor(cursor->data.color, style->color_factor));
119nk_do_slider(nk_flags *state,
121 float min,
float val,
float max,
float step,
141 bounds.x = bounds.x + style->padding.x;
142 bounds.y = bounds.y + style->padding.y;
143 bounds.h = NK_MAX(bounds.h, 2*style->padding.y);
144 bounds.w = NK_MAX(bounds.w, 2*style->padding.x + style->cursor_size.x);
145 bounds.w -= 2 * style->padding.x;
146 bounds.h -= 2 * style->padding.y;
149 if (style->show_buttons) {
158 if (nk_do_button_symbol(&ws, out, button, style->dec_symbol, NK_BUTTON_DEFAULT,
159 &style->dec_button, in, font))
163 button.x = (bounds.x + bounds.w) - button.w;
164 if (nk_do_button_symbol(&ws, out, button, style->inc_symbol, NK_BUTTON_DEFAULT,
165 &style->inc_button, in, font))
168 bounds.x = bounds.x + button.w + style->spacing.x;
169 bounds.w = bounds.w - (2*button.w + 2*style->spacing.x);
173 bounds.x += style->cursor_size.x*0.5f;
174 bounds.w -= style->cursor_size.x;
177 slider_max = NK_MAX(min, max);
178 slider_min = NK_MIN(min, max);
179 slider_value = NK_CLAMP(slider_min, val, slider_max);
180 slider_range = slider_max - slider_min;
181 slider_steps = slider_range / step;
182 cursor_offset = (slider_value - slider_min) / step;
187 logical_cursor.h = bounds.h;
188 logical_cursor.w = bounds.w / slider_steps;
189 logical_cursor.x = bounds.x + (logical_cursor.w * cursor_offset);
190 logical_cursor.y = bounds.y;
192 visual_cursor.h = style->cursor_size.y;
193 visual_cursor.w = style->cursor_size.x;
194 visual_cursor.y = (bounds.y + bounds.h*0.5f) - visual_cursor.h*0.5f;
195 visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
197 slider_value = nk_slider_behavior(state, &logical_cursor, &visual_cursor,
198 in, bounds, slider_min, slider_max, slider_value, step, slider_steps);
199 visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
202 if (style->draw_begin) style->draw_begin(out, style->userdata);
203 nk_draw_slider(out, *state, style, &bounds, &visual_cursor, slider_min, slider_value, slider_max);
204 if (style->draw_end) style->draw_end(out, style->userdata);
208nk_slider_float(
struct nk_context *ctx,
float min_value,
float *value,
float max_value,
222 NK_ASSERT(ctx->current);
223 NK_ASSERT(ctx->current->layout);
225 if (!ctx || !ctx->current || !ctx->current->layout || !value)
230 layout = win->layout;
232 state = nk_widget(&bounds, ctx);
233 if (!state)
return ret;
237 *value = nk_do_slider(&ctx->last_widget_state, &win->buffer, bounds, min_value,
238 old_value, max_value, value_step, &style->slider, in, style->font);
239 return (old_value > *value || old_value < *value);
242nk_slide_float(
struct nk_context *ctx,
float min,
float val,
float max,
float step)
244 nk_slider_float(ctx, min, &val, max, step);
return val;
247nk_slide_int(
struct nk_context *ctx,
int min,
int val,
int max,
int step)
249 float value = (float)val;
250 nk_slider_float(ctx, (
float)min, &value, (
float)max, (
float)step);
254nk_slider_int(
struct nk_context *ctx,
int min,
int *val,
int max,
int step)
257 float value = (float)*val;
258 ret = nk_slider_float(ctx, (
float)min, &value, (
float)max, (
float)step);
main API and documentation file
@ 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_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.