diff --git a/src/input.c b/src/input.c index 3e2a1d1..f676654 100644 --- a/src/input.c +++ b/src/input.c @@ -60,4 +60,6 @@ void input_init(void *mlx_ptr, void *win_ptr) mlx_hook(win_ptr, KeyPress, KeyPressMask, hook_keypress, NULL); mlx_hook(win_ptr, KeyRelease, KeyReleaseMask, hook_keyrelease, NULL); mlx_hook(win_ptr, MotionNotify, PointerMotionMask, hook_mousemove, NULL); + mlx_hook(win_ptr, ButtonPress, ButtonPressMask, hook_buttonpress, NULL); + printf(MOUSE_UNUSED_MSG); } diff --git a/src/input_utils.c b/src/input_utils.c index 1aff5af..814f2df 100644 --- a/src/input_utils.c +++ b/src/input_utils.c @@ -16,6 +16,8 @@ #include "utils.h" #include "global.h" +static bool g_intercept_mouse = false; + int hook_mousemove(int x, int y) { static const t_point_int window_middle = { @@ -23,10 +25,31 @@ int hook_mousemove(int x, int y) static bool first_call = true; const int dx = x - window_middle.x; - if (!first_call && dx) - g_input_actions.rotate += dx; - first_call = false; - if (x != window_middle.x || y != window_middle.y) - mlx_mouse_move(g_mlx, g_win, window_middle.x, window_middle.y); + if (g_intercept_mouse) + { + if (!first_call && dx) + g_input_actions.rotate += dx; + first_call = false; + if (x != window_middle.x || y != window_middle.y) + mlx_mouse_move(g_mlx, g_win, window_middle.x, window_middle.y); + } + return (0); +} + +int hook_buttonpress(int button) +{ + static const t_point_int window_middle = { + SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2}; + if (button == Button1) + { + g_intercept_mouse = !g_intercept_mouse; + if (g_intercept_mouse) + { + mlx_mouse_move(g_mlx, g_win, window_middle.x, window_middle.y); + printf(MOUSE_USED_MSG); + } + else + printf(MOUSE_UNUSED_MSG); + } return (0); } diff --git a/src/input_utils.h b/src/input_utils.h index 6e4b216..b12f669 100644 --- a/src/input_utils.h +++ b/src/input_utils.h @@ -15,6 +15,10 @@ # include "input.h" +# define MOUSE_UNUSED_MSG "Click in the window to use the pointer.\n" +# define MOUSE_USED_MSG "Click again to free the pointer.\n" + int hook_mousemove(int x, int y); +int hook_buttonpress(int button); #endif