| 1 | package net.minecraft.client;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.Blaze3D;
|
| 4 | import com.mojang.blaze3d.platform.InputConstants;
|
| 5 | import com.mojang.blaze3d.platform.Window;
|
| 6 | import com.mojang.logging.LogUtils;
|
| 7 | import java.nio.FloatBuffer;
|
| 8 | import java.nio.IntBuffer;
|
| 9 | import java.nio.file.InvalidPathException;
|
| 10 | import java.nio.file.Path;
|
| 11 | import java.nio.file.Paths;
|
| 12 | import java.util.ArrayList;
|
| 13 | import java.util.List;
|
| 14 | import java.util.Locale;
|
| 15 | import net.fabricmc.api.EnvType;
|
| 16 | import net.fabricmc.api.Environment;
|
| 17 | import net.minecraft.CrashReport;
|
| 18 | import net.minecraft.CrashReportCategory;
|
| 19 | import net.minecraft.ReportedException;
|
| 20 | import net.minecraft.client.gui.Font;
|
| 21 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 22 | import net.minecraft.client.gui.components.toasts.SystemToast;
|
| 23 | import net.minecraft.client.gui.screens.Screen;
|
| 24 | import net.minecraft.client.input.InputQuirks;
|
| 25 | import net.minecraft.client.input.MouseButtonEvent;
|
| 26 | import net.minecraft.client.input.MouseButtonInfo;
|
| 27 | import net.minecraft.util.Mth;
|
| 28 | import net.minecraft.util.SmoothDouble;
|
| 29 | import net.minecraft.util.Util;
|
| 30 | import net.minecraft.world.entity.player.Inventory;
|
| 31 | import org.joml.Vector2i;
|
| 32 | import org.jspecify.annotations.Nullable;
|
| 33 | import org.lwjgl.sdl.SDLMouse;
|
| 34 | import org.lwjgl.sdl.SDLVideo;
|
| 35 | import org.lwjgl.system.MemoryStack;
|
| 36 | import org.slf4j.Logger;
|
| 37 |
|
| 38 | @Environment(EnvType.CLIENT)
|
| 39 | public class MouseHandler {
|
| 40 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 41 | public static final long DOUBLE_CLICK_THRESHOLD_MS = 250L;
|
| 42 | private final Minecraft minecraft;
|
| 43 | private boolean isLeftPressed;
|
| 44 | private boolean isMiddlePressed;
|
| 45 | private boolean isRightPressed;
|
| 46 | private double xpos;
|
| 47 | private double ypos;
|
| 48 | private MouseHandler.@Nullable LastClick lastClick;
|
| 49 | protected @MouseButtonInfo.MouseButton int lastClickButton;
|
| 50 | private @Nullable MouseButtonInfo activeButton = null;
|
| 51 | private boolean ignoreFirstMove = true;
|
| 52 | private double mousePressedTime;
|
| 53 | private final SmoothDouble smoothTurnX = new SmoothDouble();
|
| 54 | private final SmoothDouble smoothTurnY = new SmoothDouble();
|
| 55 | private double accumulatedDX;
|
| 56 | private double accumulatedDY;
|
| 57 | private final ScrollWheelHandler scrollWheelHandler;
|
| 58 | private double lastHandleMovementTime = Double.MIN_VALUE;
|
| 59 | private boolean mouseGrabbed;
|
| 60 |
|
| 61 | public MouseHandler(final Minecraft minecraft) {
|
| 62 | this.minecraft = minecraft;
|
| 63 | this.scrollWheelHandler = new ScrollWheelHandler();
|
| 64 | }
|
| 65 |
|
| 66 | public void onButton(final long handle, final MouseButtonInfo rawButtonInfo, final @MouseButtonInfo.Action int action) {
|
| 67 | Window window = this.minecraft.getWindow();
|
| 68 | if (handle != 0L && handle == window.handle()) {
|
| 69 | this.minecraft.getFramerateLimitTracker().onInputReceived();
|
| 70 | if (this.minecraft.gui.screen() != null) {
|
| 71 | this.minecraft.setLastInputType(InputType.MOUSE);
|
| 72 | }
|
| 73 |
|
| 74 | boolean pressed = action == 1;
|
| 75 | if (pressed) {
|
| 76 | this.activeButton = rawButtonInfo;
|
| 77 | this.mousePressedTime = Blaze3D.getTime();
|
| 78 | } else if (this.activeButton != null) {
|
| 79 | this.activeButton = null;
|
| 80 | }
|
| 81 |
|
| 82 | if (this.minecraft.gui.overlay() == null) {
|
| 83 | if (pressed && this.minecraft.handleGlobalKeyPress(InputConstants.Type.MOUSE.getOrCreate(rawButtonInfo.button()), rawButtonInfo.hasControlDownWithQuirk())) {
|
| 84 | return;
|
| 85 | }
|
| 86 |
|
| 87 | if (this.minecraft.gui.screen() == null) {
|
| 88 | if (!this.mouseGrabbed && pressed) {
|
| 89 | this.grabMouse();
|
| 90 | }
|
| 91 | } else {
|
| 92 | double xm = this.getScaledXPos(window);
|
| 93 | double ym = this.getScaledYPos(window);
|
| 94 | Screen screen = this.minecraft.gui.screen();
|
| 95 | MouseButtonEvent event = new MouseButtonEvent(xm, ym, rawButtonInfo);
|
| 96 | if (pressed) {
|
| 97 | screen.afterMouseAction();
|
| 98 |
|
| 99 | try {
|
| 100 | long currentTime = Util.getMillis();
|
| 101 | boolean doubleClick = this.lastClick != null
|
| 102 | && currentTime - this.lastClick.time() < 250L
|
| 103 | && this.lastClick.screen() == screen
|
| 104 | && this.lastClickButton == event.button();
|
| 105 | if (screen.mouseClicked(event, doubleClick)) {
|
| 106 | this.lastClick = new MouseHandler.LastClick(currentTime, screen);
|
| 107 | this.lastClickButton = rawButtonInfo.button();
|
| 108 | return;
|
| 109 | }
|
| 110 | } catch (Throwable t) {
|
| 111 | CrashReport report = CrashReport.forThrowable(t, "mouseClicked event handler");
|
| 112 | CrashReportCategory mouseDetails = report.addCategory("Mouse");
|
| 113 | this.fillMousePositionDetails(mouseDetails, window);
|
| 114 | mouseDetails.setDetail("Button", event.button());
|
| 115 | throw new ReportedException(report);
|
| 116 | }
|
| 117 | } else {
|
| 118 | try {
|
| 119 | if (screen.mouseReleased(event)) {
|
| 120 | return;
|
| 121 | }
|
| 122 | } catch (Throwable t) {
|
| 123 | CrashReport report = CrashReport.forThrowable(t, "mouseReleased event handler");
|
| 124 | CrashReportCategory mouseDetails = report.addCategory("Mouse");
|
| 125 | this.fillMousePositionDetails(mouseDetails, window);
|
| 126 | mouseDetails.setDetail("Button", event.button());
|
| 127 | throw new ReportedException(report);
|
| 128 | }
|
| 129 | }
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | if (this.minecraft.gui.screen() == null && this.minecraft.gui.overlay() == null) {
|
| 134 | if (rawButtonInfo.button() == 1) {
|
| 135 | this.isLeftPressed = pressed;
|
| 136 | } else if (rawButtonInfo.button() == 2) {
|
| 137 | this.isMiddlePressed = pressed;
|
| 138 | } else if (rawButtonInfo.button() == 3) {
|
| 139 | this.isRightPressed = pressed;
|
| 140 | }
|
| 141 |
|
| 142 | InputConstants.Key mouseKey = InputConstants.Type.MOUSE.getOrCreate(rawButtonInfo.button());
|
| 143 | KeyMapping.set(mouseKey, pressed);
|
| 144 | if (pressed) {
|
| 145 | KeyMapping.click(mouseKey);
|
| 146 | }
|
| 147 | }
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | public void fillMousePositionDetails(final CrashReportCategory category, final Window window) {
|
| 152 | category.setDetail(
|
| 153 | "Mouse location",
|
| 154 | () -> String.format(
|
| 155 | Locale.ROOT, "Scaled: (%f, %f). Absolute: (%f, %f)", getScaledXPos(window, this.xpos), getScaledYPos(window, this.ypos), this.xpos, this.ypos
|
| 156 | )
|
| 157 | );
|
| 158 | category.setDetail(
|
| 159 | "Screen size",
|
| 160 | () -> String.format(
|
| 161 | Locale.ROOT,
|
| 162 | "Scaled: (%d, %d). Absolute: (%d, %d). Scale factor of %d",
|
| 163 | window.getGuiScaledWidth(),
|
| 164 | window.getGuiScaledHeight(),
|
| 165 | window.getWidth(),
|
| 166 | window.getHeight(),
|
| 167 | window.getGuiScale()
|
| 168 | )
|
| 169 | );
|
| 170 | }
|
| 171 |
|
| 172 | public void onScroll(final long handle, final double xoffset, final double yoffset) {
|
| 173 | if (handle != 0L && handle == this.minecraft.getWindow().handle()) {
|
| 174 | this.minecraft.getFramerateLimitTracker().onInputReceived();
|
| 175 | boolean discreteScroll = this.minecraft.options.discreteMouseScroll().get();
|
| 176 | double scrollSensitivity = this.minecraft.options.mouseWheelSensitivity().get();
|
| 177 | double scaledXOffset = (discreteScroll ? Math.signum(xoffset) : xoffset) * scrollSensitivity;
|
| 178 | double scaledYOffset = (discreteScroll ? Math.signum(yoffset) : yoffset) * scrollSensitivity;
|
| 179 | if (this.minecraft.gui.overlay() == null) {
|
| 180 | if (this.minecraft.gui.screen() != null) {
|
| 181 | double xm = this.getScaledXPos(this.minecraft.getWindow());
|
| 182 | double ym = this.getScaledYPos(this.minecraft.getWindow());
|
| 183 | this.minecraft.gui.screen().mouseScrolled(xm, ym, scaledXOffset, scaledYOffset);
|
| 184 | this.minecraft.gui.screen().afterMouseAction();
|
| 185 | } else if (this.minecraft.player != null) {
|
| 186 | Vector2i wheelXY = this.scrollWheelHandler.onMouseScroll(scaledXOffset, scaledYOffset);
|
| 187 | if (wheelXY.x == 0 && wheelXY.y == 0) {
|
| 188 | return;
|
| 189 | }
|
| 190 |
|
| 191 | int wheel = wheelXY.y == 0 ? -wheelXY.x : wheelXY.y;
|
| 192 | if (this.minecraft.player.isSpectator()) {
|
| 193 | if (this.minecraft.gui.hud.getSpectatorGui().isMenuActive()) {
|
| 194 | this.minecraft.gui.hud.getSpectatorGui().onMouseScrolled(-wheel);
|
| 195 | } else {
|
| 196 | float speed = Mth.clamp(this.minecraft.player.getAbilities().getFlyingSpeed() + wheelXY.y * 0.005F, 0.0F, 0.2F);
|
| 197 | this.minecraft.player.getAbilities().setFlyingSpeed(speed);
|
| 198 | }
|
| 199 | } else {
|
| 200 | Inventory inventory = this.minecraft.player.getInventory();
|
| 201 | inventory.setSelectedSlot(ScrollWheelHandler.getNextScrollWheelSelection(wheel, inventory.getSelectedSlot(), Inventory.getSelectionSize()));
|
| 202 | }
|
| 203 | }
|
| 204 | }
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | public void onDrop(final long handle, final List<String> rawPaths) {
|
| 209 | this.minecraft.getFramerateLimitTracker().onInputReceived();
|
| 210 | List<Path> files = new ArrayList<>(rawPaths.size());
|
| 211 | int failedCount = 0;
|
| 212 |
|
| 213 | for (String rawPath : rawPaths) {
|
| 214 | try {
|
| 215 | files.add(Paths.get(rawPath));
|
| 216 | } catch (InvalidPathException e) {
|
| 217 | failedCount++;
|
| 218 | LOGGER.error("Failed to parse path '{}'", rawPath, e);
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | if (this.minecraft.gui.screen() != null) {
|
| 223 | this.minecraft.gui.screen().onFilesDrop(files);
|
| 224 | }
|
| 225 |
|
| 226 | if (failedCount > 0) {
|
| 227 | SystemToast.onFileDropFailure(this.minecraft, failedCount);
|
| 228 | }
|
| 229 | }
|
| 230 |
|
| 231 | public void onMove(final long handle, final double xpos, final double ypos, final double xrel, final double yrel) {
|
| 232 | if (handle != 0L && handle == this.minecraft.getWindow().handle()) {
|
| 233 | if (this.ignoreFirstMove) {
|
| 234 | this.xpos = xpos;
|
| 235 | this.ypos = ypos;
|
| 236 | this.ignoreFirstMove = false;
|
| 237 | } else {
|
| 238 | if (this.minecraft.isWindowActive()) {
|
| 239 | if (this.mouseGrabbed) {
|
| 240 | this.accumulatedDX += xrel;
|
| 241 | this.accumulatedDY += yrel;
|
| 242 | } else {
|
| 243 | this.accumulatedDX = this.accumulatedDX + (xpos - this.xpos);
|
| 244 | this.accumulatedDY = this.accumulatedDY + (ypos - this.ypos);
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | this.xpos = xpos;
|
| 249 | this.ypos = ypos;
|
| 250 | }
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | public void handleAccumulatedMovement() {
|
| 255 | double time = Blaze3D.getTime();
|
| 256 | double mousea = time - this.lastHandleMovementTime;
|
| 257 | this.lastHandleMovementTime = time;
|
| 258 | if (this.minecraft.isWindowActive()) {
|
| 259 | Screen screen = this.minecraft.gui.screen();
|
| 260 | boolean mouseMoved = this.accumulatedDX != 0.0 || this.accumulatedDY != 0.0;
|
| 261 | if (mouseMoved) {
|
| 262 | this.minecraft.getFramerateLimitTracker().onInputReceived();
|
| 263 | }
|
| 264 |
|
| 265 | if (screen != null && this.minecraft.gui.overlay() == null && mouseMoved) {
|
| 266 | Window window = this.minecraft.getWindow();
|
| 267 | double xm = this.getScaledXPos(window);
|
| 268 | double ym = this.getScaledYPos(window);
|
| 269 |
|
| 270 | try {
|
| 271 | screen.mouseMoved(xm, ym);
|
| 272 | } catch (Throwable t) {
|
| 273 | CrashReport report = CrashReport.forThrowable(t, "mouseMoved event handler");
|
| 274 | CrashReportCategory mouseDetails = report.addCategory("Mouse");
|
| 275 | this.fillMousePositionDetails(mouseDetails, window);
|
| 276 | throw new ReportedException(report);
|
| 277 | }
|
| 278 |
|
| 279 | if (this.activeButton != null && this.mousePressedTime > 0.0) {
|
| 280 | double dx = getScaledXPos(window, this.accumulatedDX);
|
| 281 | double dy = getScaledYPos(window, this.accumulatedDY);
|
| 282 |
|
| 283 | try {
|
| 284 | screen.mouseDragged(new MouseButtonEvent(xm, ym, this.activeButton), dx, dy);
|
| 285 | } catch (Throwable t) {
|
| 286 | CrashReport report = CrashReport.forThrowable(t, "mouseDragged event handler");
|
| 287 | CrashReportCategory mouseDetails = report.addCategory("Mouse");
|
| 288 | this.fillMousePositionDetails(mouseDetails, window);
|
| 289 | throw new ReportedException(report);
|
| 290 | }
|
| 291 | }
|
| 292 |
|
| 293 | screen.afterMouseMove();
|
| 294 | }
|
| 295 |
|
| 296 | if (this.isMouseGrabbed() && this.minecraft.player != null) {
|
| 297 | this.turnPlayer(mousea);
|
| 298 | }
|
| 299 | }
|
| 300 |
|
| 301 | this.accumulatedDX = 0.0;
|
| 302 | this.accumulatedDY = 0.0;
|
| 303 | }
|
| 304 |
|
| 305 | public static double getScaledXPos(final Window window, final double x) {
|
| 306 | return x * window.getGuiScaledWidth() / window.getScreenWidth();
|
| 307 | }
|
| 308 |
|
| 309 | public double getScaledXPos(final Window window) {
|
| 310 | return getScaledXPos(window, this.xpos);
|
| 311 | }
|
| 312 |
|
| 313 | public static double getScaledYPos(final Window window, final double y) {
|
| 314 | return y * window.getGuiScaledHeight() / window.getScreenHeight();
|
| 315 | }
|
| 316 |
|
| 317 | public double getScaledYPos(final Window window) {
|
| 318 | return getScaledYPos(window, this.ypos);
|
| 319 | }
|
| 320 |
|
| 321 | private void turnPlayer(final double mousea) {
|
| 322 | double ss = this.minecraft.options.sensitivity().get() * 0.6F + 0.2F;
|
| 323 | double sensitivityMod = ss * ss * ss;
|
| 324 | double sens = sensitivityMod * 8.0;
|
| 325 | double xo;
|
| 326 | double yo;
|
| 327 | if (this.minecraft.options.smoothCamera) {
|
| 328 | double dx = this.smoothTurnX.getNewDeltaValue(this.accumulatedDX * sens, mousea * sens);
|
| 329 | double dy = this.smoothTurnY.getNewDeltaValue(this.accumulatedDY * sens, mousea * sens);
|
| 330 | xo = dx;
|
| 331 | yo = dy;
|
| 332 | } else if (this.minecraft.options.getCameraType().isFirstPerson() && this.minecraft.player.isScoping()) {
|
| 333 | this.smoothTurnX.reset();
|
| 334 | this.smoothTurnY.reset();
|
| 335 | xo = this.accumulatedDX * sensitivityMod;
|
| 336 | yo = this.accumulatedDY * sensitivityMod;
|
| 337 | } else {
|
| 338 | this.smoothTurnX.reset();
|
| 339 | this.smoothTurnY.reset();
|
| 340 | xo = this.accumulatedDX * sens;
|
| 341 | yo = this.accumulatedDY * sens;
|
| 342 | }
|
| 343 |
|
| 344 | this.minecraft.getTutorial().onMouse(xo, yo);
|
| 345 | if (this.minecraft.player != null) {
|
| 346 | this.minecraft.player.turn(this.minecraft.options.invertMouseX().get() ? -xo : xo, this.minecraft.options.invertMouseY().get() ? -yo : yo);
|
| 347 | }
|
| 348 | }
|
| 349 |
|
| 350 | public boolean isLeftPressed() {
|
| 351 | return this.isLeftPressed;
|
| 352 | }
|
| 353 |
|
| 354 | public boolean isMiddlePressed() {
|
| 355 | return this.isMiddlePressed;
|
| 356 | }
|
| 357 |
|
| 358 | public boolean isRightPressed() {
|
| 359 | return this.isRightPressed;
|
| 360 | }
|
| 361 |
|
| 362 | public double xpos() {
|
| 363 | return this.xpos;
|
| 364 | }
|
| 365 |
|
| 366 | public double ypos() {
|
| 367 | return this.ypos;
|
| 368 | }
|
| 369 |
|
| 370 | public void setIgnoreFirstMove() {
|
| 371 | this.ignoreFirstMove = true;
|
| 372 | }
|
| 373 |
|
| 374 | public void resyncMousePosition() {
|
| 375 | if (!this.mouseGrabbed) {
|
| 376 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 377 | FloatBuffer x = stack.mallocFloat(1);
|
| 378 | FloatBuffer y = stack.mallocFloat(1);
|
| 379 | IntBuffer windowX = stack.mallocInt(1);
|
| 380 | IntBuffer windowY = stack.mallocInt(1);
|
| 381 | SDLMouse.SDL_GetGlobalMouseState(x, y);
|
| 382 | if (SDLVideo.SDL_GetWindowPosition(this.minecraft.getWindow().handle(), windowX, windowY)) {
|
| 383 | this.xpos = x.get(0) - windowX.get(0);
|
| 384 | this.ypos = y.get(0) - windowY.get(0);
|
| 385 | } else {
|
| 386 | SDLMouse.SDL_GetMouseState(x, y);
|
| 387 | this.xpos = x.get(0);
|
| 388 | this.ypos = y.get(0);
|
| 389 | }
|
| 390 | }
|
| 391 | }
|
| 392 | }
|
| 393 |
|
| 394 | public boolean isMouseGrabbed() {
|
| 395 | return this.mouseGrabbed;
|
| 396 | }
|
| 397 |
|
| 398 | public void grabMouse() {
|
| 399 | if (this.minecraft.isWindowActive()) {
|
| 400 | if (!this.mouseGrabbed) {
|
| 401 | if (InputQuirks.RESTORE_KEY_STATE_AFTER_MOUSE_GRAB) {
|
| 402 | KeyMapping.setAll();
|
| 403 | }
|
| 404 |
|
| 405 | this.mouseGrabbed = true;
|
| 406 | this.xpos = this.minecraft.getWindow().getScreenWidth() / 2.0;
|
| 407 | this.ypos = this.minecraft.getWindow().getScreenHeight() / 2.0;
|
| 408 | InputConstants.grabMouse(this.minecraft.getWindow(), this.xpos, this.ypos);
|
| 409 | this.minecraft.gui.setScreen(null);
|
| 410 | this.minecraft.missTime = 10000;
|
| 411 | this.ignoreFirstMove = true;
|
| 412 | }
|
| 413 | }
|
| 414 | }
|
| 415 |
|
| 416 | public void releaseMouse() {
|
| 417 | if (this.mouseGrabbed) {
|
| 418 | this.mouseGrabbed = false;
|
| 419 | this.xpos = this.minecraft.getWindow().getScreenWidth() / 2.0;
|
| 420 | this.ypos = this.minecraft.getWindow().getScreenHeight() / 2.0;
|
| 421 | InputConstants.releaseMouse(this.minecraft.getWindow(), this.xpos, this.ypos);
|
| 422 | }
|
| 423 | }
|
| 424 |
|
| 425 | public void cursorEntered() {
|
| 426 | this.ignoreFirstMove = true;
|
| 427 | }
|
| 428 |
|
| 429 | public void drawDebugMouseInfo(final Font font, final GuiGraphicsExtractor graphics) {
|
| 430 | Window window = this.minecraft.getWindow();
|
| 431 | double x = this.getScaledXPos(window);
|
| 432 | double y = this.getScaledYPos(window) - 8.0;
|
| 433 | String text = String.format(Locale.ROOT, "%.0f,%.0f", x, y);
|
| 434 | graphics.text(font, text, (int)x, (int)y, -1);
|
| 435 | }
|
| 436 |
|
| 437 | @Environment(EnvType.CLIENT)
|
| 438 | private record LastClick(long time, Screen screen) {
|
| 439 | }
|
| 440 | }
|
| 441 |
|