From 4a071b61b0de7c3b1d8e600584c94970b35a4067 Mon Sep 17 00:00:00 2001 From: vurtun Date: Tue, 26 Apr 2016 14:43:43 +0200 Subject: [PATCH] Added text input and fixed some style issues Text manipulation in SDL1.2 is quite a pain to say the least. But since this demo is already quite limited in some aspects I think it is fine to just get things running. That being said if used seriously then text input still needs some work. --- demo/sdl1.2_gfx/main.c | 7 +-- demo/sdl1.2_gfx/nuklear_sdl.c | 97 +++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 48 deletions(-) diff --git a/demo/sdl1.2_gfx/main.c b/demo/sdl1.2_gfx/main.c index 13e8d72..ab35a13 100755 --- a/demo/sdl1.2_gfx/main.c +++ b/demo/sdl1.2_gfx/main.c @@ -26,7 +26,6 @@ main(void) { static SDL_Surface *screen_surface; struct nk_color background; - int win_width, win_height; int running = 1; struct nk_context *ctx; float bg[4]; @@ -43,7 +42,6 @@ main(void) } ctx = nk_sdl_init(screen_surface); - background = nk_rgb(28,48,62); while (running) { @@ -65,6 +63,8 @@ main(void) enum {EASY, HARD}; static int op = EASY; static int property = 20; + static char buffer[64]; + static int len; nk_layout_row_static(ctx, 30, 80, 1); if (nk_button_label(ctx, "button", NK_BUTTON_DEFAULT)) @@ -74,9 +74,10 @@ main(void) if (nk_option_label(ctx, "hard", op == HARD)) op = HARD; nk_layout_row_dynamic(ctx, 25, 1); nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1); + nk_edit_string(ctx, NK_EDIT_SIMPLE, buffer, &len, 64, 0); {struct nk_panel combo; - nk_layout_row_dynamic(ctx, 20, 1); + nk_layout_row_dynamic(ctx, 30, 1); nk_label(ctx, "background:", NK_TEXT_LEFT); nk_layout_row_dynamic(ctx, 25, 1); if (nk_combo_begin_color(ctx, &combo, background, 400)) { diff --git a/demo/sdl1.2_gfx/nuklear_sdl.c b/demo/sdl1.2_gfx/nuklear_sdl.c index 661b50c..f03a599 100755 --- a/demo/sdl1.2_gfx/nuklear_sdl.c +++ b/demo/sdl1.2_gfx/nuklear_sdl.c @@ -175,20 +175,22 @@ static void nk_sdl_draw_text(SDL_Surface *surface, short x, short y, unsigned short w, unsigned short h, const char *text, int len, nk_sdl_Font *font, struct nk_color cbg, struct nk_color cfg) { - int i; - + int i; nk_sdl_fill_rect(surface, x, y, len * font->width, font->height, 0, cbg); for (i = 0; i < len; i++) { - characterRGBA(surface, x, y, text[i], cfg.r, cfg.g, cfg.b, cfg.a); + characterRGBA(surface, x, y, text[i], cfg.r, cfg.g, cfg.b, cfg.a); x += font->width; } } -static void interpolate_color(struct nk_color c1, struct nk_color c2, struct nk_color *result, float fraction) { +static void +interpolate_color(struct nk_color c1, struct nk_color c2, struct nk_color *result, float fraction) +{ float r = c1.r + (c2.r - c1.r) * fraction; float g = c1.g + (c2.g - c1.g) * fraction; float b = c1.b + (c2.b - c1.b) * fraction; float a = c1.a + (c2.a - c1.a) * fraction; + result->r = (nk_byte)NK_CLAMP(0, r, 255); result->g = (nk_byte)NK_CLAMP(0, g, 255); result->b = (nk_byte)NK_CLAMP(0, b, 255); @@ -196,13 +198,13 @@ static void interpolate_color(struct nk_color c1, struct nk_color c2, struct nk_ } static void -nk_sdl_fill_rect_multi_color(SDL_Surface *surface, short x, short y, unsigned short w, unsigned short h, +nk_sdl_fill_rect_multi_color(SDL_Surface *surface, short x, short y, unsigned short w, unsigned short h, struct nk_color left, struct nk_color top, struct nk_color right, struct nk_color bottom) { struct nk_color X1, X2, Y; float fraction_x, fraction_y; int i,j; - + for (j = 0; j < h; j++) { fraction_y = ((float)j) / h; for (i = 0; i < w; i++) { @@ -215,7 +217,6 @@ nk_sdl_fill_rect_multi_color(SDL_Surface *surface, short x, short y, unsigned sh } } - static void nk_sdl_clear(SDL_Surface *surface, struct nk_color col) { @@ -310,7 +311,6 @@ nk_sdl_render(struct nk_color clear) default: break; } } - nk_sdl_blit(sdl.screen_surface); nk_clear(&sdl.ctx); @@ -338,13 +338,12 @@ NK_API struct nk_context* nk_sdl_init(SDL_Surface *screen_surface) { struct nk_user_font font; - sdl_font = (nk_sdl_Font*)calloc(1, sizeof(nk_sdl_Font)); sdl_font->width = 8; /* Default in the SDL_gfx library */ sdl_font->height = 8; /* Default in the SDL_gfx library */ if (!sdl_font) return NULL; - + font.userdata = nk_handle_ptr(sdl_font); font.height = (float)sdl_font->height; font.width = nk_sdl_get_text_width; @@ -369,42 +368,50 @@ nk_sdl_handle_event(SDL_Event *evt) int down = evt->type == SDL_KEYDOWN; SDLMod state = SDL_GetModState(); SDLKey sym = evt->key.keysym.sym; - if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) - nk_input_key(ctx, NK_KEY_SHIFT, down); - else if (sym == SDLK_DELETE) - nk_input_key(ctx, NK_KEY_DEL, down); - else if (sym == SDLK_RETURN) - nk_input_key(ctx, NK_KEY_ENTER, down); - else if (sym == SDLK_TAB) - nk_input_key(ctx, NK_KEY_TAB, down); - else if (sym == SDLK_BACKSPACE) - nk_input_key(ctx, NK_KEY_BACKSPACE, down); - else if (sym == SDLK_HOME) - nk_input_key(ctx, NK_KEY_TEXT_START, down); - else if (sym == SDLK_END) - nk_input_key(ctx, NK_KEY_TEXT_END, down); - else if (sym == SDLK_z) - nk_input_key(ctx, NK_KEY_TEXT_UNDO, down && (state == KMOD_LCTRL)); - else if (sym == SDLK_r) - nk_input_key(ctx, NK_KEY_TEXT_REDO, down && (state == KMOD_LCTRL)); - else if (sym == SDLK_c) - nk_input_key(ctx, NK_KEY_COPY, down && (state == KMOD_LCTRL)); - else if (sym == SDLK_v) - nk_input_key(ctx, NK_KEY_PASTE, down && (state == KMOD_LCTRL)); - else if (sym == SDLK_x) - nk_input_key(ctx, NK_KEY_CUT, down && (state == KMOD_LCTRL)); - else if (sym == SDLK_b) - nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down && (state == KMOD_LCTRL)); - else if (sym == SDLK_e) - nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down && (state == KMOD_LCTRL)); - else if (sym == SDLK_LEFT) { - if (state == KMOD_LCTRL) + + if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) nk_input_key(ctx, NK_KEY_SHIFT, down); + else if (sym == SDLK_DELETE) nk_input_key(ctx, NK_KEY_DEL, down); + else if (sym == SDLK_RETURN) nk_input_key(ctx, NK_KEY_ENTER, down); + else if (sym == SDLK_TAB) nk_input_key(ctx, NK_KEY_TAB, down); + else if (sym == SDLK_LEFT) nk_input_key(ctx, NK_KEY_LEFT, down); + else if (sym == SDLK_RIGHT) nk_input_key(ctx, NK_KEY_RIGHT, down); + else if (sym == SDLK_BACKSPACE) nk_input_key(ctx, NK_KEY_BACKSPACE, down); + else if (sym == SDLK_HOME) nk_input_key(ctx, NK_KEY_TEXT_START, down); + else if (sym == SDLK_END) nk_input_key(ctx, NK_KEY_TEXT_END, down); + else if (sym == SDLK_SPACE && !down) nk_input_char(ctx, ' '); + else { + if (sym == SDLK_c && state == SDLK_LCTRL) + nk_input_key(ctx, NK_KEY_COPY, down); + else if (sym == SDLK_v && state == SDLK_LCTRL) + nk_input_key(ctx, NK_KEY_PASTE, down); + else if (sym == SDLK_x && state == SDLK_LCTRL) + nk_input_key(ctx, NK_KEY_CUT, down); + else if (sym == SDLK_z && state == SDLK_LCTRL) + nk_input_key(ctx, NK_KEY_TEXT_UNDO, down); + else if (sym == SDLK_r && state == SDLK_LCTRL) + nk_input_key(ctx, NK_KEY_TEXT_REDO, down); + else if (sym == SDLK_LEFT && state == SDLK_LCTRL) nk_input_key(ctx, NK_KEY_TEXT_WORD_LEFT, down); - else nk_input_key(ctx, NK_KEY_LEFT, down); - } else if (sym == SDLK_RIGHT) { - if (state == KMOD_LCTRL) + else if (sym == SDLK_RIGHT && state == SDLK_LCTRL) nk_input_key(ctx, NK_KEY_TEXT_WORD_RIGHT, down); - else nk_input_key(ctx, NK_KEY_RIGHT, down); + else if (sym == SDLK_b && state == SDLK_LCTRL) + nk_input_key(ctx, NK_KEY_TEXT_LINE_START, down); + else if (sym == SDLK_e && state == SDLK_LCTRL) + nk_input_key(ctx, NK_KEY_TEXT_LINE_END, down); + else if (!down) { + /* This demo does not provide full unicode support since the default + * sdl1.2 font only allows runes in range 0-255. But this demo + * already is quite limited and not really meant for full blown Apps + * anyway. So I think ASCII support for Debugging Tools should be enough */ + if (sym >= SDLK_0 && sym <= SDLK_9) { + nk_rune rune = '0' + sym - SDLK_0; + nk_input_unicode(ctx, rune); + } else if (sym >= SDLK_a && sym <= SDLK_z) { + nk_rune rune = 'a' + sym - SDLK_a; + rune = ((state == KMOD_LSHIFT) ? (nk_rune)nk_to_upper((int)rune):rune); + nk_input_unicode(ctx, rune); + } + } } } else if (evt->type == SDL_MOUSEBUTTONDOWN || evt->type == SDL_MOUSEBUTTONUP) { /* mouse button */ @@ -425,7 +432,7 @@ nk_sdl_handle_event(SDL_Event *evt) } } -NK_API void +NK_API void nk_sdl_shutdown(void) { free(sdl_font);