Skip to content

Commit

Permalink
Show hand cursor when hovering over clickable area
Browse files Browse the repository at this point in the history
  • Loading branch information
jorio committed Feb 6, 2023
1 parent 8abfdf1 commit fbb518d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
33 changes: 33 additions & 0 deletions src/SDLU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ static SDL_Palette* s_grayscalePalette;
static int s_mouseButton;
static MPoint s_mousePosition;

// system mouse cursors
static SDL_Cursor* s_standardCursor = NULL;
static SDL_Cursor* s_handCursor = NULL;

// for event loop
static MBoolean s_isForeground = true;

Expand Down Expand Up @@ -206,6 +210,9 @@ void SDLU_Init()

s_grayscalePalette = SDL_AllocPalette(256);
SDL_SetPaletteColors(s_grayscalePalette, grayscaleColors, 0, arrsize(grayscaleColors));

s_standardCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_ARROW);
s_handCursor = SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_HAND);
}


Expand Down Expand Up @@ -506,3 +513,29 @@ void SDLU_Present()
}
#endif
}


void SDLU_SetSystemCursor(int which)
{
#if USE_CURSOR_SPRITE
SDL_ShowCursor(SDL_DISABLE);
#else
switch (which)
{
case SYSTEM_CURSOR_OFF:
SDL_ShowCursor(SDL_DISABLE);
SDL_SetCursor(s_standardCursor);
break;

case SYSTEM_CURSOR_ARROW:
SDL_SetCursor(s_standardCursor);
SDL_ShowCursor(SDL_ENABLE);
break;

case SYSTEM_CURSOR_HAND:
SDL_SetCursor(s_handCursor);
SDL_ShowCursor(SDL_ENABLE);
break;
}
#endif
}
5 changes: 5 additions & 0 deletions src/SDLU.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#define BYTES_PER_MASK_PIXEL 1
#define MASK_DEPTH 8

#define SYSTEM_CURSOR_OFF 0
#define SYSTEM_CURSOR_ARROW 1
#define SYSTEM_CURSOR_HAND 2

void SDLU_Init();
void SDLU_CreateRendererTexture();
SDL_Rect* SDLU_MRectToSDLRect( const MRect* in, SDL_Rect* out );
Expand All @@ -46,3 +50,4 @@ MBoolean SDLU_CheckASCIITyping(char* ascii);
MBoolean SDLU_CheckSDLTyping(SDL_Keycode* sdlKey);
MBoolean SDLU_IsForeground();
void SDLU_Present();
void SDLU_SetSystemCursor(int which);
7 changes: 2 additions & 5 deletions src/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ void GameStartMenu( void )
#if USE_CURSOR_SPRITE
cursorBackSurface = SDLU_InitSurface( &cursorBackSDLRect, 32 );
SDL_FillRect( cursorBackSurface, &cursorBackSDLRect, black );
#else
SDL_ShowCursor( 1 );
#endif

// make drawing surface
Expand Down Expand Up @@ -503,6 +501,7 @@ void GameStartMenu( void )
drawRect[kCursor].bottom = max<short>( drawRect[kCursor].bottom, mouse.v + kCursorHeight );
drawRect[kCursor].right = max<short>( drawRect[kCursor].right, mouse.h + kCursorWidth );
#endif
SDLU_SetSystemCursor( selected < 0 ? SYSTEM_CURSOR_ARROW : SYSTEM_CURSOR_HAND );

// Copy down everything
if( shouldFullRepaint )
Expand Down Expand Up @@ -548,9 +547,7 @@ void GameStartMenu( void )
}
else
{
#if !USE_CURSOR_SPRITE
SDL_ShowCursor( 0 );
#endif
SDLU_SetSystemCursor( SYSTEM_CURSOR_OFF );
}

switch( selected )
Expand Down
2 changes: 0 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,8 +559,6 @@ void CenterRectOnScreen( MRect *rect, double locationX, double locationY )

void ReserveMonitor( void )
{
// SDL_ShowCursor( SDL_DISABLE );

SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");

int resW = 640;
Expand Down
9 changes: 2 additions & 7 deletions src/pause.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1063,10 +1063,6 @@ void HandleDialog( int type )

SDLU_StartWatchingTyping();

#if !USE_CURSOR_SPRITE
SDL_ShowCursor( 1 );
#endif

DoFullRepaint = ItsTimeToRedraw;

while( ((dialogStage != kClosing) || !dialogStageComplete) && !finished )
Expand All @@ -1091,9 +1087,7 @@ void HandleDialog( int type )

if( DialogSelected[dialogType]( &dialogItem, inASCII, inSDLKey ) )
{
#if !USE_CURSOR_SPRITE
SDL_ShowCursor( 0 );
#endif
SDLU_SetSystemCursor( SYSTEM_CURSOR_OFF );

dialogStage = kClosing;
dialogTarget = 0;
Expand Down Expand Up @@ -1145,6 +1139,7 @@ void HandleDialog( int type )
// ... and cursor
DrawDialogCursor( &pauseRect, &dialogShade );
#endif
SDLU_SetSystemCursor( dialogItem == kNothing ? SYSTEM_CURSOR_ARROW : SYSTEM_CURSOR_HAND );
}

SurfaceCurveEdges( drawSurface, &pauseRect );
Expand Down

0 comments on commit fbb518d

Please sign in to comment.