Autohide mouse cursor after half a second

This commit is contained in:
Wayward Heart 2023-09-04 18:04:29 -05:00
parent f59f213e3f
commit 21fac5a946
3 changed files with 19 additions and 3 deletions

View file

@ -125,7 +125,7 @@ bool EventThread::allocUserEvents()
EventThread::EventThread()
: ctrl(0),
fullscreen(false),
showCursor(true)
showCursor(false)
{
textInputLock = SDL_CreateMutex();
}
@ -135,6 +135,20 @@ EventThread::~EventThread()
SDL_DestroyMutex(textInputLock);
}
SDL_TimerID hideCursorTimerID = 0;
Uint32 cursorTimerCallback(Uint32 interval, void* param)
{
EventThread *ethread = static_cast<EventThread*>(param);
hideCursorTimerID = 0;
ethread->requestShowCursor(ethread->getShowCursor());
return 0;
}
void EventThread::cursorTimer()
{
SDL_RemoveTimer(hideCursorTimerID);
hideCursorTimerID = SDL_AddTimer(500, cursorTimerCallback, this);
}
void EventThread::process(RGSSThreadData &rtData)
{
SDL_Event event;
@ -445,6 +459,7 @@ void EventThread::process(RGSSThreadData &rtData)
case SDL_MOUSEMOTION :
mouseState.x = event.motion.x;
mouseState.y = event.motion.y;
cursorTimer();
updateCursorState(cursorInWindow, gameScreen);
break;
@ -687,7 +702,7 @@ void EventThread::updateCursorState(bool inWindow,
bool inScreen = inWindow && SDL_PointInRect(&pos, &screen);
if (inScreen)
SDL_ShowCursor(showCursor ? SDL_TRUE : SDL_FALSE);
SDL_ShowCursor(showCursor || hideCursorTimerID ? SDL_TRUE : SDL_FALSE);
else
SDL_ShowCursor(SDL_TRUE);
}

View file

@ -124,6 +124,7 @@ private:
void setFullscreen(SDL_Window *, bool mode);
void updateCursorState(bool inWindow,
const SDL_Rect &screen);
void cursorTimer();
bool fullscreen;
bool showCursor;

View file

@ -210,7 +210,7 @@ int main(int argc, char *argv[]) {
#endif
/* initialize SDL first */
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER) < 0) {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER | SDL_INIT_TIMER) < 0) {
showInitError(std::string("Error initializing SDL: ") + SDL_GetError());
return 0;
}