1+ package com .github .strubium .windowmanager .window ;
2+
3+ import org .lwjgl .glfw .GLFW ;
4+
5+ /**
6+ * The InputUtils class provides help working with mice
7+ */
8+ public class InputUtils {
9+
10+ private final long window ;
11+
12+ /**
13+ * Constructs a MouseUtils object with the specified window handle.
14+ *
15+ * @param window The window handle
16+ */
17+ public InputUtils (long window ) {
18+ this .window = window ;
19+ }
20+
21+
22+ /**
23+ * Gets the mouse position relative to the window.
24+ *
25+ * @return A float array containing [mouseX, mouseY] coordinates
26+ */
27+ public float [] getMousePosition () {
28+ double [] mouseX = new double [1 ];
29+ double [] mouseY = new double [1 ];
30+ GLFW .glfwGetCursorPos (window , mouseX , mouseY );
31+
32+ return new float []{(float ) mouseX [0 ], (float ) mouseY [0 ]};
33+ }
34+
35+ /**
36+ * Converts mouse coordinates to OpenGL coordinates with an offset
37+ *
38+ * @param mouseX The x-coordinate of the mouse
39+ * @param mouseY The y-coordinate of the mouse
40+ * @param screenWidth The width of the screen or window
41+ * @param screenHeight The height of the screen or window
42+ * @return A float array containing [openglMouseX, openglMouseY] coordinates
43+ */
44+ public static float [] convertToOpenGLCoordinatesOffset (float mouseX , float mouseY , int screenWidth , int screenHeight , float offsetX , float offsetY ) {
45+ float openglMouseX = mouseX / screenWidth * 2 - 1 ;
46+ float openglMouseY = 1 - mouseY / screenHeight * 2 ;
47+
48+ return new float []{openglMouseX + offsetX , openglMouseY + offsetY };
49+ }
50+
51+ /**
52+ * Converts mouse coordinates to OpenGL coordinates.
53+ *
54+ * @param mouseX The x-coordinate of the mouse
55+ * @param mouseY The y-coordinate of the mouse
56+ * @param screenWidth The width of the screen or window
57+ * @param screenHeight The height of the screen or window
58+ * @return A float array containing [openglMouseX, openglMouseY] coordinates
59+ */
60+ public static float [] convertToOpenGLCoordinates (float mouseX , float mouseY , int screenWidth , int screenHeight ) {
61+ float openglMouseX = mouseX / screenWidth * 2 - 1 ;
62+ float openglMouseY = 1 - mouseY / screenHeight * 2 ;
63+
64+ return new float []{openglMouseX , openglMouseY };
65+ }
66+
67+ public boolean isMouseButtonPressed (int button ) {
68+ return GLFW .glfwGetMouseButton (window , button ) == GLFW .GLFW_PRESS ;
69+ }
70+
71+ public boolean isKeyPressed (int button ) {
72+ return GLFW .glfwGetKey (window , button ) == GLFW .GLFW_PRESS ;
73+ }
74+
75+ }
0 commit comments