Improved soft keyboard support

This commit is contained in:
seibelj 2017-01-03 13:05:17 -05:00
parent bde7c3c4e9
commit 1cf83d258e
3 changed files with 48 additions and 14 deletions

View File

@ -90,7 +90,7 @@ int main(void)
font = nk_allegro5_font_create_from_file("../../extra_font/Roboto-Regular.ttf", 12, 0);
struct nk_context *ctx;
ctx = nk_allegro5_init(font, display, WINDOW_WIDTH, WINDOW_HEIGHT);
ctx = nk_allegro5_init(font, display, WINDOW_WIDTH, WINDOW_HEIGHT, NULL, NULL);
/* Load Fonts: if none of these are loaded a default font will be used */
/* Load Cursor: if you uncomment cursor loading please hide the cursor */

View File

@ -21,15 +21,18 @@
#include <allegro5/allegro_ttf.h>
typedef struct NkAllegro5Font NkAllegro5Font;
NK_API struct nk_context* nk_allegro5_init(NkAllegro5Font *font, ALLEGRO_DISPLAY *dsp, unsigned int width, unsigned int height);
NK_API void nk_allegro5_handle_event(ALLEGRO_EVENT *ev);
NK_API void nk_allegro5_shutdown(void);
NK_API void nk_allegro5_render(void);
NK_API struct nk_context* nk_allegro5_init(NkAllegro5Font *font, ALLEGRO_DISPLAY *dsp,
unsigned int width, unsigned int height,
void (*open_keyboard_func)(void),
void (*close_keyboard_func)(void));
NK_API void nk_allegro5_handle_event(ALLEGRO_EVENT *ev);
NK_API void nk_allegro5_shutdown(void);
NK_API void nk_allegro5_render(void);
/* Fonts. We wrap normal allegro fonts in some nuklear book keeping */
NK_API NkAllegro5Font* nk_allegro5_font_create_from_file(const char *file_name, int font_size, int flags);
NK_API void nk_allegro5_font_del(NkAllegro5Font *font);
NK_API void nk_allegro5_font_set_font(NkAllegro5Font *font);
NK_API NkAllegro5Font* nk_allegro5_font_create_from_file(const char *file_name, int font_size, int flags);
NK_API void nk_allegro5_font_del(NkAllegro5Font *font);
NK_API void nk_allegro5_font_set_font(NkAllegro5Font *font);
#endif
/*
@ -447,7 +450,9 @@ nk_allegro5_clipboard_copy(nk_handle usr, const char *text, int len)
}
NK_API struct nk_context*
nk_allegro5_init(NkAllegro5Font *allegro5font, ALLEGRO_DISPLAY *dsp, unsigned int width, unsigned int height)
nk_allegro5_init(NkAllegro5Font *allegro5font, ALLEGRO_DISPLAY *dsp,
unsigned int width, unsigned int height,
void (*open_keyboard_func)(void), void (*close_keyboard_func)(void))
{
if (!al_init_primitives_addon()) {
fprintf(stdout, "Unable to initialize required allegro5 primitives addon\n");
@ -469,6 +474,10 @@ nk_allegro5_init(NkAllegro5Font *allegro5font, ALLEGRO_DISPLAY *dsp, unsigned in
allegro5.ctx.clip.copy = nk_allegro5_clipboard_copy;
allegro5.ctx.clip.paste = nk_allegro5_clipboard_paste;
allegro5.ctx.clip.userdata = nk_handle_ptr(0);
#ifdef NK_INCLUDE_DYNAMIC_SOFT_KEYBOARD
allegro5.ctx.open_keyboard = open_keyboard_func;
allegro5.ctx.close_keyboard = close_keyboard_func;
#endif
return &allegro5.ctx;
}

View File

@ -127,6 +127,14 @@ OPTIONAL DEFINES:
Can be combined with the style structures.
<!> If used needs to be defined for implementation and header <!>
NK_INCLUDE_DYNAMIC_SOFT_KEYBOARD
Defining this means that when a textedit field is begun editing,
nuklear will look for ctx->open_keyboard() and ctx->close_keyboard()
functions to call. Set these functions during nk_context initialization
in your backend. This is "DYNAMIC" because a PC soft keyboard is always open,
but a mobile device will show and hide the soft keyboard dynamically
as needed.
NK_BUTTON_TRIGGER_ON_RELEASE
Different platforms require button clicks occuring either on buttons being
pressed (up to down) or released (down to up).
@ -477,6 +485,8 @@ typedef void (*nk_plugin_free)(nk_handle, void *old);
typedef int(*nk_plugin_filter)(const struct nk_text_edit*, nk_rune unicode);
typedef void(*nk_plugin_paste)(nk_handle, struct nk_text_edit*);
typedef void(*nk_plugin_copy)(nk_handle, const char*, int len);
typedef void(*nk_plugin_open_keyboard)(void);
typedef void(*nk_plugin_close_keyboard)(void);
struct nk_allocator {
nk_handle userdata;
@ -2932,6 +2942,10 @@ struct nk_context {
#endif
#ifdef NK_INCLUDE_COMMAND_USERDATA
nk_handle userdata;
#endif
#ifdef NK_INCLUDE_DYNAMIC_SOFT_KEYBOARD
nk_plugin_open_keyboard open_keyboard;
nk_plugin_close_keyboard close_keyboard;
#endif
/* text editor objects are quite big because of an internal
* undo/redo stack. Therefore it does not make sense to have one for
@ -14126,7 +14140,8 @@ nk_edit_draw_text(struct nk_command_buffer *out,
}
NK_INTERN nk_flags
nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
nk_do_edit(struct nk_context *ctx,
nk_flags *state, struct nk_command_buffer *out,
struct nk_rect bounds, nk_flags flags, nk_plugin_filter filter,
struct nk_text_edit *edit, const struct nk_style_edit *style,
struct nk_input *in, const struct nk_user_font *font)
@ -14181,13 +14196,23 @@ nk_do_edit(nk_flags *state, struct nk_command_buffer *out,
edit->cursor = edit->string.len;
in = 0;
}
#ifdef NK_INCLUDE_DYNAMIC_SOFT_KEYBOARD
if (ctx->open_keyboard)
ctx->open_keyboard();
#endif
} else if (!edit->active) edit->mode = NK_TEXT_EDIT_MODE_VIEW;
if (flags & NK_EDIT_READ_ONLY)
edit->mode = NK_TEXT_EDIT_MODE_VIEW;
ret = (edit->active) ? NK_EDIT_ACTIVE: NK_EDIT_INACTIVE;
if (prev_state != edit->active)
{
ret |= (edit->active) ? NK_EDIT_ACTIVATED: NK_EDIT_DEACTIVATED;
#ifdef NK_INCLUDE_DYNAMIC_SOFT_KEYBOARD
if (!edit->active)
ctx->close_keyboard();
#endif
}
/* handle user input */
if (edit->active && in)
@ -14753,7 +14778,7 @@ nk_draw_property(struct nk_command_buffer *out, const struct nk_style_property *
}
NK_INTERN void
nk_do_property(nk_flags *ws,
nk_do_property(struct nk_context *ctx, nk_flags *ws,
struct nk_command_buffer *out, struct nk_rect property,
const char *name, struct nk_property_variant *variant,
float inc_per_pixel, char *buffer, int *len,
@ -14893,7 +14918,7 @@ nk_do_property(nk_flags *ws,
text_edit->string.buffer.memory.ptr = dst;
text_edit->string.buffer.size = NK_MAX_NUMBER_BUFFER;
text_edit->mode = NK_TEXT_EDIT_MODE_INSERT;
nk_do_edit(ws, out, edit, NK_EDIT_ALWAYS_INSERT_MODE, filters[filter],
nk_do_edit(ctx, ws, out, edit, NK_EDIT_ALWAYS_INSERT_MODE, filters[filter],
text_edit, &style->edit, (*state == NK_PROPERTY_EDIT) ? in: 0, font);
*length = text_edit->string.len;
@ -19701,7 +19726,7 @@ nk_edit_buffer(struct nk_context *ctx, nk_flags flags,
filter = (!filter) ? nk_filter_default: filter;
prev_state = (unsigned char)edit->active;
in = (flags & NK_EDIT_READ_ONLY) ? 0: in;
ret_flags = nk_do_edit(&ctx->last_widget_state, &win->buffer, bounds, flags,
ret_flags = nk_do_edit(ctx, &ctx->last_widget_state, &win->buffer, bounds, flags,
filter, edit, &style->edit, in, style->font);
if (ctx->last_widget_state & NK_WIDGET_STATE_HOVER)
@ -19828,7 +19853,7 @@ nk_property(struct nk_context *ctx, const char *name, struct nk_property_variant
/* execute property widget */
old_state = *state;
nk_do_property(&ctx->last_widget_state, &win->buffer, bounds, name,
nk_do_property(ctx, &ctx->last_widget_state, &win->buffer, bounds, name,
variant, inc_per_pixel, buffer, len, state, cursor,
&style->property, filter, in, style->font, &ctx->text_edit);