Skip to content

Commit

Permalink
https://github.com/xtreme8000/BetterSpades/issues/161
Browse files Browse the repository at this point in the history
Ignore mouse movement after losing focus.
  • Loading branch information
forked-from-1kasper committed Jan 15, 2024
1 parent c5b1836 commit 897bbfe
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/BetterSpades/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ typedef struct {
void (*input_mouseclick)(double x, double y, int button, int action, int mods);
void (*input_mousescroll)(double yoffset);
void (*input_touch)(void * finger, int action, float x, float y, float dx, float dy);
void (*focus)(bool);
void (*hover)(bool);
Texture * (*ui_images)(int icon_id, bool * resize);
char render_world;
char render_localplayer;
Expand Down
3 changes: 3 additions & 0 deletions include/BetterSpades/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
#define MAIN_H

#include <BetterSpades/window.h>
#include <stdbool.h>

void reshape(struct window_instance * window, int width, int height);
void text_input(struct window_instance * window, const uint8_t *);
void keys(struct window_instance * window, int key, int action, int mods);
void mouse_click(struct window_instance * window, int button, int action, int mods);
void mouse(struct window_instance * window, double x, double y);
void mouse_scroll(struct window_instance * window, double xoffset, double yoffset);
void mouse_focus(struct window_instance *, bool);
void mouse_hover(struct window_instance *, bool);
void on_error(int i, const char * s);

#endif
2 changes: 2 additions & 0 deletions include/BetterSpades/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ int window_cpucores();
void window_title(char * suffix);
void window_sendkey(int action, int keycode, int mod);

int window_get_mousemode();

typedef void (*Idle)(double);
typedef void (*Display)(void);

Expand Down
15 changes: 15 additions & 0 deletions src/GUI/glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ static void window_impl_keys(GLFWwindow * window, int key, int scancode, int act
window_sendkey(a, key, mods & GLFW_MOD_CONTROL);
}

static void window_cursor_enter_callback(GLFWwindow * window, int entered) {
mouse_hover(hud_window, entered);
}

static void window_focus_callback(GLFWwindow * window, int focused) {
mouse_focus(hud_window, focused);
}

int window_get_mousemode() {
int s = glfwGetInputMode(hud_window->impl, GLFW_CURSOR);
return s == GLFW_CURSOR_DISABLED ? WINDOW_CURSOR_DISABLED : WINDOW_CURSOR_ENABLED;
}

void window_init(int * argc, char ** argv) {
static struct window_instance i;
hud_window = &i;
Expand Down Expand Up @@ -147,6 +160,8 @@ void window_init(int * argc, char ** argv) {
glfwSetMouseButtonCallback(hud_window->impl, window_impl_mouseclick);
glfwSetScrollCallback(hud_window->impl, window_impl_mousescroll);
glfwSetCharCallback(hud_window->impl, window_impl_textinput);
glfwSetWindowFocusCallback(hud_window->impl, window_focus_callback);
glfwSetCursorEnterCallback(hud_window->impl, window_cursor_enter_callback);

if (glfwRawMouseMotionSupported())
glfwSetInputMode(hud_window->impl, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
Expand Down
9 changes: 9 additions & 0 deletions src/GUI/glut.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ void window_mouse_button(int button, int state, int x, int y) {
mouse_click(hud_window, but, action, mod);
}

int window_get_mousemode() {
return captured ? WINDOW_CURSOR_DISABLED : WINDOW_CURSOR_ENABLED;
}

void window_mouse_motion(int x, int y) {
static int warped = 0;
mx = x; my = y;
Expand All @@ -99,6 +103,10 @@ void window_mouse_motion(int x, int y) {
} else mouse(hud_window, x, y);
}

void window_entry(int state) {
mouse_hover(hud_window, state == GLUT_ENTERED ? true : false);
}

void window_init(int * argc, char ** argv) {
static struct window_instance i;
hud_window = &i;
Expand All @@ -118,6 +126,7 @@ void window_init(int * argc, char ** argv) {
glutMouseFunc(window_mouse_button);
glutMotionFunc(window_mouse_motion);
glutPassiveMotionFunc(window_mouse_motion);
glutEntryFunc(window_entry);
}

static Idle idle = NULL;
Expand Down
21 changes: 20 additions & 1 deletion src/GUI/sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ int get_sdl_button(int button) {
}
}

int window_get_mousemode() {
int s = SDL_GetRelativeMouseMode();
return s ? WINDOW_CURSOR_DISABLED : WINDOW_CURSOR_ENABLED;
}

void window_update() {
SDL_GL_SwapWindow(hud_window->impl);
SDL_Event event;
Expand All @@ -79,12 +84,26 @@ void window_update() {
case SDL_MOUSEBUTTONDOWN: mouse_click(hud_window, get_sdl_button(event.button.button), WINDOW_PRESS, 0); break;
case SDL_MOUSEBUTTONUP: mouse_click(hud_window, get_sdl_button(event.button.button), WINDOW_RELEASE, 0); break;

case SDL_WINDOWEVENT:
case SDL_WINDOWEVENT: {
if (event.window.event == SDL_WINDOWEVENT_RESIZED
|| event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
reshape(hud_window, event.window.data1, event.window.data2);
}

if (event.window.event == SDL_WINDOWEVENT_LEAVE)
mouse_hover(hud_window, false);

if (event.window.event == SDL_WINDOWEVENT_ENTER)
mouse_hover(hud_window, true);

if (event.window.event == SDL_WINDOWEVENT_FOCUS_LOST)
mouse_focus(hud_window, false);

if (event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED)
mouse_focus(hud_window, true);

break;
}

case SDL_MOUSEWHEEL: mouse_scroll(hud_window, event.wheel.x, event.wheel.y); break;

Expand Down
31 changes: 31 additions & 0 deletions src/hud.c
Original file line number Diff line number Diff line change
Expand Up @@ -1257,13 +1257,29 @@ static void hud_ingame_scroll(double yoffset) {
}
}

bool window_focused = true, window_hovered = true;

void hud_ingame_focus(bool focused) {
window_focused = focused;
if (!focused) window_mousemode(WINDOW_CURSOR_ENABLED);
}

void hud_ingame_hover(bool hovered) {
window_hovered = hovered;
if (!hovered) window_mousemode(WINDOW_CURSOR_ENABLED);
}

static double last_x, last_y;
static void hud_ingame_mouselocation(double x, double y) {
if (window_get_mousemode() != WINDOW_CURSOR_DISABLED)
return;

if (show_exit) {
last_x = x;
last_y = y;
return;
}

float dx = x - last_x;
float dy = y - last_y;
last_x = x;
Expand All @@ -1285,6 +1301,13 @@ static void hud_ingame_mouselocation(double x, double y) {
}

static void hud_ingame_mouseclick(double x, double y, int button, int action, int mods) {
if (window_get_mousemode() == WINDOW_CURSOR_ENABLED) {
if (window_focused && window_hovered && action == WINDOW_RELEASE && !show_exit)
window_mousemode(WINDOW_CURSOR_DISABLED);

return;
}

if (button == WINDOW_MOUSE_LMB)
button_map[0] = (action == WINDOW_PRESS);

Expand Down Expand Up @@ -1954,6 +1977,8 @@ HUD hud_ingame = {
hud_ingame_mouseclick,
hud_ingame_scroll,
hud_ingame_touch,
hud_ingame_focus,
hud_ingame_hover,
NULL,
1,
0,
Expand Down Expand Up @@ -2464,6 +2489,8 @@ HUD hud_serverlist = {
NULL,
NULL,
hud_serverlist_touch,
NULL,
NULL,
hud_serverlist_ui_images,
0,
0,
Expand Down Expand Up @@ -2644,6 +2671,8 @@ HUD hud_settings = {
NULL,
NULL,
hud_settings_touch,
NULL,
NULL,
hud_settings_ui_images,
0,
0,
Expand Down Expand Up @@ -2794,6 +2823,8 @@ HUD hud_controls = {
NULL,
NULL,
hud_controls_touch,
NULL,
NULL,
hud_settings_ui_images,
0,
0,
Expand Down
8 changes: 8 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,14 @@ void mouse_click(struct window_instance * window, int button, int action, int mo
}
}

void mouse_focus(struct window_instance * window, bool focused) {
if (hud_active->focus) hud_active->focus(focused);
}

void mouse_hover(struct window_instance * window, bool hovered) {
if (hud_active->hover) hud_active->hover(hovered);
}

void mouse(struct window_instance * window, double x, double y) {
if (hud_active->input_mouselocation)
hud_active->input_mouselocation(x, y);
Expand Down

0 comments on commit 897bbfe

Please # to comment.