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

Fix nk_property not focusing when clicking mouse when the clicked property box is before the currently focused property box in the current window #688

Open
wants to merge 5 commits 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
84 changes: 55 additions & 29 deletions nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -3834,11 +3834,11 @@ NK_API struct nk_vec2 nk_rect_size(struct nk_rect);
NK_API int nk_strlen(const char *str);
NK_API int nk_stricmp(const char *s1, const char *s2);
NK_API int nk_stricmpn(const char *s1, const char *s2, int n);
NK_API int nk_strtoi(const char *str, const char **endptr);
NK_API float nk_strtof(const char *str, const char **endptr);
NK_API int nk_strtoi(const char *str, char **endptr);
NK_API float nk_strtof(const char *str, char **endptr);
#ifndef NK_STRTOD
#define NK_STRTOD nk_strtod
NK_API double nk_strtod(const char *str, const char **endptr);
NK_API double nk_strtod(const char *str, char **endptr);
#endif
NK_API int nk_strfilter(const char *text, const char *regexp);
NK_API int nk_strmatch_fuzzy_string(char const *str, char const *pattern, int *out_score);
Expand Down Expand Up @@ -5593,6 +5593,10 @@ struct nk_property_state {
unsigned int seq;
unsigned int old;
int state;
int prev_state;
nk_hash prev_name;
char prev_buffer[NK_MAX_NUMBER_BUFFER];
int prev_length;
};

struct nk_window {
Expand Down Expand Up @@ -6753,7 +6757,7 @@ nk_strlen(const char *str)
return siz;
}
NK_API int
nk_strtoi(const char *str, const char **endptr)
nk_strtoi(const char *str, char **endptr)
{
int neg = 1;
const char *p = str;
Expand All @@ -6773,11 +6777,11 @@ nk_strtoi(const char *str, const char **endptr)
p++;
}
if (endptr)
*endptr = p;
*endptr = (char *)p;
return neg*value;
}
NK_API double
nk_strtod(const char *str, const char **endptr)
nk_strtod(const char *str, char **endptr)
{
double m;
double neg = 1.0;
Expand Down Expand Up @@ -6830,11 +6834,11 @@ nk_strtod(const char *str, const char **endptr)
}
number = value * neg;
if (endptr)
*endptr = p;
*endptr = (char *)p;
return number;
}
NK_API float
nk_strtof(const char *str, const char **endptr)
nk_strtof(const char *str, char **endptr)
{
float float_value;
double double_value;
Expand Down Expand Up @@ -28584,6 +28588,28 @@ nk_draw_property(struct nk_command_buffer *out, const struct nk_style_property *
nk_widget_text(out, *label, name, len, &text, NK_TEXT_CENTERED, font);
}
}
NK_INTERN void
nk_property_save(struct nk_property_variant *variant, char *buffer, int len)
{
buffer[len] = '\0';
switch (variant->kind) {
default: break;
case NK_PROPERTY_INT:
variant->value.i = nk_strtoi(buffer, 0);
variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
break;
case NK_PROPERTY_FLOAT:
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
variant->value.f = nk_strtof(buffer, 0);
variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
break;
case NK_PROPERTY_DOUBLE:
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
variant->value.d = nk_strtod(buffer, 0);
variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
break;
}
}
NK_LIB void
nk_do_property(nk_flags *ws,
struct nk_command_buffer *out, struct nk_rect property,
Expand Down Expand Up @@ -28707,7 +28733,7 @@ nk_do_property(nk_flags *ws,
variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d + variant->step.d, variant->max_value.d); break;
}
}
if (old != NK_PROPERTY_EDIT && (*state == NK_PROPERTY_EDIT)) {
if (!old && (*state == NK_PROPERTY_EDIT)) {
/* property has been activated so setup buffer */
NK_MEMCPY(buffer, dst, (nk_size)*length);
*cursor = nk_utf_len(buffer, *length);
Expand Down Expand Up @@ -28742,24 +28768,7 @@ nk_do_property(nk_flags *ws,
if (active && !text_edit->active) {
/* property is now not active so convert edit text to value*/
*state = NK_PROPERTY_DEFAULT;
buffer[*len] = '\0';
switch (variant->kind) {
default: break;
case NK_PROPERTY_INT:
variant->value.i = nk_strtoi(buffer, 0);
variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
break;
case NK_PROPERTY_FLOAT:
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
variant->value.f = nk_strtof(buffer, 0);
variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
break;
case NK_PROPERTY_DOUBLE:
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
variant->value.d = nk_strtod(buffer, 0);
variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
break;
}
nk_property_save(variant, buffer, *len);
}
}
NK_LIB struct nk_property_variant
Expand Down Expand Up @@ -28807,6 +28816,7 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant

struct nk_rect bounds;
enum nk_widget_layout_states s;
nk_bool hot;

int *state = 0;
nk_hash hash = 0;
Expand All @@ -28816,6 +28826,7 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
int *select_begin = 0;
int *select_end = 0;
int old_state;
int prev_state;

char dummy_buffer[NK_MAX_NUMBER_BUFFER];
int dummy_state = NK_PROPERTY_DEFAULT;
Expand All @@ -28842,8 +28853,15 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
name++; /* special number hash */
} else hash = nk_murmur_hash(name, (int)nk_strlen(name), 42);

/* check if property is previously hot */
if (win->property.prev_state == NK_PROPERTY_EDIT && hash == win->property.prev_name) {
nk_property_save(variant, win->property.prev_buffer, win->property.prev_length);
win->property.prev_state = NK_PROPERTY_DEFAULT;
}

/* check if property is currently hot item */
if (win->property.active && hash == win->property.name) {
hot = win->property.active && hash == win->property.name;
if (hot) {
buffer = win->property.buffer;
len = &win->property.length;
cursor = &win->property.cursor;
Expand All @@ -28861,6 +28879,7 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant

/* execute property widget */
old_state = *state;
prev_state = win->property.state;
ctx->text_edit.clip = ctx->clip;
in = ((s == NK_WIDGET_ROM && !win->property.active) ||
layout->flags & NK_WINDOW_ROM || s == NK_WIDGET_DISABLED) ? 0 : &ctx->input;
Expand All @@ -28869,7 +28888,14 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
select_end, &style->property, filter, in, style->font, &ctx->text_edit,
ctx->button_behavior);

if (in && *state != NK_PROPERTY_DEFAULT && !win->property.active) {
if (in && *state != NK_PROPERTY_DEFAULT && !hot) {
/* another property was active */
if (win->property.active /* && hash != win->property.name */) {
win->property.prev_state = prev_state;
win->property.prev_name = win->property.name;
win->property.prev_length = win->property.length;
NK_MEMCPY(win->property.prev_buffer, win->property.buffer, win->property.length);
}
/* current property is now hot */
win->property.active = 1;
NK_MEMCPY(win->property.buffer, buffer, (nk_size)*len);
Expand Down
4 changes: 4 additions & 0 deletions src/nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -5371,6 +5371,10 @@ struct nk_property_state {
unsigned int seq;
unsigned int old;
int state;
int prev_state;
nk_hash prev_name;
char prev_buffer[NK_MAX_NUMBER_BUFFER];
int prev_length;
};

struct nk_window {
Expand Down
64 changes: 43 additions & 21 deletions src/nuklear_property.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ nk_draw_property(struct nk_command_buffer *out, const struct nk_style_property *
nk_widget_text(out, *label, name, len, &text, NK_TEXT_CENTERED, font);
}
}
NK_INTERN void
nk_property_save(struct nk_property_variant *variant, char *buffer, int len)
{
buffer[len] = '\0';
switch (variant->kind) {
default: break;
case NK_PROPERTY_INT:
variant->value.i = nk_strtoi(buffer, 0);
variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
break;
case NK_PROPERTY_FLOAT:
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
variant->value.f = nk_strtof(buffer, 0);
variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
break;
case NK_PROPERTY_DOUBLE:
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
variant->value.d = nk_strtod(buffer, 0);
variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
break;
}
}
NK_LIB void
nk_do_property(nk_flags *ws,
struct nk_command_buffer *out, struct nk_rect property,
Expand Down Expand Up @@ -233,7 +255,7 @@ nk_do_property(nk_flags *ws,
variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d + variant->step.d, variant->max_value.d); break;
}
}
if (old != NK_PROPERTY_EDIT && (*state == NK_PROPERTY_EDIT)) {
if (!old && (*state == NK_PROPERTY_EDIT)) {
/* property has been activated so setup buffer */
NK_MEMCPY(buffer, dst, (nk_size)*length);
*cursor = nk_utf_len(buffer, *length);
Expand Down Expand Up @@ -268,24 +290,7 @@ nk_do_property(nk_flags *ws,
if (active && !text_edit->active) {
/* property is now not active so convert edit text to value*/
*state = NK_PROPERTY_DEFAULT;
buffer[*len] = '\0';
switch (variant->kind) {
default: break;
case NK_PROPERTY_INT:
variant->value.i = nk_strtoi(buffer, 0);
variant->value.i = NK_CLAMP(variant->min_value.i, variant->value.i, variant->max_value.i);
break;
case NK_PROPERTY_FLOAT:
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
variant->value.f = nk_strtof(buffer, 0);
variant->value.f = NK_CLAMP(variant->min_value.f, variant->value.f, variant->max_value.f);
break;
case NK_PROPERTY_DOUBLE:
nk_string_float_limit(buffer, NK_MAX_FLOAT_PRECISION);
variant->value.d = nk_strtod(buffer, 0);
variant->value.d = NK_CLAMP(variant->min_value.d, variant->value.d, variant->max_value.d);
break;
}
nk_property_save(variant, buffer, *len);
}
}
NK_LIB struct nk_property_variant
Expand Down Expand Up @@ -333,6 +338,7 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant

struct nk_rect bounds;
enum nk_widget_layout_states s;
nk_bool hot;

int *state = 0;
nk_hash hash = 0;
Expand All @@ -342,6 +348,7 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
int *select_begin = 0;
int *select_end = 0;
int old_state;
int prev_state;

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

/* check if property is previously hot */
if (win->property.prev_state == NK_PROPERTY_EDIT && hash == win->property.prev_name) {
nk_property_save(variant, win->property.prev_buffer, win->property.prev_length);
win->property.prev_state = NK_PROPERTY_DEFAULT;
}

/* check if property is currently hot item */
if (win->property.active && hash == win->property.name) {
hot = win->property.active && hash == win->property.name;
if (hot) {
buffer = win->property.buffer;
len = &win->property.length;
cursor = &win->property.cursor;
Expand All @@ -387,6 +401,7 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant

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

if (in && *state != NK_PROPERTY_DEFAULT && !win->property.active) {
if (in && *state != NK_PROPERTY_DEFAULT && !hot) {
/* another property was active */
if (win->property.active /* && hash != win->property.name */) {
win->property.prev_state = prev_state;
win->property.prev_name = win->property.name;
win->property.prev_length = win->property.length;
NK_MEMCPY(win->property.prev_buffer, win->property.buffer, win->property.length);
}
/* current property is now hot */
win->property.active = 1;
NK_MEMCPY(win->property.buffer, buffer, (nk_size)*len);
Expand Down
4 changes: 2 additions & 2 deletions src/nuklear_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ nk_strtoi(const char *str, char **endptr)
p++;
}
if (endptr)
*endptr = p;
*endptr = (char *)p;
return neg*value;
}
NK_API double
Expand Down Expand Up @@ -218,7 +218,7 @@ nk_strtod(const char *str, char **endptr)
}
number = value * neg;
if (endptr)
*endptr = p;
*endptr = (char *)p;
return number;
}
NK_API float
Expand Down