From cfcd58dd78c4f8c625043ac674382856cf81f5b7 Mon Sep 17 00:00:00 2001 From: SNMetamorph <25657591+SNMetamorph@users.noreply.github.com> Date: Wed, 27 Apr 2022 22:32:20 +0400 Subject: [PATCH] engine: client: extended VGUI API interface --- engine/client/vgui/vgui_draw.c | 93 ++++++---------------------------- engine/client/vgui/vgui_draw.h | 1 + engine/cursor_type.h | 32 ++++++------ engine/platform/platform.h | 2 +- engine/platform/sdl/events.c | 1 + engine/platform/sdl/in_sdl.c | 35 ++++++------- engine/vgui_api.h | 35 +++++-------- 7 files changed, 65 insertions(+), 134 deletions(-) diff --git a/engine/client/vgui/vgui_draw.c b/engine/client/vgui/vgui_draw.c index fa775a2d..a7456256 100644 --- a/engine/client/vgui/vgui_draw.c +++ b/engine/client/vgui/vgui_draw.c @@ -21,10 +21,6 @@ GNU General Public License for more details. #include "keydefs.h" #include "ref_common.h" #include "input.h" -#ifdef XASH_SDL -#include -static SDL_Cursor* s_pDefaultCursor[20]; -#endif #include "platform/platform.h" static enum VGUI_KeyCode s_pVirtualKeyTrans[256]; @@ -32,20 +28,6 @@ static enum VGUI_DefaultCursor s_currentCursor; static HINSTANCE s_pVGuiSupport; // vgui_support library static convar_t *vgui_utf8 = NULL; -// Helper functions for vgui backend - -/*void VGUI_HideCursor( void ) -{ - host.mouse_visible = false; - SDL_HideCursor(); -} - -void VGUI_ShowCursor( void ) -{ - host.mouse_visible = true; - SDL_ShowCursor(); -}*/ - void GAME_EXPORT *VGUI_EngineMalloc(size_t size) { return Z_Malloc( size ); @@ -66,63 +48,9 @@ void GAME_EXPORT VGUI_GetMousePos( int *_x, int *_y ) *_x = x / xscale, *_y = y / yscale; } -void VGUI_InitCursors( void ) -{ - // load up all default cursors -#if SDL_VERSION_ATLEAST( 2, 0, 0 ) - s_pDefaultCursor[dc_none] = NULL; - s_pDefaultCursor[dc_arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW); - s_pDefaultCursor[dc_ibeam] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM); - s_pDefaultCursor[dc_hourglass]= SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAIT); - s_pDefaultCursor[dc_crosshair]= SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR); - s_pDefaultCursor[dc_up] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW); - s_pDefaultCursor[dc_sizenwse] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE); - s_pDefaultCursor[dc_sizenesw] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW); - s_pDefaultCursor[dc_sizewe] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE); - s_pDefaultCursor[dc_sizens] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS); - s_pDefaultCursor[dc_sizeall] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL); - s_pDefaultCursor[dc_no] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO); - s_pDefaultCursor[dc_hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND); -#endif -} - void GAME_EXPORT VGUI_CursorSelect( enum VGUI_DefaultCursor cursor ) { - qboolean visible; - - if( cls.key_dest != key_game || cl.paused ) - return; - - switch( cursor ) - { - case dc_user: - case dc_none: - visible = false; - break; - default: - visible = true; - break; - } - - host.mouse_visible = visible; - -#if SDL_VERSION_ATLEAST( 2, 0, 0 ) - /// TODO: platform cursors - - if( CVAR_TO_BOOL( touch_emulate ) ) - return; - - if( host.mouse_visible ) - { - SDL_SetCursor( s_pDefaultCursor[cursor] ); - SDL_ShowCursor( true ); - } - else - { - SDL_ShowCursor( false ); - Key_EnableTextInput( false, true ); - } -#endif + Platform_SetCursorType( cursor ); s_currentCursor = cursor; } @@ -134,7 +62,7 @@ byte GAME_EXPORT VGUI_GetColor( int i, int j) // Define and initialize vgui API int GAME_EXPORT VGUI_UtfProcessChar( int in ) { - if( CVAR_TO_BOOL( vgui_utf8 ) ) + if( CVAR_TO_BOOL( vgui_utf8 )) return Con_UtfProcessCharForce( in ); else return in; @@ -163,6 +91,9 @@ vguiapi_t vgui = NULL, VGUI_GetMousePos, VGUI_UtfProcessChar, + Platform_GetClipboardText, + Platform_SetClipboardText, + Platform_GetKeyModifiers, NULL, NULL, NULL, @@ -170,6 +101,7 @@ vguiapi_t vgui = NULL, NULL, NULL, + NULL }; qboolean VGui_IsActive( void ) @@ -478,8 +410,9 @@ void VGui_KeyEvent( int key, int down ) switch( key ) { case K_MOUSE1: - if( down && host.mouse_visible ) - Key_EnableTextInput( true, false ); + if( down && host.mouse_visible ) { + Key_EnableTextInput(true, false); + } vgui.Mouse( down ? MA_PRESSED : MA_RELEASED, MOUSE_LEFT ); return; case K_MOUSE2: @@ -515,7 +448,7 @@ void VGui_MouseMove( int x, int y ) void VGui_Paint( void ) { - if(vgui.initialized) + if( vgui.initialized ) vgui.Paint(); } @@ -531,3 +464,9 @@ void *GAME_EXPORT VGui_GetPanel( void ) return vgui.GetPanel(); return NULL; } + +void VGui_ReportTextInput( const char *text ) +{ + if ( vgui.initialized ) + vgui.TextInput( text ); +} diff --git a/engine/client/vgui/vgui_draw.h b/engine/client/vgui/vgui_draw.h index bf9c4396..dd5a4ad2 100644 --- a/engine/client/vgui/vgui_draw.h +++ b/engine/client/vgui/vgui_draw.h @@ -33,6 +33,7 @@ void VGui_KeyEvent( int key, int down ); void VGui_MouseMove( int x, int y ); qboolean VGui_IsActive( void ); void *VGui_GetPanel( void ); +void VGui_ReportTextInput( const char *text ); #ifdef __cplusplus } #endif diff --git a/engine/cursor_type.h b/engine/cursor_type.h index 6264eda7..da377083 100644 --- a/engine/cursor_type.h +++ b/engine/cursor_type.h @@ -19,21 +19,21 @@ GNU General Public License for more details. typedef enum { - CursorType_User, - CursorType_None, - CursorType_Arrow, - CursorType_Ibeam, - CursorType_Wait, - CursorType_Crosshair, - CursorType_Up, - CursorType_SizeNwSe, - CursorType_SizeNeSw, - CursorType_SizeWe, - CursorType_SizeNs, - CursorType_SizeAll, - CursorType_No, - CursorType_Hand, - CursorType_Last -} cursor_type_t; + dc_user, + dc_none, + dc_arrow, + dc_ibeam, + dc_hourglass, + dc_crosshair, + dc_up, + dc_sizenwse, + dc_sizenesw, + dc_sizewe, + dc_sizens, + dc_sizeall, + dc_no, + dc_hand, + dc_last +} VGUI_DefaultCursor; #endif diff --git a/engine/platform/platform.h b/engine/platform/platform.h index a353f407..e764421a 100644 --- a/engine/platform/platform.h +++ b/engine/platform/platform.h @@ -82,7 +82,7 @@ void Platform_GetMousePos( int *x, int *y ); void Platform_SetMousePos( int x, int y ); void Platform_PreCreateMove( void ); void Platform_MouseMove( float *x, float *y ); -void Platform_SetCursorType( cursor_type_t type ); +void Platform_SetCursorType( VGUI_DefaultCursor type ); // Clipboard int Platform_GetClipboardText( char *buffer, size_t size ); void Platform_SetClipboardText( const char *buffer ); diff --git a/engine/platform/sdl/events.c b/engine/platform/sdl/events.c index 619279ab..86d5cde0 100644 --- a/engine/platform/sdl/events.c +++ b/engine/platform/sdl/events.c @@ -318,6 +318,7 @@ SDLash_InputEvent static void SDLash_InputEvent( SDL_TextInputEvent input ) { char *text; + VGui_ReportTextInput( input.text ); for( text = input.text; *text; text++ ) { int ch; diff --git a/engine/platform/sdl/in_sdl.c b/engine/platform/sdl/in_sdl.c index 1d1a19fe..04caed32 100644 --- a/engine/platform/sdl/in_sdl.c +++ b/engine/platform/sdl/in_sdl.c @@ -25,9 +25,10 @@ GNU General Public License for more details. #include "vid_common.h" SDL_Joystick *g_joy = NULL; -static SDL_Cursor *g_pDefaultCursor[CursorType_Last]; #if !SDL_VERSION_ATLEAST( 2, 0, 0 ) #define SDL_WarpMouseInWindow( win, x, y ) SDL_WarpMouse( ( x ), ( y ) ) +#else +static SDL_Cursor *g_pDefaultCursor[dc_last]; #endif /* @@ -260,19 +261,19 @@ static void SDLash_InitCursors( void ) { // load up all default cursors #if SDL_VERSION_ATLEAST( 2, 0, 0 ) - g_pDefaultCursor[CursorType_None] = NULL; - g_pDefaultCursor[CursorType_Arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW); - g_pDefaultCursor[CursorType_Ibeam] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM); - g_pDefaultCursor[CursorType_Wait] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAIT); - g_pDefaultCursor[CursorType_Crosshair] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR); - g_pDefaultCursor[CursorType_Up] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW); - g_pDefaultCursor[CursorType_SizeNwSe] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE); - g_pDefaultCursor[CursorType_SizeNeSw] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW); - g_pDefaultCursor[CursorType_SizeWe] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE); - g_pDefaultCursor[CursorType_SizeNs] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS); - g_pDefaultCursor[CursorType_SizeAll] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL); - g_pDefaultCursor[CursorType_No] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO); - g_pDefaultCursor[CursorType_Hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND); + g_pDefaultCursor[dc_none] = NULL; + g_pDefaultCursor[dc_arrow] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW); + g_pDefaultCursor[dc_ibeam] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_IBEAM); + g_pDefaultCursor[dc_hourglass] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_WAIT); + g_pDefaultCursor[dc_crosshair] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR); + g_pDefaultCursor[dc_up] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW); + g_pDefaultCursor[dc_sizenwse] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENWSE); + g_pDefaultCursor[dc_sizenesw] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENESW); + g_pDefaultCursor[dc_sizewe] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEWE); + g_pDefaultCursor[dc_sizens] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZENS); + g_pDefaultCursor[dc_sizeall] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_SIZEALL); + g_pDefaultCursor[dc_no] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_NO); + g_pDefaultCursor[dc_hand] = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND); #endif initialized = true; } @@ -284,7 +285,7 @@ Platform_SetCursorType ======================== */ -void Platform_SetCursorType( cursor_type_t type ) +void Platform_SetCursorType( VGUI_DefaultCursor type ) { qboolean visible; @@ -295,8 +296,8 @@ void Platform_SetCursorType( cursor_type_t type ) switch( type ) { - case CursorType_User: - case CursorType_None: + case dc_user: + case dc_none: visible = false; break; default: diff --git a/engine/vgui_api.h b/engine/vgui_api.h index a54c4d8d..2ce89226 100644 --- a/engine/vgui_api.h +++ b/engine/vgui_api.h @@ -16,6 +16,8 @@ GNU General Public License for more details. #define VGUI_API_H #include "xash3d_types.h" +#include "key_modifiers.h" +#include "cursor_type.h" // VGUI generic vertex @@ -157,28 +159,10 @@ enum VGUI_MouseAction MA_WHEEL }; -enum VGUI_DefaultCursor -{ - dc_user, - dc_none, - dc_arrow, - dc_ibeam, - dc_hourglass, - dc_crosshair, - dc_up, - dc_sizenwse, - dc_sizenesw, - dc_sizewe, - dc_sizens, - dc_sizeall, - dc_no, - dc_hand, - dc_last -}; - typedef struct vguiapi_s { qboolean initialized; + // called from vgui_support void (*DrawInit)( void ); void (*DrawShutdown)( void ); void (*SetupDrawingText)( int *pColor ); @@ -193,18 +177,23 @@ typedef struct vguiapi_s void (*GetTextureSizes)( int *width, int *height ); int (*GenerateTexture)( void ); void *(*EngineMalloc)( size_t size ); - void (*CursorSelect)( enum VGUI_DefaultCursor cursor ); + void (*CursorSelect)( VGUI_DefaultCursor cursor ); byte (*GetColor)( int i, int j ); qboolean (*IsInGame)( void ); - void (*Unused)( void ); + void (*Unused)( void ); void (*GetCursorPos)( int *x, int *y ); int (*ProcessUtfChar)( int ch ); + int (*GetClipboardText)( char *buffer, size_t bufferSize ); + void (*SetClipboardText)( const char *text ); + key_modifier_t (*GetKeyModifiers)( void ); + // called from engine side void (*Startup)( int width, int height ); void (*Shutdown)( void ); void *(*GetPanel)( void ); void (*Paint)( void ); - void (*Mouse)(enum VGUI_MouseAction action, int code ); - void (*Key)(enum VGUI_KeyAction action,enum VGUI_KeyCode code ); + void (*Mouse)( enum VGUI_MouseAction action, int code ); + void (*Key)( enum VGUI_KeyAction action, enum VGUI_KeyCode code ); void (*MouseMove)( int x, int y ); + void (*TextInput)( const char *text ); } vguiapi_t; #endif // VGUI_API_H