From 63513ec47582932cced9807da43295e4bb898cf3 Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sun, 22 Apr 2018 14:01:56 +0300 Subject: [PATCH] Small refactoring of EnableTextInput --- engine/client/keys.c | 25 ++++++++++++++++++++----- engine/client/vgui/vgui_draw.c | 14 +++++--------- engine/platform/sdl/events.c | 32 +++++--------------------------- engine/platform/sdl/events.h | 2 +- 4 files changed, 31 insertions(+), 42 deletions(-) diff --git a/engine/client/keys.c b/engine/client/keys.c index 3b6677b4..0cdf5a08 100644 --- a/engine/client/keys.c +++ b/engine/client/keys.c @@ -695,16 +695,31 @@ void Key_Event( int key, qboolean down ) } } +/* +================ +Key_EnableTextInput + +================ +*/ void Key_EnableTextInput( qboolean enable, qboolean force ) { + void (*pfnEnableTextInput)( qboolean enable ); + #if XASH_INPUT == INPUT_SDL - SDLash_EnableTextInput( enable, force ); + pfnEnableTextInput = SDLash_EnableTextInput; #elif XASH_INPUT == INPUT_ANDROID - Android_EnableTextInput( enable, force ); -#endif -#if 0 - Joy_EnableTextInput( enable, force ); + pfnEnableTextInput = Android_EnableTextInput; +#else +#error "Here must be a text input for your platform" + return; #endif + if( enable && ( !host.textmode || force ) ) + pfnEnableTextInput( true ); + else if( !enable ) + pfnEnableTextInput( false ); + + if( !force ) + host.textmode = enable; } /* diff --git a/engine/client/vgui/vgui_draw.c b/engine/client/vgui/vgui_draw.c index aef13e4b..b6d40312 100644 --- a/engine/client/vgui/vgui_draw.c +++ b/engine/client/vgui/vgui_draw.c @@ -32,7 +32,6 @@ static enum VGUI_KeyCode s_pVirtualKeyTrans[256]; static enum VGUI_DefaultCursor s_currentCursor; #ifdef XASH_SDL #include -#include "platform/sdl/events.h" static SDL_Cursor* s_pDefaultCursor[20]; #endif static void *s_pVGuiSupport; // vgui_support library @@ -161,9 +160,8 @@ void GAME_EXPORT VGUI_SetVisible( qboolean state ) SDL_ShowCursor( state ); if( !state ) SDL_GetRelativeMouseState( NULL, NULL ); - - SDLash_EnableTextInput( state, true ); #endif + Key_EnableTextInput( state, true ); } int GAME_EXPORT VGUI_UtfProcessChar( int in ) @@ -491,21 +489,19 @@ void VGui_KeyEvent( int key, int down ) if( !vgui.initialized ) return; -#ifdef XASH_SDL if( host.mouse_visible ) - SDLash_EnableTextInput( 1, false ); -#endif + Key_EnableTextInput( true, false ); switch( key ) { case K_MOUSE1: - vgui.Mouse( down?MA_PRESSED:MA_RELEASED, MOUSE_LEFT ); + vgui.Mouse( down ? MA_PRESSED : MA_RELEASED, MOUSE_LEFT ); return; case K_MOUSE2: - vgui.Mouse( down?MA_PRESSED:MA_RELEASED, MOUSE_RIGHT ); + vgui.Mouse( down ? MA_PRESSED : MA_RELEASED, MOUSE_RIGHT ); return; case K_MOUSE3: - vgui.Mouse( down?MA_PRESSED:MA_RELEASED, MOUSE_MIDDLE ); + vgui.Mouse( down ? MA_PRESSED : MA_RELEASED, MOUSE_MIDDLE ); return; case K_MWHEELDOWN: vgui.Mouse( MA_WHEEL, 1 ); diff --git a/engine/platform/sdl/events.c b/engine/platform/sdl/events.c index c8d492ee..63950070 100644 --- a/engine/platform/sdl/events.c +++ b/engine/platform/sdl/events.c @@ -177,17 +177,14 @@ SDLash_InputEvent */ static void SDLash_InputEvent( SDL_TextInputEvent input ) { - int i; - - // Pass characters one by one to Con_CharEvent - for(i = 0; input.text[i]; ++i) + for( char *text = input.text; *text; text++ ) { int ch; if( !Q_stricmp( cl_charset->string, "utf-8" ) ) - ch = (unsigned char)input.text[i]; + ch = (unsigned char)*text; else - ch = Con_UtfProcessCharForce( (unsigned char)input.text[i] ); + ch = Con_UtfProcessCharForce( (unsigned char)*text ); if( !ch ) continue; @@ -202,28 +199,9 @@ SDLash_EnableTextInput ============= */ -void SDLash_EnableTextInput( int enable, qboolean force ) +void SDLash_EnableTextInput( qboolean enable ) { - if( force ) - { - if( enable ) - SDL_StartTextInput(); - else - SDL_StopTextInput(); - } - else if( enable ) - { - if( !host.textmode ) - { - SDL_StartTextInput(); - } - host.textmode = true; - } - else - { - SDL_StopTextInput(); - host.textmode = false; - } + enable ? SDL_StartTextInput() : SDL_StopTextInput(); } /* diff --git a/engine/platform/sdl/events.h b/engine/platform/sdl/events.h index 663baae0..57c5cfd3 100644 --- a/engine/platform/sdl/events.h +++ b/engine/platform/sdl/events.h @@ -20,7 +20,7 @@ GNU General Public License for more details. #ifdef XASH_SDL void SDLash_RunEvents( void ); -void SDLash_EnableTextInput( int enable, qboolean force ); +void SDLash_EnableTextInput( qboolean enable ); int SDLash_JoyInit( int numjoy ); // pass -1 to init every joystick #endif // XASH_SDL