Skip to content

Commit 9f3ec57

Browse files
committed
Introduce minimal API for text keyboard request with text area and cursor support
1 parent fcd64f8 commit 9f3ec57

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/nuklear.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4694,6 +4694,11 @@ struct nk_keyboard {
46944694
struct nk_key keys[NK_KEY_MAX];
46954695
char text[NK_INPUT_MAX];
46964696
int text_len;
4697+
4698+
/*Text keyboard request system*/
4699+
nk_bool text_want; /*When true, text input is actively requested*/
4700+
struct nk_rect text_area; /*Input area (valid only when text_want == true)*/
4701+
struct nk_rect text_cursor; /*Cursor position (valid only when text_want == true)*/
46974702
};
46984703

46994704
struct nk_input {
@@ -4718,6 +4723,10 @@ NK_API nk_bool nk_input_is_mouse_released(const struct nk_input*, enum nk_button
47184723
NK_API nk_bool nk_input_is_key_pressed(const struct nk_input*, enum nk_keys);
47194724
NK_API nk_bool nk_input_is_key_released(const struct nk_input*, enum nk_keys);
47204725
NK_API nk_bool nk_input_is_key_down(const struct nk_input*, enum nk_keys);
4726+
/* This is a utility function that should be called inside a widget
4727+
* to mark text keyboard for opening.
4728+
* Returns true only on the first request each frame */
4729+
NK_API nk_bool nk_input_want_text_keyboard(struct nk_input*);
47214730

47224731
/* ===============================================================
47234732
*

src/nuklear_edit.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
166166
char cursor_follow = 0;
167167
struct nk_rect old_clip;
168168
struct nk_rect clip;
169+
/*Variables for text keyboard(kb) support*/
170+
nk_bool kb_text_want = nk_false;
171+
struct nk_rect kb_text_cursor = nk_rect(0.0f, 0.0f, 0.0f, 0.0f);
169172

170173
NK_ASSERT(state);
171174
NK_ASSERT(out);
@@ -251,13 +254,17 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
251254
cursor_follow = nk_true;
252255
}
253256
}
254-
if (old_mode != edit->mode) {
257+
/* If mode changes and text keys are pressed in the same frame,
258+
* mode change takes priority (may transition to VIEW mode) */
259+
if (old_mode != edit->mode)
255260
in->keyboard.text_len = 0;
256-
}}
261+
else if (edit->mode == NK_TEXT_EDIT_MODE_INSERT || edit->mode == NK_TEXT_EDIT_MODE_REPLACE)
262+
kb_text_want = nk_input_want_text_keyboard(in);
263+
}
257264

258265
/* text input */
259266
edit->filter = filter;
260-
if (in->keyboard.text_len) {
267+
if (kb_text_want && in->keyboard.text_len) {
261268
nk_textedit_text(edit, in->keyboard.text, in->keyboard.text_len);
262269
cursor_follow = nk_true;
263270
in->keyboard.text_len = 0;
@@ -618,6 +625,7 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
618625
cursor.x = area.x + cursor_pos.x - edit->scrollbar.x;
619626
cursor.y = area.y + cursor_pos.y + row_height/2.0f - cursor.h/2.0f;
620627
cursor.y -= edit->scrollbar.y;
628+
kb_text_cursor = cursor;
621629
nk_fill_rect(out, cursor, 0, cursor_color);
622630
} else {
623631
/* draw cursor inside text */
@@ -637,6 +645,7 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
637645
txt.padding = nk_vec2(0,0);
638646
txt.background = cursor_color;;
639647
txt.text = cursor_text_color;
648+
kb_text_cursor = label;
640649
nk_fill_rect(out, label, 0, cursor_color);
641650
nk_widget_text(out, label, cursor_ptr, glyph_len, &txt, NK_TEXT_LEFT, font);
642651
}
@@ -673,6 +682,11 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
673682
background_color, text_color, nk_false);
674683
}
675684
nk_push_scissor(out, old_clip);}
685+
if (kb_text_want) {
686+
NK_ASSERT(in);
687+
in->keyboard.text_cursor = kb_text_cursor;
688+
in->keyboard.text_area = clip;
689+
}
676690
return ret;
677691
}
678692
NK_API void

src/nuklear_input.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ nk_input_begin(struct nk_context *ctx)
1717
for (i = 0; i < NK_BUTTON_MAX; ++i)
1818
in->mouse.buttons[i].clicked = 0;
1919

20+
in->keyboard.text_want = nk_false;
21+
in->keyboard.text_area = nk_rect(0.0f, 0.0f, 0.0f, 0.0f);
22+
in->keyboard.text_cursor = nk_rect(0.0f, 0.0f, 0.0f, 0.0f);
23+
2024
in->keyboard.text_len = 0;
2125
in->mouse.scroll_delta = nk_vec2(0,0);
2226
in->mouse.prev.x = in->mouse.pos.x;
@@ -287,4 +291,12 @@ nk_input_is_key_down(const struct nk_input *i, enum nk_keys key)
287291
if (k->down) return nk_true;
288292
return nk_false;
289293
}
290-
294+
NK_API nk_bool
295+
nk_input_want_text_keyboard(struct nk_input* i)
296+
{
297+
nk_bool ret;
298+
NK_ASSERT(i);
299+
ret = !i->keyboard.text_want;
300+
i->keyboard.text_want = nk_true;
301+
return ret;
302+
}

0 commit comments

Comments
 (0)