Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add knob #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions demo/common/nuklear_console_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions nuklear_console.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
92 changes: 92 additions & 0 deletions nuklear_console_knob.h
Original file line number Diff line number Diff line change
@@ -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
48 changes: 36 additions & 12 deletions nuklear_console_property.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -69,13 +69,15 @@ 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;
}
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;
Expand All @@ -92,13 +94,15 @@ 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;
}
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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion vendor/Nuklear
Loading