Skip to content

Commit

Permalink
Fix #1046 Can’t use mouse button for key bind
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Nov 19, 2017
1 parent 5bac2c1 commit 882449b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 38 deletions.
44 changes: 28 additions & 16 deletions src/main/java/mezz/jei/config/KeyBindings.java
@@ -1,34 +1,46 @@
package mezz.jei.config;

import com.google.common.collect.ImmutableList;
import net.minecraft.client.settings.KeyBinding;
import net.minecraftforge.client.settings.KeyConflictContext;
import net.minecraftforge.client.settings.KeyModifier;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import org.lwjgl.input.Keyboard;

import java.util.List;

public final class KeyBindings {
private static final String categoryName = Constants.MOD_ID + " (" + Constants.NAME + ')';

public static final KeyBinding toggleOverlay = new KeyBinding("key.jei.toggleOverlay", KeyConflictContext.GUI, KeyModifier.CONTROL, Keyboard.KEY_O, categoryName);
public static final KeyBinding focusSearch = new KeyBinding("key.jei.focusSearch", KeyConflictContext.GUI, KeyModifier.CONTROL, Keyboard.KEY_F, categoryName);
public static final KeyBinding showRecipe = new KeyBinding("key.jei.showRecipe", KeyConflictContext.GUI, Keyboard.KEY_R, categoryName);
public static final KeyBinding showUses = new KeyBinding("key.jei.showUses", KeyConflictContext.GUI, Keyboard.KEY_U, categoryName);
public static final KeyBinding recipeBack = new KeyBinding("key.jei.recipeBack", KeyConflictContext.GUI, Keyboard.KEY_BACK, categoryName);
public static final KeyBinding toggleCheatMode = new KeyBinding("key.jei.toggleCheatMode", KeyConflictContext.GUI, Keyboard.KEY_NONE, categoryName);
public static final KeyBinding previousPage = new KeyBinding("key.jei.previousPage", KeyConflictContext.GUI, Keyboard.KEY_PRIOR, categoryName);
public static final KeyBinding nextPage = new KeyBinding("key.jei.nextPage", KeyConflictContext.GUI, Keyboard.KEY_NEXT, categoryName);
public static final KeyBinding toggleOverlay;
public static final KeyBinding focusSearch;
public static final KeyBinding toggleCheatMode;
public static final KeyBinding showRecipe;
public static final KeyBinding showUses;
public static final KeyBinding recipeBack;
public static final KeyBinding previousPage;
public static final KeyBinding nextPage;
private static final List<KeyBinding> allBindings;

static {
allBindings = ImmutableList.of(
toggleOverlay = new KeyBinding("key.jei.toggleOverlay", KeyConflictContext.GUI, KeyModifier.CONTROL, Keyboard.KEY_O, categoryName),
focusSearch = new KeyBinding("key.jei.focusSearch", KeyConflictContext.GUI, KeyModifier.CONTROL, Keyboard.KEY_F, categoryName),
toggleCheatMode = new KeyBinding("key.jei.toggleCheatMode", KeyConflictContext.GUI, Keyboard.KEY_NONE, categoryName),
showRecipe = new KeyBinding("key.jei.showRecipe", KeyConflictContext.GUI, Keyboard.KEY_R, categoryName),
showUses = new KeyBinding("key.jei.showUses", KeyConflictContext.GUI, Keyboard.KEY_U, categoryName),
recipeBack = new KeyBinding("key.jei.recipeBack", KeyConflictContext.GUI, Keyboard.KEY_BACK, categoryName),
previousPage = new KeyBinding("key.jei.previousPage", KeyConflictContext.GUI, Keyboard.KEY_PRIOR, categoryName),
nextPage = new KeyBinding("key.jei.nextPage", KeyConflictContext.GUI, Keyboard.KEY_NEXT, categoryName)
);
}

private KeyBindings() {
}

public static void init() {
ClientRegistry.registerKeyBinding(toggleOverlay);
ClientRegistry.registerKeyBinding(focusSearch);
ClientRegistry.registerKeyBinding(showRecipe);
ClientRegistry.registerKeyBinding(showUses);
ClientRegistry.registerKeyBinding(recipeBack);
ClientRegistry.registerKeyBinding(toggleCheatMode);
ClientRegistry.registerKeyBinding(previousPage);
ClientRegistry.registerKeyBinding(nextPage);
for (KeyBinding binding : allBindings) {
ClientRegistry.registerKeyBinding(binding);
}
}
}
27 changes: 19 additions & 8 deletions src/main/java/mezz/jei/gui/recipes/RecipesGui.java
Expand Up @@ -296,26 +296,37 @@ protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOEx
}
}

if (handleKeybinds(mouseButton - 100)) {
return;
}

super.mouseClicked(mouseX, mouseY, mouseButton);
}

@Override
protected void keyTyped(char typedChar, int keyCode) throws IOException {
if (InputHandler.isInventoryCloseKey(keyCode) || InputHandler.isInventoryToggleKey(keyCode)) {
close();
if (handleKeybinds(keyCode)) {
keyHandled = true;
} else if (KeyBindings.recipeBack.isActiveAndMatches(keyCode)) {
}
}

private boolean handleKeybinds(int eventKey) {
if (InputHandler.isInventoryCloseKey(eventKey) || InputHandler.isInventoryToggleKey(eventKey)) {
close();
return true;
} else if (KeyBindings.recipeBack.isActiveAndMatches(eventKey)) {
back();
keyHandled = true;
return true;
} else if (!Internal.getRuntime().getItemListOverlay().isMouseOver(MouseHelper.getX(), MouseHelper.getY())) {
if (KeyBindings.nextPage.isActiveAndMatches(keyCode)) {
if (KeyBindings.nextPage.isActiveAndMatches(eventKey)) {
logic.nextPage();
keyHandled = true;
} else if (KeyBindings.previousPage.isActiveAndMatches(keyCode)) {
return true;
} else if (KeyBindings.previousPage.isActiveAndMatches(eventKey)) {
logic.previousPage();
keyHandled = true;
return true;
}
}
return false;
}

public boolean isOpen() {
Expand Down
42 changes: 28 additions & 14 deletions src/main/java/mezz/jei/input/InputHandler.java
Expand Up @@ -76,17 +76,21 @@ private boolean handleMouseClick(GuiScreen guiScreen, int mouseButton, int mouse
if (clicked != null && handleMouseClickedFocus(mouseButton, clicked)) {
return true;
}
if (handleFocusKeybinds(mouseButton - 100)) {
return true;
}

if (guiScreen instanceof GuiContainer) {
GuiContainer guiContainer = (GuiContainer) guiScreen;
RecipeClickableArea clickableArea = recipeRegistry.getRecipeClickableArea(guiContainer, mouseX - guiContainer.getGuiLeft(), mouseY - guiContainer.getGuiTop());
if (clickableArea != null) {
List<String> recipeCategoryUids = clickableArea.getRecipeCategoryUids();
recipesGui.showCategories(recipeCategoryUids);
return true;
}
}

return false;
return handleGlobalKeybinds(mouseButton - 100);
}

@Nullable
Expand Down Expand Up @@ -198,6 +202,19 @@ private boolean handleKeyDown(char typedChar, int eventKey) {
}
}

if (handleGlobalKeybinds(eventKey)) {
return true;
}

if (!isContainerTextFieldFocused()) {
return handleFocusKeybinds(eventKey) ||
(ingredientListOverlay.isEnabled() && ingredientListOverlay.onKeyPressed(typedChar, eventKey));
}

return false;
}

private boolean handleGlobalKeybinds(int eventKey) {
if (KeyBindings.toggleOverlay.isActiveAndMatches(eventKey)) {
Config.toggleOverlayEnabled();
return false;
Expand All @@ -215,23 +232,20 @@ private boolean handleKeyDown(char typedChar, int eventKey) {
}
}

if (!isContainerTextFieldFocused()) {
final boolean showRecipe = KeyBindings.showRecipe.isActiveAndMatches(eventKey);
final boolean showUses = KeyBindings.showUses.isActiveAndMatches(eventKey);
if (showRecipe || showUses) {
IClickedIngredient<?> clicked = getIngredientUnderMouseForKey(MouseHelper.getX(), MouseHelper.getY());
if (clicked != null) {
IFocus.Mode mode = showRecipe ? IFocus.Mode.OUTPUT : IFocus.Mode.INPUT;
recipesGui.show(new Focus<Object>(mode, clicked.getValue()));
return true;
}
}
return false;
}

if (ingredientListOverlay.isEnabled() && ingredientListOverlay.onKeyPressed(typedChar, eventKey)) {
private boolean handleFocusKeybinds(int eventKey) {
final boolean showRecipe = KeyBindings.showRecipe.isActiveAndMatches(eventKey);
final boolean showUses = KeyBindings.showUses.isActiveAndMatches(eventKey);
if (showRecipe || showUses) {
IClickedIngredient<?> clicked = getIngredientUnderMouseForKey(MouseHelper.getX(), MouseHelper.getY());
if (clicked != null) {
IFocus.Mode mode = showRecipe ? IFocus.Mode.OUTPUT : IFocus.Mode.INPUT;
recipesGui.show(new Focus<Object>(mode, clicked.getValue()));
return true;
}
}

return false;
}

Expand Down

0 comments on commit 882449b

Please sign in to comment.