Skip to content

Commit

Permalink
Finish implementing mouse inputs in VanillaScreenProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jul 28, 2024
1 parent 47cb1d3 commit 0cc5322
Showing 1 changed file with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@

import static com.google.common.base.Preconditions.checkState;
import static java.lang.Math.round;
import static java.lang.System.currentTimeMillis;
import static net.smyler.smylib.SmyLib.getGameClient;

public class VanillaScreenProxy extends net.minecraft.client.gui.screens.Screen {
private final Screen screen;
private final long[] lastClickTimesMs = new long[getGameClient().mouse().getButtonCount()];
private final float[] lastClickX = new float[getGameClient().mouse().getButtonCount()];
private final float[] lastClickY = new float[getGameClient().mouse().getButtonCount()];

public VanillaScreenProxy(Screen screen) {
super(Component.literal("SmyLib screen"));
Expand All @@ -21,7 +25,6 @@ public VanillaScreenProxy(Screen screen) {

@Override
public void render(GuiGraphics guiGraphics, int x, int y, float partialTicks) {

GameClient game = getGameClient();
WrappedGuiGraphics uiDrawContext = (WrappedGuiGraphics) game.guiDrawContext();
checkState(
Expand Down Expand Up @@ -57,25 +60,36 @@ public boolean isPauseScreen() {
return this.screen.shouldPauseGame();
}

//FIXME mouse and keyboard support in screen proxy

@Override
public boolean mouseClicked(double x, double y, int button) {
//TODO implement double clicks
this.screen.onClick((float)x, (float)y, button, null);
float xf = (float) x;
float yf = (float) y;
boolean hasMoved = this.lastClickX[button] != xf || this.lastClickY[button] != yf;
long ct = currentTimeMillis();
long dt = ct - lastClickTimesMs[button];
if (dt < 500 && !hasMoved) {
this.screen.onDoubleClick(xf, yf, button, null);
} else {
this.screen.onClick(xf, yf, button, null);
}
this.lastClickTimesMs[button] = ct;
this.lastClickX[button] = xf;
this.lastClickY[button] = yf;
return super.mouseClicked(x, y, button);
}

@Override
public boolean mouseReleased(double x, double y, int button) {
this.screen.onMouseReleased((float)x, (float)y, button, null); //FIXME track dragged widget
this.screen.onMouseReleased((float)x, (float)y, button, null);
return super.mouseReleased(x, y, button);
}

@Override
public boolean mouseDragged(double x, double y, int button, double dX, double dY) {
//TODO verify arguments are what they seem
this.screen.onMouseDragged((float)x, (float)y, (float)dX, (float) dY, button, null, 0); //FIXME provide dt
long ct = currentTimeMillis();
long dt = ct - this.lastClickTimesMs[button];
this.lastClickTimesMs[button] = ct;
this.screen.onMouseDragged((float)x, (float)y, (float)dX, (float) dY, button, null, dt);
return super.mouseDragged(x, y, button, dX, dY);
}

Expand Down Expand Up @@ -103,7 +117,7 @@ public void mouseMoved(double d, double e) {
}

public net.smyler.smylib.gui.screen.Screen getScreen() {
return screen;
return this.screen;
}

private void drawBackground(GuiGraphics guiGraphics) {
Expand Down

0 comments on commit 0cc5322

Please sign in to comment.