Do not duplicate relative mouse mode state
The relative mouse mode is tracked by SDL, and accessible via SDL_GetRelativeMouseMode(). This is more robust in case SDL changes the relative mouse mode on its own.
This commit is contained in:
parent
43ae418752
commit
7848a387c8
4 changed files with 34 additions and 27 deletions
|
@ -163,14 +163,21 @@ sc_screen_is_relative_mode(struct sc_screen *screen) {
|
|||
}
|
||||
|
||||
static void
|
||||
sc_screen_capture_mouse(struct sc_screen *screen, bool capture) {
|
||||
sc_screen_set_mouse_capture(bool capture) {
|
||||
if (SDL_SetRelativeMouseMode(capture)) {
|
||||
LOGE("Could not set relative mouse mode to %s: %s",
|
||||
capture ? "true" : "false", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
screen->mouse_captured = capture;
|
||||
static inline bool
|
||||
sc_screen_get_mouse_capture(void) {
|
||||
return SDL_GetRelativeMouseMode();
|
||||
}
|
||||
|
||||
static inline void
|
||||
sc_screen_toggle_mouse_capture(void) {
|
||||
sc_screen_set_mouse_capture(!sc_screen_get_mouse_capture());
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -372,7 +379,6 @@ sc_screen_init(struct sc_screen *screen,
|
|||
screen->fullscreen = false;
|
||||
screen->maximized = false;
|
||||
screen->event_failed = false;
|
||||
screen->mouse_captured = false;
|
||||
screen->mouse_capture_key_pressed = 0;
|
||||
|
||||
screen->req.x = params->window_x;
|
||||
|
@ -710,7 +716,7 @@ sc_screen_update_frame(struct sc_screen *screen) {
|
|||
|
||||
if (sc_screen_is_relative_mode(screen)) {
|
||||
// Capture mouse on start
|
||||
sc_screen_capture_mouse(screen, true);
|
||||
sc_screen_set_mouse_capture(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -823,7 +829,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
|
|||
break;
|
||||
case SDL_WINDOWEVENT_FOCUS_LOST:
|
||||
if (relative_mode) {
|
||||
sc_screen_capture_mouse(screen, false);
|
||||
sc_screen_set_mouse_capture(false);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -853,8 +859,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
|
|||
if (key == cap) {
|
||||
// A mouse capture key has been pressed then released:
|
||||
// toggle the capture mouse mode
|
||||
sc_screen_capture_mouse(screen,
|
||||
!screen->mouse_captured);
|
||||
sc_screen_toggle_mouse_capture();
|
||||
}
|
||||
// Mouse capture keys are never forwarded to the device
|
||||
return;
|
||||
|
@ -864,7 +869,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
|
|||
case SDL_MOUSEWHEEL:
|
||||
case SDL_MOUSEMOTION:
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
if (relative_mode && !screen->mouse_captured) {
|
||||
if (relative_mode && !sc_screen_get_mouse_capture()) {
|
||||
// Do not forward to input manager, the mouse will be captured
|
||||
// on SDL_MOUSEBUTTONUP
|
||||
return;
|
||||
|
@ -880,8 +885,8 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
|
|||
}
|
||||
break;
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
if (relative_mode && !screen->mouse_captured) {
|
||||
sc_screen_capture_mouse(screen, true);
|
||||
if (relative_mode && !sc_screen_get_mouse_capture()) {
|
||||
sc_screen_set_mouse_capture(true);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -60,7 +60,6 @@ struct sc_screen {
|
|||
|
||||
bool event_failed; // in case SDL_PushEvent() returned an error
|
||||
|
||||
bool mouse_captured; // only relevant in relative mouse mode
|
||||
// To enable/disable mouse capture, a mouse capture key (LALT, LGUI or
|
||||
// RGUI) must be pressed. This variable tracks the pressed capture key.
|
||||
SDL_Keycode mouse_capture_key_pressed;
|
||||
|
|
|
@ -5,15 +5,21 @@
|
|||
#include "util/log.h"
|
||||
|
||||
static void
|
||||
sc_screen_otg_capture_mouse(struct sc_screen_otg *screen, bool capture) {
|
||||
assert(screen->mouse);
|
||||
sc_screen_otg_set_mouse_capture(bool capture) {
|
||||
if (SDL_SetRelativeMouseMode(capture)) {
|
||||
LOGE("Could not set relative mouse mode to %s: %s",
|
||||
capture ? "true" : "false", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
screen->mouse_captured = capture;
|
||||
static inline bool
|
||||
sc_screen_otg_get_mouse_capture(void) {
|
||||
return SDL_GetRelativeMouseMode();
|
||||
}
|
||||
|
||||
static inline void
|
||||
sc_screen_otg_toggle_mouse_capture(void) {
|
||||
sc_screen_otg_set_mouse_capture(!sc_screen_otg_get_mouse_capture());
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -31,7 +37,6 @@ sc_screen_otg_init(struct sc_screen_otg *screen,
|
|||
screen->keyboard = params->keyboard;
|
||||
screen->mouse = params->mouse;
|
||||
|
||||
screen->mouse_captured = false;
|
||||
screen->mouse_capture_key_pressed = 0;
|
||||
|
||||
const char *title = params->window_title;
|
||||
|
@ -81,7 +86,7 @@ sc_screen_otg_init(struct sc_screen_otg *screen,
|
|||
|
||||
if (screen->mouse) {
|
||||
// Capture mouse on start
|
||||
sc_screen_otg_capture_mouse(screen, true);
|
||||
sc_screen_otg_set_mouse_capture(true);
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -193,7 +198,7 @@ sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) {
|
|||
break;
|
||||
case SDL_WINDOWEVENT_FOCUS_LOST:
|
||||
if (screen->mouse) {
|
||||
sc_screen_otg_capture_mouse(screen, false);
|
||||
sc_screen_otg_set_mouse_capture(false);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -227,8 +232,7 @@ sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) {
|
|||
if (key == cap) {
|
||||
// A mouse capture key has been pressed then released:
|
||||
// toggle the capture mouse mode
|
||||
sc_screen_otg_capture_mouse(screen,
|
||||
!screen->mouse_captured);
|
||||
sc_screen_otg_toggle_mouse_capture();
|
||||
}
|
||||
// Mouse capture keys are never forwarded to the device
|
||||
return;
|
||||
|
@ -240,26 +244,26 @@ sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) {
|
|||
}
|
||||
break;
|
||||
case SDL_MOUSEMOTION:
|
||||
if (screen->mouse && screen->mouse_captured) {
|
||||
if (screen->mouse && sc_screen_otg_get_mouse_capture()) {
|
||||
sc_screen_otg_process_mouse_motion(screen, &event->motion);
|
||||
}
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
if (screen->mouse && screen->mouse_captured) {
|
||||
if (screen->mouse && sc_screen_otg_get_mouse_capture()) {
|
||||
sc_screen_otg_process_mouse_button(screen, &event->button);
|
||||
}
|
||||
break;
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
if (screen->mouse) {
|
||||
if (screen->mouse_captured) {
|
||||
if (sc_screen_otg_get_mouse_capture()) {
|
||||
sc_screen_otg_process_mouse_button(screen, &event->button);
|
||||
} else {
|
||||
sc_screen_otg_capture_mouse(screen, true);
|
||||
sc_screen_otg_set_mouse_capture(true);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SDL_MOUSEWHEEL:
|
||||
if (screen->mouse && screen->mouse_captured) {
|
||||
if (screen->mouse && sc_screen_otg_get_mouse_capture()) {
|
||||
sc_screen_otg_process_mouse_wheel(screen, &event->wheel);
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -18,7 +18,6 @@ struct sc_screen_otg {
|
|||
SDL_Texture *texture;
|
||||
|
||||
// See equivalent mechanism in screen.h
|
||||
bool mouse_captured;
|
||||
SDL_Keycode mouse_capture_key_pressed;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue