Skip to content

Commit

Permalink
Fix cursor movement using controller
Browse files Browse the repository at this point in the history
  • Loading branch information
PeytonPlayz595 committed Jul 8, 2024
1 parent 54806c3 commit af42bbe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 37 deletions.
59 changes: 23 additions & 36 deletions src/teavm/java/net/PeytonPlayz585/shadow/input/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;

Expand All @@ -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];
Expand Down Expand Up @@ -142,35 +144,16 @@ private static void updateAxes(Gamepad gamePad) {
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;
Expand Down Expand Up @@ -213,10 +196,11 @@ public static void addCursor(int x, int y) {
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);
Expand All @@ -229,15 +213,18 @@ public static void removeCursor() {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit af42bbe

Please sign in to comment.