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
58 changes: 53 additions & 5 deletions nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

/* ===============================================================
*
Expand Down Expand Up @@ -18072,16 +18090,39 @@ 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++;
#else
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)
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 18 additions & 0 deletions src/nuklear.h
Original file line number Diff line number Diff line change
Expand Up @@ -4516,12 +4516,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 {
Expand All @@ -4545,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);

/* ===============================================================
*
Expand Down
4 changes: 4 additions & 0 deletions src/nuklear_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +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
Expand Down
2 changes: 1 addition & 1 deletion src/nuklear_edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,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;
}
Expand Down
34 changes: 30 additions & 4 deletions src/nuklear_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,39 @@ NK_API void
nk_input_key(struct nk_context *ctx, enum nk_keys key, nk_bool down)
{
struct nk_input *in;
nk_bool prev_frame_down;
NK_ASSERT(ctx);
if (!ctx) return;
in = &ctx->input;
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)
in->keyboard.keys[key].clicked++;
#else
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(!prev_frame_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 && !prev_frame_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)
Expand Down Expand Up @@ -275,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;
}