Skip to content

Commit

Permalink
Add MouseInput and Event
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Jul 30, 2024
1 parent eaf8717 commit 961e424
Show file tree
Hide file tree
Showing 32 changed files with 236 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@

package freeworld.client;

import freeworld.client.event.CursorPosEvent;
import freeworld.client.render.Camera;
import freeworld.client.render.GameRenderer;
import freeworld.client.render.RenderSystem;
import freeworld.client.render.gl.GLStateMgr;
import freeworld.client.render.model.block.BlockModelManager;
import freeworld.client.render.screen.Screen;
import freeworld.client.render.screen.ingame.CreativeTabScreen;
import freeworld.client.render.screen.ingame.PauseScreen;
import freeworld.client.render.screen.Screen;
import freeworld.client.render.world.BlockHitResult;
import freeworld.client.render.world.WorldRenderer;
import freeworld.core.registry.Registries;
import freeworld.math.Vector2d;
import freeworld.math.Vector3d;
import freeworld.math.Vector3i;
import freeworld.util.Direction;
import freeworld.util.Logging;
import freeworld.util.Timer;
import freeworld.util.math.MathUtil;
import freeworld.core.Timer;
import freeworld.world.World;
import freeworld.world.block.BlockType;
import freeworld.world.block.BlockTypes;
Expand Down Expand Up @@ -68,11 +68,7 @@ public final class Freeworld implements AutoCloseable {
private int framebufferHeight;
private final Timer timer = new Timer(Timer.DEFAULT_TPS);
private final Camera camera = new Camera();
private double cursorX;
private double cursorY;
private double cursorDeltaX;
private double cursorDeltaY;
private boolean disableCursor = false;
private MouseInput mouseInput;
private GameRenderer gameRenderer;
private BlockModelManager blockModelManager;
private World world;
Expand Down Expand Up @@ -120,14 +116,15 @@ public void start() {
glfw.windowHint(GLFW.POSITION_Y, (videoMode.height() - INIT_WINDOW_HEIGHT) / 2);
}

window = glfw.createWindow(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT, "freeworld ~: toggle camera", MemorySegment.NULL, MemorySegment.NULL);
window = glfw.createWindow(INIT_WINDOW_WIDTH, INIT_WINDOW_HEIGHT, "freeworld", MemorySegment.NULL, MemorySegment.NULL);
if (Unmarshal.isNullPointer(window)) {
throw new IllegalStateException("Failed to create GLFW window");
}

mouseInput = new MouseInput(window);
CursorPosEvent.DISABLED.subscribe(this::onCursorPosDisabled);
glfw.setKeyCallback(window, (_, key, scancode, action, mods) -> onKey(key, scancode, action, mods));
glfw.setFramebufferSizeCallback(window, (_, width, height) -> onResize(width, height));
glfw.setCursorPosCallback(window, (_, posX, posY) -> onCursorPos(posX, posY));
glfw.setScrollCallback(window, (_, scrollX, scrollY) -> onScroll(scrollX, scrollY));

final Pair.OfInt framebufferSize = glfw.getFramebufferSize(window);
Expand All @@ -139,13 +136,12 @@ public void start() {
}

BlockTypes.bootstrap();
Registries.BLOCK_TYPE.freeze();
EntityTypes.bootstrap();
Registries.ENTITY_TYPE.freeze();

blockModelManager = new BlockModelManager();
blockModelManager.bootstrap();

setScreen(new PauseScreen(this));
world = new World("New world", new Random().nextLong());
player = world.createEntity(EntityTypes.PLAYER, new Vector3d(0.0, 64.0, 0.0));

Expand All @@ -157,23 +153,15 @@ public void start() {

private void onKey(int key, int scancode, int action, int mods) {
switch (action) {
case GLFW.RELEASE -> {
switch (key) {
case GLFW.KEY_GRAVE_ACCENT -> {
disableCursor = !disableCursor;
glfw.setInputMode(window, GLFW.CURSOR, disableCursor ? GLFW.CURSOR_DISABLED : GLFW.CURSOR_NORMAL);
}
}
}
case GLFW.PRESS -> {
switch (key) {
case GLFW.KEY_ESCAPE -> {
if (screen != null) {
if (screen.escapeCanClose()) {
screen.close();
setScreen(null);
}
} else if (world != null) {
openScreen(new PauseScreen(this, null));
setScreen(new PauseScreen(this));
}
}
default -> {
Expand All @@ -190,7 +178,7 @@ private void onKey(int key, int scancode, int action, int mods) {
case GLFW.KEY_8 -> hotBarSelection = 7;
case GLFW.KEY_9 -> hotBarSelection = 8;
case GLFW.KEY_0 -> hotBarSelection = 9;
case GLFW.KEY_E -> openScreen(new CreativeTabScreen(this, null));
case GLFW.KEY_E -> setScreen(new CreativeTabScreen(this));
case GLFW.KEY_SPACE -> {
if (gameTick - spaceTick < 5) {
player.flying = !player.flying();
Expand All @@ -204,6 +192,15 @@ private void onKey(int key, int scancode, int action, int mods) {
}
}
}
if (world != null) {
if (screen == null) {
mouseInput.disable();
} else {
mouseInput.enable();
}
} else {
mouseInput.enable();
}
}
}
}
Expand All @@ -218,25 +215,19 @@ private void onResize(int width, int height) {
}
}

private void onCursorPos(double x, double y) {
cursorDeltaX = x - cursorX;
cursorDeltaY = y - cursorY;
if (disableCursor) {
final double pitch = -cursorDeltaY * MOUSE_SENSITIVITY;
final double yaw = -cursorDeltaX * MOUSE_SENSITIVITY;
final double updateX = Math.clamp(player.rotation().x() + pitch, -90.0, 90.0);
double updateY = player.rotation().y() + yaw;

if (updateY < 0.0) {
updateY += 360.0;
} else if (updateY >= 360.0) {
updateY -= 360.0;
}
private void onCursorPosDisabled(CursorPosEvent event) {
final double pitch = -event.deltaY() * MOUSE_SENSITIVITY;
final double yaw = -event.deltaX() * MOUSE_SENSITIVITY;
final double updateX = Math.clamp(player.rotation().x() + pitch, -90.0, 90.0);
double updateY = player.rotation().y() + yaw;

player.rotation = new Vector2d(updateX, updateY);
if (updateY < 0.0) {
updateY += 360.0;
} else if (updateY >= 360.0) {
updateY -= 360.0;
}
cursorX = x;
cursorY = y;

player.rotation = new Vector2d(updateX, updateY);
}

private void onScroll(double x, double y) {
Expand Down Expand Up @@ -359,9 +350,9 @@ public void close() {
glfw.setErrorCallback(null);
}

public void openScreen(@Nullable Screen screen) {
public void setScreen(@Nullable Screen screen) {
if (this.screen != null) {
this.screen.dispose();
this.screen.onClose();
}
this.screen = screen;
if (screen != null) {
Expand Down Expand Up @@ -409,6 +400,10 @@ public Camera camera() {
return camera;
}

public MouseInput mouseInput() {
return mouseInput;
}

public GameRenderer gameRenderer() {
return gameRenderer;
}
Expand Down
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;
}
}
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<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import freeworld.client.render.world.BlockRenderer;
import freeworld.client.render.world.WorldRenderer;
import freeworld.client.world.chunk.ClientChunk;
import freeworld.core.Identifier;
import freeworld.util.Identifier;
import freeworld.math.Matrix4f;
import freeworld.math.Vector3i;
import freeworld.util.Direction;
Expand Down Expand Up @@ -130,6 +130,18 @@ private GLProgram initBootstrapProgram(GLStateMgr gl, String path, VertexLayout
public void render(GLStateMgr gl, double partialTick) {
gl.clear(GL10C.COLOR_BUFFER_BIT | GL10C.DEPTH_BUFFER_BIT);

if (worldRenderer != null) {
renderWorld(gl, partialTick);
}

gl.clear(GL10C.DEPTH_BUFFER_BIT);
if (client.world() != null) {
renderHud(gl, partialTick);
}
renderGui(gl, partialTick);
}

private void renderWorld(GLStateMgr gl, double partialTick) {
gl.disableBlend();
gl.enableCullFace();
gl.enableDepthTest();
Expand Down Expand Up @@ -182,16 +194,18 @@ public void render(GLStateMgr gl, double partialTick) {
}
tessellator.end(gl);
}
}

gl.clear(GL10C.DEPTH_BUFFER_BIT);

private void renderHud(GLStateMgr gl, double partialTick) {
gl.disableCullFace();
gl.disableDepthTest();
gl.enableBlend();
gl.setBlendFunc(GL10C.SRC_ALPHA, GL10C.ONE_MINUS_SRC_ALPHA);
hudRenderer.update(client.scaledFramebufferWidth(), client.scaledFramebufferHeight());
hudRenderer.render(guiGraphics, gl, partialTick);
}

private void renderGui(GLStateMgr gl, double partialTick) {
gl.disableCullFace();
gl.disableDepthTest();
gl.enableBlend();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import freeworld.client.render.vertex.VertexLayout;
import freeworld.core.Identifier;
import freeworld.util.Identifier;
import freeworld.util.file.BuiltinFiles;
import freeworld.util.Logging;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import freeworld.client.render.gl.GLStateMgr;
import freeworld.client.render.texture.TextureAtlas;
import freeworld.client.render.texture.TextureManager;
import freeworld.core.Identifier;
import freeworld.core.registry.Registries;
import freeworld.util.Identifier;
import freeworld.registry.Registries;
import freeworld.math.Matrix4f;
import freeworld.world.block.BlockType;
import overrungl.opengl.GL10C;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package freeworld.client.render.model;

import freeworld.core.Identifier;
import freeworld.util.Identifier;

/**
* @author squid233
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package freeworld.client.render.model.block;

import freeworld.core.Identifier;
import freeworld.util.Identifier;

import java.util.List;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

package freeworld.client.render.model.block;

import freeworld.core.Identifier;
import freeworld.util.Identifier;
import freeworld.math.Vector2f;
import freeworld.util.Direction;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
package freeworld.client.render.model.block;

import freeworld.client.render.texture.Texture;
import freeworld.core.Identifier;
import freeworld.core.registry.DefaultedRegistry;
import freeworld.core.registry.Registries;
import freeworld.core.registry.Registry;
import freeworld.util.Identifier;
import freeworld.registry.DefaultedRegistry;
import freeworld.registry.Registries;
import freeworld.registry.Registry;
import freeworld.world.block.BlockType;
import freeworld.world.block.BlockTypes;

Expand Down
Loading

0 comments on commit 961e424

Please sign in to comment.