diff --git a/demo/common/nuklear_console_demo.c b/demo/common/nuklear_console_demo.c index 346398b..651f2eb 100644 --- a/demo/common/nuklear_console_demo.c +++ b/demo/common/nuklear_console_demo.c @@ -31,6 +31,10 @@ static float property_float_test = 0.4f; static int slider_int_test = 20; static float slider_float_test = 0.4f; +// Knob +static int knob_int_test = 20; +static float knob_float_test = 0.4f; + // Radio static int radio_option = 1; static int radio_option2 = 0; @@ -255,6 +259,8 @@ nk_console* nuklear_console_demo_init(struct nk_context* ctx, void* user_data, s { nk_console_property_int(properties, "Property Int", 10, &property_int_test, 30, 1, 1); nk_console_property_float(properties, "Property Float", 0.0f, &property_float_test, 2.0f, 0.1f, 1); + nk_console_knob_int(properties, "Knob Int", 0, &knob_int_test, 30, 1, 1); + nk_console_knob_float(properties, "Knob Float", 0.0f, &knob_float_test, 2.0f, 0.1f, 1); nk_console_button_onclick(properties, "Back", &nk_console_button_back); } diff --git a/nuklear_console.h b/nuklear_console.h index 16ce80e..05a8514 100644 --- a/nuklear_console.h +++ b/nuklear_console.h @@ -70,6 +70,8 @@ typedef enum { NK_CONSOLE_INPUT, NK_CONSOLE_INPUT_ACTIVE, NK_CONSOLE_RADIO, + NK_CONSOLE_KNOB_INT, + NK_CONSOLE_KNOB_FLOAT, } nk_console_widget_type; typedef struct nk_console_message { @@ -212,6 +214,7 @@ NK_API void nk_console_set_user_data(nk_console* console, void* user_data); #include "nuklear_console_spacing.h" #include "nuklear_console_textedit.h" #include "nuklear_console_textedit_text.h" +#include "nuklear_console_knob.h" #undef NK_CONSOLE_HEADER_ONLY #if defined(__cplusplus) @@ -295,6 +298,7 @@ NK_API nk_bool nk_input_is_mouse_moved(const struct nk_input* input); #include "nuklear_console_spacing.h" #include "nuklear_console_textedit.h" #include "nuklear_console_textedit_text.h" +#include "nuklear_console_knob.h" NK_API const char* nk_console_get_label(nk_console* widget) { if (widget == NULL) { diff --git a/nuklear_console_knob.h b/nuklear_console_knob.h new file mode 100644 index 0000000..7194ff4 --- /dev/null +++ b/nuklear_console_knob.h @@ -0,0 +1,92 @@ +#ifndef NK_CONSOLE_KNOB_H__ +#define NK_CONSOLE_KNOB_H__ + +#if defined(__cplusplus) +extern "C" { +#endif + +/** + * Data for Property and Slider widgets. + */ +typedef struct nk_console_knob_data { + nk_console_property_data property; /* Inherited from property */ + enum nk_heading zero_direction; + float dead_zone_degrees; +} nk_console_knob_data; + +NK_API nk_console* nk_console_knob_int(nk_console* parent, const char* label, int min, int* val, int max, int step, float inc_per_pixel); +NK_API nk_console* nk_console_knob_float(nk_console* parent, const char* label, float min, float* val, float max, float step, float inc_per_pixel); + +#if defined(__cplusplus) +} +#endif + +#endif // NK_CONSOLE_KNOB_H__ + +#if defined(NK_CONSOLE_IMPLEMENTATION) && !defined(NK_CONSOLE_HEADER_ONLY) +#ifndef NK_CONSOLE_KNOB_IMPLEMENTATION_ONCE +#define NK_CONSOLE_KNOB_IMPLEMENTATION_ONCE + +#if defined(__cplusplus) +extern "C" { +#endif + +NK_API nk_console* nk_console_knob_int(nk_console* parent, const char* label, int min, int* val, int max, int step, float inc_per_pixel) { + // Create the property data. + nk_console_knob_data* data = (nk_console_knob_data*)NK_CONSOLE_MALLOC(nk_handle_id(0), NULL, sizeof(nk_console_knob_data)); + nk_zero(data, sizeof(nk_console_knob_data)); + data->property.min_int = min; + data->property.val_int = val; + data->property.max_int = max; + data->property.step_int = step; + data->property.inc_per_pixel = inc_per_pixel; + + data->zero_direction = NK_DOWN; + data->dead_zone_degrees = 60.0f; + + nk_console* widget = nk_console_label(parent, label); + widget->render = &nk_console_property_render; + widget->type = NK_CONSOLE_KNOB_INT; + widget->selectable = nk_true; + widget->data = (void*)data; + widget->columns = label != NULL ? 2 : 1; + + if (val != NULL) { + if (*val < min) { + *val = min; + } + else if (*val > max) { + *val = max; + } + } + + return widget; +} + +NK_API nk_console* nk_console_knob_float(nk_console* parent, const char* label, float min, float* val, float max, float step, float inc_per_pixel) { + nk_console* widget = nk_console_knob_int(parent, label, 0, NULL, 0, 0, inc_per_pixel); + nk_console_knob_data* data = (nk_console_knob_data*)widget->data; + widget->type = NK_CONSOLE_KNOB_FLOAT; + data->property.min_float = min; + data->property.val_float = val; + data->property.max_float = max; + data->property.step_float = step; + + if (val != NULL) { + if (*val < min) { + *val = min; + } + else if (*val > max) { + *val = max; + } + } + + return widget; +} + +#if defined(__cplusplus) +} +#endif + +#endif // NK_CONSOLE_KNOB_IMPLEMENTATION_ONCE +#endif // NK_CONSOLE_IMPLEMENTATION diff --git a/nuklear_console_property.h b/nuklear_console_property.h index 79e164b..c8968fb 100644 --- a/nuklear_console_property.h +++ b/nuklear_console_property.h @@ -46,12 +46,12 @@ NK_API struct nk_rect nk_console_property_render(nk_console* console) { return nk_rect(0, 0, 0, 0); } - if (console->type == NK_CONSOLE_PROPERTY_INT || console->type == NK_CONSOLE_SLIDER_INT) { + if (console->type == NK_CONSOLE_PROPERTY_INT || console->type == NK_CONSOLE_SLIDER_INT || console->type == NK_CONSOLE_KNOB_INT) { if (data->val_int == NULL) { return nk_rect(0, 0, 0, 0); } } - else if (console->type == NK_CONSOLE_PROPERTY_FLOAT || console->type == NK_CONSOLE_SLIDER_FLOAT) { + else if (console->type == NK_CONSOLE_PROPERTY_FLOAT || console->type == NK_CONSOLE_SLIDER_FLOAT || console->type == NK_CONSOLE_SLIDER_FLOAT) { if (data->val_float == NULL) { return nk_rect(0, 0, 0, 0); } @@ -69,6 +69,7 @@ NK_API struct nk_rect nk_console_property_render(nk_console* console) { switch (console->type) { case NK_CONSOLE_SLIDER_INT: case NK_CONSOLE_PROPERTY_INT: + case NK_CONSOLE_KNOB_INT: *data->val_int = *data->val_int - data->step_int; if (*data->val_int < data->min_int) { *data->val_int = data->min_int; @@ -76,6 +77,7 @@ NK_API struct nk_rect nk_console_property_render(nk_console* console) { break; case NK_CONSOLE_SLIDER_FLOAT: case NK_CONSOLE_PROPERTY_FLOAT: + case NK_CONSOLE_KNOB_FLOAT: *data->val_float = *data->val_float - data->step_float; if (*data->val_float < data->min_float) { *data->val_float = data->min_float; @@ -92,6 +94,7 @@ NK_API struct nk_rect nk_console_property_render(nk_console* console) { switch (console->type) { case NK_CONSOLE_SLIDER_INT: case NK_CONSOLE_PROPERTY_INT: + case NK_CONSOLE_KNOB_INT: *data->val_int = *data->val_int + data->step_int; if (*data->val_int > data->max_int) { *data->val_int = data->max_int; @@ -99,6 +102,7 @@ NK_API struct nk_rect nk_console_property_render(nk_console* console) { break; case NK_CONSOLE_SLIDER_FLOAT: case NK_CONSOLE_PROPERTY_FLOAT: + case NK_CONSOLE_KNOB_FLOAT: *data->val_float = *data->val_float + data->step_float; if (*data->val_float > data->max_float) { *data->val_float = data->max_float; @@ -119,6 +123,9 @@ NK_API struct nk_rect nk_console_property_render(nk_console* console) { enum nk_symbol_type right = console->ctx->style.property.sym_right; struct nk_color bar_normal = console->ctx->style.slider.bar_normal; struct nk_style_item cursor_normal = console->ctx->style.slider.cursor_normal; + struct nk_color knob_normal = console->ctx->style.knob.knob_normal; + struct nk_color knob_border_color = console->ctx->style.knob.knob_border_color; + struct nk_color border_color = console->ctx->style.knob.border_color; if (!nk_console_is_active_widget(console)) { console->ctx->style.property.sym_left = NK_SYMBOL_NONE; @@ -127,6 +134,9 @@ NK_API struct nk_rect nk_console_property_render(nk_console* console) { else { console->ctx->style.slider.bar_normal = console->ctx->style.slider.bar_hover; console->ctx->style.slider.cursor_normal = console->ctx->style.slider.cursor_hover; + console->ctx->style.knob.knob_normal = console->ctx->style.knob.cursor_hover; + console->ctx->style.knob.knob_border_color = console->ctx->style.knob.cursor_hover; + console->ctx->style.knob.border_color = console->ctx->style.knob.cursor_hover; } // Display the label @@ -147,27 +157,38 @@ NK_API struct nk_rect nk_console_property_render(nk_console* console) { } // Display the widget - char name[NK_MAX_NUMBER_BUFFER]; - NK_MEMCPY(name + 2, console->label, (nk_size)(nk_strlen(console->label) + 1)); - name[0] = '#'; - name[1] = '#'; - int original_val_int = data->val_int != NULL ? *data->val_int : 0; float original_val_float = data->val_float != NULL ? *data->val_float : 0; switch (console->type) { - case NK_CONSOLE_PROPERTY_INT: + case NK_CONSOLE_PROPERTY_INT: { + char name[NK_MAX_NUMBER_BUFFER]; + NK_MEMCPY(name + 2, console->label, (nk_size)(nk_strlen(console->label) + 1)); + name[0] = '#'; + name[1] = '#'; nk_property_int(console->ctx, name, data->min_int, data->val_int, data->max_int, data->step_int, data->inc_per_pixel); - break; - case NK_CONSOLE_PROPERTY_FLOAT: + } break; + case NK_CONSOLE_PROPERTY_FLOAT: { + char name[NK_MAX_NUMBER_BUFFER]; + NK_MEMCPY(name + 2, console->label, (nk_size)(nk_strlen(console->label) + 1)); + name[0] = '#'; + name[1] = '#'; nk_property_float(console->ctx, name, data->min_float, data->val_float, data->max_float, data->step_float, data->inc_per_pixel); - break; + } break; case NK_CONSOLE_SLIDER_INT: nk_slider_int(console->ctx, data->min_int, data->val_int, data->max_int, data->step_int); break; case NK_CONSOLE_SLIDER_FLOAT: nk_slider_float(console->ctx, data->min_float, data->val_float, data->max_float, data->step_float); break; + case NK_CONSOLE_KNOB_FLOAT: { + nk_console_knob_data* knob_data = (nk_console_knob_data*)console->data; + nk_knob_float(console->ctx, data->min_float, data->val_float, data->max_float, data->step_float, knob_data->zero_direction, knob_data->dead_zone_degrees); + } break; + case NK_CONSOLE_KNOB_INT: { + nk_console_knob_data* knob_data = (nk_console_knob_data*)console->data; + nk_knob_int(console->ctx, data->min_int, data->val_int, data->max_int, data->step_int, knob_data->zero_direction, knob_data->dead_zone_degrees); + } break; default: // Nothing break; @@ -186,6 +207,9 @@ NK_API struct nk_rect nk_console_property_render(nk_console* console) { else { console->ctx->style.slider.bar_normal = bar_normal; console->ctx->style.slider.cursor_normal = cursor_normal; + console->ctx->style.knob.knob_normal = knob_normal; + console->ctx->style.knob.knob_border_color = knob_border_color; + console->ctx->style.knob.border_color = border_color; } if (console->disabled) { @@ -212,7 +236,7 @@ NK_API nk_console* nk_console_property_int(nk_console* parent, const char* label data->inc_per_pixel = inc_per_pixel; nk_console* widget = nk_console_label(parent, label); - widget->render = nk_console_property_render; + widget->render = &nk_console_property_render; widget->type = NK_CONSOLE_PROPERTY_INT; widget->selectable = nk_true; widget->data = (void*)data; diff --git a/vendor/Nuklear b/vendor/Nuklear index f7847e6..4ffef5e 160000 --- a/vendor/Nuklear +++ b/vendor/Nuklear @@ -1 +1 @@ -Subproject commit f7847e602440a45fc1993aa88cd0f4d81ec881e8 +Subproject commit 4ffef5eb11b2f7a283bb63832884d0ea66e98705