From 6c674ccdd458314ced75459649c6acf1489b6056 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Wed, 26 Oct 2016 08:57:46 +0200 Subject: [PATCH] added shifting up video display when activating screen keyboard (Android) --- src/events.c | 19 +++++++------------ src/libgame/gadgets.c | 14 +++++++++++++- src/libgame/sdl.c | 26 ++++++++++++++++++++++++-- src/libgame/system.c | 25 +++++++++++++++++++++++++ src/libgame/system.h | 10 ++++++++++ src/screens.c | 8 ++------ 6 files changed, 81 insertions(+), 21 deletions(-) diff --git a/src/events.c b/src/events.c index 2d1e0659..e197be5c 100644 --- a/src/events.c +++ b/src/events.c @@ -867,21 +867,16 @@ void HandleTextEvent(TextEvent *event) GetKeyModState()); #endif -#if defined(PLATFORM_ANDROID) - if (game_status == GAME_MODE_PSEUDO_TYPENAME) - { - HandleTypeName(0, key); - +#if !defined(HAS_SCREEN_KEYBOARD) + // non-mobile devices: only handle key input with modifier keys pressed here + // (every other key input is handled directly as physical key input event) + if (!checkTextInputKeyModState()) return; - } #endif - // only handle key input with text modifier keys pressed - if (checkTextInputKeyModState()) - { - HandleKey(key, KEY_PRESSED); - HandleKey(key, KEY_RELEASED); - } + // process text input as "classic" (with uppercase etc.) key input event + HandleKey(key, KEY_PRESSED); + HandleKey(key, KEY_RELEASED); } void HandlePauseResumeEvent(PauseResumeEvent *event) diff --git a/src/libgame/gadgets.c b/src/libgame/gadgets.c index ce939a75..736f1465 100644 --- a/src/libgame/gadgets.c +++ b/src/libgame/gadgets.c @@ -1605,6 +1605,8 @@ boolean HandleGadgets(int mx, int my, int button) if (gadget_pressed_inside_select_line) new_gi = NULL; + + StopTextInput(); } gadget_pressed = @@ -1706,6 +1708,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); } else if (gi->type & GD_TYPE_TEXT_AREA && button != 0 && !motion_status) { @@ -1720,6 +1724,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); } else if (gi->type & GD_TYPE_SELECTBOX && gi->selectbox.open && !keep_selectbox_open) @@ -2082,7 +2088,11 @@ boolean HandleGadgetsKeyInput(Key key) gi->type & GD_TYPE_SELECTBOX)) return FALSE; - if (key == KSYM_Return) /* valid for both text input and selectbox */ + if (key == KSYM_Escape) + { + StopTextInput(); + } + else if (key == KSYM_Return) /* valid for both text input and selectbox */ { boolean gadget_changed = ((gi->event_mask & GD_EVENT_TEXT_RETURN) != 0); @@ -2094,6 +2104,8 @@ boolean HandleGadgetsKeyInput(Key key) strcpy(gi->textinput.last_value, gi->textinput.value); else gadget_changed = FALSE; + + StopTextInput(); } else if (gi->type & GD_TYPE_SELECTBOX) { diff --git a/src/libgame/sdl.c b/src/libgame/sdl.c index f805ddfd..ba3b6ac4 100644 --- a/src/libgame/sdl.c +++ b/src/libgame/sdl.c @@ -129,6 +129,28 @@ static void UpdateScreenExt(SDL_Rect *rect, boolean with_frame_delay) SDL_UpdateTexture(sdl_texture, NULL, screen->pixels, screen->pitch); } + 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 (video.screen_rendering_mode == SPECIAL_RENDERING_TARGET || + video.screen_rendering_mode == SPECIAL_RENDERING_DOUBLE) + { + src_rect2 = &src_rect_up; + dst_rect2 = &dst_rect_up; + } + else + { + src_rect1 = &src_rect_up; + dst_rect1 = &dst_rect_up; + } + } +#endif + // clear render target buffer SDL_RenderClear(sdl_renderer); @@ -139,7 +161,7 @@ static void UpdateScreenExt(SDL_Rect *rect, boolean with_frame_delay) // copy backbuffer texture to render target buffer if (video.screen_rendering_mode != SPECIAL_RENDERING_TARGET) - SDL_RenderCopy(sdl_renderer, sdl_texture_stream, NULL, NULL); + SDL_RenderCopy(sdl_renderer, sdl_texture_stream, src_rect1, dst_rect1); if (video.screen_rendering_mode != SPECIAL_RENDERING_BITMAP) FinalizeScreen(DRAW_TO_SCREEN); @@ -149,7 +171,7 @@ static void UpdateScreenExt(SDL_Rect *rect, boolean with_frame_delay) video.screen_rendering_mode == SPECIAL_RENDERING_DOUBLE) { SDL_SetRenderTarget(sdl_renderer, NULL); - SDL_RenderCopy(sdl_renderer, sdl_texture_target, NULL, NULL); + SDL_RenderCopy(sdl_renderer, sdl_texture_target, src_rect2, dst_rect2); } #endif diff --git a/src/libgame/system.c b/src/libgame/system.c index d8c79937..df401b5a 100644 --- a/src/libgame/system.c +++ b/src/libgame/system.c @@ -386,6 +386,8 @@ void InitVideoBuffer(int width, int height, int depth, boolean fullscreen) video.frame_delay = 0; video.frame_delay_value = GAME_FRAME_DELAY; + video.shifted_up = FALSE; + SDLInitVideoBuffer(fullscreen); video.initialized = TRUE; @@ -1511,6 +1513,29 @@ KeyMod GetKeyModStateFromEvents() return HandleKeyModState(KSYM_UNDEFINED, 0); } +void StartTextInput(int x, int y) +{ +#if defined(TARGET_SDL2) + SDL_StartTextInput(); + +#if defined(HAS_SCREEN_KEYBOARD) + if (y > video.height / 2) + video.shifted_up = TRUE; +#endif +#endif +} + +void StopTextInput() +{ +#if defined(TARGET_SDL2) + SDL_StopTextInput(); + +#if defined(HAS_SCREEN_KEYBOARD) + video.shifted_up = FALSE; +#endif +#endif +} + boolean CheckCloseWindowEvent(ClientMessageEvent *event) { if (event->type != EVENT_CLIENTMESSAGE) diff --git a/src/libgame/system.h b/src/libgame/system.h index 48e06ae1..009cbf81 100644 --- a/src/libgame/system.h +++ b/src/libgame/system.h @@ -92,6 +92,12 @@ #define TOUCH_DROP_DISTANCE_DEFAULT 5 +/* values for screen keyboard on mobile devices */ +#if defined(PLATFORM_ANDROID) +#define HAS_SCREEN_KEYBOARD +#endif + + /* default input keys */ #define DEFAULT_KEY_LEFT KSYM_Left #define DEFAULT_KEY_RIGHT KSYM_Right @@ -801,6 +807,8 @@ struct VideoSystemInfo unsigned int frame_delay; unsigned int frame_delay_value; + boolean shifted_up; + boolean initialized; }; @@ -1488,6 +1496,8 @@ Key GetEventKey(KeyEvent *, boolean); KeyMod HandleKeyModState(Key, int); KeyMod GetKeyModState(); KeyMod GetKeyModStateFromEvents(); +void StartTextInput(int, int); +void StopTextInput(); boolean CheckCloseWindowEvent(ClientMessageEvent *); void InitJoysticks(); diff --git a/src/screens.c b/src/screens.c index 23636186..d38878c3 100644 --- a/src/screens.c +++ b/src/screens.c @@ -3585,9 +3585,7 @@ void HandleTypeName(int newxpos, Key key) xpos = newxpos; -#if defined(TARGET_SDL2) - SDL_StartTextInput(); -#endif + StartTextInput(startx, starty); } else if (is_valid_key_char && xpos < MAX_PLAYER_NAME_LEN) { @@ -3634,9 +3632,7 @@ void HandleTypeName(int newxpos, Key key) DrawText(startx, starty, setup.player_name, font_nr); -#if defined(TARGET_SDL2) - SDL_StopTextInput(); -#endif + StopTextInput(); } } -- 2.34.1