Skip to content

Commit

Permalink
Enable layers to use multiple keybindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyfts committed Jul 14, 2024
1 parent ad5f31a commit 2581238
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import java.util.List;

import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.settings.KeyBinding;

import com.gtnewhorizons.navigator.api.NavigatorApi;
import com.gtnewhorizons.navigator.api.journeymap.drawsteps.JMInteractableStep;
import com.gtnewhorizons.navigator.api.journeymap.drawsteps.JMRenderStep;
import com.gtnewhorizons.navigator.api.model.layers.WaypointProviderManager;
import com.gtnewhorizons.navigator.api.model.steps.RenderStep;
import com.gtnewhorizons.navigator.api.util.Util;

public abstract class JMInteractableLayerRenderer extends JMLayerRenderer {

Expand Down Expand Up @@ -84,14 +84,16 @@ public void drawCustomTooltip(FontRenderer fontRenderer, int mouseX, int mouseY,
}
}

public void onActionKeyPressed() {
if (hoveredDrawStep != null) {
/**
* @param keyCode The key code of the key that was pressed
* @return true if the key press was handled, false otherwise
*/
public boolean onKeyPressed(int keyCode) {
if (Util.isKeyPressed(NavigatorApi.ACTION_KEY) && hoveredDrawStep != null) {
hoveredDrawStep.onActionKeyPressed();
manager.forceRefresh();
return true;
}
}

public KeyBinding getActionKey() {
return NavigatorApi.ACTION_KEY;
return false;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/gtnewhorizons/navigator/api/util/Util.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.gtnewhorizons.navigator.api.util;

import net.minecraft.client.settings.KeyBinding;

import org.lwjgl.input.Keyboard;

import cpw.mods.fml.common.Loader;

public class Util {
Expand Down Expand Up @@ -49,4 +53,8 @@ public static int coordChunkToBlock(int chunkCoord) {
public static double journeyMapScaleToLinear(final int jzoom) {
return Math.pow(2, jzoom);
}

public static boolean isKeyPressed(KeyBinding key) {
return key.isPressed() || Keyboard.isKeyDown(key.getKeyCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import java.util.List;

import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.settings.KeyBinding;

import com.gtnewhorizons.navigator.api.NavigatorApi;
import com.gtnewhorizons.navigator.api.model.layers.WaypointProviderManager;
import com.gtnewhorizons.navigator.api.util.Util;
import com.gtnewhorizons.navigator.api.xaero.rendersteps.XaeroInteractableStep;
import com.gtnewhorizons.navigator.api.xaero.rendersteps.XaeroRenderStep;

Expand Down Expand Up @@ -39,13 +39,6 @@ public void drawCustomTooltip(GuiScreen gui, double mouseX, double mouseY, doubl
}
}

public void doActionKeyPress() {
if (manager.isLayerActive() && hovered != null) {
hovered.onActionButton();
manager.forceRefresh();
}
}

public final void onMapClick(boolean isDoubleClick, int mouseX, int mouseY, int mouseBlockX, int mouseBlockZ) {
if (manager.getOpenModGui()
.equals(getLayerMod())) {
Expand Down Expand Up @@ -81,7 +74,16 @@ public void onClick(boolean isDoubleClick, int mouseX, int mouseY, int mouseBloc
public void onClickOutsideRenderStep(boolean isDoubleClick, int mouseX, int mouseY, int mouseBlockX,
int mouseBlockZ) {}

public KeyBinding getActionKey() {
return NavigatorApi.ACTION_KEY;
/**
* @param keyCode The key code of the key that was pressed
* @return true if the key press was handled, false otherwise
*/
public boolean onKeyPressed(int keyCode) {
if (Util.isKeyPressed(NavigatorApi.ACTION_KEY) && hovered != null) {
hovered.onActionButton();
manager.forceRefresh();
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import com.gtnewhorizons.navigator.api.model.buttons.ButtonManager;
import com.gtnewhorizons.navigator.api.model.layers.LayerManager;
import com.gtnewhorizons.navigator.api.model.layers.LayerRenderer;
import com.llamalad7.mixinextras.sugar.Local;

import journeymap.client.Constants;
import journeymap.client.io.ThemeFileHandler;
import journeymap.client.log.LogFormatter;
import journeymap.client.log.StatTimer;
Expand Down Expand Up @@ -233,12 +233,11 @@ public void drawScreen(int width, int height, float f) {
}

@Inject(method = "keyTyped", at = @At(value = "HEAD"), remap = true, require = 1, cancellable = true)
private void navigator$onKeyPress(CallbackInfo ci) {
private void navigator$onKeyPress(CallbackInfo ci, @Local(argsOnly = true) int keyCode) {
if ((chat == null || chat.isHidden())) {
LayerRenderer layer = NavigatorApi.getActiveLayerFor(JourneyMap);
if (layer instanceof JMInteractableLayerRenderer waypointProvider) {
if (Constants.isPressed(waypointProvider.getActionKey())) {
waypointProvider.onActionKeyPressed();
if (waypointProvider.onKeyPressed(keyCode)) {
ci.cancel();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,18 @@ protected GuiMapMixin(GuiScreen parent, GuiScreen escape) {
}
}

@Inject(method = "onInputPress", at = @At("TAIL"))
@Inject(
method = "onInputPress",
at = @At(
value = "INVOKE",
target = "Lxaero/map/misc/Misc;inputMatchesKeyBinding(ZILnet/minecraft/client/settings/KeyBinding;)Z",
ordinal = 1),
cancellable = true)
private void navigator$injectListenKeypress(boolean mouse, int code, CallbackInfoReturnable<Boolean> cir) {
LayerRenderer activeLayer = NavigatorApi.getActiveLayerFor(XaeroWorldMap);
if (activeLayer instanceof XaeroInteractableLayerRenderer interactableLayer
&& Misc.inputMatchesKeyBinding(mouse, code, interactableLayer.getActionKey())) {
interactableLayer.doActionKeyPress();
&& interactableLayer.onKeyPressed(code)) {
cir.setReturnValue(true);
}
}

Expand Down

0 comments on commit 2581238

Please sign in to comment.