Only show cursor when controller is active

This commit is contained in:
PeytonPlayz595 2024-07-08 09:13:48 -07:00
parent 48f8a49dd3
commit 204b25528e
3 changed files with 25 additions and 8 deletions

View File

@ -38,6 +38,7 @@ public class Controller {
private static ButtonState[] button = new ButtonState[30]; private static ButtonState[] button = new ButtonState[30];
public static HTMLImageElement cursor; public static HTMLImageElement cursor;
public static boolean isActive = false;
public static final int getDX() { public static final int getDX() {
if(dx < 0.0) { if(dx < 0.0) {
@ -158,6 +159,10 @@ public class Controller {
} }
} }
if(getDX() > 0 || getDY() > 0 || getDX() < 0 || getDY() < 0) {
isActive = true;
}
forward = axes[1] < -threshold; forward = axes[1] < -threshold;
backwards = axes[1] > threshold; backwards = axes[1] > threshold;
left = axes[0] < -threshold; left = axes[0] < -threshold;
@ -174,9 +179,11 @@ public class Controller {
if(button[i] == ButtonState.PRESSED) { if(button[i] == ButtonState.PRESSED) {
button[i] = ButtonState.HELD; button[i] = ButtonState.HELD;
isActive = true;
} else { } else {
if(!(button[i] == ButtonState.HELD)) { if(!(button[i] == ButtonState.HELD)) {
button[i] = ButtonState.PRESSED; button[i] = ButtonState.PRESSED;
isActive = true;
} }
} }
} else if(!gamePad.getButtons()[i].isPressed() && index == activeController) { } else if(!gamePad.getButtons()[i].isPressed() && index == activeController) {
@ -252,6 +259,7 @@ public class Controller {
@Override @Override
public void handleEvent(GamepadEvent arg0) { public void handleEvent(GamepadEvent arg0) {
connectedControllers.add(arg0.getGamepad().getIndex()); connectedControllers.add(arg0.getGamepad().getIndex());
isActive = true;
System.out.println("Controller connected!"); System.out.println("Controller connected!");
} }
}); });
@ -263,6 +271,7 @@ public class Controller {
if(connectedControllers.contains(index)) { if(connectedControllers.contains(index)) {
connectedControllers.remove(index); connectedControllers.remove(index);
resetButtonStates(); resetButtonStates();
isActive = false;
System.out.println("Controller disconnected!"); System.out.println("Controller disconnected!");
} }
} }

View File

@ -129,6 +129,7 @@ public class PlatformInput {
int b = evt.getButton(); int b = evt.getButton();
buttonStates[b == 1 ? 2 : (b == 2 ? 1 : b)] = true; buttonStates[b == 1 ? 2 : (b == 2 ? 1 : b)] = true;
mouseEvents.add(evt); mouseEvents.add(evt);
Controller.isActive = false;
} }
}); });
canvas.addEventListener("mouseup", mouseup = new EventListener<MouseEvent>() { canvas.addEventListener("mouseup", mouseup = new EventListener<MouseEvent>() {
@ -139,6 +140,7 @@ public class PlatformInput {
int b = evt.getButton(); int b = evt.getButton();
buttonStates[b == 1 ? 2 : (b == 2 ? 1 : b)] = false; buttonStates[b == 1 ? 2 : (b == 2 ? 1 : b)] = false;
mouseEvents.add(evt); mouseEvents.add(evt);
Controller.isActive = false;
} }
}); });
canvas.addEventListener("mousemove", mousemove = new EventListener<MouseEvent>() { canvas.addEventListener("mousemove", mousemove = new EventListener<MouseEvent>() {
@ -153,18 +155,21 @@ public class PlatformInput {
if(hasBeenActive()) { if(hasBeenActive()) {
mouseEvents.add(evt); mouseEvents.add(evt);
} }
Controller.isActive = false;
} }
}); });
canvas.addEventListener("mouseenter", mouseenter = new EventListener<MouseEvent>() { canvas.addEventListener("mouseenter", mouseenter = new EventListener<MouseEvent>() {
@Override @Override
public void handleEvent(MouseEvent evt) { public void handleEvent(MouseEvent evt) {
isMouseOverWindow = true; isMouseOverWindow = true;
Controller.isActive = false;
} }
}); });
canvas.addEventListener("mouseleave", mouseleave = new EventListener<MouseEvent>() { canvas.addEventListener("mouseleave", mouseleave = new EventListener<MouseEvent>() {
@Override @Override
public void handleEvent(MouseEvent evt) { public void handleEvent(MouseEvent evt) {
isMouseOverWindow = false; isMouseOverWindow = false;
Controller.isActive = false;
} }
}); });
win.addEventListener("keydown", keydown = new EventListener<KeyboardEvent>() { win.addEventListener("keydown", keydown = new EventListener<KeyboardEvent>() {
@ -180,6 +185,7 @@ public class PlatformInput {
int ww = processFunctionKeys(w); int ww = processFunctionKeys(w);
keyStates[KeyboardConstants.getEaglerKeyFromBrowser(ww, ww == w ? evt.getLocation() : 0)] = true; keyStates[KeyboardConstants.getEaglerKeyFromBrowser(ww, ww == w ? evt.getLocation() : 0)] = true;
keyEvents.add(evt); keyEvents.add(evt);
Controller.isActive = false;
} }
}); });
win.addEventListener("keyup", keyup = new EventListener<KeyboardEvent>() { win.addEventListener("keyup", keyup = new EventListener<KeyboardEvent>() {
@ -198,6 +204,7 @@ public class PlatformInput {
} }
} }
keyEvents.add(evt); keyEvents.add(evt);
Controller.isActive = false;
} }
}); });
win.addEventListener("keypress", keypress = new EventListener<KeyboardEvent>() { win.addEventListener("keypress", keypress = new EventListener<KeyboardEvent>() {
@ -205,6 +212,7 @@ public class PlatformInput {
public void handleEvent(KeyboardEvent evt) { public void handleEvent(KeyboardEvent evt) {
evt.preventDefault(); evt.preventDefault();
evt.stopPropagation(); evt.stopPropagation();
Controller.isActive = false;
if(enableRepeatEvents && evt.isRepeat()) keyEvents.add(evt); if(enableRepeatEvents && evt.isRepeat()) keyEvents.add(evt);
} }
}); });
@ -215,6 +223,7 @@ public class PlatformInput {
evt.stopPropagation(); evt.stopPropagation();
mouseEvents.add(evt); mouseEvents.add(evt);
mouseDWheel += evt.getDeltaY(); mouseDWheel += evt.getDeltaY();
Controller.isActive = false;
} }
}); });
win.addEventListener("blur", new EventListener<WheelEvent>() { win.addEventListener("blur", new EventListener<WheelEvent>() {
@ -227,12 +236,14 @@ public class PlatformInput {
for(int i = 0; i < keyStates.length; ++i) { for(int i = 0; i < keyStates.length; ++i) {
keyStates[i] = false; keyStates[i] = false;
} }
Controller.isActive = false;
} }
}); });
win.addEventListener("focus", new EventListener<WheelEvent>() { win.addEventListener("focus", new EventListener<WheelEvent>() {
@Override @Override
public void handleEvent(WheelEvent evt) { public void handleEvent(WheelEvent evt) {
isWindowFocused = true; isWindowFocused = true;
Controller.isActive = false;
} }
}); });
win.getDocument().addEventListener("pointerlockchange", pointerlock = new EventListener<WheelEvent>() { win.getDocument().addEventListener("pointerlockchange", pointerlock = new EventListener<WheelEvent>() {
@ -252,6 +263,7 @@ public class PlatformInput {
}, 60); }, 60);
mouseDX = 0.0D; mouseDX = 0.0D;
mouseDY = 0.0D; mouseDY = 0.0D;
Controller.isActive = false;
} }
}); });

View File

@ -1300,14 +1300,10 @@ public class Minecraft extends ModData implements IThreadListener {
Controller.tick(); Controller.tick();
if(this.currentScreen != null) { if(this.currentScreen != null && Controller.isActive && Controller.cursor == null) {
if(Controller.cursor == null) { Controller.addCursor(0, 0);
Controller.addCursor(0, 0); } else if(Controller.cursor != null) {
} Controller.removeCursor();
} else {
if(Controller.cursor != null) {
Controller.removeCursor();
}
} }
if (this.rightClickDelayTimer > 0) { if (this.rightClickDelayTimer > 0) {