Skip to content

Commit

Permalink
Fixed mouse input
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Nov 27, 2023
1 parent f372004 commit 7d37f6c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
22 changes: 20 additions & 2 deletions src/main/java/dev/hilligans/ourcraft/Ourcraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.hilligans.ourcraft.client.input.handler.providers.MouseHandlerProvider;
import dev.hilligans.ourcraft.client.input.Input;
import dev.hilligans.ourcraft.client.input.RepeatingInput;
import dev.hilligans.ourcraft.client.input.handlers.MouseHandler;
import dev.hilligans.ourcraft.client.rendering.graphics.fixedfunctiongl.FixedFunctionGLEngine;
import dev.hilligans.ourcraft.client.rendering.graphics.implementations.WorldCamera;
import dev.hilligans.ourcraft.client.rendering.graphics.opengl.OpenGLEngine;
Expand Down Expand Up @@ -52,6 +53,7 @@
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.BiConsumer;

import static org.lwjgl.glfw.GLFW.*;

Expand Down Expand Up @@ -187,7 +189,7 @@ public static void registerDefaultContent(ModContent modContent) {
Protocols.register(modContent);

if (modContent.getGameInstance().side.equals(Side.CLIENT)) {
modContent.registerKeybinds(new Input("ourcraft:mouse_button_handler::0") {
modContent.registerKeybinds(new Input("ourcraft:mouse_handler::0") {
@Override
public void press(RenderWindow renderWindow, float strength) {
Client client = renderWindow.getClient();
Expand Down Expand Up @@ -220,7 +222,23 @@ public void press(RenderWindow renderWindow, float strength) {
Client client = renderWindow.getClient();
client.playerData.f3 = !client.playerData.f3;
}
});
}.onlyWithPipelines("ourcraft:new_world_pipeline"));

modContent.registerKeybinds(new Input("ourcraft:mouse_handler::" + MouseHandler.MOUSE_X) {
@Override
public void press(RenderWindow window, float strength) {
window.getCamera().addRotation(0, strength/100);
GLFW.glfwSetCursorPos(window.getWindowID(), window.getWindowWidth()/2,window.getWindowHeight()/2);
}
}.onlyWithPipelines("ourcraft:new_world_pipeline"));

modContent.registerKeybinds(new Input("ourcraft:mouse_handler::" + MouseHandler.MOUSE_Y) {
@Override
public void press(RenderWindow window, float strength) {
window.getCamera().addRotation(-strength/100,0);
GLFW.glfwSetCursorPos(window.getWindowID(), window.getWindowWidth()/2,window.getWindowHeight()/2);
}
}.onlyWithPipelines("ourcraft:new_world_pipeline"));

modContent.registerKeybinds(new RepeatingInput("ourcraft:key_press_handler::" + GLFW_KEY_W,
(window, strength) -> window.getCamera().moveForward(5f * strength)).onlyWithPipelines("ourcraft:new_world_pipeline"));
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/dev/hilligans/ourcraft/client/input/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Input implements IRegistryElement {
public String key;
public String modID;
public int[] keyBinds;
public String[] pipelines = new String[]{};
public String[] pipelines = null;

public ArrayList<String> boundKey = new ArrayList<>(1);

Expand All @@ -33,6 +33,18 @@ public Input(String defaultBind, boolean repeating) {
this.repeating = repeating;
}

public boolean canInput(String pipeline) {
if(pipelines == null) {
return true;
}
for(String s : pipelines) {
if(pipeline.equals(s)) {
return true;
}
}
return false;
}

public void press(RenderWindow renderWindow, float strength) {}

public void repeat(RenderWindow renderWindow, float strength) {}
Expand Down Expand Up @@ -78,10 +90,10 @@ public String getDisplay() {
return displayName;
}

public void process(InputHandler inputHandler, int input, float mode, RenderWindow window, int action) {
public void process(InputHandler inputHandler, int input, float mode, RenderWindow window, int action, float strength) {
//TODO fix
if(action == GLFW_PRESS) {
press(window, mode);
press(window, strength);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ public void handleInput(int input, int mode, long windowID, int action, int scan
ArrayList<Input> inputs = this.inputs[input];
if(inputs != null) {
for(Input input1 : inputs) {
input1.process(this,input,mode,window,action);
if(input1.canInput(window.renderPipeline.getIdentifierName())) {
input1.process(this, input, mode, window, action, strength);
}
}
}
}
Expand All @@ -90,7 +92,9 @@ public void tick(float deltaTime) {
if(strength != 0) {
ArrayList<Input> inputs1 = repeatingInputs[val];
for (Input input : inputs1) {
input.repeat(window,strength * deltaTime);
if(input.canInput(window.renderPipeline.getIdentifierName())) {
input.repeat(window, strength * deltaTime);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.hilligans.ourcraft.client.rendering.graphics.api.IInputProvider;
import dev.hilligans.ourcraft.client.rendering.graphics.RenderWindow;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWCursorPosCallback;
import org.lwjgl.glfw.GLFWMouseButtonCallback;

public class MouseHandler implements IInputProvider {
Expand All @@ -23,11 +24,25 @@ public void invoke(long window, int button, int action, int mods) {
handler.handleInput(button,action,window,action, mouse);
}
});

GLFW.glfwSetCursorPosCallback(window.getWindowID(), new GLFWCursorPosCallback() {
@Override
public void invoke(long w, double xpos, double ypos) {
xpos -= window.getWindowWidth() / 2;
ypos -= window.getWindowHeight() / 2;
if(xpos != 0) {
handler.handleInput(MOUSE_X, 2, w, 1,0,0, (float) xpos, mouse);
}
if(ypos != 0) {
handler.handleInput(MOUSE_Y, 2, w, 1,0,0, (float) ypos, mouse);
}
}
});
}

@Override
public int getSize() {
return GLFW.GLFW_MOUSE_BUTTON_LAST;
return GLFW.GLFW_MOUSE_BUTTON_LAST + 3;
}

@Override
Expand Down Expand Up @@ -55,16 +70,34 @@ public String getButtonName(int button, int extra) {
if(button == GLFW.GLFW_MOUSE_BUTTON_MIDDLE) {
return "Mouse Button Middle";
}
if(button == MOUSE_X) {
return "Mouse X";
}
if(button == MOUSE_Y) {
return "Mouse Y";
}
return "Mouse Button " + button;
}

@Override
public String getResourceName() {
return "mouse_button_handler";
return "mouse_handler";
}

@Override
public String getResourceOwner() {
return "ourcraft";
}

public static int MOUSE_BUTTON_1 = 0,
MOUSE_BUTTON_2 = 1,
MOUSE_BUTTON_3 = 2,
MOUSE_BUTTON_4 = 3,
MOUSE_BUTTON_5 = 4,
MOUSE_BUTTON_6 = 5,
MOUSE_BUTTON_7 = 6,
MOUSE_BUTTON_8 = 7,
MOUSE_X = 8,
MOUSE_Y = 9;

}

0 comments on commit 7d37f6c

Please sign in to comment.