raycast cursor

This commit is contained in:
2025-04-27 20:12:38 +02:00
parent 1b61eca58b
commit cbbce43ede
4 changed files with 74 additions and 5 deletions

View File

@@ -1,5 +1,6 @@
package chess.view.DDDrender;
import org.joml.Vector2f;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
@@ -35,7 +36,11 @@ public class Window {
private final Queue<Runnable> tasks;
private final List<Consumer<Float>> regularTasks;
private Coordinate lastCell = null;
public final Signal1<Coordinate> OnCellClick = new Signal1<>();
public final Signal1<Coordinate> OnCellEnter = new Signal1<>();
public final Signal1<Coordinate> OnCellExit = new Signal1<>();
public Window(Renderer renderer, World world) {
this.renderer = new Renderer();
@@ -143,6 +148,32 @@ public class Window {
}
}
private void checkCursor(int windowWidth, int windowHeight) {
double x[] = new double[1];
double y[] = new double[1];
glfwGetCursorPos(this.window, x, y);
Vector2f cursorPos = this.cam.getCursorWorldFloorPos(new Vector2f((float) x[0], (float) y[0]), windowWidth, windowHeight);
Coordinate selectedCell = DDDPlacement.vector_to_coordinates(cursorPos);
if (this.lastCell == null) {
this.lastCell = selectedCell;
if (selectedCell.isValid())
this.OnCellEnter.emit(selectedCell);
}
else if (!this.lastCell.equals(selectedCell)) {
if (this.lastCell.isValid())
this.OnCellExit.emit(this.lastCell);
if (selectedCell.isValid())
this.OnCellEnter.emit(selectedCell);
this.lastCell = selectedCell;
}
glfwSetMouseButtonCallback(this.window, (window, button, action, mods) -> {
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS) {
if (this.lastCell.isValid())
this.OnCellClick.emit(this.lastCell);
}
});
}
private void loop() {
// This line is critical for LWJGL's interoperation with GLFW's
// OpenGL context, or any context that is managed externally.
@@ -186,6 +217,8 @@ public class Window {
// invoked during this call.
glfwPollEvents();
checkCursor(width[0], height[0]);
executeTasks(deltaTime);
glfwGetWindowSize(window, width, height);