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

Key repeat for nk_keys #639

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
14 changes: 14 additions & 0 deletions src/nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -4516,12 +4516,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;
PROP65 marked this conversation as resolved.
Show resolved Hide resolved
};

struct nk_input {
Expand Down
2 changes: 2 additions & 0 deletions src/nuklear_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/nuklear_edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/nuklear_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down