/
UserInput.java
139 lines (116 loc) · 3.96 KB
/
UserInput.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package mezz.jei.common.input;
import com.google.common.base.MoreObjects;
import com.mojang.blaze3d.platform.InputConstants;
import mezz.jei.api.runtime.IJeiKeyMapping;
import mezz.jei.common.platform.IPlatformInputHelper;
import mezz.jei.common.platform.Services;
import net.minecraft.SharedConstants;
import net.minecraft.client.KeyMapping;
import java.util.Optional;
public class UserInput {
@FunctionalInterface
public interface KeyPressable {
boolean keyPressed(int keyCode, int scanCode, int modifiers);
}
@FunctionalInterface
public interface MouseClickable {
boolean mouseClicked(double mouseX, double mouseY, int mouseButton);
}
@FunctionalInterface
public interface MouseOverable {
boolean isMouseOver(double mouseX, double mouseY);
}
public static UserInput fromVanilla(int keyCode, int scanCode, int modifiers, InputType inputType) {
InputConstants.Key input = InputConstants.getKey(keyCode, scanCode);
return new UserInput(input, MouseUtil.getX(), MouseUtil.getY(), modifiers, inputType);
}
public static Optional<UserInput> fromVanilla(double mouseX, double mouseY, int mouseButton, InputType inputType) {
if (mouseButton < 0) {
return Optional.empty();
}
InputConstants.Key input = InputConstants.Type.MOUSE.getOrCreate(mouseButton);
UserInput userInput = new UserInput(input, mouseX, mouseY, 0, inputType);
return Optional.of(userInput);
}
private final InputConstants.Key key;
private final double mouseX;
private final double mouseY;
private final int modifiers;
private final InputType clickState;
public UserInput(InputConstants.Key key, double mouseX, double mouseY, int modifiers, InputType clickState) {
this.key = key;
this.mouseX = mouseX;
this.mouseY = mouseY;
this.modifiers = modifiers;
this.clickState = clickState;
}
public InputConstants.Key getKey() {
return key;
}
public double getMouseX() {
return mouseX;
}
public double getMouseY() {
return mouseY;
}
public InputType getClickState() {
return clickState;
}
public boolean isSimulate() {
return clickState == InputType.SIMULATE;
}
public boolean isMouse() {
return this.key.getType() == InputConstants.Type.MOUSE;
}
public boolean isKeyboard() {
return this.key.getType() == InputConstants.Type.KEYSYM;
}
public boolean isAllowedChatCharacter() {
return isKeyboard() && SharedConstants.isAllowedChatCharacter((char) this.key.getValue());
}
public boolean is(IJeiKeyMapping keyMapping) {
return keyMapping.isActiveAndMatches(this.key);
}
public boolean is(KeyMapping keyMapping) {
IPlatformInputHelper inputHelper = Services.PLATFORM.getInputHelper();
return inputHelper.isActiveAndMatches(keyMapping, this.key);
}
public boolean callVanilla(MouseOverable mouseOverable, MouseClickable mouseClickable) {
if (this.key.getType() == InputConstants.Type.MOUSE) {
if (mouseOverable.isMouseOver(mouseX, mouseY)) {
if (this.isSimulate()) {
// we can't easily simulate the click, just say we could handle it
return true;
}
return mouseClickable.mouseClicked(mouseX, mouseY, this.key.getValue());
}
}
return false;
}
public boolean callVanilla(KeyPressable keyPressable) {
if (this.key.getType() == InputConstants.Type.KEYSYM) {
if (this.isSimulate()) {
// we can't easily simulate the key press, just say we could handle it
return true;
}
return keyPressable.keyPressed(this.key.getValue(), 0, this.modifiers);
}
return false;
}
public boolean callVanilla(MouseOverable mouseOverable, MouseClickable mouseClickable, KeyPressable keyPressable) {
return switch (this.key.getType()) {
case KEYSYM -> callVanilla(keyPressable);
case MOUSE -> callVanilla(mouseOverable, mouseClickable);
default -> false;
};
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("clickState", clickState)
.add("key", key.getDisplayName().getString())
.add("modifiers", modifiers)
.add("mouse", String.format("%s, %s", mouseX, mouseY))
.toString();
}
}