Skip to content

Commit

Permalink
Only show cursor when controller is active
Browse files Browse the repository at this point in the history
  • Loading branch information
PeytonPlayz595 committed Jul 8, 2024
1 parent 48f8a49 commit 204b255
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class Controller {
private static ButtonState[] button = new ButtonState[30];

public static HTMLImageElement cursor;
public static boolean isActive = false;

public static final int getDX() {
if(dx < 0.0) {
Expand Down Expand Up @@ -158,6 +159,10 @@ private static void updateAxes(Gamepad gamePad) {
}
}

if(getDX() > 0 || getDY() > 0 || getDX() < 0 || getDY() < 0) {
isActive = true;
}

forward = axes[1] < -threshold;
backwards = axes[1] > threshold;
left = axes[0] < -threshold;
Expand All @@ -174,9 +179,11 @@ private static void updateButtons(Gamepad gamePad, int index) {

if(button[i] == ButtonState.PRESSED) {
button[i] = ButtonState.HELD;
isActive = true;
} else {
if(!(button[i] == ButtonState.HELD)) {
button[i] = ButtonState.PRESSED;
isActive = true;
}
}
} else if(!gamePad.getButtons()[i].isPressed() && index == activeController) {
Expand Down Expand Up @@ -252,6 +259,7 @@ private static enum ButtonState {
@Override
public void handleEvent(GamepadEvent arg0) {
connectedControllers.add(arg0.getGamepad().getIndex());
isActive = true;
System.out.println("Controller connected!");
}
});
Expand All @@ -263,6 +271,7 @@ public void handleEvent(GamepadEvent arg0) {
if(connectedControllers.contains(index)) {
connectedControllers.remove(index);
resetButtonStates();
isActive = false;
System.out.println("Controller disconnected!");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public void handleEvent(MouseEvent evt) {
int b = evt.getButton();
buttonStates[b == 1 ? 2 : (b == 2 ? 1 : b)] = true;
mouseEvents.add(evt);
Controller.isActive = false;
}
});
canvas.addEventListener("mouseup", mouseup = new EventListener<MouseEvent>() {
Expand All @@ -139,6 +140,7 @@ public void handleEvent(MouseEvent evt) {
int b = evt.getButton();
buttonStates[b == 1 ? 2 : (b == 2 ? 1 : b)] = false;
mouseEvents.add(evt);
Controller.isActive = false;
}
});
canvas.addEventListener("mousemove", mousemove = new EventListener<MouseEvent>() {
Expand All @@ -153,18 +155,21 @@ public void handleEvent(MouseEvent evt) {
if(hasBeenActive()) {
mouseEvents.add(evt);
}
Controller.isActive = false;
}
});
canvas.addEventListener("mouseenter", mouseenter = new EventListener<MouseEvent>() {
@Override
public void handleEvent(MouseEvent evt) {
isMouseOverWindow = true;
Controller.isActive = false;
}
});
canvas.addEventListener("mouseleave", mouseleave = new EventListener<MouseEvent>() {
@Override
public void handleEvent(MouseEvent evt) {
isMouseOverWindow = false;
Controller.isActive = false;
}
});
win.addEventListener("keydown", keydown = new EventListener<KeyboardEvent>() {
Expand All @@ -180,6 +185,7 @@ public void handleEvent(KeyboardEvent evt) {
int ww = processFunctionKeys(w);
keyStates[KeyboardConstants.getEaglerKeyFromBrowser(ww, ww == w ? evt.getLocation() : 0)] = true;
keyEvents.add(evt);
Controller.isActive = false;
}
});
win.addEventListener("keyup", keyup = new EventListener<KeyboardEvent>() {
Expand All @@ -198,13 +204,15 @@ public void handleEvent(KeyboardEvent evt) {
}
}
keyEvents.add(evt);
Controller.isActive = false;
}
});
win.addEventListener("keypress", keypress = new EventListener<KeyboardEvent>() {
@Override
public void handleEvent(KeyboardEvent evt) {
evt.preventDefault();
evt.stopPropagation();
Controller.isActive = false;
if(enableRepeatEvents && evt.isRepeat()) keyEvents.add(evt);
}
});
Expand All @@ -215,6 +223,7 @@ public void handleEvent(WheelEvent evt) {
evt.stopPropagation();
mouseEvents.add(evt);
mouseDWheel += evt.getDeltaY();
Controller.isActive = false;
}
});
win.addEventListener("blur", new EventListener<WheelEvent>() {
Expand All @@ -227,12 +236,14 @@ public void handleEvent(WheelEvent evt) {
for(int i = 0; i < keyStates.length; ++i) {
keyStates[i] = false;
}
Controller.isActive = false;
}
});
win.addEventListener("focus", new EventListener<WheelEvent>() {
@Override
public void handleEvent(WheelEvent evt) {
isWindowFocused = true;
Controller.isActive = false;
}
});
win.getDocument().addEventListener("pointerlockchange", pointerlock = new EventListener<WheelEvent>() {
Expand All @@ -252,6 +263,7 @@ public void onTimer() {
}, 60);
mouseDX = 0.0D;
mouseDY = 0.0D;
Controller.isActive = false;
}
});

Expand Down
12 changes: 4 additions & 8 deletions src/teavm/java/net/minecraft/client/Minecraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -1300,14 +1300,10 @@ public void runTick() throws IOException {

Controller.tick();

if(this.currentScreen != null) {
if(Controller.cursor == null) {
Controller.addCursor(0, 0);
}
} else {
if(Controller.cursor != null) {
Controller.removeCursor();
}
if(this.currentScreen != null && Controller.isActive && Controller.cursor == null) {
Controller.addCursor(0, 0);
} else if(Controller.cursor != null) {
Controller.removeCursor();
}

if (this.rightClickDelayTimer > 0) {
Expand Down

0 comments on commit 204b255

Please sign in to comment.