-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnuklear_console_property.h
More file actions
312 lines (278 loc) · 12.4 KB
/
nuklear_console_property.h
File metadata and controls
312 lines (278 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#ifndef NK_CONSOLE_PROPERTY_H__
#define NK_CONSOLE_PROPERTY_H__
/**
* Data for Property and Slider widgets.
*/
typedef struct nk_console_property_data {
int min_int; /** The minimum value, represented as an integer. */
int max_int; /** The maximum value, represented as an integer. */
int step_int; /** How much each step should increment. */
float min_float; /** The minimum value, represented as a float. */
float max_float; /** The maximum value, represented as a float. */
float step_float; /** How much each step should increment. */
float inc_per_pixel; /** The increment per pixel value as a float. */
int* val_int; /** Pointer to the integer value. */
float* val_float; /** Pointer to the float value. */
} nk_console_property_data;
#if defined(__cplusplus)
extern "C" {
#endif
NK_API nk_console* nk_console_property_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_property_float(nk_console* parent, const char* label, float min, float* val, float max, float step, float inc_per_pixel);
NK_API nk_console* nk_console_slider_int(nk_console* parent, const char* label, int min, int* val, int max, int step);
NK_API nk_console* nk_console_slider_float(nk_console* parent, const char* label, float min, float* val, float max, float step);
NK_API struct nk_rect nk_console_property_render(nk_console* console);
#if defined(__cplusplus)
}
#endif
#endif // NK_CONSOLE_PROPERTY_H__
#if defined(NK_CONSOLE_IMPLEMENTATION) && !defined(NK_CONSOLE_HEADER_ONLY)
#ifndef NK_CONSOLE_PROPERTY_IMPLEMENTATION_ONCE
#define NK_CONSOLE_PROPERTY_IMPLEMENTATION_ONCE
#if defined(__cplusplus)
extern "C" {
#endif
NK_API struct nk_rect nk_console_property_render(nk_console* console) {
nk_console_property_data* data = (nk_console_property_data*)console->data;
if (data == NULL) {
return nk_rect(0, 0, 0, 0);
}
switch (console->type) {
case NK_CONSOLE_PROPERTY_INT:
case NK_CONSOLE_SLIDER_INT:
case NK_CONSOLE_KNOB_INT:
if (data->val_int == NULL) {
return nk_rect(0, 0, 0, 0);
}
break;
case NK_CONSOLE_PROPERTY_FLOAT:
case NK_CONSOLE_SLIDER_FLOAT:
case NK_CONSOLE_KNOB_FLOAT:
if (data->val_float == NULL) {
return nk_rect(0, 0, 0, 0);
}
break;
default:
return nk_rect(0, 0, 0, 0);
}
nk_console_layout_widget(console);
nk_console* top = nk_console_get_top(console);
// Allow changing the value with left/right
if (!console->disabled && nk_console_is_active_widget(console)) {
nk_console_top_data* top_data = (nk_console_top_data*)top->data;
if (!top_data->input_processed) {
if (nk_console_button_pushed(top, NK_GAMEPAD_BUTTON_LEFT)) {
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;
}
break;
default:
// Nothing.
break;
}
nk_console_trigger_event(console, NK_CONSOLE_EVENT_CHANGED);
top_data->input_processed = nk_true;
}
else if (nk_console_button_pushed(top, NK_GAMEPAD_BUTTON_RIGHT)) {
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;
}
break;
default:
// Nothing
break;
}
nk_console_trigger_event(console, NK_CONSOLE_EVENT_CHANGED);
top_data->input_processed = nk_true;
}
}
}
// Style
enum nk_symbol_type left = console->ctx->style.property.sym_left;
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;
console->ctx->style.property.sym_right = NK_SYMBOL_NONE;
}
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
if (nk_strlen(console->label) > 0) {
if (!nk_console_is_active_widget(console)) {
nk_widget_disable_begin(console->ctx);
}
nk_label(console->ctx, console->label, NK_TEXT_LEFT);
if (!nk_console_is_active_widget(console)) {
nk_widget_disable_end(console->ctx);
}
}
struct nk_rect widget_bounds = nk_layout_widget_bounds(console->ctx);
if (console->disabled) {
nk_widget_disable_begin(console->ctx);
}
// Display the widget
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: {
char name[NK_MAX_NUMBER_BUFFER];
int label_len = nk_strlen(console->label);
if (label_len > NK_MAX_NUMBER_BUFFER - 3) {
label_len = NK_MAX_NUMBER_BUFFER - 3;
}
name[0] = '#';
name[1] = '#';
NK_MEMCPY(name + 2, console->label, (nk_size)label_len);
name[label_len + 2] = '\0';
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: {
char name[NK_MAX_NUMBER_BUFFER];
int label_len = nk_strlen(console->label);
if (label_len > NK_MAX_NUMBER_BUFFER - 3) {
label_len = NK_MAX_NUMBER_BUFFER - 3;
}
name[0] = '#';
name[1] = '#';
NK_MEMCPY(name + 2, console->label, (nk_size)label_len);
name[label_len + 2] = '\0';
nk_property_float(console->ctx, name, data->min_float, data->val_float, data->max_float, data->step_float, data->inc_per_pixel);
} 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;
}
// Invoke the onchange callback if needed.
if (original_val_int != ((data->val_int == NULL) ? 0 : *data->val_int) || original_val_float != ((data->val_float == NULL) ? 0 : *data->val_float)) {
nk_console_trigger_event(console, NK_CONSOLE_EVENT_CHANGED);
}
// Style Restoration
if (!nk_console_is_active_widget(console)) {
console->ctx->style.property.sym_left = left;
console->ctx->style.property.sym_right = right;
}
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) {
nk_widget_disable_end(console->ctx);
}
// Allow switching up/down in widgets
if (nk_console_is_active_widget(console)) {
nk_console_check_up_down(console);
nk_console_check_tooltip(console);
}
return widget_bounds;
}
NK_API nk_console* nk_console_property_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_property_data* data = (nk_console_property_data*)NK_CONSOLE_MALLOC(nk_handle_id(0), NULL, sizeof(nk_console_property_data));
nk_zero(data, sizeof(nk_console_property_data));
data->min_int = min;
data->val_int = val;
data->max_int = max;
data->step_int = step;
data->inc_per_pixel = inc_per_pixel;
nk_console* widget = nk_console_label(parent, label);
widget->render = &nk_console_property_render;
widget->type = NK_CONSOLE_PROPERTY_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_property_float(nk_console* parent, const char* label, float min, float* val, float max, float step, float inc_per_pixel) {
nk_console* widget = nk_console_property_int(parent, label, 0, NULL, 0, 0, inc_per_pixel);
nk_console_property_data* data = (nk_console_property_data*)widget->data;
widget->type = NK_CONSOLE_PROPERTY_FLOAT;
data->min_float = min;
data->val_float = val;
data->max_float = max;
data->step_float = step;
if (val != NULL) {
if (*val < min) {
*val = min;
}
else if (*val > max) {
*val = max;
}
}
return widget;
}
NK_API nk_console* nk_console_slider_int(nk_console* parent, const char* label, int min, int* val, int max, int step) {
nk_console* widget = nk_console_property_int(parent, label, min, val, max, step, 0);
widget->type = NK_CONSOLE_SLIDER_INT;
return widget;
}
NK_API nk_console* nk_console_slider_float(nk_console* parent, const char* label, float min, float* val, float max, float step) {
nk_console* widget = nk_console_property_float(parent, label, min, val, max, step, 0);
widget->type = NK_CONSOLE_SLIDER_FLOAT;
return widget;
}
#if defined(__cplusplus)
}
#endif
#endif // NK_CONSOLE_PROPERTY_IMPLEMENTATION_ONCE
#endif // NK_CONSOLE_IMPLEMENTATION