Skip to content

Commit 0cc5322

Browse files
committed
Finish implementing mouse inputs in VanillaScreenProxy
1 parent 47cb1d3 commit 0cc5322

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

smylib/fabric/src/main/java/net/smyler/smylib/gui/screen/VanillaScreenProxy.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88

99
import static com.google.common.base.Preconditions.checkState;
1010
import static java.lang.Math.round;
11+
import static java.lang.System.currentTimeMillis;
1112
import static net.smyler.smylib.SmyLib.getGameClient;
1213

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

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

2226
@Override
2327
public void render(GuiGraphics guiGraphics, int x, int y, float partialTicks) {
24-
2528
GameClient game = getGameClient();
2629
WrappedGuiGraphics uiDrawContext = (WrappedGuiGraphics) game.guiDrawContext();
2730
checkState(
@@ -57,25 +60,36 @@ public boolean isPauseScreen() {
5760
return this.screen.shouldPauseGame();
5861
}
5962

60-
//FIXME mouse and keyboard support in screen proxy
61-
6263
@Override
6364
public boolean mouseClicked(double x, double y, int button) {
64-
//TODO implement double clicks
65-
this.screen.onClick((float)x, (float)y, button, null);
65+
float xf = (float) x;
66+
float yf = (float) y;
67+
boolean hasMoved = this.lastClickX[button] != xf || this.lastClickY[button] != yf;
68+
long ct = currentTimeMillis();
69+
long dt = ct - lastClickTimesMs[button];
70+
if (dt < 500 && !hasMoved) {
71+
this.screen.onDoubleClick(xf, yf, button, null);
72+
} else {
73+
this.screen.onClick(xf, yf, button, null);
74+
}
75+
this.lastClickTimesMs[button] = ct;
76+
this.lastClickX[button] = xf;
77+
this.lastClickY[button] = yf;
6678
return super.mouseClicked(x, y, button);
6779
}
6880

6981
@Override
7082
public boolean mouseReleased(double x, double y, int button) {
71-
this.screen.onMouseReleased((float)x, (float)y, button, null); //FIXME track dragged widget
83+
this.screen.onMouseReleased((float)x, (float)y, button, null);
7284
return super.mouseReleased(x, y, button);
7385
}
7486

7587
@Override
7688
public boolean mouseDragged(double x, double y, int button, double dX, double dY) {
77-
//TODO verify arguments are what they seem
78-
this.screen.onMouseDragged((float)x, (float)y, (float)dX, (float) dY, button, null, 0); //FIXME provide dt
89+
long ct = currentTimeMillis();
90+
long dt = ct - this.lastClickTimesMs[button];
91+
this.lastClickTimesMs[button] = ct;
92+
this.screen.onMouseDragged((float)x, (float)y, (float)dX, (float) dY, button, null, dt);
7993
return super.mouseDragged(x, y, button, dX, dY);
8094
}
8195

@@ -103,7 +117,7 @@ public void mouseMoved(double d, double e) {
103117
}
104118

105119
public net.smyler.smylib.gui.screen.Screen getScreen() {
106-
return screen;
120+
return this.screen;
107121
}
108122

109123
private void drawBackground(GuiGraphics guiGraphics) {

0 commit comments

Comments
 (0)