Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
XyperCode committed Feb 11, 2024
1 parent 56ab4c8 commit db12428
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 72 deletions.
136 changes: 69 additions & 67 deletions common/src/main/java/io/github/ultreon/controllerx/ControllerX.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public class ControllerX {

private static ControllerX instance;

public final ControllerInput controllerInput;
private final ControllerHud controllerHud;
private final KeyboardHud keyboardHud;
public ControllerInput controllerInput;
private ControllerHud controllerHud;
private KeyboardHud keyboardHud;
private InputType inputType = InputType.KEYBOARD_AND_MOUSE;
private int inputCooldown;
private boolean canChangeInput = true;
Expand All @@ -49,6 +49,71 @@ public class ControllerX {
private ControllerX() {
instance = this;

ClientLifecycleEvent.CLIENT_STARTED.register(this::clientStarted);

LOGGER.info("ControllerX initialized");
}

private EventResult initGui(Screen screen, ScreenAccess screenAccess) {
if (controllerInput.isVirtualKeyboardOpen()) {
// SCARY!
virtualKeyboard.getScreen().resize(Minecraft.getInstance(), Minecraft.getInstance().getWindow().getGuiScaledWidth(), Minecraft.getInstance().getWindow().getGuiScaledHeight());
return EventResult.pass();
}
return EventResult.pass();
}

private void initKeyboardLayout() {
this.controllerInput.setLayout(KeyboardLayouts.QWERTY);
}

private void tickInput(Minecraft minecraft) {
Screen screen = minecraft.screen;

if (screen != null) {
controllerInput.updateScreen(screen);
}

if (inputCooldown > 0) {
inputCooldown--;
if (inputCooldown == 0) {
canChangeInput = true;
}
}
}

private void renderGui(Screen screen, GuiGraphics gfx, int mouseX, int mouseY, float partialTicks) {
if (controllerInput.isVirtualKeyboardOpen()) {
virtualKeyboard.render(gfx, mouseX, mouseY, partialTicks);
return;
}
controllerHud.render(gfx, partialTicks);
}

public static ResourceLocation res(String path) {
return new ResourceLocation(MOD_ID, path);
}

@ExpectPlatform
public static double getEntityReach(Player player) {
throw new AssertionError();
}

@ExpectPlatform
public static double getBlockReach(Player player) {
throw new AssertionError();
}

private void clientStarted(Minecraft instance) {
ControllerContext.freeze();

KeyboardHud.addMapping(Minecraft.getInstance().options.keyAttack);
KeyboardHud.addMapping(Minecraft.getInstance().options.keyUse);
KeyboardHud.addMapping(Minecraft.getInstance().options.keyJump);
KeyboardHud.addMapping(Minecraft.getInstance().options.keyShift);
KeyboardHud.addMapping(Minecraft.getInstance().options.keyChat);
KeyboardHud.addMapping(Minecraft.getInstance().options.keyCommand);

SDL_Init(SdlSubSystemConst.SDL_INIT_EVENTS | SdlSubSystemConst.SDL_INIT_GAMECONTROLLER | SdlSubSystemConst.SDL_INIT_JOYSTICK);
ClientLifecycleEvent.CLIENT_STOPPING.register(ControllerX::quitGame);
controllerInput = new ControllerInput(this);
Expand All @@ -62,14 +127,13 @@ private ControllerX() {

ClientTickEvent.CLIENT_PRE.register(this::tickInput);

ClientLifecycleEvent.CLIENT_STARTED.register(ControllerX::clientStarted);

if (controllerInput.isConnected()) {
inputType = InputType.CONTROLLER;
}

this.initKeyboardLayout();
virtualKeyboard = new VirtualKeyboard();

ClientScreenInputEvent.KEY_PRESSED_PRE.register((client, screen, keyCode, scanCode, modifiers) -> {
setInputType(InputType.KEYBOARD_AND_MOUSE);

Expand Down Expand Up @@ -128,68 +192,6 @@ private ControllerX() {
return EventResult.pass();
});

LOGGER.info("ControllerX initialized");
}

private EventResult initGui(Screen screen, ScreenAccess screenAccess) {
if (controllerInput.isVirtualKeyboardOpen()) {
// SCARY!
virtualKeyboard.getScreen().resize(Minecraft.getInstance(), Minecraft.getInstance().getWindow().getGuiScaledWidth(), Minecraft.getInstance().getWindow().getGuiScaledHeight());
return EventResult.pass();
}
return EventResult.pass();
}

private void initKeyboardLayout() {
this.controllerInput.setLayout(KeyboardLayouts.QWERTY);
}

private void tickInput(Minecraft minecraft) {
Screen screen = minecraft.screen;

if (screen != null) {
controllerInput.updateScreen(screen);
}

if (inputCooldown > 0) {
inputCooldown--;
if (inputCooldown == 0) {
canChangeInput = true;
}
}
}

private void renderGui(Screen screen, GuiGraphics gfx, int mouseX, int mouseY, float partialTicks) {
if (controllerInput.isVirtualKeyboardOpen()) {
virtualKeyboard.render(gfx, mouseX, mouseY, partialTicks);
return;
}
controllerHud.render(gfx, partialTicks);
}

public static ResourceLocation res(String path) {
return new ResourceLocation(MOD_ID, path);
}

@ExpectPlatform
public static double getEntityReach(Player player) {
throw new AssertionError();
}

@ExpectPlatform
public static double getBlockReach(Player player) {
throw new AssertionError();
}

private static void clientStarted(Minecraft instance) {
ControllerContext.freeze();

KeyboardHud.addMapping(Minecraft.getInstance().options.keyAttack);
KeyboardHud.addMapping(Minecraft.getInstance().options.keyUse);
KeyboardHud.addMapping(Minecraft.getInstance().options.keyJump);
KeyboardHud.addMapping(Minecraft.getInstance().options.keyShift);
KeyboardHud.addMapping(Minecraft.getInstance().options.keyChat);
KeyboardHud.addMapping(Minecraft.getInstance().options.keyCommand);
}

private void renderHud(GuiGraphics gfx, float ignoredPartialTicks) {
Expand Down
1 change: 0 additions & 1 deletion common/src/main/resources/controllerx-common.mixins.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"required": true,
"refmap": "ControllerX-common-refmap.json",
"minVersion": "0.8",
"package": "io.github.ultreon.controllerx.mixin",
"compatibilityLevel": "JAVA_17",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package io.github.ultreon.controllerx.mixin.forge;

import io.github.ultreon.controllerx.ControllerX;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraftforge.client.ForgeHooksClient;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArgs;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;

@Mixin(GameRenderer.class)
Expand All @@ -15,11 +19,12 @@ public class GameRendererMixin {
@Unique
private Object controllerX$oldMouseY;

@ModifyArgs(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/client/ForgeHooksClient;drawScreen(Lnet/minecraft/client/gui/screens/Screen;Lnet/minecraft/client/gui/GuiGraphics;IIF)V"))
private void onRender(Args args) {
@Redirect(method = "render", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/client/ForgeHooksClient;drawScreen(Lnet/minecraft/client/gui/screens/Screen;Lnet/minecraft/client/gui/GuiGraphics;IIF)V"))
private void onRender(Screen screen, GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
if (ControllerX.get().controllerInput.isVirtualKeyboardOpen()) {
args.set(2, Integer.MIN_VALUE);
args.set(3, Integer.MIN_VALUE);
ForgeHooksClient.drawScreen(screen, guiGraphics, Integer.MIN_VALUE, Integer.MIN_VALUE, partialTick);
} else {
ForgeHooksClient.drawScreen(screen, guiGraphics, mouseX, mouseY, partialTick);
}/* else if (!Objects.equals(args.get(1), controllerX$oldMouseX) || !Objects.equals(args.get(2), controllerX$oldMouseY)) {
controllerX$oldMouseX = args.get(1);
controllerX$oldMouseY = args.get(2);
Expand Down

0 comments on commit db12428

Please sign in to comment.