API hooks for movement and screen pages

This commit is contained in:
Martin Prokoph
2024-07-22 15:23:20 +02:00
parent 540920009f
commit a55e506af0
8 changed files with 96 additions and 45 deletions

View File

@@ -288,7 +288,7 @@ public class MidnightInput {
float rightX = polarUtil.polarX;
float rightY = polarUtil.polarY;
boolean isRadialMenu = client.currentScreen instanceof RingScreen || (MidnightControlsCompat.isEmotecraftPresent() && EmotecraftCompat.isEmotecraftScreen(client.currentScreen));
boolean isRadialMenu = client.currentScreen instanceof RingScreen || (EmotecraftCompat.isPresent() && EmotecraftCompat.isEmotecraftScreen(client.currentScreen));
if (!isRadialMenu) {
for (int i = 0; i < GLFW_GAMEPAD_AXIS_LEFT_TRIGGER; i++) {
@@ -505,8 +505,10 @@ public class MidnightInput {
}
axisValue = (float) Math.min(axisValue / MidnightControlsConfig.getAxisMaxValue(storage.axis), 1);
InputManager.BUTTON_VALUES.put(ButtonBinding.axisAsButton(storage.axis, true), storage.polarity == AxisStorage.Polarity.PLUS ? axisValue : 0.f);
InputManager.BUTTON_VALUES.put(ButtonBinding.axisAsButton(storage.axis, false), storage.polarity == AxisStorage.Polarity.MINUS ? axisValue : 0.f);
if (!MidnightControlsCompat.handleMovement(storage, axisValue)) {
InputManager.BUTTON_VALUES.put(ButtonBinding.axisAsButton(storage.axis, true), storage.polarity == AxisStorage.Polarity.PLUS ? axisValue : 0.f);
InputManager.BUTTON_VALUES.put(ButtonBinding.axisAsButton(storage.axis, false), storage.polarity == AxisStorage.Polarity.MINUS ? axisValue : 0.f);
}
}
private boolean handleScreenScrolling(Screen screen, AxisStorage storage) {
@@ -585,7 +587,7 @@ public class MidnightInput {
else if (y > border) index = 6;
}
if (client.currentScreen instanceof RingScreen && index > -1) RingPage.selected = index;
if (MidnightControlsCompat.isEmotecraftPresent() && EmotecraftCompat.isEmotecraftScreen(client.currentScreen)) EmotecraftCompat.handleEmoteSelector(index);
if (EmotecraftCompat.isPresent() && EmotecraftCompat.isEmotecraftScreen(client.currentScreen)) EmotecraftCompat.handleEmoteSelector(index);
}
public boolean handleListWidgetScrolling(List<? extends Element> children, float value) {

View File

@@ -10,6 +10,7 @@
package eu.midnightdust.midnightcontrols.client.compat;
import eu.midnightdust.midnightcontrols.client.MidnightControlsClient;
import eu.midnightdust.midnightcontrols.client.util.storage.AxisStorage;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
@@ -48,6 +49,16 @@ public interface CompatHandler {
*/
default void handleCamera(@NotNull MinecraftClient client, double targetYaw, double targetPitch) {};
/**
* Handles movement for players as well as vehicles
*
* @param storage the storage containing info about the current axis
* @param adjustedValue the value of the axis, adjusted for max values and non-analogue movement, recommended for player movement
*/
default boolean handleMovement(@NotNull MinecraftClient client, AxisStorage storage, float adjustedValue) {
return false;
}
/**
* Handles custom tab behavior
*
@@ -57,6 +68,15 @@ public interface CompatHandler {
return false;
};
/**
* Handles custom page behavior
*
* @param forward whether the direction is forward or backward
*/
default boolean handlePages(Screen screen, boolean forward) {
return false;
};
/**
* Returns whether the mouse is required on the specified screen.
*

View File

@@ -12,6 +12,8 @@ import org.lwjgl.glfw.GLFW;
import static eu.midnightdust.midnightcontrols.MidnightControls.id;
public class EMICompat implements CompatHandler {
protected static boolean isPresent;
public static boolean handleEmiPages(boolean direction) {
if (isEMIEnabled() && MidnightControlsClient.input.actionGuiCooldown == 0 && EmiScreenManager.getSearchPanel() != null && EmiScreenManager.getSearchPanel().pageLeft != null && EmiScreenManager.getSearchPanel().pageRight != null) {
if (direction) EmiScreenManager.getSearchPanel().pageRight.onPress();
@@ -38,6 +40,14 @@ public class EMICompat implements CompatHandler {
.filter(((buttonBinding) -> EmiApi.getHandledScreen() != null))
.register();
}
/**
* Returns whether EMI is present.
*
* @return true if EMI is present, else false
*/
public static boolean isPresent() {
return isPresent;
}
public static boolean isEMIEnabled() {
return EmiConfig.enabled;
}

View File

@@ -7,7 +7,9 @@ import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
public class EmotecraftCompat {
protected static boolean isPresent;
private static final MinecraftClient client = MinecraftClient.getInstance();
public static void openEmotecraftScreen(Screen parent) {
client.setScreen(new EmoteMenuImpl(parent));
}
@@ -32,4 +34,13 @@ public class EmotecraftCompat {
InputManager.INPUT_MANAGER.updateMousePosition(client);
}
}
/**
* Returns whether Emotecraft is present.
*
* @return true if Emotecraft is present, else false
*/
public static boolean isPresent() {
return isPresent;
}
}

View File

@@ -6,11 +6,7 @@ import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
public class InventoryTabsCompat implements CompatHandler {
private static InventoryTabsCompat INSTANCE;
@Override
public void handle() {
INSTANCE = this;
}
protected static boolean isPresent;
@Override
public boolean handleTabs(Screen screen, boolean next) {
@@ -19,26 +15,39 @@ public class InventoryTabsCompat implements CompatHandler {
int tabIndex = tabManager.tabs.indexOf(tabManager.currentTab);
if (next) {
if (tabIndex < tabManager.tabs.size() - 1) tabManager.onTabClick(tabManager.tabs.get(tabIndex + 1));
else tabManager.onTabClick(tabManager.tabs.get(0));
else tabManager.onTabClick(tabManager.tabs.getFirst());
} else {
if (tabIndex > 0) tabManager.onTabClick(tabManager.tabs.get(tabIndex - 1));
else tabManager.onTabClick(tabManager.tabs.get(tabManager.tabs.size() - 1));
else tabManager.onTabClick(tabManager.tabs.getLast());
}
return true;
}
return false;
}
public static boolean handleInventoryTabs(Screen screen, boolean next) {
return INSTANCE.handleTabs(screen, next);
}
public static void handleInventoryPage(Screen screen, boolean next) {
@Override
public boolean handlePages(Screen screen, boolean next) {
if (screen instanceof HandledScreen<?> && !(screen instanceof CreativeInventoryScreen)) {
TabManager tabManager = TabManager.getInstance();
if (next) {
if (tabManager.canGoForwardAPage()) tabManager.setCurrentPage(tabManager.currentPage + 1);
if (tabManager.canGoForwardAPage()) {
tabManager.setCurrentPage(tabManager.currentPage + 1);
return true;
}
} else {
if (tabManager.canGoBackAPage()) tabManager.setCurrentPage(tabManager.currentPage - 1);
if (tabManager.canGoBackAPage()) {
tabManager.setCurrentPage(tabManager.currentPage - 1);
return true;
}
}
}
return false;
}
/**
* Returns whether InventoryTabs is present.
*
* @return true if InventoryTabs is present, else false
*/
public static boolean isPresent() {
return isPresent;
}
}

View File

@@ -11,6 +11,7 @@ package eu.midnightdust.midnightcontrols.client.compat;
import eu.midnightdust.lib.util.PlatformFunctions;
import eu.midnightdust.midnightcontrols.client.controller.InputManager;
import eu.midnightdust.midnightcontrols.client.util.storage.AxisStorage;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.util.hit.BlockHitResult;
@@ -39,9 +40,10 @@ public class MidnightControlsCompat {
* Initializes compatibility with other mods if needed.
*/
public static void init() {
if (isEMIPresent()) {
if (PlatformFunctions.isModLoaded("emi")) {
log("Adding EMI compatibility...");
registerCompatHandler(new EMICompat());
EMICompat.isPresent = true;
}
if (PlatformFunctions.isModLoaded("hardcorequesting") && LambdaReflection.doesClassExist(HQMCompat.GUI_BASE_CLASS_PATH)) {
log("Adding HQM compatibility...");
@@ -62,6 +64,10 @@ public class MidnightControlsCompat {
if (PlatformFunctions.isModLoaded("inventorytabs")) {
log("Adding Inventory Tabs compatibility...");
registerCompatHandler(new InventoryTabsCompat());
InventoryTabsCompat.isPresent = true;
}
if (PlatformFunctions.isModLoaded("emotecraft")) {
EmotecraftCompat.isPresent = true;
}
HANDLERS.forEach(CompatHandler::handle);
InputManager.loadButtonBindings();
@@ -103,6 +109,15 @@ public class MidnightControlsCompat {
public static boolean handleTabs(Screen screen, boolean forward) {
return streamCompatHandlers().anyMatch(handler -> handler.handleTabs(screen, forward));
}
/**
* Handles custom pages for modded screens
*
* @param screen the screen
* @return true if the handle was fired and succeed, else false
*/
public static boolean handlePages(Screen screen, boolean forward) {
return streamCompatHandlers().anyMatch(handler -> handler.handlePages(screen, forward));
}
/**
* Returns a slot at the specified location if possible.
@@ -178,29 +193,14 @@ public class MidnightControlsCompat {
public static void handleCamera(double targetYaw, double targetPitch) {
MidnightControlsCompat.HANDLERS.forEach(handler -> handler.handleCamera(client, targetYaw, targetPitch));
}
/**
* Returns whether EMI is present.
* Handles movement for players as well as vehicles
*
* @return true if EMI is present, else false
* @param storage the storage containing info about the current axis
* @param adjustedValue the value of the axis, adjusted for max values and non-analogue movement, recommended for player movement
* @return true if the handle was fired and succeed, else false
*/
public static boolean isEMIPresent() {
return PlatformFunctions.isModLoaded("emi");
}
/**
* Returns whether InventoryTabs is present.
*
* @return true if InventoryTabs is present, else false
*/
public static boolean isInventoryTabsPresent() {
return PlatformFunctions.isModLoaded("inventorytabs");
}
/**
* Returns whether Emotecraft is present.
*
* @return true if Emotecraft is present, else false
*/
public static boolean isEmotecraftPresent() {
return PlatformFunctions.isModLoaded("emotecraft");
public static boolean handleMovement(AxisStorage storage, float adjustedValue) {
return streamCompatHandlers().anyMatch(handler -> handler.handleMovement(client, storage, adjustedValue));
}
}

View File

@@ -88,8 +88,7 @@ public class InputHandlers {
var tabs = recipeBookAccessor.getTabButtons();
var currentTab = recipeBookAccessor.getCurrentTab();
if (currentTab == null || !recipeBook.isOpen()) {
if (MidnightControlsCompat.isInventoryTabsPresent()) InventoryTabsCompat.handleInventoryTabs(client.currentScreen, next);
return false;
return MidnightControlsCompat.handleTabs(client.currentScreen, next);
}
int nextTab = tabs.indexOf(currentTab) + (next ? 1 : -1);
if (nextTab < 0)
@@ -144,9 +143,9 @@ public class InputHandlers {
if (client.currentScreen instanceof CreativeInventoryScreen creativeScreen) {
return ItemGroupUtil.cyclePage(next, creativeScreen);
}
if (MidnightControlsCompat.isInventoryTabsPresent()) InventoryTabsCompat.handleInventoryPage(client.currentScreen, next);
return false;
return MidnightControlsCompat.handlePages(client.currentScreen, next);
};
}
public static PressAction handleExit() {

View File

@@ -57,7 +57,7 @@ public abstract class HandledScreenMixin implements HandledScreenAccessor {
if (MidnightControlsConfig.controlsMode == ControlsMode.CONTROLLER && MidnightControlsConfig.hudEnable) {
var client = MinecraftClient.getInstance();
int x = 2, y = client.getWindow().getScaledHeight() - 2 - MidnightControlsRenderer.ICON_SIZE;
if (MidnightControlsCompat.isEMIPresent() && EMICompat.isEMIEnabled()) {
if (EMICompat.isPresent() && EMICompat.isEMIEnabled()) {
x += 42;
}
if (!ButtonBinding.TAKE_ALL.isNotBound()) x = MidnightControlsRenderer.drawButtonTip(context, x, y, ButtonBinding.TAKE_ALL,true, client) + 2;
@@ -66,7 +66,7 @@ public abstract class HandledScreenMixin implements HandledScreenAccessor {
x = 2;
y -= 24;
}
if (MidnightControlsCompat.isEMIPresent() && EMICompat.isEMIEnabled() && EMICompat.isSearchBarCentered()) {
if (EMICompat.isPresent() && EMICompat.isEMIEnabled() && EMICompat.isSearchBarCentered()) {
x = client.getWindow().getScaledWidth() - 4 - client.textRenderer.getWidth(Text.translatable("midnightcontrols.action.pickup"))
- client.textRenderer.getWidth(Text.translatable("midnightcontrols.action.quick_move"))
- 2 * MidnightControlsRenderer.getBindingIconWidth(ButtonBinding.TAKE) - MidnightControlsRenderer.getBindingIconWidth(ButtonBinding.QUICK_MOVE);