|
| 1 | +/* |
| 2 | + * Minecraft Forge, Patchwork Project |
| 3 | + * Copyright (c) 2016-2019, 2019 |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation version 2.1 |
| 8 | + * of the License. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | + */ |
| 19 | + |
| 20 | +package com.patchworkmc.mixin.gui; |
| 21 | + |
| 22 | +import net.minecraftforge.client.event.GuiScreenEvent; |
| 23 | +import net.minecraftforge.common.MinecraftForge; |
| 24 | +import org.spongepowered.asm.mixin.Final; |
| 25 | +import org.spongepowered.asm.mixin.Mixin; |
| 26 | +import org.spongepowered.asm.mixin.Shadow; |
| 27 | +import org.spongepowered.asm.mixin.injection.At; |
| 28 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 29 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 30 | +import org.spongepowered.asm.mixin.injection.callback.LocalCapture; |
| 31 | + |
| 32 | +import net.minecraft.client.MinecraftClient; |
| 33 | +import net.minecraft.client.Mouse; |
| 34 | +import net.minecraft.client.gui.Element; |
| 35 | +import net.minecraft.client.gui.screen.Screen; |
| 36 | + |
| 37 | +@Mixin(Mouse.class) |
| 38 | +public abstract class MixinMouse { |
| 39 | + @Shadow |
| 40 | + @Final |
| 41 | + private MinecraftClient client; |
| 42 | + |
| 43 | + @Shadow |
| 44 | + private int activeButton; |
| 45 | + |
| 46 | + @Shadow |
| 47 | + private boolean middleButtonClicked; |
| 48 | + |
| 49 | + @Shadow |
| 50 | + private double cursorDeltaY; |
| 51 | + |
| 52 | + @Shadow |
| 53 | + private double cursorDeltaX; |
| 54 | + |
| 55 | + @Inject(method = "method_1611", at = @At("HEAD"), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true) |
| 56 | + public void preMouseClicked(boolean[] handled, double mouseX, double mouseY, int button, CallbackInfo info) { |
| 57 | + if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseClickedEvent.Pre(client.currentScreen, mouseX, mouseY, button))) { |
| 58 | + handled[0] = true; |
| 59 | + info.cancel(); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Inject(method = "method_1611", at = @At("RETURN"), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true) |
| 64 | + private void postMouseClicked(boolean[] handled, double mouseX, double mouseY, int button, CallbackInfo info) { |
| 65 | + if (handled[0]) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseClickedEvent.Post(client.currentScreen, mouseX, mouseY, button))) { |
| 70 | + handled[0] = true; |
| 71 | + info.cancel(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Inject(method = "method_1605", at = @At("HEAD"), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true) |
| 76 | + private void preMouseReleased(boolean[] handled, double mouseX, double mouseY, int button, CallbackInfo info) { |
| 77 | + if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseReleasedEvent.Pre(client.currentScreen, mouseX, mouseY, button))) { |
| 78 | + handled[0] = true; |
| 79 | + info.cancel(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Inject(method = "method_1605", at = @At("RETURN"), locals = LocalCapture.CAPTURE_FAILHARD) |
| 84 | + private void postMouseReleased(boolean[] handled, double mouseX, double mouseY, int button, CallbackInfo info) { |
| 85 | + if (handled[0]) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseReleasedEvent.Post(client.currentScreen, mouseX, mouseY, button))) { |
| 90 | + handled[0] = true; |
| 91 | + info.cancel(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Inject(method = "method_1602", at = @At("HEAD"), cancellable = true) |
| 96 | + private void preMouseDragged(Element element, double mouseX, double mouseY, double deltaX, double deltaY, CallbackInfo info) { |
| 97 | + if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseDragEvent.Pre((Screen) element, mouseX, mouseY, activeButton, deltaX, deltaY))) { |
| 98 | + info.cancel(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Inject(method = "method_1602", at = @At("RETURN")) |
| 103 | + private void postMouseDragged(Element element, double mouseX, double mouseY, double deltaX, double deltaY, CallbackInfo info) { |
| 104 | + MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseDragEvent.Post((Screen) element, mouseX, mouseY, activeButton, deltaX, deltaY)); |
| 105 | + } |
| 106 | + |
| 107 | + @Inject(method = "onMouseScroll", at = @At(value = "INVOKE", |
| 108 | + target = "Lnet/minecraft/client/gui/screen/Screen;mouseScrolled(DDD)Z", |
| 109 | + ordinal = 0), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true) |
| 110 | + private void preMouseScrolled(long window, double xOffset, double yOffset, CallbackInfo info, double amount, double mouseX, double mouseY) { |
| 111 | + if (MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseScrollEvent.Pre(client.currentScreen, mouseX, mouseY, amount))) { |
| 112 | + info.cancel(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Inject(method = "onMouseScroll", at = @At(value = "INVOKE", |
| 117 | + target = "Lnet/minecraft/client/gui/screen/Screen;mouseScrolled(DDD)Z", |
| 118 | + ordinal = 0, shift = At.Shift.BY, by = 2), locals = LocalCapture.CAPTURE_FAILHARD, cancellable = true) |
| 119 | + private void postMouseScrolled(long window, double xOffset, double yOffset, CallbackInfo info, double amount, double mouseX, double mouseY) { |
| 120 | + MinecraftForge.EVENT_BUS.post(new GuiScreenEvent.MouseScrollEvent.Post(client.currentScreen, mouseX, mouseY, amount)); |
| 121 | + } |
| 122 | + |
| 123 | + public boolean isMiddleDown() { |
| 124 | + return this.middleButtonClicked; |
| 125 | + } |
| 126 | + |
| 127 | + public double getXVelocity() { |
| 128 | + return this.cursorDeltaX; |
| 129 | + } |
| 130 | + |
| 131 | + public double getYVelocity() { |
| 132 | + return this.cursorDeltaY; |
| 133 | + } |
| 134 | +} |
0 commit comments