generated from Over-Run/project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
236 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
modules/freeworld.client/src/main/java/freeworld/client/MouseInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* freeworld - 3D sandbox game | ||
* Copyright (C) 2024 XenFork Union | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; | ||
* only version 2.1 of the License. | ||
*/ | ||
|
||
package freeworld.client; | ||
|
||
import freeworld.client.event.CursorPosEvent; | ||
import overrungl.glfw.GLFW; | ||
|
||
import java.lang.foreign.MemorySegment; | ||
|
||
/** | ||
* @author squid233 | ||
* @since 0.1.0 | ||
*/ | ||
public final class MouseInput { | ||
private final GLFW glfw = GLFW.INSTANCE; | ||
private final MemorySegment window; | ||
private double cursorX; | ||
private double cursorY; | ||
private double cursorDeltaX; | ||
private double cursorDeltaY; | ||
private boolean disabled = false; | ||
|
||
public MouseInput(MemorySegment window) { | ||
this.window = window; | ||
glfw.setCursorPosCallback(window, (_, x, y) -> { | ||
cursorDeltaX = x - cursorX; | ||
cursorDeltaY = y - cursorY; | ||
if (disabled) { | ||
CursorPosEvent.DISABLED.publish(new CursorPosEvent(x, y, cursorDeltaX, cursorDeltaY)); | ||
} | ||
cursorX = x; | ||
cursorY = y; | ||
}); | ||
} | ||
|
||
public void enable() { | ||
disabled = false; | ||
glfw.setInputMode(window, GLFW.CURSOR, GLFW.CURSOR_NORMAL); | ||
} | ||
|
||
public void disable() { | ||
disabled = true; | ||
glfw.setInputMode(window, GLFW.CURSOR, GLFW.CURSOR_DISABLED); | ||
} | ||
|
||
public double cursorX() { | ||
return cursorX; | ||
} | ||
|
||
public double cursorY() { | ||
return cursorY; | ||
} | ||
|
||
public double cursorDeltaX() { | ||
return cursorDeltaX; | ||
} | ||
|
||
public double cursorDeltaY() { | ||
return cursorDeltaY; | ||
} | ||
|
||
public boolean disabled() { | ||
return disabled; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
modules/freeworld.client/src/main/java/freeworld/client/event/CursorPosEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* freeworld - 3D sandbox game | ||
* Copyright (C) 2024 XenFork Union | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; | ||
* only version 2.1 of the License. | ||
*/ | ||
|
||
package freeworld.client.event; | ||
|
||
import freeworld.event.Event; | ||
|
||
/** | ||
* @author squid233 | ||
* @since 0.1.0 | ||
*/ | ||
public record CursorPosEvent(double x, double y, double deltaX, double deltaY) { | ||
public static final Event<CursorPosEvent> DISABLED = new Event<>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.