From 11151ca6f0be82d56207e4f482722a0e304d02fe Mon Sep 17 00:00:00 2001 From: Carlos Aragones Date: Tue, 16 Nov 2021 09:55:27 +0100 Subject: [PATCH] Add option to invert the Mouse Y coordinate on MacOS --- CMakeLists.txt | 5 +++++ README.md | 14 ++++++++++++++ src/macosx/OSXView.m | 6 +++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 66b1a32..01cf2f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,6 +104,7 @@ option(MINIFB_BUILD_EXAMPLES "Build minifb example programs" TRUE) if(APPLE AND NOT IOS) option(USE_METAL_API "Build the project using metal API code" ON) + option(USE_INVERTED_Y_ON_MACOS "Use default mouse position: (0, 0) at (left, down)" OFF) elseif(UNIX) option(USE_WAYLAND_API "Build the project using wayland API code" OFF) if(NOT USE_WAYLAND_API) @@ -180,6 +181,10 @@ elseif(APPLE) add_definitions(-DUSE_METAL_API) endif() + if(USE_INVERTED_Y_ON_MACOS) + add_definitions(-DUSE_INVERTED_Y_ON_MACOS) + endif() + list(APPEND SrcLib ${SrcMacOSX}) elseif(UNIX) diff --git a/README.md b/README.md index b30e74c..1ef65ff 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,20 @@ cd build cmake .. -DUSE_METAL_API=OFF ``` +#### Coordinate system + +On MacOS, the default mouse coordinate system is (0, 0) -> (left, bottom). But as we want to create a multiplatform library we inverted the coordinates in such a way that now (0, 0) -> (left, top), like in the other platforms. + +In any case, if you want to get the default coordinate system you can use the CMake flag: USE_INVERTED_Y_ON_MACOS=ON + +``` +mkdir build +cd build +cmake .. -DUSE_INVERTED_Y_ON_MACOS=ON +``` + +_Note: In the future, we may use a global option so that all platforms behave in the same way. Probably: -DUSE_INVERTED_Y_ + if you use **tundra**: ``` diff --git a/src/macosx/OSXView.m b/src/macosx/OSXView.m index 0bf0378..b00fca3 100644 --- a/src/macosx/OSXView.m +++ b/src/macosx/OSXView.m @@ -186,8 +186,12 @@ NSPoint point = [event locationInWindow]; //NSPoint localPoint = [self convertPoint:point fromView:nil]; window_data->mouse_pos_x = point.x; +#if defined(USE_INVERTED_Y_ON_MACOS) window_data->mouse_pos_y = point.y; - kCall(mouse_move_func, point.x, point.y); +#else + window_data->mouse_pos_y = window_data->window_height - point.y; +#endif + kCall(mouse_move_func, window_data->mouse_pos_x, window_data->mouse_pos_y); } }