2#include "nuklear_internal.h"
11nk_knob_behavior(nk_flags *state,
struct nk_input *in,
12 struct nk_rect bounds,
float knob_min,
float knob_max,
float knob_value,
13 float knob_step,
float knob_steps,
14 enum nk_heading zero_direction,
float dead_zone_percent)
18 origin.x = bounds.x + (bounds.w / 2);
19 origin.y = bounds.y + (bounds.h / 2);
21 nk_widget_state_reset(state);
25 in->mouse.buttons[NK_BUTTON_LEFT].down &&
26 nk_input_has_mouse_click_down_in_rect(in, NK_BUTTON_LEFT, bounds, nk_true)){
28 const float direction_rads[4] = {
36 angle = NK_ATAN2(in->mouse.pos.y - origin.y, in->mouse.pos.x - origin.x) + direction_rads[zero_direction];
37 angle -= (angle > NK_PI * 2) ? NK_PI * 3 : NK_PI;
40 angle *= 1.0f / (1.0f - dead_zone_percent);
41 angle = NK_CLAMP(-NK_PI, angle, NK_PI);
44 angle = (angle + NK_PI) / (NK_PI * 2);
47 knob_value = knob_min + ( (int)(angle * knob_steps + (knob_step / 2)) ) * knob_step;
48 knob_value = NK_CLAMP(knob_min, knob_value, knob_max);
52 if (nk_input_is_mouse_hovering_rect(in, bounds)){
55 if (in->mouse.scroll_delta.y > 0 ||
56 (in->keyboard.keys[NK_KEY_UP].down && in->keyboard.keys[NK_KEY_UP].clicked)) {
57 knob_value += knob_step;
60 if (in->mouse.scroll_delta.y < 0 ||
61 (in->keyboard.keys[NK_KEY_DOWN].down && in->keyboard.keys[NK_KEY_DOWN].clicked)) {
62 knob_value -= knob_step;
65 in->mouse.scroll_delta.y = 0;
66 knob_value = NK_CLAMP(knob_min, knob_value, knob_max);
69 !nk_input_is_mouse_prev_hovering_rect(in, bounds))
71 else if (nk_input_is_mouse_prev_hovering_rect(in, bounds))
78 const struct nk_style_knob *style,
const struct nk_rect *bounds,
float min,
float value,
float max,
79 enum nk_heading zero_direction,
float dead_zone_percent)
89 background = &style->active;
90 knob_color = style->knob_active;
91 cursor = style->cursor_active;
93 background = &style->hover;
94 knob_color = style->knob_hover;
95 cursor = style->cursor_hover;
97 background = &style->normal;
98 knob_color = style->knob_normal;
99 cursor = style->cursor_normal;
103 switch(background->type) {
104 case NK_STYLE_ITEM_IMAGE:
105 nk_draw_image(out, *bounds, &background->data.image, nk_rgb_factor(nk_white, style->color_factor));
107 case NK_STYLE_ITEM_NINE_SLICE:
108 nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_rgb_factor(nk_white, style->color_factor));
110 case NK_STYLE_ITEM_COLOR:
111 nk_fill_rect(out, *bounds, 0, nk_rgb_factor(background->data.color, style->color_factor));
112 nk_stroke_rect(out, *bounds, 0, style->border, nk_rgb_factor(style->border_color, style->color_factor));
117 nk_fill_circle(out, *bounds, nk_rgb_factor(knob_color, style->color_factor));
118 if(style->knob_border > 0){
119 struct nk_rect border_bounds = *bounds;
120 border_bounds.x += style->knob_border / 2;
121 border_bounds.y += style->knob_border / 2;
122 border_bounds.w -= style->knob_border;
123 border_bounds.h -= style->knob_border;
124 nk_stroke_circle(out, border_bounds, style->knob_border, nk_rgb_factor(style->knob_border_color, style->color_factor));
127 float half_circle_size = (bounds->w / 2);
128 float angle = (value - min) / (max - min);
129 float alive_zone = 1.0f - dead_zone_percent;
130 struct nk_vec2 cursor_start, cursor_end;
131 const float direction_rads[4] = {
138 angle = (angle * alive_zone) + (dead_zone_percent / 2);
144 angle += direction_rads[zero_direction];
145 if(angle > NK_PI * 2)
148 cursor_start.x = bounds->x + half_circle_size + (angle > NK_PI);
149 cursor_start.y = bounds->y + half_circle_size + (angle < NK_PI_HALF || angle > (NK_PI * 1.5f));
151 cursor_end.x = cursor_start.x + (half_circle_size * NK_COS(angle));
152 cursor_end.y = cursor_start.y + (half_circle_size * NK_SIN(angle));
155 cursor_start.x = (cursor_start.x + cursor_end.x) / 2;
156 cursor_start.y = (cursor_start.y + cursor_end.y) / 2;
159 nk_stroke_line(out, cursor_start.x, cursor_start.y, cursor_end.x, cursor_end.y, 2, nk_rgb_factor(cursor, style->color_factor));
163nk_do_knob(nk_flags *state,
165 float min,
float val,
float max,
float step,
166 enum nk_heading zero_direction,
float dead_zone_percent,
181 bounds.y = bounds.y + style->padding.y;
182 bounds.x = bounds.x + style->padding.x;
183 bounds.h = NK_MAX(bounds.h, 2*style->padding.y);
184 bounds.w = NK_MAX(bounds.w, 2*style->padding.x);
185 bounds.w -= 2 * style->padding.x;
186 bounds.h -= 2 * style->padding.y;
187 if(bounds.h < bounds.w){
188 bounds.x += (bounds.w - bounds.h) / 2;
193 knob_max = NK_MAX(min, max);
194 knob_min = NK_MIN(min, max);
195 knob_value = NK_CLAMP(knob_min, val, knob_max);
196 knob_range = knob_max - knob_min;
197 knob_steps = knob_range / step;
199 knob_value = nk_knob_behavior(state, in, bounds, knob_min, knob_max, knob_value, step, knob_steps, zero_direction, dead_zone_percent);
202 if (style->draw_begin) style->draw_begin(out, style->userdata);
203 nk_draw_knob(out, *state, style, &bounds, knob_min, knob_value, knob_max, zero_direction, dead_zone_percent);
204 if (style->draw_end) style->draw_end(out, style->userdata);
208nk_knob_float(
struct nk_context *ctx,
float min_value,
float *value,
float max_value,
209 float value_step,
enum nk_heading zero_direction,
float dead_zone_degrees)
222 NK_ASSERT(ctx->current);
223 NK_ASSERT(ctx->current->layout);
225 NK_ASSERT(NK_BETWEEN(dead_zone_degrees, 0.0f, 360.0f));
226 if (!ctx || !ctx->current || !ctx->current->layout || !value)
231 layout = win->layout;
233 state = nk_widget(&bounds, ctx);
234 if (!state)
return ret;
238 *value = nk_do_knob(&ctx->last_widget_state, &win->buffer, bounds, min_value,
239 old_value, max_value, value_step, zero_direction, dead_zone_degrees / 360.0f, &style->knob, in);
241 return (old_value > *value || old_value < *value);
244nk_knob_int(
struct nk_context *ctx,
int min,
int *val,
int max,
int step,
245 enum nk_heading zero_direction,
float dead_zone_degrees)
248 float value = (float)*val;
249 ret = nk_knob_float(ctx, (
float)min, &value, (
float)max, (
float)step, zero_direction, dead_zone_degrees);
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_stroke_line(struct nk_command_buffer *b, float x0, float y0, float x1, float y1, float line_thickness, struct nk_color)
shape outlines
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.