From 26e09c240a9888b08220be18a61d9a2144f6b99c Mon Sep 17 00:00:00 2001 From: SNMetamorph <25657591+SNMetamorph@users.noreply.github.com> Date: Fri, 22 Apr 2022 21:31:10 +0400 Subject: [PATCH] engine: platform: added Platform_SetCursorType --- engine/cursor_type.h | 39 ++++++++++++++++++ engine/platform/platform.h | 2 + engine/platform/sdl/in_sdl.c | 77 ++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 engine/cursor_type.h diff --git a/engine/cursor_type.h b/engine/cursor_type.h new file mode 100644 index 00000000..6264eda7 --- /dev/null +++ b/engine/cursor_type.h @@ -0,0 +1,39 @@ +/* +cursor_type.h - enumeration of possible mouse cursor types +Copyright (C) 2022 FWGS Team + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. +*/ + +#pragma once +#ifndef CURSOR_TYPE_H +#define CURSOR_TYPE_H + +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; + +#endif diff --git a/engine/platform/platform.h b/engine/platform/platform.h index f4894f36..02645265 100644 --- a/engine/platform/platform.h +++ b/engine/platform/platform.h @@ -20,6 +20,7 @@ GNU General Public License for more details. #include "common.h" #include "system.h" #include "defaults.h" +#include "cursor_type.h" /* ============================================================================== @@ -79,6 +80,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 ); // Clipboard int Platform_GetClipboardText( char *buffer, size_t size ); void Platform_SetClipboardText( const char *buffer ); diff --git a/engine/platform/sdl/in_sdl.c b/engine/platform/sdl/in_sdl.c index 963e7d6c..dec29ed1 100644 --- a/engine/platform/sdl/in_sdl.c +++ b/engine/platform/sdl/in_sdl.c @@ -25,6 +25,7 @@ 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 ) ) #endif @@ -246,4 +247,80 @@ int Platform_JoyInit( int numjoy ) return SDLash_JoyInit_Old(numjoy); } +/* +======================== +SDLash_InitCursors + +======================== +*/ +static void SDLash_InitCursors( void ) +{ + static qboolean initialized = false; + if( !initialized ) + { + // 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); +#endif + initialized = true; + } +} + +/* +======================== +Platform_SetCursorType + +======================== +*/ +void Platform_SetCursorType( cursor_type_t type ) +{ + qboolean visible; + + if (cls.key_dest != key_game || cl.paused) + return; + + SDLash_InitCursors(); + + switch( type ) + { + case CursorType_User: + case CursorType_None: + visible = false; + break; + default: + visible = true; + break; + } + +#if SDL_VERSION_ATLEAST( 2, 0, 0 ) + if( CVAR_TO_BOOL( touch_emulate )) + return; + + if (visible && !host.mouse_visible) + { + SDL_SetCursor( g_pDefaultCursor[type] ); + SDL_ShowCursor( true ); + Key_EnableTextInput( true, false ); + } + else if (!visible && host.mouse_visible) + { + SDL_ShowCursor( false ); + Key_EnableTextInput( false, false ); + } + host.mouse_visible = visible; +#endif +} + #endif // XASH_DEDICATED