Skip to content

Commit 3bcdec2

Browse files
authored
Merge pull request #898 from rswinkle/property_focus_previous
Fix previous property focus issues
2 parents 994f788 + 3010177 commit 3bcdec2

4 files changed

Lines changed: 96 additions & 44 deletions

File tree

nuklear.h

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5773,6 +5773,10 @@ struct nk_property_state {
57735773
unsigned int seq;
57745774
unsigned int old;
57755775
int state;
5776+
int prev_state;
5777+
nk_hash prev_name;
5778+
char prev_buffer[NK_MAX_NUMBER_BUFFER];
5779+
int prev_length;
57765780
};
57775781

57785782
struct nk_window {
@@ -7033,7 +7037,7 @@ nk_strtod(const char *str, char **endptr)
70337037
}
70347038
number = value * neg;
70357039
if (endptr)
7036-
*endptr = p;
7040+
*endptr = (char *)p;
70377041
return number;
70387042
}
70397043
#endif
@@ -28809,6 +28813,28 @@ nk_draw_property(struct nk_command_buffer *out, const struct nk_style_property *
2880928813
nk_widget_text(out, *label, name, len, &text, NK_TEXT_CENTERED, font);
2881028814
}
2881128815
}
28816+
NK_INTERN void
28817+
nk_property_save(struct nk_property_variant *variant, char *buffer, int len)
28818+
{
28819+
buffer[len] = '\0';
28820+
switch (variant->kind) {
28821+
default: break;
28822+
case NK_PROPERTY_INT:
28823+
variant->value.i = nk_strtoi(buffer, 0);
28824+
variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
28825+
break;
28826+
case NK_PROPERTY_FLOAT:
28827+
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
28828+
variant->value.f = nk_strtof(buffer, 0);
28829+
variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
28830+
break;
28831+
case NK_PROPERTY_DOUBLE:
28832+
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
28833+
variant->value.d = NK_STRTOD(buffer, 0);
28834+
variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
28835+
break;
28836+
}
28837+
}
2881228838
NK_LIB void
2881328839
nk_do_property(nk_flags *ws,
2881428840
struct nk_command_buffer *out, struct nk_rect property,
@@ -28932,7 +28958,7 @@ nk_do_property(nk_flags *ws,
2893228958
variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d + variant->step.d, variant->max_value.d); break;
2893328959
}
2893428960
}
28935-
if (old != NK_PROPERTY_EDIT && (*state == NK_PROPERTY_EDIT)) {
28961+
if (!old && (*state == NK_PROPERTY_EDIT)) {
2893628962
/* property has been activated so setup buffer */
2893728963
NK_MEMCPY(buffer, dst, (nk_size)*length);
2893828964
*cursor = nk_utf_len(buffer, *length);
@@ -28967,24 +28993,7 @@ nk_do_property(nk_flags *ws,
2896728993
if (active && !text_edit->active) {
2896828994
/* property is now not active so convert edit text to value*/
2896928995
*state = NK_PROPERTY_DEFAULT;
28970-
buffer[*len] = '\0';
28971-
switch (variant->kind) {
28972-
default: break;
28973-
case NK_PROPERTY_INT:
28974-
variant->value.i = nk_strtoi(buffer, 0);
28975-
variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
28976-
break;
28977-
case NK_PROPERTY_FLOAT:
28978-
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
28979-
variant->value.f = nk_strtof(buffer, 0);
28980-
variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
28981-
break;
28982-
case NK_PROPERTY_DOUBLE:
28983-
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
28984-
variant->value.d = NK_STRTOD(buffer, 0);
28985-
variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
28986-
break;
28987-
}
28996+
nk_property_save(variant, buffer, *len);
2898828997
}
2898928998
}
2899028999
NK_LIB struct nk_property_variant
@@ -29032,6 +29041,7 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
2903229041

2903329042
struct nk_rect bounds;
2903429043
enum nk_widget_layout_states s;
29044+
nk_bool hot;
2903529045

2903629046
int *state = 0;
2903729047
nk_hash hash = 0;
@@ -29041,6 +29051,7 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
2904129051
int *select_begin = 0;
2904229052
int *select_end = 0;
2904329053
int old_state;
29054+
int prev_state;
2904429055

2904529056
char dummy_buffer[NK_MAX_NUMBER_BUFFER];
2904629057
int dummy_state = NK_PROPERTY_DEFAULT;
@@ -29067,8 +29078,15 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
2906729078
name++; /* special number hash */
2906829079
} else hash = nk_murmur_hash(name, (int)nk_strlen(name), 42);
2906929080

29081+
/* check if property is previously hot */
29082+
if (win->property.prev_state == NK_PROPERTY_EDIT && hash == win->property.prev_name) {
29083+
nk_property_save(variant, win->property.prev_buffer, win->property.prev_length);
29084+
win->property.prev_state = NK_PROPERTY_DEFAULT;
29085+
}
29086+
2907029087
/* check if property is currently hot item */
29071-
if (win->property.active && hash == win->property.name) {
29088+
hot = win->property.active && hash == win->property.name;
29089+
if (hot) {
2907229090
buffer = win->property.buffer;
2907329091
len = &win->property.length;
2907429092
cursor = &win->property.cursor;
@@ -29086,6 +29104,7 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
2908629104

2908729105
/* execute property widget */
2908829106
old_state = *state;
29107+
prev_state = win->property.state;
2908929108
ctx->text_edit.clip = ctx->clip;
2909029109
in = ((s == NK_WIDGET_ROM && !win->property.active) ||
2909129110
layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED) ? 0 : &ctx->input;
@@ -29094,7 +29113,14 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
2909429113
select_end, &style->property, filter, in, style->font, &ctx->text_edit,
2909529114
ctx->button_behavior);
2909629115

29097-
if (in && *state != NK_PROPERTY_DEFAULT && !win->property.active) {
29116+
if (in && *state != NK_PROPERTY_DEFAULT && !hot) {
29117+
/* another property was active */
29118+
if (win->property.active /* && hash != win->property.name */) {
29119+
win->property.prev_state = prev_state;
29120+
win->property.prev_name = win->property.name;
29121+
win->property.prev_length = win->property.length;
29122+
NK_MEMCPY(win->property.prev_buffer, win->property.buffer, win->property.length);
29123+
}
2909829124
/* current property is now hot */
2909929125
win->property.active = 1;
2910029126
NK_MEMCPY(win->property.buffer, buffer, (nk_size)*len);

src/nuklear.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5550,6 +5550,10 @@ struct nk_property_state {
55505550
unsigned int seq;
55515551
unsigned int old;
55525552
int state;
5553+
int prev_state;
5554+
nk_hash prev_name;
5555+
char prev_buffer[NK_MAX_NUMBER_BUFFER];
5556+
int prev_length;
55535557
};
55545558

55555559
struct nk_window {

src/nuklear_property.c

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,28 @@ nk_draw_property(struct nk_command_buffer *out, const struct nk_style_property *
110110
nk_widget_text(out, *label, name, len, &text, NK_TEXT_CENTERED, font);
111111
}
112112
}
113+
NK_INTERN void
114+
nk_property_save(struct nk_property_variant *variant, char *buffer, int len)
115+
{
116+
buffer[len] = '\0';
117+
switch (variant->kind) {
118+
default: break;
119+
case NK_PROPERTY_INT:
120+
variant->value.i = nk_strtoi(buffer, 0);
121+
variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
122+
break;
123+
case NK_PROPERTY_FLOAT:
124+
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
125+
variant->value.f = nk_strtof(buffer, 0);
126+
variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
127+
break;
128+
case NK_PROPERTY_DOUBLE:
129+
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
130+
variant->value.d = NK_STRTOD(buffer, 0);
131+
variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
132+
break;
133+
}
134+
}
113135
NK_LIB void
114136
nk_do_property(nk_flags *ws,
115137
struct nk_command_buffer *out, struct nk_rect property,
@@ -233,7 +255,7 @@ nk_do_property(nk_flags *ws,
233255
variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d + variant->step.d, variant->max_value.d); break;
234256
}
235257
}
236-
if (old != NK_PROPERTY_EDIT && (*state == NK_PROPERTY_EDIT)) {
258+
if (!old && (*state == NK_PROPERTY_EDIT)) {
237259
/* property has been activated so setup buffer */
238260
NK_MEMCPY(buffer, dst, (nk_size)*length);
239261
*cursor = nk_utf_len(buffer, *length);
@@ -268,24 +290,7 @@ nk_do_property(nk_flags *ws,
268290
if (active && !text_edit->active) {
269291
/* property is now not active so convert edit text to value*/
270292
*state = NK_PROPERTY_DEFAULT;
271-
buffer[*len] = '\0';
272-
switch (variant->kind) {
273-
default: break;
274-
case NK_PROPERTY_INT:
275-
variant->value.i = nk_strtoi(buffer, 0);
276-
variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
277-
break;
278-
case NK_PROPERTY_FLOAT:
279-
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
280-
variant->value.f = nk_strtof(buffer, 0);
281-
variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
282-
break;
283-
case NK_PROPERTY_DOUBLE:
284-
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
285-
variant->value.d = NK_STRTOD(buffer, 0);
286-
variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
287-
break;
288-
}
293+
nk_property_save(variant, buffer, *len);
289294
}
290295
}
291296
NK_LIB struct nk_property_variant
@@ -333,6 +338,7 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
333338

334339
struct nk_rect bounds;
335340
enum nk_widget_layout_states s;
341+
nk_bool hot;
336342

337343
int *state = 0;
338344
nk_hash hash = 0;
@@ -342,6 +348,7 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
342348
int *select_begin = 0;
343349
int *select_end = 0;
344350
int old_state;
351+
int prev_state;
345352

346353
char dummy_buffer[NK_MAX_NUMBER_BUFFER];
347354
int dummy_state = NK_PROPERTY_DEFAULT;
@@ -368,8 +375,15 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
368375
name++; /* special number hash */
369376
} else hash = nk_murmur_hash(name, (int)nk_strlen(name), 42);
370377

378+
/* check if property is previously hot */
379+
if (win->property.prev_state == NK_PROPERTY_EDIT && hash == win->property.prev_name) {
380+
nk_property_save(variant, win->property.prev_buffer, win->property.prev_length);
381+
win->property.prev_state = NK_PROPERTY_DEFAULT;
382+
}
383+
371384
/* check if property is currently hot item */
372-
if (win->property.active && hash == win->property.name) {
385+
hot = win->property.active && hash == win->property.name;
386+
if (hot) {
373387
buffer = win->property.buffer;
374388
len = &win->property.length;
375389
cursor = &win->property.cursor;
@@ -387,6 +401,7 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
387401

388402
/* execute property widget */
389403
old_state = *state;
404+
prev_state = win->property.state;
390405
ctx->text_edit.clip = ctx->clip;
391406
in = ((s == NK_WIDGET_ROM && !win->property.active) ||
392407
layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED) ? 0 : &ctx->input;
@@ -395,7 +410,14 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
395410
select_end, &style->property, filter, in, style->font, &ctx->text_edit,
396411
ctx->button_behavior);
397412

398-
if (in && *state != NK_PROPERTY_DEFAULT && !win->property.active) {
413+
if (in && *state != NK_PROPERTY_DEFAULT && !hot) {
414+
/* another property was active */
415+
if (win->property.active /* && hash != win->property.name */) {
416+
win->property.prev_state = prev_state;
417+
win->property.prev_name = win->property.name;
418+
win->property.prev_length = win->property.length;
419+
NK_MEMCPY(win->property.prev_buffer, win->property.buffer, win->property.length);
420+
}
399421
/* current property is now hot */
400422
win->property.active = 1;
401423
NK_MEMCPY(win->property.buffer, buffer, (nk_size)*len);

src/nuklear_util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ nk_strtod(const char *str, char **endptr)
217217
}
218218
number = value * neg;
219219
if (endptr)
220-
*endptr = p;
220+
*endptr = (char *)p;
221221
return number;
222222
}
223223
#endif

0 commit comments

Comments
 (0)