-
Notifications
You must be signed in to change notification settings - Fork 287
/
InputHandler.java
373 lines (337 loc) · 13.6 KB
/
InputHandler.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package mezz.jei.input;
import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import mezz.jei.input.click.ClickFocusHandler;
import mezz.jei.input.click.GuiAreaClickHandler;
import mezz.jei.input.click.MouseClickState;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.inventory.ContainerScreen;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.util.InputMappings;
import mezz.jei.api.gui.handlers.IGuiClickableArea;
import mezz.jei.api.ingredients.IIngredientHelper;
import mezz.jei.api.recipe.IFocus;
import mezz.jei.api.runtime.IIngredientManager;
import mezz.jei.bookmarks.BookmarkList;
import mezz.jei.config.IEditModeConfig;
import mezz.jei.config.IWorldConfig;
import mezz.jei.config.IngredientBlacklistType;
import mezz.jei.config.KeyBindings;
import mezz.jei.events.EventBusHelper;
import mezz.jei.gui.Focus;
import mezz.jei.gui.GuiScreenHelper;
import mezz.jei.gui.overlay.IngredientListOverlay;
import mezz.jei.gui.overlay.bookmarks.LeftAreaDispatcher;
import mezz.jei.gui.recipes.RecipesGui;
import mezz.jei.ingredients.IngredientFilter;
import mezz.jei.ingredients.IngredientManager;
import mezz.jei.util.ReflectionUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class InputHandler {
private static final Logger LOGGER = LogManager.getLogger();
private final IIngredientManager ingredientManager;
private final WeakReference<IngredientFilter> weakIngredientFilter;
private final RecipesGui recipesGui;
private final IngredientListOverlay ingredientListOverlay;
private final IEditModeConfig editModeConfig;
private final IWorldConfig worldConfig;
private final GuiScreenHelper guiScreenHelper;
private final LeftAreaDispatcher leftAreaDispatcher;
private final BookmarkList bookmarkList;
private final List<IShowsRecipeFocuses> showsRecipeFocuses = new ArrayList<>();
private final IMouseHandler mouseClickHandler;
private final IMouseDragHandler mouseDragHandler;
public InputHandler(
RecipesGui recipesGui,
IngredientFilter ingredientFilter,
IngredientManager ingredientManager,
IngredientListOverlay ingredientListOverlay,
IEditModeConfig editModeConfig,
IWorldConfig worldConfig,
GuiScreenHelper guiScreenHelper,
LeftAreaDispatcher leftAreaDispatcher,
BookmarkList bookmarkList
) {
this.ingredientManager = ingredientManager;
this.weakIngredientFilter = new WeakReference<>(ingredientFilter);
this.recipesGui = recipesGui;
this.ingredientListOverlay = ingredientListOverlay;
this.editModeConfig = editModeConfig;
this.worldConfig = worldConfig;
this.guiScreenHelper = guiScreenHelper;
this.leftAreaDispatcher = leftAreaDispatcher;
this.bookmarkList = bookmarkList;
this.showsRecipeFocuses.add(recipesGui);
this.showsRecipeFocuses.add(ingredientListOverlay);
this.showsRecipeFocuses.add(leftAreaDispatcher);
this.showsRecipeFocuses.add(new GuiContainerWrapper(guiScreenHelper));
this.mouseClickHandler = new CombinedMouseHandler(
new ClickEditHandler(),
ingredientListOverlay.getMouseHandler(),
leftAreaDispatcher.getMouseHandler(),
new ClickFocusHandler(this, recipesGui),
new ClickGlobalHandler(),
new GuiAreaClickHandlerGenerator()
);
this.mouseDragHandler = ingredientListOverlay.getMouseDragHandler();
}
public void registerToEventBus() {
EventBusHelper.registerWeakListener(this, GuiScreenEvent.InitGuiEvent.class, InputHandler::onInitGuiEvent);
EventBusHelper.registerWeakListener(this, GuiScreenEvent.KeyboardKeyPressedEvent.Pre.class, InputHandler::onGuiKeyPressedEvent);
EventBusHelper.registerWeakListener(this, GuiScreenEvent.KeyboardCharTypedEvent.Pre.class, InputHandler::onGuiCharTypedEvent);
EventBusHelper.registerWeakListener(this, GuiScreenEvent.KeyboardKeyPressedEvent.Post.class, InputHandler::onGuiKeyboardEvent);
EventBusHelper.registerWeakListener(this, GuiScreenEvent.KeyboardCharTypedEvent.Post.class, InputHandler::onGuiCharTypedEvent);
EventBusHelper.registerWeakListener(this, GuiScreenEvent.MouseClickedEvent.Pre.class, InputHandler::onGuiMouseEvent);
EventBusHelper.registerWeakListener(this, GuiScreenEvent.MouseReleasedEvent.Pre.class, InputHandler::onGuiMouseEvent);
EventBusHelper.registerWeakListener(this, GuiScreenEvent.MouseScrollEvent.Pre.class, InputHandler::onGuiMouseEvent);
}
public void onInitGuiEvent(GuiScreenEvent.InitGuiEvent event) {
this.mouseClickHandler.handleMouseClickedOut(0);
this.mouseClickHandler.handleMouseClickedOut(1);
this.mouseClickHandler.handleMouseClickedOut(2);
this.mouseDragHandler.handleDragCanceled();
}
/**
* When we have keyboard focus, use Pre
*/
public void onGuiKeyPressedEvent(GuiScreenEvent.KeyboardKeyPressedEvent.Pre event) {
if (hasKeyboardFocus()) {
handleKeyEvent(event.getKeyCode(), event.getScanCode(), event.getModifiers());
event.setCanceled(true);
}
}
/**
* When we have keyboard focus, use Pre
*/
public void onGuiCharTypedEvent(GuiScreenEvent.KeyboardCharTypedEvent.Pre event) {
if (hasKeyboardFocus() && handleCharTyped(event.getCodePoint(), event.getModifiers())) {
event.setCanceled(true);
}
}
/**
* Without keyboard focus, use Post
*/
public void onGuiKeyboardEvent(GuiScreenEvent.KeyboardKeyPressedEvent.Post event) {
if (!hasKeyboardFocus() && handleKeyEvent(event.getKeyCode(), event.getScanCode(), event.getModifiers())) {
event.setCanceled(true);
}
}
/**
* Without keyboard focus, use Post
*/
public void onGuiCharTypedEvent(GuiScreenEvent.KeyboardCharTypedEvent.Post event) {
if (!hasKeyboardFocus() && handleCharTyped(event.getCodePoint(), event.getModifiers())) {
event.setCanceled(true);
}
}
public void onGuiMouseEvent(GuiScreenEvent.MouseClickedEvent.Pre event) {
int mouseButton = event.getButton();
if (mouseButton > -1) {
Screen screen = event.getGui();
double mouseX = event.getMouseX();
double mouseY = event.getMouseY();
IMouseHandler handler = this.mouseClickHandler.handleClick(screen, mouseX, mouseY, mouseButton, MouseClickState.SIMULATE);
IMouseDragHandler dragHandler = null;
if (mouseButton == 0) {
dragHandler = this.mouseDragHandler.handleDragStart(screen, mouseX, mouseY);
}
if (handler != null || dragHandler != null) {
event.setCanceled(true);
}
}
}
public void onGuiMouseEvent(GuiScreenEvent.MouseReleasedEvent.Pre event) {
int mouseButton = event.getButton();
if (mouseButton > -1) {
Screen screen = event.getGui();
double mouseX = event.getMouseX();
double mouseY = event.getMouseY();
IMouseHandler handled = this.mouseClickHandler.handleClick(screen, mouseX, mouseY, mouseButton, MouseClickState.EXECUTE);
IMouseDragHandler dragHandled = null;
if (mouseButton == 0) {
dragHandled = this.mouseDragHandler.handleDragComplete(screen, mouseX, mouseY);
}
if (handled != null || dragHandled != null) {
event.setCanceled(true);
}
}
}
public void onGuiMouseEvent(GuiScreenEvent.MouseScrollEvent.Pre event) {
double dWheel = event.getScrollDelta();
double mouseX = event.getMouseX();
double mouseY = event.getMouseY();
IMouseHandler overlayMouseHandler = ingredientListOverlay.getMouseHandler();
if (overlayMouseHandler.handleMouseScrolled(mouseX, mouseY, dWheel) ||
leftAreaDispatcher.handleMouseScrolled(mouseX, mouseY, dWheel)) {
event.setCanceled(true);
}
}
@Nullable
public IClickedIngredient<?> getFocusUnderMouseForClick(double mouseX, double mouseY, int mouseButton) {
for (IShowsRecipeFocuses gui : showsRecipeFocuses) {
if (!isConflictingVanillaMouseButton(mouseButton) || gui.canSetFocusWithMouse()) {
IClickedIngredient<?> clicked = gui.getIngredientUnderMouse(mouseX, mouseY);
if (clicked != null) {
return clicked;
}
}
}
return null;
}
private static boolean isConflictingVanillaMouseButton(int mouseButton) {
return mouseButton <= 2;
}
@Nullable
private IClickedIngredient<?> getIngredientUnderMouseForKey(double mouseX, double mouseY) {
for (IShowsRecipeFocuses gui : showsRecipeFocuses) {
IClickedIngredient<?> clicked = gui.getIngredientUnderMouse(mouseX, mouseY);
if (clicked != null) {
return clicked;
}
}
return null;
}
public class ClickEditHandler implements IMouseHandler {
@Override
public IMouseHandler handleClick(Screen screen, double mouseX, double mouseY, int mouseButton, MouseClickState clickState) {
if (!worldConfig.isEditModeEnabled()) {
return null;
}
IClickedIngredient<?> clicked = getFocusUnderMouseForClick(mouseX, mouseY, mouseButton);
if (clicked == null) {
return null;
}
if (!clickState.isSimulate()) {
handler(clicked);
}
return LimitedAreaMouseHandler.create(this, clicked.getArea());
}
private <V> void handler(IClickedIngredient<V> clicked) {
V ingredient = clicked.getValue();
IngredientBlacklistType blacklistType = Screen.hasControlDown() ? IngredientBlacklistType.WILDCARD : IngredientBlacklistType.ITEM;
IIngredientHelper<V> ingredientHelper = ingredientManager.getIngredientHelper(ingredient);
IngredientFilter ingredientFilter = weakIngredientFilter.get();
if (ingredientFilter == null) {
LOGGER.error("Can't edit the config blacklist, the ingredient filter is null");
} else {
if (editModeConfig.isIngredientOnConfigBlacklist(ingredient, ingredientHelper)) {
editModeConfig.removeIngredientFromConfigBlacklist(ingredient, blacklistType, ingredientHelper);
} else {
editModeConfig.addIngredientToConfigBlacklist(ingredient, blacklistType, ingredientHelper);
}
}
}
}
public class ClickGlobalHandler implements IMouseHandler {
@Override
public IMouseHandler handleClick(Screen screen, double mouseX, double mouseY, int mouseButton, MouseClickState clickState) {
InputMappings.Input input = InputMappings.Type.MOUSE.getOrCreate(mouseButton);
if (handleGlobalKeybinds(input, clickState)) {
return this;
}
return null;
}
}
public class GuiAreaClickHandlerGenerator implements IMouseHandler {
@Nullable
@Override
public IMouseHandler handleClick(Screen screen, double mouseX, double mouseY, int mouseButton, MouseClickState clickState) {
if (screen instanceof ContainerScreen) {
ContainerScreen<?> guiContainer = (ContainerScreen<?>) screen;
IGuiClickableArea clickableArea = guiScreenHelper.getGuiClickableArea(guiContainer, mouseX - guiContainer.getGuiLeft(), mouseY - guiContainer.getGuiTop());
if (clickableArea != null) {
return new GuiAreaClickHandler(recipesGui, clickableArea, guiContainer);
}
}
return null;
}
}
private boolean hasKeyboardFocus() {
return ingredientListOverlay.hasKeyboardFocus();
}
private boolean handleCharTyped(char codePoint, int modifiers) {
return ingredientListOverlay.onCharTyped(codePoint, modifiers);
}
private boolean handleKeyEvent(int keyCode, int scanCode, int modifiers) {
InputMappings.Input input = InputMappings.getKey(keyCode, scanCode);
if (ingredientListOverlay.hasKeyboardFocus()) {
if (KeyBindings.isInventoryCloseKey(input) || KeyBindings.isEnterKey(keyCode)) {
ingredientListOverlay.clearKeyboardFocus();
return true;
} else if (ingredientListOverlay.onKeyPressed(keyCode, scanCode, modifiers)) {
return true;
}
}
if (handleGlobalKeybinds(input, MouseClickState.VANILLA)) {
return true;
}
if (!isContainerTextFieldFocused() && !ingredientListOverlay.hasKeyboardFocus()) {
IClickedIngredient<?> clicked = getIngredientUnderMouseForKey(MouseUtil.getX(), MouseUtil.getY());
if (clicked != null && handleFocusKeybinds(clicked, input, MouseClickState.VANILLA)) {
ingredientListOverlay.clearKeyboardFocus();
return true;
}
return ingredientListOverlay.onKeyPressed(keyCode, scanCode, modifiers);
}
return false;
}
private boolean handleGlobalKeybinds(InputMappings.Input input, MouseClickState clickState) {
if (KeyBindings.toggleOverlay.isActiveAndMatches(input)) {
if (!clickState.isSimulate()) {
worldConfig.toggleOverlayEnabled();
}
return true;
}
if (KeyBindings.toggleBookmarkOverlay.isActiveAndMatches(input)) {
if (!clickState.isSimulate()) {
worldConfig.toggleBookmarkEnabled();
}
return true;
}
return ingredientListOverlay.onGlobalKeyPressed(input, clickState);
}
public boolean handleFocusKeybinds(IClickedIngredient<?> clicked, InputMappings.Input input, MouseClickState clickState) {
final boolean showRecipe = KeyBindings.showRecipe.isActiveAndMatches(input);
final boolean showUses = KeyBindings.showUses.isActiveAndMatches(input);
if (showRecipe || showUses) {
if (!clickState.isSimulate()) {
IFocus.Mode mode = showRecipe ? IFocus.Mode.OUTPUT : IFocus.Mode.INPUT;
recipesGui.show(new Focus<Object>(mode, clicked.getValue()));
}
this.ingredientListOverlay.clearKeyboardFocus();
return true;
} else if (KeyBindings.bookmark.isActiveAndMatches(input)) {
if (!clickState.isSimulate()) {
if (bookmarkList.remove(clicked.getValue())) {
if (bookmarkList.isEmpty()) {
worldConfig.setBookmarkEnabled(false);
}
return true;
} else {
worldConfig.setBookmarkEnabled(true);
return bookmarkList.add(clicked.getValue());
}
}
this.ingredientListOverlay.clearKeyboardFocus();
return true;
}
return false;
}
private boolean isContainerTextFieldFocused() {
Minecraft minecraft = Minecraft.getInstance();
if (minecraft == null) {
return false;
}
Screen screen = minecraft.screen;
if (screen == null) {
return false;
}
TextFieldWidget textField = ReflectionUtil.getFieldWithClass(screen, TextFieldWidget.class);
return textField != null && textField.isVisible() && textField.isFocused();
}
}