From 32a3010947030c0b85d27712b5bf6fafcc798b32 Mon Sep 17 00:00:00 2001 From: PROP 65 <132837123+PROP65@users.noreply.github.com> Date: Wed, 8 May 2024 23:47:06 +0000 Subject: [PATCH 01/10] Update nuklear.h --- src/nuklear.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/nuklear.h b/src/nuklear.h index 9fb0be45a..557b9c1b5 100644 --- a/src/nuklear.h +++ b/src/nuklear.h @@ -4499,12 +4499,26 @@ struct nk_mouse { struct nk_key { nk_bool down; + float down_time; unsigned int clicked; }; + +#ifndef NK_INPUT_REPEATER_DELAY +#define NK_INPUT_REPEATER_DELAY 0.33f +#endif + +#ifndef NK_INPUT_REPEATER_INTERVAL +#define NK_INPUT_REPEATER_INTERVAL 0.05f +#endif + struct nk_keyboard { struct nk_key keys[NK_KEY_MAX]; char text[NK_INPUT_MAX]; int text_len; + float repeater_delay; + float repeater_interval; + /* copied from nk_context.delta_time_seconds on nk_end_input call */ + float delta; }; struct nk_input { From 6e8b94dd9a11f26f8eb83fa2fc4d30d59c7adc4a Mon Sep 17 00:00:00 2001 From: PROP 65 <132837123+PROP65@users.noreply.github.com> Date: Wed, 8 May 2024 23:49:48 +0000 Subject: [PATCH 02/10] Update nuklear_context.c --- src/nuklear_context.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/nuklear_context.c b/src/nuklear_context.c index 727990a1d..a9e9ff20e 100644 --- a/src/nuklear_context.c +++ b/src/nuklear_context.c @@ -13,6 +13,8 @@ nk_setup(struct nk_context *ctx, const struct nk_user_font *font) if (!ctx) return; nk_zero_struct(*ctx); nk_style_default(ctx); + ctx->input.keyboard.repeater_delay = NK_INPUT_REPEATER_DELAY; + ctx->input.keyboard.repeater_interval = NK_INPUT_REPEATER_INTERVAL; ctx->seq = 1; if (font) ctx->style.font = font; #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT From de68c080678a238ff91a6287ea888b620dbd8b7c Mon Sep 17 00:00:00 2001 From: PROP 65 <132837123+PROP65@users.noreply.github.com> Date: Wed, 8 May 2024 23:49:55 +0000 Subject: [PATCH 03/10] Update nuklear_input.c --- src/nuklear_input.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/nuklear_input.c b/src/nuklear_input.c index 49d878ba6..f27db0e10 100644 --- a/src/nuklear_input.c +++ b/src/nuklear_input.c @@ -40,6 +40,7 @@ nk_input_end(struct nk_context *ctx) in->mouse.ungrab = 0; in->mouse.grab = 0; } + ctx->input.keyboard.delta = ctx->delta_time_seconds; } NK_API void nk_input_motion(struct nk_context *ctx, int x, int y) From 0c52c19aea84a37f73668c8a8001baf4cb80117f Mon Sep 17 00:00:00 2001 From: PROP 65 <132837123+PROP65@users.noreply.github.com> Date: Wed, 8 May 2024 23:56:00 +0000 Subject: [PATCH 04/10] Update nuklear_edit.c --- src/nuklear_edit.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/nuklear_edit.c b/src/nuklear_edit.c index a7b2bfcf1..693453ae0 100644 --- a/src/nuklear_edit.c +++ b/src/nuklear_edit.c @@ -247,9 +247,24 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out, int old_mode = edit->mode; for (i = 0; i < NK_KEY_MAX; ++i) { if (i == NK_KEY_ENTER || i == NK_KEY_TAB) continue; /* special case */ + + if (!nk_input_is_key_down(in, (enum nk_keys)i)){ + in->keyboard.keys[i].down_time = 0.0f; + continue; + } + + in->keyboard.keys[i].down_time += in->keyboard.delta; + if (nk_input_is_key_pressed(in, (enum nk_keys)i)) { + /* single press or begin holding */ + nk_textedit_key(edit, (enum nk_keys)i, shift_mod, font, row_height); + cursor_follow = nk_true; + in->keyboard.keys[i].down_time = 0.0f; + } else if (in->keyboard.keys[i].down_time >= in->keyboard.repeater_delay) { + /* held for at least delay */ nk_textedit_key(edit, (enum nk_keys)i, shift_mod, font, row_height); cursor_follow = nk_true; + in->keyboard.keys[i].down_time -= in->keyboard.repeater_interval; } } if (old_mode != edit->mode) { From cc1e8ac21a3e17f4ac56022767f26e1f48dbb136 Mon Sep 17 00:00:00 2001 From: PROP 65 <132837123+PROP65@users.noreply.github.com> Date: Fri, 30 Aug 2024 07:00:44 +0000 Subject: [PATCH 05/10] Update nuklear.h --- src/nuklear.h | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/nuklear.h b/src/nuklear.h index c9063bfc5..52c8a226a 100644 --- a/src/nuklear.h +++ b/src/nuklear.h @@ -4516,8 +4516,11 @@ struct nk_mouse { struct nk_key { nk_bool down; - float down_time; + nk_bool fire; unsigned int clicked; +#ifdef NK_KEY_REPEAT + float down_time; +#endif }; #ifndef NK_INPUT_REPEATER_DELAY @@ -4532,10 +4535,10 @@ struct nk_keyboard { struct nk_key keys[NK_KEY_MAX]; char text[NK_INPUT_MAX]; int text_len; +#ifdef NK_KEY_REPEAT float repeater_delay; float repeater_interval; - /* copied from nk_context.delta_time_seconds on nk_end_input call */ - float delta; +#endif }; struct nk_input { @@ -4559,6 +4562,7 @@ NK_API nk_bool nk_input_is_mouse_released(const struct nk_input*, enum nk_button NK_API nk_bool nk_input_is_key_pressed(const struct nk_input*, enum nk_keys); NK_API nk_bool nk_input_is_key_released(const struct nk_input*, enum nk_keys); NK_API nk_bool nk_input_is_key_down(const struct nk_input*, enum nk_keys); +NK_API nk_bool nk_input_is_key_fired(const struct nk_input*, enum nk_keys); /* =============================================================== * From cb83edc36f45faa91038ad0ca660f0efa89f8864 Mon Sep 17 00:00:00 2001 From: PROP 65 <132837123+PROP65@users.noreply.github.com> Date: Fri, 30 Aug 2024 07:01:29 +0000 Subject: [PATCH 06/10] Update nuklear_context.c --- src/nuklear_context.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/nuklear_context.c b/src/nuklear_context.c index 3e4f00060..4d4225a3a 100644 --- a/src/nuklear_context.c +++ b/src/nuklear_context.c @@ -13,8 +13,10 @@ nk_setup(struct nk_context *ctx, const struct nk_user_font *font) if (!ctx) return; nk_zero_struct(*ctx); nk_style_default(ctx); +#ifdef NK_KEY_REPEAT ctx->input.keyboard.repeater_delay = NK_INPUT_REPEATER_DELAY; ctx->input.keyboard.repeater_interval = NK_INPUT_REPEATER_INTERVAL; +#endif ctx->seq = 1; if (font) ctx->style.font = font; #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT From 1074094b7d76fe2927a990b3886bcc27851ac800 Mon Sep 17 00:00:00 2001 From: PROP 65 <132837123+PROP65@users.noreply.github.com> Date: Fri, 30 Aug 2024 07:03:29 +0000 Subject: [PATCH 07/10] Update nuklear_input.c --- src/nuklear_input.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/nuklear_input.c b/src/nuklear_input.c index f27db0e10..4dea270e1 100644 --- a/src/nuklear_input.c +++ b/src/nuklear_input.c @@ -40,7 +40,6 @@ nk_input_end(struct nk_context *ctx) in->mouse.ungrab = 0; in->mouse.grab = 0; } - ctx->input.keyboard.delta = ctx->delta_time_seconds; } NK_API void nk_input_motion(struct nk_context *ctx, int x, int y) @@ -58,9 +57,12 @@ NK_API void nk_input_key(struct nk_context *ctx, enum nk_keys key, nk_bool down) { struct nk_input *in; + nk_bool old_down; NK_ASSERT(ctx); if (!ctx) return; in = &ctx->input; + old_down = ctx->input.keyboard.keys[key].down; + in->keyboard.keys[key].fire = nk_false; #ifdef NK_KEYSTATE_BASED_INPUT if (in->keyboard.keys[key].down != down) in->keyboard.keys[key].clicked++; @@ -68,6 +70,26 @@ nk_input_key(struct nk_context *ctx, enum nk_keys key, nk_bool down) in->keyboard.keys[key].clicked++; #endif in->keyboard.keys[key].down = down; + +#ifdef NK_KEY_REPEAT + if (!down) { + /* reset time held counter */ + in->keyboard.keys[key].down_time = 0.0f; + } else if(!old_down) { + /* first frame key down */ + in->keyboard.keys[key].fire = nk_true; + } else { + /* handle key repeat */ + in->keyboard.keys[key].down_time += ctx->delta_time_seconds; + if (in->keyboard.keys[key].down_time >= in->keyboard.repeater_delay) { + in->keyboard.keys[key].fire = nk_true; + in->keyboard.keys[key].down_time -= in->keyboard.repeater_interval; + } + } +#else + if(down && !old_down) + in->keyboard.keys[key].fire = nk_true; +#endif } NK_API void nk_input_button(struct nk_context *ctx, enum nk_buttons id, int x, int y, nk_bool down) @@ -276,10 +298,13 @@ nk_input_is_key_released(const struct nk_input *i, enum nk_keys key) NK_API nk_bool nk_input_is_key_down(const struct nk_input *i, enum nk_keys key) { - const struct nk_key *k; if (!i) return nk_false; - k = &i->keyboard.keys[key]; - if (k->down) return nk_true; - return nk_false; + return i->keyboard.keys[key].down; +} +NK_API nk_bool +nk_input_is_key_fired(const struct nk_input *i, enum nk_keys key) +{ + if (!i) return nk_false; + return i->keyboard.keys[key].fire; } From 52b1eb6619b5c10bed441d416f6f31b03da98ce4 Mon Sep 17 00:00:00 2001 From: PROP 65 <132837123+PROP65@users.noreply.github.com> Date: Fri, 30 Aug 2024 07:04:45 +0000 Subject: [PATCH 08/10] Update nuklear_edit.c --- src/nuklear_edit.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/nuklear_edit.c b/src/nuklear_edit.c index 693453ae0..acaf01882 100644 --- a/src/nuklear_edit.c +++ b/src/nuklear_edit.c @@ -247,24 +247,9 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out, int old_mode = edit->mode; for (i = 0; i < NK_KEY_MAX; ++i) { if (i == NK_KEY_ENTER || i == NK_KEY_TAB) continue; /* special case */ - - if (!nk_input_is_key_down(in, (enum nk_keys)i)){ - in->keyboard.keys[i].down_time = 0.0f; - continue; - } - - in->keyboard.keys[i].down_time += in->keyboard.delta; - - if (nk_input_is_key_pressed(in, (enum nk_keys)i)) { - /* single press or begin holding */ - nk_textedit_key(edit, (enum nk_keys)i, shift_mod, font, row_height); - cursor_follow = nk_true; - in->keyboard.keys[i].down_time = 0.0f; - } else if (in->keyboard.keys[i].down_time >= in->keyboard.repeater_delay) { - /* held for at least delay */ + if (nk_input_is_key_fired(in, (enum nk_keys)i)) { nk_textedit_key(edit, (enum nk_keys)i, shift_mod, font, row_height); cursor_follow = nk_true; - in->keyboard.keys[i].down_time -= in->keyboard.repeater_interval; } } if (old_mode != edit->mode) { From 8788f2c6f6a20887ed0f1c27051ac59e0bb54fe9 Mon Sep 17 00:00:00 2001 From: PROP 65 <132837123+PROP65@users.noreply.github.com> Date: Fri, 30 Aug 2024 07:23:39 +0000 Subject: [PATCH 09/10] rebuild nuklear.h --- nuklear.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/nuklear.h b/nuklear.h index 19553b418..be25a1b66 100644 --- a/nuklear.h +++ b/nuklear.h @@ -4738,12 +4738,29 @@ struct nk_mouse { struct nk_key { nk_bool down; + nk_bool fire; unsigned int clicked; +#ifdef NK_KEY_REPEAT + float down_time; +#endif }; + +#ifndef NK_INPUT_REPEATER_DELAY +#define NK_INPUT_REPEATER_DELAY 0.33f +#endif + +#ifndef NK_INPUT_REPEATER_INTERVAL +#define NK_INPUT_REPEATER_INTERVAL 0.05f +#endif + struct nk_keyboard { struct nk_key keys[NK_KEY_MAX]; char text[NK_INPUT_MAX]; int text_len; +#ifdef NK_KEY_REPEAT + float repeater_delay; + float repeater_interval; +#endif }; struct nk_input { @@ -4767,6 +4784,7 @@ NK_API nk_bool nk_input_is_mouse_released(const struct nk_input*, enum nk_button NK_API nk_bool nk_input_is_key_pressed(const struct nk_input*, enum nk_keys); NK_API nk_bool nk_input_is_key_released(const struct nk_input*, enum nk_keys); NK_API nk_bool nk_input_is_key_down(const struct nk_input*, enum nk_keys); +NK_API nk_bool nk_input_is_key_fired(const struct nk_input*, enum nk_keys); /* =============================================================== * @@ -18072,9 +18090,12 @@ NK_API void nk_input_key(struct nk_context *ctx, enum nk_keys key, nk_bool down) { struct nk_input *in; + nk_bool old_down; NK_ASSERT(ctx); if (!ctx) return; in = &ctx->input; + old_down = ctx->input.keyboard.keys[key].down; + in->keyboard.keys[key].fire = nk_false; #ifdef NK_KEYSTATE_BASED_INPUT if (in->keyboard.keys[key].down != down) in->keyboard.keys[key].clicked++; @@ -18082,6 +18103,26 @@ nk_input_key(struct nk_context *ctx, enum nk_keys key, nk_bool down) in->keyboard.keys[key].clicked++; #endif in->keyboard.keys[key].down = down; + +#ifdef NK_KEY_REPEAT + if (!down) { + /* reset time held counter */ + in->keyboard.keys[key].down_time = 0.0f; + } else if(!old_down) { + /* first frame key down */ + in->keyboard.keys[key].fire = nk_true; + } else { + /* handle key repeat */ + in->keyboard.keys[key].down_time += ctx->delta_time_seconds; + if (in->keyboard.keys[key].down_time >= in->keyboard.repeater_delay) { + in->keyboard.keys[key].fire = nk_true; + in->keyboard.keys[key].down_time -= in->keyboard.repeater_interval; + } + } +#else + if(down && !old_down) + in->keyboard.keys[key].fire = nk_true; +#endif } NK_API void nk_input_button(struct nk_context *ctx, enum nk_buttons id, int x, int y, nk_bool down) @@ -18290,11 +18331,14 @@ nk_input_is_key_released(const struct nk_input *i, enum nk_keys key) NK_API nk_bool nk_input_is_key_down(const struct nk_input *i, enum nk_keys key) { - const struct nk_key *k; if (!i) return nk_false; - k = &i->keyboard.keys[key]; - if (k->down) return nk_true; - return nk_false; + return i->keyboard.keys[key].down; +} +NK_API nk_bool +nk_input_is_key_fired(const struct nk_input *i, enum nk_keys key) +{ + if (!i) return nk_false; + return i->keyboard.keys[key].fire; } @@ -19178,6 +19222,10 @@ nk_setup(struct nk_context *ctx, const struct nk_user_font *font) if (!ctx) return; nk_zero_struct(*ctx); nk_style_default(ctx); +#ifdef NK_KEY_REPEAT + ctx->input.keyboard.repeater_delay = NK_INPUT_REPEATER_DELAY; + ctx->input.keyboard.repeater_interval = NK_INPUT_REPEATER_INTERVAL; +#endif ctx->seq = 1; if (font) ctx->style.font = font; #ifdef NK_INCLUDE_VERTEX_BUFFER_OUTPUT @@ -27889,7 +27937,7 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out, int old_mode = edit->mode; for (i = 0; i < NK_KEY_MAX; ++i) { if (i == NK_KEY_ENTER || i == NK_KEY_TAB) continue; /* special case */ - if (nk_input_is_key_pressed(in, (enum nk_keys)i)) { + if (nk_input_is_key_fired(in, (enum nk_keys)i)) { nk_textedit_key(edit, (enum nk_keys)i, shift_mod, font, row_height); cursor_follow = nk_true; } From 47b6a900ceb83690e94fd25f4044eb2b87be3d71 Mon Sep 17 00:00:00 2001 From: PROP 65 <132837123+PROP65@users.noreply.github.com> Date: Sat, 31 Aug 2024 20:32:37 +0000 Subject: [PATCH 10/10] Rename variable to improve readability --- src/nuklear_input.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/nuklear_input.c b/src/nuklear_input.c index 4dea270e1..05204021d 100644 --- a/src/nuklear_input.c +++ b/src/nuklear_input.c @@ -57,11 +57,11 @@ NK_API void nk_input_key(struct nk_context *ctx, enum nk_keys key, nk_bool down) { struct nk_input *in; - nk_bool old_down; + nk_bool prev_frame_down; NK_ASSERT(ctx); if (!ctx) return; in = &ctx->input; - old_down = ctx->input.keyboard.keys[key].down; + prev_frame_down = ctx->input.keyboard.keys[key].down; in->keyboard.keys[key].fire = nk_false; #ifdef NK_KEYSTATE_BASED_INPUT if (in->keyboard.keys[key].down != down) @@ -75,7 +75,7 @@ nk_input_key(struct nk_context *ctx, enum nk_keys key, nk_bool down) if (!down) { /* reset time held counter */ in->keyboard.keys[key].down_time = 0.0f; - } else if(!old_down) { + } else if(!prev_frame_down) { /* first frame key down */ in->keyboard.keys[key].fire = nk_true; } else { @@ -87,7 +87,7 @@ nk_input_key(struct nk_context *ctx, enum nk_keys key, nk_bool down) } } #else - if(down && !old_down) + if(down && !prev_frame_down) in->keyboard.keys[key].fire = nk_true; #endif }