Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 6847994

Browse files
committed
Implement InputEvent through patchwork-events-input
1 parent a7d1dd3 commit 6847994

8 files changed

Lines changed: 417 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
archivesBaseName = "patchwork-events-input"
2+
version = getSubprojectVersion(project, "0.1.0")
3+
4+
dependencies {
5+
compile project(path: ':patchwork-fml', configuration: 'dev')
6+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
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.event.input;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import org.spongepowered.asm.mixin.Shadow;
24+
import org.spongepowered.asm.mixin.injection.At;
25+
import org.spongepowered.asm.mixin.injection.Inject;
26+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
27+
import net.minecraftforge.client.event.InputEvent;
28+
import net.minecraftforge.common.MinecraftForge;
29+
30+
import net.minecraft.client.Keyboard;
31+
import net.minecraft.client.MinecraftClient;
32+
33+
@Mixin(Keyboard.class)
34+
public abstract class MixinKeyboard {
35+
@Shadow
36+
MinecraftClient client;
37+
38+
@Inject(method = "onKey", at = @At("RETURN"))
39+
private void fireKeyInput(long window, int key, int scancode, int i, int j, CallbackInfo info) {
40+
if (window == this.client.window.getHandle()) {
41+
MinecraftForge.EVENT_BUS.post(new InputEvent.KeyInputEvent(key, scancode, i, j));
42+
}
43+
}
44+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
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.event.input;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import org.spongepowered.asm.mixin.Shadow;
24+
import org.spongepowered.asm.mixin.injection.At;
25+
import org.spongepowered.asm.mixin.injection.At.Shift;
26+
import org.spongepowered.asm.mixin.injection.Inject;
27+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
28+
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
29+
import net.minecraftforge.client.event.InputEvent;
30+
import net.minecraftforge.common.MinecraftForge;
31+
import net.minecraftforge.eventbus.api.Event;
32+
33+
import net.minecraft.client.Mouse;
34+
35+
@Mixin(Mouse.class)
36+
public abstract class MixinMouse {
37+
@Shadow
38+
boolean middleButtonClicked;
39+
@Shadow
40+
abstract boolean wasLeftButtonClicked();
41+
@Shadow
42+
abstract boolean wasRightButtonClicked();
43+
@Shadow
44+
abstract double getX();
45+
@Shadow
46+
abstract double getY();
47+
48+
@Inject(method = "onMouseButton", at = @At("RETURN"), cancellable = true)
49+
private void fireMouseInput(long window, int button, int action, int mods, CallbackInfo info) {
50+
MinecraftForge.EVENT_BUS.post(new InputEvent.MouseInputEvent(button, action, mods));
51+
}
52+
53+
@Inject(method = "onMouseButton", at = @At(value = "FIELD", ordinal = 3, target = "Lnet/minecraft/client/Mouse;client:Lnet/minecraft/client/MinecraftClient;", shift = Shift.BEFORE), cancellable = true)
54+
private void onRawMouseClicked(long window, int button, int action, int mods, CallbackInfo info) {
55+
if (MinecraftForge.EVENT_BUS.post(new InputEvent.RawMouseEvent(button, action, mods))) {
56+
info.cancel();
57+
}
58+
}
59+
60+
@Inject(method = "onMouseScroll", locals = LocalCapture.CAPTURE_FAILHARD, at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isSpectator()Z", shift = Shift.BEFORE), cancellable = true)
61+
private void onMouseScroll(long window, double d, double e, CallbackInfo info, double f, float i) {
62+
final Event event = new InputEvent.MouseScrollEvent(f, wasLeftButtonClicked(), middleButtonClicked, wasRightButtonClicked(), getX(), getY());
63+
64+
if (MinecraftForge.EVENT_BUS.post(event)) {
65+
info.cancel();
66+
}
67+
}
68+
}
Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
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 net.minecraftforge.client.event;
21+
22+
import net.minecraftforge.eventbus.api.Event;
23+
import org.lwjgl.glfw.GLFW;
24+
25+
public class InputEvent extends Event {
26+
/**
27+
* A cancellable mouse event fired before key bindings are updated.
28+
*/
29+
public static class RawMouseEvent extends InputEvent {
30+
private final int action;
31+
private final int button;
32+
private final int mods;
33+
34+
// For EventBus
35+
public RawMouseEvent() {
36+
this(-1, -1, -1);
37+
}
38+
39+
public RawMouseEvent(int button, int action, int mods) {
40+
this.button = button;
41+
this.action = action;
42+
this.mods = mods;
43+
}
44+
45+
/**
46+
* The mouse button that triggered this event.
47+
* https://www.glfw.org/docs/latest/group__buttons.html
48+
*
49+
* @see GLFW mouse constants starting with "GLFW_MOUSE_BUTTON_"
50+
*/
51+
public int getButton() {
52+
return this.button;
53+
}
54+
55+
/**
56+
* Integer representing the mouse button's action.
57+
*
58+
* @see GLFW#GLFW_PRESS
59+
* @see GLFW#GLFW_RELEASE
60+
*/
61+
public int getAction() {
62+
return this.action;
63+
}
64+
65+
/**
66+
* Bit field representing the modifier keys pressed.
67+
* https://www.glfw.org/docs/latest/group__mods.html
68+
*
69+
* @see GLFW#GLFW_MOD_SHIFT
70+
* @see GLFW#GLFW_MOD_CONTROL
71+
* @see GLFW#GLFW_MOD_ALT
72+
* @see GLFW#GLFW_MOD_SUPER
73+
*/
74+
public int getMods() {
75+
return this.mods;
76+
}
77+
78+
@Override
79+
public boolean isCancelable() {
80+
return true;
81+
}
82+
}
83+
84+
/**
85+
* This event fires when a mouse input is detected.
86+
*/
87+
public static class MouseInputEvent extends InputEvent {
88+
private final int button;
89+
private final int action;
90+
private final int mods;
91+
92+
// For EventBus
93+
public MouseInputEvent() {
94+
this(-1, -1, -1);
95+
}
96+
97+
public MouseInputEvent(int button, int action, int mods) {
98+
this.button = button;
99+
this.action = action;
100+
this.mods = mods;
101+
}
102+
103+
/**
104+
* The mouse button that triggered this event.
105+
* https://www.glfw.org/docs/latest/group__buttons.html
106+
*
107+
* @see GLFW mouse constants starting with "GLFW_MOUSE_BUTTON_"
108+
*/
109+
public int getButton() {
110+
return this.button;
111+
}
112+
113+
/**
114+
* Integer representing the mouse button's action.
115+
*
116+
* @see GLFW#GLFW_PRESS
117+
* @see GLFW#GLFW_RELEASE
118+
*/
119+
public int getAction() {
120+
return this.action;
121+
}
122+
123+
/**
124+
* Bit field representing the modifier keys pressed.
125+
* https://www.glfw.org/docs/latest/group__mods.html
126+
*
127+
* @see GLFW#GLFW_MOD_SHIFT
128+
* @see GLFW#GLFW_MOD_CONTROL
129+
* @see GLFW#GLFW_MOD_ALT
130+
* @see GLFW#GLFW_MOD_SUPER
131+
*/
132+
public int getMods() {
133+
return this.mods;
134+
}
135+
}
136+
137+
/**
138+
* This event fires when the mouse scroll wheel is used outside of a gui.
139+
*/
140+
public static class MouseScrollEvent extends InputEvent {
141+
private final double scrollDelta;
142+
private final double mouseX;
143+
private final double mouseY;
144+
private final boolean leftDown;
145+
private final boolean middleDown;
146+
private final boolean rightDown;
147+
148+
// For EventBus
149+
public MouseScrollEvent() {
150+
this(-1, false, false, false, -1, -1);
151+
}
152+
153+
public MouseScrollEvent(double scrollDelta, boolean leftDown, boolean middleDown, boolean rightDown, double mouseX, double mouseY) {
154+
this.scrollDelta = scrollDelta;
155+
this.leftDown = leftDown;
156+
this.middleDown = middleDown;
157+
this.rightDown = rightDown;
158+
this.mouseX = mouseX;
159+
this.mouseY = mouseY;
160+
}
161+
162+
public double getScrollDelta() {
163+
return this.scrollDelta;
164+
}
165+
166+
public boolean isLeftDown() {
167+
return this.leftDown;
168+
}
169+
170+
public boolean isRightDown() {
171+
return this.rightDown;
172+
}
173+
174+
public boolean isMiddleDown() {
175+
return this.middleDown;
176+
}
177+
178+
public double getMouseX() {
179+
return this.mouseX;
180+
}
181+
182+
public double getMouseY() {
183+
return this.mouseY;
184+
}
185+
186+
@Override
187+
public boolean isCancelable() {
188+
return true;
189+
}
190+
}
191+
192+
/**
193+
* This event fires when a keyboard input is detected.
194+
*/
195+
public static class KeyInputEvent extends InputEvent {
196+
private final int key;
197+
private final int scanCode;
198+
private final int action;
199+
private final int modifiers;
200+
201+
// For EventBus
202+
public KeyInputEvent() {
203+
this(-1, -1, -1, -1);
204+
}
205+
206+
public KeyInputEvent(int key, int scanCode, int action, int modifiers) {
207+
this.key = key;
208+
this.scanCode = scanCode;
209+
this.action = action;
210+
this.modifiers = modifiers;
211+
}
212+
213+
/**
214+
* The keyboard key that triggered this event.
215+
* https://www.glfw.org/docs/latest/group__keys.html
216+
*
217+
* @see GLFW key constants starting with "GLFW_KEY_"
218+
*/
219+
public int getKey() {
220+
return this.key;
221+
}
222+
223+
/**
224+
* Platform-specific scan code.
225+
* Used for {@link InputMappings#getInputByCode(int, int)}
226+
*
227+
* <p>The scan code is unique for every key, regardless of whether it has a key code.
228+
* Scan codes are platform-specific but consistent over time, so keys will have different scan codes depending
229+
* on the platform but they are safe to save to disk as custom key bindings.
230+
*/
231+
public int getScanCode() {
232+
return this.scanCode;
233+
}
234+
235+
/**
236+
* Integer representing the key's action.
237+
*
238+
* @see GLFW#GLFW_PRESS
239+
* @see GLFW#GLFW_RELEASE
240+
* @see GLFW#GLFW_REPEAT
241+
*/
242+
public int getAction() {
243+
return this.action;
244+
}
245+
246+
/**
247+
* Bit field representing the modifier keys pressed.
248+
* https://www.glfw.org/docs/latest/group__mods.html
249+
*
250+
* @see GLFW#GLFW_MOD_SHIFT
251+
* @see GLFW#GLFW_MOD_CONTROL
252+
* @see GLFW#GLFW_MOD_ALT
253+
* @see GLFW#GLFW_MOD_SUPER
254+
*/
255+
public int getModifiers() {
256+
return this.modifiers;
257+
}
258+
}
259+
}
22.2 KB
Loading

0 commit comments

Comments
 (0)