-
Notifications
You must be signed in to change notification settings - Fork 287
/
TextFieldInputHandler.java
101 lines (84 loc) · 2.77 KB
/
TextFieldInputHandler.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
package mezz.jei.common.input.handlers;
import com.mojang.blaze3d.platform.InputConstants;
import mezz.jei.common.input.IKeyBindings;
import mezz.jei.common.input.IUserInputHandler;
import mezz.jei.common.input.UserInput;
import mezz.jei.core.util.TextHistory;
import mezz.jei.common.input.GuiTextFieldFilter;
import net.minecraft.client.gui.screens.Screen;
import java.util.Optional;
public class TextFieldInputHandler implements IUserInputHandler {
private final GuiTextFieldFilter textFieldFilter;
public TextFieldInputHandler(GuiTextFieldFilter textFieldFilter) {
this.textFieldFilter = textFieldFilter;
}
@Override
public Optional<IUserInputHandler> handleUserInput(Screen screen, UserInput input, IKeyBindings keyBindings) {
if (handleUserInputBoolean(input, keyBindings)) {
return Optional.of(this);
}
return Optional.empty();
}
private boolean handleUserInputBoolean(UserInput input, IKeyBindings keyBindings) {
if (input.is(keyBindings.getEnterKey()) || input.is(keyBindings.getEscapeKey())) {
return handleSetFocused(input, false);
}
if (input.is(keyBindings.getFocusSearch())) {
return handleSetFocused(input, true);
}
if (input.is(keyBindings.getHoveredClearSearchBar()) &&
textFieldFilter.isMouseOver(input.getMouseX(), input.getMouseY())
) {
return handleHoveredClearSearchBar(input);
}
if (input.callVanilla(
textFieldFilter::isMouseOver,
textFieldFilter::mouseClicked,
textFieldFilter::keyPressed
)) {
return true;
}
if (input.is(keyBindings.getPreviousSearch())) {
return handleNavigateHistory(input, TextHistory.Direction.PREVIOUS);
}
if (input.is(keyBindings.getNextSearch())) {
return handleNavigateHistory(input, TextHistory.Direction.NEXT);
}
// If we can handle this input as a typed char,
// treat it as handled to prevent other handlers from using it.
return textFieldFilter.canConsumeInput() && input.isAllowedChatCharacter();
}
private boolean handleSetFocused(UserInput input, boolean focused) {
if (textFieldFilter.isFocused() != focused) {
if (!input.isSimulate()) {
textFieldFilter.setFocused(focused);
}
return true;
}
return false;
}
private boolean handleHoveredClearSearchBar(UserInput input) {
if (!input.isSimulate()) {
textFieldFilter.setValue("");
textFieldFilter.setFocused(true);
}
return true;
}
private boolean handleNavigateHistory(UserInput input, TextHistory.Direction direction) {
if (textFieldFilter.isFocused()) {
return textFieldFilter.getHistory(direction)
.map(newText -> {
if (!input.isSimulate()) {
textFieldFilter.setValue(newText);
}
return true;
})
.orElse(false);
}
return false;
}
@Override
public void handleMouseClickedOut(InputConstants.Key input) {
textFieldFilter.setFocused(false);
}
}