engine: platform: added Platform_SetCursorType

This commit is contained in:
SNMetamorph 2022-04-22 21:31:10 +04:00 committed by a1batross
parent 15eb6808c4
commit 26e09c240a
3 changed files with 118 additions and 0 deletions

39
engine/cursor_type.h Normal file
View File

@ -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

View File

@ -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 );

View File

@ -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