Skip to content

Commit 1d16dd7

Browse files
committed
Merge remote-tracking branch 'origin/main'
# Conflicts: # build.gradle # src/main/java/com/github/strubium/windowmanager/imgui/GuiBuilder.java
2 parents 5195a38 + 848f5ff commit 1d16dd7

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
}

src/main/java/com/github/strubium/windowmanager/window/WindowManager.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package com.github.strubium.windowmanager.window;
22

33
import org.lwjgl.glfw.GLFWErrorCallback;
4+
import org.lwjgl.glfw.GLFWImage;
45
import org.lwjgl.glfw.GLFWVidMode;
6+
import org.lwjgl.stb.STBImage;
57
import org.lwjgl.opengl.GL;
68
import org.lwjgl.system.MemoryStack;
79

10+
import java.nio.ByteBuffer;
811
import java.nio.IntBuffer;
912

1013
import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks;
@@ -20,7 +23,8 @@
2023
*/
2124
public class WindowManager {
2225

23-
public long window; // Window handle
26+
/**The window handle*/
27+
public long window; //
2428
private boolean fullscreen;
2529
private final int windowWidth;
2630
private final int windowHeight;
@@ -186,4 +190,32 @@ public void setWindowTitle(String newTitle) {
186190
glfwSetWindowTitle(window, newTitle);
187191
}
188192

193+
/**
194+
* Set the window icon
195+
*
196+
* @param imagePath The path to the icon image (must be PNG or similar)
197+
*/
198+
public void setWindowIcon(String imagePath) {
199+
try (MemoryStack stack = stackPush()) {
200+
IntBuffer width = stack.mallocInt(1);
201+
IntBuffer height = stack.mallocInt(1);
202+
IntBuffer channels = stack.mallocInt(1);
203+
204+
ByteBuffer image = STBImage.stbi_load(imagePath, width, height, channels, 4);
205+
if (image == null) {
206+
throw new RuntimeException("Failed to load icon image: " + STBImage.stbi_failure_reason());
207+
}
208+
209+
GLFWImage.Buffer iconBuffer = GLFWImage.malloc(1, stack);
210+
iconBuffer.position(0);
211+
iconBuffer.width(width.get(0));
212+
iconBuffer.height(height.get(0));
213+
iconBuffer.pixels(image);
214+
215+
glfwSetWindowIcon(window, iconBuffer);
216+
217+
STBImage.stbi_image_free(image);
218+
}
219+
}
220+
189221
}

0 commit comments

Comments
 (0)