1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-18 10:38:31 +02:00

Show and hide mouse cursor based on user input

This is quite simple - whenever the user uses their keyboard or
controller, we hide the mouse cursor. Whenever they move the mouse, we
show it again. This makes it so the cursor gets out of the way when they
play the game, but reappears when they need it.

There is also a timeout, to prevent strobing if the user decides to use
the keyboard/controller and mouse at the same time. There is no timeout
from hiding the mouse cursor, but there is a timeout from showing the
mouse cursor - this because it's okay if the mouse lingers for a few
frames when you start using the keyboard, but really annoying if the
mouse doesn't instantly appear when you move it.
This commit is contained in:
Misa 2021-04-16 22:46:43 -07:00
parent c76c67b125
commit 83ad5e66de

View File

@ -81,8 +81,60 @@ void KeyPoll::toggleFullscreen(void)
}
}
static int changemousestate(
int timeout,
const bool show,
const bool hide
) {
int prev;
int new_;
if (timeout > 0)
{
return --timeout;
}
/* If we want to both show and hide at the same time, prioritize showing */
if (show)
{
new_ = SDL_ENABLE;
}
else if (hide)
{
new_ = SDL_DISABLE;
}
else
{
return timeout;
}
prev = SDL_ShowCursor(SDL_QUERY);
if (prev == new_)
{
return timeout;
}
SDL_ShowCursor(new_);
switch (new_)
{
case SDL_DISABLE:
timeout = 0;
break;
case SDL_ENABLE:
timeout = 30;
break;
}
return timeout;
}
void KeyPoll::Poll(void)
{
static int mousetoggletimeout = 0;
bool showmouse = false;
bool hidemouse = false;
bool altpressed = false;
bool fullscreenkeybind = false;
SDL_Event evt;
@ -324,8 +376,33 @@ void KeyPoll::Poll(void)
VVV_exit(0);
break;
}
switch (evt.type)
{
case SDL_KEYDOWN:
if (evt.key.repeat == 0)
{
hidemouse = true;
}
break;
case SDL_TEXTINPUT:
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERAXISMOTION:
hidemouse = true;
break;
case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONDOWN:
showmouse = true;
break;
}
}
mousetoggletimeout = changemousestate(
mousetoggletimeout,
showmouse,
hidemouse
);
if (fullscreenkeybind)
{
toggleFullscreen();