From 97d0f78c43d16fbf73ea0438148a4da781c75600 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Thu, 27 Oct 2016 09:59:03 +0200 Subject: [PATCH] improved shifting up video display when activating screen keyboard (Android) --- src/events.c | 5 +++++ src/libgame/gadgets.c | 6 ++++-- src/libgame/sdl.c | 27 +++++++++++++++++++++++---- src/libgame/system.c | 19 ++++++++++++++++--- src/libgame/system.h | 7 ++++++- src/screens.c | 2 +- 6 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/events.c b/src/events.c index e197be5c..5bcdd5fb 100644 --- a/src/events.c +++ b/src/events.c @@ -412,6 +412,11 @@ void HandleButtonEvent(ButtonEvent *event) event->x, event->y); #endif +#if defined(HAS_SCREEN_KEYBOARD) + if (video.shifted_up) + event->y += video.shifted_up_pos; +#endif + motion_status = FALSE; if (event->type == EVENT_BUTTONPRESS) diff --git a/src/libgame/gadgets.c b/src/libgame/gadgets.c index 736f1465..98e46a5a 100644 --- a/src/libgame/gadgets.c +++ b/src/libgame/gadgets.c @@ -1709,7 +1709,8 @@ boolean HandleGadgets(int mx, int my, int button) if (gi->textinput.cursor_position != old_cursor_position) DrawGadget(gi, DG_PRESSED, gi->direct_draw); - StartTextInput(gi->x, gi->y); + if (press_event) + StartTextInput(gi->x, gi->y, gi->width, gi->height); } else if (gi->type & GD_TYPE_TEXT_AREA && button != 0 && !motion_status) { @@ -1725,7 +1726,8 @@ boolean HandleGadgets(int mx, int my, int button) if (gi->textarea.cursor_position != old_cursor_position) DrawGadget(gi, DG_PRESSED, gi->direct_draw); - StartTextInput(gi->x, gi->y); + if (press_event) + StartTextInput(gi->x, gi->y, gi->width, gi->height); } else if (gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open && !keep_selectbox_open) diff --git a/src/libgame/sdl.c b/src/libgame/sdl.c index ba3b6ac4..7a108238 100644 --- a/src/libgame/sdl.c +++ b/src/libgame/sdl.c @@ -131,12 +131,31 @@ static void UpdateScreenExt(SDL_Rect *rect, boolean with_frame_delay) SDL_Rect *src_rect1 = NULL, *dst_rect1 = NULL; SDL_Rect *src_rect2 = NULL, *dst_rect2 = NULL; -#if defined(HAS_SCREEN_KEYBOARD) - SDL_Rect src_rect_up = { 0, video.height / 2, video.width, video.height / 2 }; - SDL_Rect dst_rect_up = { 0, 0, video.width, video.height / 2 }; - if (video.shifted_up) +#if defined(HAS_SCREEN_KEYBOARD) + if (video.shifted_up || video.shifted_up_delay) { + int time_current = SDL_GetTicks(); + int pos = video.shifted_up_pos; + int pos_last = video.shifted_up_pos_last; + + if (!DelayReachedExt(&video.shifted_up_delay, video.shifted_up_delay_value, + time_current)) + { + int delay = time_current - video.shifted_up_delay; + int delay_value = video.shifted_up_delay_value; + + pos = pos_last + (pos - pos_last) * delay / delay_value; + } + else + { + video.shifted_up_pos_last = pos; + video.shifted_up_delay = 0; + } + + SDL_Rect src_rect_up = { 0, pos, video.width, video.height - pos }; + SDL_Rect dst_rect_up = { 0, 0, video.width, video.height - pos }; + if (video.screen_rendering_mode == SPECIAL_RENDERING_TARGET || video.screen_rendering_mode == SPECIAL_RENDERING_DOUBLE) { diff --git a/src/libgame/system.c b/src/libgame/system.c index df401b5a..7d85f617 100644 --- a/src/libgame/system.c +++ b/src/libgame/system.c @@ -387,6 +387,10 @@ void InitVideoBuffer(int width, int height, int depth, boolean fullscreen) video.frame_delay_value = GAME_FRAME_DELAY; video.shifted_up = FALSE; + video.shifted_up_pos = 0; + video.shifted_up_pos_last = 0; + video.shifted_up_delay = 0; + video.shifted_up_delay_value = ONE_SECOND_DELAY / 4; SDLInitVideoBuffer(fullscreen); @@ -1513,14 +1517,18 @@ KeyMod GetKeyModStateFromEvents() return HandleKeyModState(KSYM_UNDEFINED, 0); } -void StartTextInput(int x, int y) +void StartTextInput(int x, int y, int width, int height) { #if defined(TARGET_SDL2) SDL_StartTextInput(); #if defined(HAS_SCREEN_KEYBOARD) - if (y > video.height / 2) + if (y + height > SCREEN_KEYBOARD_POS(video.height)) + { + video.shifted_up_pos = y + height - SCREEN_KEYBOARD_POS(video.height); + video.shifted_up_delay = SDL_GetTicks(); video.shifted_up = TRUE; + } #endif #endif } @@ -1531,7 +1539,12 @@ void StopTextInput() SDL_StopTextInput(); #if defined(HAS_SCREEN_KEYBOARD) - video.shifted_up = FALSE; + if (video.shifted_up) + { + video.shifted_up_pos = 0; + video.shifted_up_delay = SDL_GetTicks(); + video.shifted_up = FALSE; + } #endif #endif } diff --git a/src/libgame/system.h b/src/libgame/system.h index 009cbf81..b4e0df64 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -95,6 +95,7 @@ /* values for screen keyboard on mobile devices */ #if defined(PLATFORM_ANDROID) #define HAS_SCREEN_KEYBOARD +#define SCREEN_KEYBOARD_POS(h) ((h) / 2) #endif @@ -808,6 +809,10 @@ struct VideoSystemInfo unsigned int frame_delay_value; boolean shifted_up; + int shifted_up_pos; + int shifted_up_pos_last; + unsigned int shifted_up_delay; + unsigned int shifted_up_delay_value; boolean initialized; }; @@ -1496,7 +1501,7 @@ Key GetEventKey(KeyEvent *, boolean); KeyMod HandleKeyModState(Key, int); KeyMod GetKeyModState(); KeyMod GetKeyModStateFromEvents(); -void StartTextInput(int, int); +void StartTextInput(int, int, int, int); void StopTextInput(); boolean CheckCloseWindowEvent(ClientMessageEvent *); diff --git a/src/screens.c b/src/screens.c index d38878c3..71f73cdb 100644 --- a/src/screens.c +++ b/src/screens.c @@ -3585,7 +3585,7 @@ void HandleTypeName(int newxpos, Key key) xpos = newxpos; - StartTextInput(startx, starty); + StartTextInput(startx, starty, pos->width, pos->height); } else if (is_valid_key_char && xpos < MAX_PLAYER_NAME_LEN) { -- 2.34.1