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;
30 *state = NK_WIDGET_STATE_ACTIVE;
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))
43 *state = NK_WIDGET_STATE_HOVERED;
44 if (*state & NK_WIDGET_STATE_HOVER &&
45 !nk_input_is_mouse_prev_hovering_rect(in, bounds))
46 *state |= NK_WIDGET_STATE_ENTERED;
47 else if (nk_input_is_mouse_prev_hovering_rect(in, bounds))
48 *state |= NK_WIDGET_STATE_LEFT;
54 const struct nk_rect *visual_cursor,
float min,
float value,
float max)
68 if (state & NK_WIDGET_STATE_ACTIVED) {
69 background = &style->active;
70 bar_color = style->bar_active;
71 cursor = &style->cursor_active;
72 }
else if (state & NK_WIDGET_STATE_HOVER) {
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 = (bounds->y + bounds->h*0.5f) - style->bar_height*0.5f;
86 bar.h = style->bar_height;
89 fill.w = visual_cursor->x + 0.5f * visual_cursor->w - 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 slider_max = NK_MAX(min, max);
174 slider_min = NK_MIN(min, max);
175 slider_value = NK_CLAMP(slider_min, val, slider_max);
176 slider_range = slider_max - slider_min;
177 slider_steps = slider_range / step;
178 cursor_offset = (slider_value - slider_min) / step;
183 logical_cursor.h = bounds.h;
184 logical_cursor.w = bounds.w / slider_steps;
185 logical_cursor.x = bounds.x + (logical_cursor.w * cursor_offset);
186 logical_cursor.y = bounds.y;
188 visual_cursor.h = style->cursor_size.y;
189 visual_cursor.w = style->cursor_size.x;
190 visual_cursor.y = (bounds.y + bounds.h*0.5f) - visual_cursor.h*0.5f;
191 visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
193 slider_value = nk_slider_behavior(state, &logical_cursor, &visual_cursor,
194 in, bounds, slider_min, slider_max, slider_value, step, slider_steps);
195 visual_cursor.x = logical_cursor.x - visual_cursor.w*0.5f;
198 if (style->draw_begin) style->draw_begin(out, style->userdata);
199 nk_draw_slider(out, *state, style, &bounds, &visual_cursor, slider_min, slider_value, slider_max);
200 if (style->draw_end) style->draw_end(out, style->userdata);
204nk_slider_float(
struct nk_context *ctx,
float min_value,
float *value,
float max_value,
215 enum nk_widget_layout_states state;
218 NK_ASSERT(ctx->current);
219 NK_ASSERT(ctx->current->layout);
221 if (!ctx || !ctx->current || !ctx->current->layout || !value)
226 layout = win->layout;
228 state = nk_widget(&bounds, ctx);
229 if (!state)
return ret;
230 in = ( state == NK_WIDGET_DISABLED || layout->flags & NK_WINDOW_ROM) ? 0 : &ctx->input;
233 *value = nk_do_slider(&ctx->last_widget_state, &win->buffer, bounds, min_value,
234 old_value, max_value, value_step, &style->slider, in, style->font);
235 return (old_value > *value || old_value < *value);
238nk_slide_float(
struct nk_context *ctx,
float min,
float val,
float max,
float step)
240 nk_slider_float(ctx, min, &val, max, step);
return val;
243nk_slide_int(
struct nk_context *ctx,
int min,
int val,
int max,
int step)
245 float value = (float)val;
246 nk_slider_float(ctx, (
float)min, &value, (
float)max, (
float)step);
250nk_slider_int(
struct nk_context *ctx,
int min,
int *val,
int max,
int step)
253 float value = (float)*val;
254 ret = nk_slider_float(ctx, (
float)min, &value, (
float)max, (
float)step);