/
ClientInputHandler.java
107 lines (91 loc) · 3.2 KB
/
ClientInputHandler.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
package mezz.jei.common.input;
import mezz.jei.common.input.handlers.DragRouter;
import mezz.jei.common.input.handlers.UserInputRouter;
import mezz.jei.core.util.ReflectionUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.screens.Screen;
import java.util.List;
public class ClientInputHandler {
private final List<ICharTypedHandler> charTypedHandlers;
private final UserInputRouter inputRouter;
private final DragRouter dragRouter;
private final IInternalKeyMappings keybindings;
private final ReflectionUtil reflectionUtil = new ReflectionUtil();
public ClientInputHandler(
List<ICharTypedHandler> charTypedHandlers,
UserInputRouter inputRouter,
DragRouter dragRouter,
IInternalKeyMappings keybindings
) {
this.charTypedHandlers = charTypedHandlers;
this.inputRouter = inputRouter;
this.dragRouter = dragRouter;
this.keybindings = keybindings;
}
public void onInitGui() {
this.inputRouter.handleGuiChange();
this.dragRouter.handleGuiChange();
}
/**
* When we have keyboard focus, use Pre
*/
public boolean onKeyboardKeyPressedPre(Screen screen, UserInput input) {
if (!isContainerTextFieldFocused(screen)) {
return this.inputRouter.handleUserInput(screen, input, keybindings);
}
return false;
}
/**
* Without keyboard focus, use Post
*/
public boolean onKeyboardKeyPressedPost(Screen screen, UserInput input) {
if (isContainerTextFieldFocused(screen)) {
return this.inputRouter.handleUserInput(screen, input, keybindings);
}
return false;
}
/**
* When we have keyboard focus, use Pre
*/
public boolean onKeyboardCharTypedPre(Screen screen, char codePoint, int modifiers) {
if (!isContainerTextFieldFocused(screen)) {
return handleCharTyped(codePoint, modifiers);
}
return false;
}
/**
* Without keyboard focus, use Post
*/
public void onKeyboardCharTypedPost(Screen screen, char codePoint, int modifiers) {
if (isContainerTextFieldFocused(screen)) {
handleCharTyped(codePoint, modifiers);
}
}
public boolean onGuiMouseClicked(Screen screen, UserInput input) {
boolean handled = this.inputRouter.handleUserInput(screen, input, keybindings);
if (Minecraft.getInstance().screen == screen && input.is(keybindings.getLeftClick())) {
handled |= this.dragRouter.startDrag(screen, input);
}
return handled;
}
public boolean onGuiMouseReleased(Screen screen, UserInput input) {
boolean handled = this.inputRouter.handleUserInput(screen, input, keybindings);
if (input.is(keybindings.getLeftClick())) {
handled |= this.dragRouter.completeDrag(screen, input);
}
return handled;
}
public boolean onGuiMouseScroll(double mouseX, double mouseY, double scrollDelta) {
return this.inputRouter.handleMouseScrolled(mouseX, mouseY, scrollDelta);
}
private boolean handleCharTyped(char codePoint, int modifiers) {
return this.charTypedHandlers.stream()
.filter(ICharTypedHandler::hasKeyboardFocus)
.anyMatch(handler -> handler.onCharTyped(codePoint, modifiers));
}
private boolean isContainerTextFieldFocused(Screen screen) {
return reflectionUtil.getFieldWithClass(screen, EditBox.class)
.anyMatch(textField -> textField.isActive() && textField.isFocused());
}
}