From af42bbe2fa164e60b365752cff7e2f8ff5d72a95 Mon Sep 17 00:00:00 2001 From: PeytonPlayz595 <106421860+PeytonPlayz595@users.noreply.github.com> Date: Sun, 7 Jul 2024 20:03:09 -0700 Subject: [PATCH] Fix cursor movement using controller --- .../shadow/input/Controller.java | 59 ++++++++----------- .../v1_8/internal/PlatformOpenGL.java | 2 +- 2 files changed, 24 insertions(+), 37 deletions(-) diff --git a/src/teavm/java/net/PeytonPlayz585/shadow/input/Controller.java b/src/teavm/java/net/PeytonPlayz585/shadow/input/Controller.java index a973eac..9dbbe31 100644 --- a/src/teavm/java/net/PeytonPlayz585/shadow/input/Controller.java +++ b/src/teavm/java/net/PeytonPlayz585/shadow/input/Controller.java @@ -11,6 +11,7 @@ import org.teavm.jso.dom.html.HTMLImageElement; import org.teavm.jso.gamepad.GamepadEvent; import net.lax1dude.eaglercraft.v1_8.Display; +import net.lax1dude.eaglercraft.v1_8.internal.PlatformRuntime; import org.teavm.jso.gamepad.Gamepad; @@ -29,6 +30,7 @@ public class Controller { private static double threshold = 0.3; //Fixes 'slight' issues of stick drift people have been complaining about private static double cameraThreshold = 4.0; + private static double cursorThreshold = 0.4; private static int activeController = -1; private static ButtonState[] states = new ButtonState[30]; @@ -142,35 +144,16 @@ public class Controller { dy = -axes[3] * multiplier; if(cursor != null) { - double dx1; - double dy1 = dy; + int dx1 = getDX(); + int dy1 = getDY(); - if(dx1 < 0.0) { - if(dx1 < -cameraThreshold) { - dx1 = dx; - } - } else if(dx > 0.0) { - if(dx > cameraThreshold) { - dx1 = dx; - } - } else { - dx1 = 0; + if(dx1 > 0 || dx1 < 0) { + updateCursorX(dx1); } - if(dy1 < 0.0) { - if(dy1 < -cameraThreshold) { - dy1 = dy; - } - } else if(dy1 > 0.0) { - if(dy1 > cameraThreshold) { - dy1 = dy; - } - } else { - dy1 = 0; + if(dy1 > 0 || dy1 < 0) { + updateCursorY(-dy1); } - - - updateCursor((int)dx1, (int)dy1); } forward = axes[1] < -threshold; @@ -213,10 +196,11 @@ public class Controller { cursor.setAttribute("id", "cursor"); cursor.setSrc(cursorSrc); cursor.setAttribute("draggable", "false"); - cursor.getStyle().setProperty("left", "0px"); - cursor.getStyle().setProperty("top", "0px"); - cursor.getStyle().setProperty("width", "auto"); - cursor.getStyle().setProperty("height", "auto"); + cursor.getStyle().setProperty("position", "fixed"); + cursor.getStyle().setProperty("top", "0"); + cursor.getStyle().setProperty("left", "0"); + cursor.getStyle().setProperty("width", "auto"); + cursor.getStyle().setProperty("height", "auto"); HTMLBodyElement body = (HTMLBodyElement) Window.current().getDocument().getBody(); body.appendChild(cursor); @@ -229,15 +213,18 @@ public class Controller { cursor = null; } - private static void updateCursor(int dx, int dy) { - int dx1 = cursor.getAbsoluteLeft() + dx; - int dy1 = cursor.getAbsoluteTop() + dy; + private static void updateCursorX(int x) { + int newX = cursor.getOffsetLeft() + x; - int x = Math.min(Math.max(dx1, 0), Display.getWidth() - cursor.getWidth()); - int y = Math.min(Math.max(dy1, 0), Display.getHeight() - cursor.getHeight()); + newX = Math.max(0, Math.min(newX, PlatformRuntime.canvas.getClientWidth() - cursor.getWidth())); + cursor.getStyle().setProperty("left", Integer.toString(newX) + "px"); + } + + private static void updateCursorY(int y) { + int newY = cursor.getOffsetTop() + y; - cursor.getStyle().setProperty("left", dx1 + "px"); - cursor.getStyle().setProperty("top", dy1 + "px"); + newY = Math.max(0, Math.min(newY, PlatformRuntime.canvas.getClientHeight() - cursor.getHeight())); + cursor.getStyle().setProperty("top", Integer.toString(newY) + "px"); } private static Gamepad getGamepad(int index) { diff --git a/src/teavm/java/net/lax1dude/eaglercraft/v1_8/internal/PlatformOpenGL.java b/src/teavm/java/net/lax1dude/eaglercraft/v1_8/internal/PlatformOpenGL.java index 9398c09..cbe7ea8 100644 --- a/src/teavm/java/net/lax1dude/eaglercraft/v1_8/internal/PlatformOpenGL.java +++ b/src/teavm/java/net/lax1dude/eaglercraft/v1_8/internal/PlatformOpenGL.java @@ -33,7 +33,7 @@ public class PlatformOpenGL { private static final Logger logger = LogManager.getLogger("PlatformOpenGL"); - static WebGL2RenderingContext ctx = null; + public static WebGL2RenderingContext ctx = null; static boolean hasDebugRenderInfoExt = false; static boolean hasFramebufferHDR16FSupport = false;