From 0df0214a469f797a150ad61d1361b2bbe164cc92 Mon Sep 17 00:00:00 2001 From: nextdayy <79922345+nextdayy@users.noreply.github.com> Date: Fri, 29 Nov 2024 13:47:05 +0000 Subject: [PATCH] enable HiDPI support on legacy --- gradle.properties | 2 +- .../api/ui/v1/keybind/OCKeybindHelper.kt | 5 ++ .../v1/internal/ScreenPlatformImpl.java | 4 +- .../oneconfig/internal/OneConfig.java | 1 + .../internal/OneConfigMixinInit.java | 4 ++ .../internal/mixin/GuiScreenMixin.java | 16 ++++++ .../internal/mixin/MinecraftMixin.java | 44 +++++++++++++++-- .../mixin/hidpi/EntityRendererMixin.java | 47 ++++++++++++++++++ .../hidpi/LoadingScreenRendererMixin.java | 40 +++++++++++++++ .../mixin/hidpi/ScaledResolutionMixin.java | 49 +++++++++++++++++++ 10 files changed, 205 insertions(+), 7 deletions(-) create mode 100644 versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/hidpi/EntityRendererMixin.java create mode 100644 versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/hidpi/LoadingScreenRendererMixin.java create mode 100644 versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/hidpi/ScaledResolutionMixin.java diff --git a/gradle.properties b/gradle.properties index df371d853..c232d6c2e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ name=OneConfig mod_id=oneconfig version_major=1 version_minor=0 -version_patch=0-alpha.39 +version_patch=0-alpha.40 polyfrost.defaults.loom=3 diff --git a/modules/ui/src/main/kotlin/org/polyfrost/oneconfig/api/ui/v1/keybind/OCKeybindHelper.kt b/modules/ui/src/main/kotlin/org/polyfrost/oneconfig/api/ui/v1/keybind/OCKeybindHelper.kt index bbded14ba..c3ddc6bdc 100644 --- a/modules/ui/src/main/kotlin/org/polyfrost/oneconfig/api/ui/v1/keybind/OCKeybindHelper.kt +++ b/modules/ui/src/main/kotlin/org/polyfrost/oneconfig/api/ui/v1/keybind/OCKeybindHelper.kt @@ -46,6 +46,11 @@ class OCKeybindHelper : KeybindHelper() { ) else super.build() } + fun inScreens(): OCKeybindHelper { + inScreens = true + return this + } + fun register() = build().register() fun KeyBinder.Bind.register() = KeybindManager.registerKeybind(this) diff --git a/versions/src/main/java/org/polyfrost/oneconfig/api/platform/v1/internal/ScreenPlatformImpl.java b/versions/src/main/java/org/polyfrost/oneconfig/api/platform/v1/internal/ScreenPlatformImpl.java index c6088d4fb..6313430e4 100644 --- a/versions/src/main/java/org/polyfrost/oneconfig/api/platform/v1/internal/ScreenPlatformImpl.java +++ b/versions/src/main/java/org/polyfrost/oneconfig/api/platform/v1/internal/ScreenPlatformImpl.java @@ -96,7 +96,7 @@ public int windowWidth() { //#if MC>=11502 //$$ return Minecraft.getInstance().getMainWindow().getWidth(); //#else - return Minecraft.getMinecraft().displayWidth; + return (int) (Minecraft.getMinecraft().displayWidth / org.lwjgl.opengl.Display.getPixelScaleFactor()); //#endif } @@ -105,7 +105,7 @@ public int windowHeight() { //#if MC>=11502 //$$ return Minecraft.getInstance().getMainWindow().getHeight(); //#else - return Minecraft.getMinecraft().displayHeight; + return (int) (Minecraft.getMinecraft().displayHeight / org.lwjgl.opengl.Display.getPixelScaleFactor()); //#endif } diff --git a/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java b/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java index 1b10216b5..db826d4ba 100644 --- a/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java +++ b/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java @@ -111,6 +111,7 @@ private static void registerCommands() { private static void registerKeybinds() { OCKeybindHelper builder = OCKeybindHelper.builder(); + if (Platform.loader().isDevelopmentEnvironment()) builder.inScreens(); builder.mods(KeyModifiers.RSHIFT).does((s) -> { if (s) OneConfigUI.INSTANCE.open(); return Unit.INSTANCE; diff --git a/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfigMixinInit.java b/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfigMixinInit.java index ede347bfe..8fd4469ab 100644 --- a/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfigMixinInit.java +++ b/versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfigMixinInit.java @@ -96,6 +96,10 @@ public List getMixins() { } else { // legacy mixins.add("GuiScreenMixin"); + + mixins.add("hidpi.ScaledResolutionMixin"); + mixins.add("hidpi.EntityRendererMixin"); + mixins.add("hidpi.LoadingScreenRendererMixin"); } return mixins; diff --git a/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/GuiScreenMixin.java b/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/GuiScreenMixin.java index 1aa7620c6..3952f742a 100644 --- a/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/GuiScreenMixin.java +++ b/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/GuiScreenMixin.java @@ -27,15 +27,18 @@ package org.polyfrost.oneconfig.internal.mixin; //#if MC<=11202 +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; +import org.lwjgl.opengl.Display; import org.polyfrost.oneconfig.api.event.v1.EventManager; import org.polyfrost.oneconfig.api.event.v1.events.KeyInputEvent; import org.polyfrost.oneconfig.api.event.v1.events.MouseInputEvent; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @Mixin(GuiScreen.class) @@ -78,5 +81,18 @@ public abstract class GuiScreenMixin { //$$ } //$$ } //#endif + + + // HiDPI fixes + + @Redirect(method = "handleMouseInput", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayWidth:I", ordinal = 0)) + private int hiDpiFixMouseX(Minecraft mc) { + return (int) (mc.displayWidth / Display.getPixelScaleFactor()); + } + + @Redirect(method = "handleMouseInput", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayHeight:I", ordinal = 0)) + private int hiDpiFixMouseY(Minecraft mc) { + return (int) (mc.displayHeight / Display.getPixelScaleFactor()); + } } //#endif \ No newline at end of file diff --git a/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/MinecraftMixin.java b/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/MinecraftMixin.java index 9ba7b5703..92cccdd83 100644 --- a/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/MinecraftMixin.java +++ b/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/MinecraftMixin.java @@ -33,9 +33,7 @@ import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.ModifyArg; +import org.spongepowered.asm.mixin.injection.*; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; //#if FORGE @@ -50,6 +48,11 @@ public abstract class MinecraftMixin { @Shadow private Timer timer; + @Shadow public int displayWidth; + @Shadow public int displayHeight; + @Shadow private int tempDisplayWidth; + @Shadow private int tempDisplayHeight; + //@formatter:off @Unique private static final String UPDATE_CAMERA_AND_RENDER = @@ -75,7 +78,7 @@ public abstract class MinecraftMixin { } //#if MC<=11300 - @Inject(method = "resize", at = @At("HEAD")) + @Inject(method = "resize", at = @At("TAIL")) private void ocfg$resizeCallback(int width, int height, CallbackInfo ci) { EventManager.INSTANCE.post(new ResizeEvent(width, height)); } @@ -210,4 +213,37 @@ public abstract class MinecraftMixin { } //#endif + + // HiDPI fixes + + //#if MC<11300 + @Inject(method = "startGame", at = @At("HEAD")) + private void hiDpiFixInit(CallbackInfo ci) { + System.setProperty("org.lwjgl.opengl.Display.enableHighDPI", "true"); + } + + @ModifyVariable(method = "resize", at = @At(value = "HEAD"), ordinal = 0, argsOnly = true) + private int hiDpiFixResizeW(int value) { + return (int) (value * org.lwjgl.opengl.Display.getPixelScaleFactor()); + } + + @ModifyVariable(method = "resize", at = @At(value = "HEAD"), ordinal = 1, argsOnly = true) + private int hiDpiFixResizeH(int value) { + return (int) (value * org.lwjgl.opengl.Display.getPixelScaleFactor()); + } + + @ModifyVariable(method = "drawSplashScreen", at = @At("STORE"), ordinal = 0) + private int hiDpiFixSplashScale(int value) { + return (int) (value * org.lwjgl.opengl.Display.getPixelScaleFactor()); + } + + @Inject(method = "startGame", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;createDisplay()V", shift = At.Shift.AFTER)) + private void hiDpiFixDisplaySizes(CallbackInfo ci) { + float scale = org.lwjgl.opengl.Display.getPixelScaleFactor(); + this.displayWidth = (int) (this.displayWidth * scale); + this.displayHeight = (int) (this.displayHeight * scale); + this.tempDisplayWidth = (int) (this.tempDisplayWidth * scale); + this.tempDisplayHeight = (int) (this.tempDisplayHeight * scale); + } + //#endif } \ No newline at end of file diff --git a/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/hidpi/EntityRendererMixin.java b/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/hidpi/EntityRendererMixin.java new file mode 100644 index 000000000..ac6aee2e6 --- /dev/null +++ b/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/hidpi/EntityRendererMixin.java @@ -0,0 +1,47 @@ +/* + * This file is part of OneConfig. + * OneConfig - Next Generation Config Library for Minecraft: Java Edition + * Copyright (C) 2021~2024 Polyfrost. + * + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * OneConfig is licensed under the terms of version 3 of the GNU Lesser + * General Public License as published by the Free Software Foundation, AND + * under the Additional Terms Applicable to OneConfig, as published by Polyfrost, + * either version 1.0 of the Additional Terms, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License. If not, see . You should + * have also received a copy of the Additional Terms Applicable + * to OneConfig, as published by Polyfrost. If not, see + * + */ + +package org.polyfrost.oneconfig.internal.mixin.hidpi; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.EntityRenderer; +import org.lwjgl.opengl.Display; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(EntityRenderer.class) +public abstract class EntityRendererMixin { + @Redirect(method = "updateCameraAndRender", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayWidth:I", ordinal = 0)) + private int hiDpiFixMouseX(Minecraft mc) { + return (int) (mc.displayWidth / Display.getPixelScaleFactor()); + } + + @Redirect(method = "updateCameraAndRender", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayHeight:I", ordinal = 0)) + private int hiDpiFixMouseY(Minecraft mc) { + return (int) (mc.displayHeight / Display.getPixelScaleFactor()); + } +} diff --git a/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/hidpi/LoadingScreenRendererMixin.java b/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/hidpi/LoadingScreenRendererMixin.java new file mode 100644 index 000000000..6716ab589 --- /dev/null +++ b/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/hidpi/LoadingScreenRendererMixin.java @@ -0,0 +1,40 @@ +/* + * This file is part of OneConfig. + * OneConfig - Next Generation Config Library for Minecraft: Java Edition + * Copyright (C) 2021~2024 Polyfrost. + * + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * OneConfig is licensed under the terms of version 3 of the GNU Lesser + * General Public License as published by the Free Software Foundation, AND + * under the Additional Terms Applicable to OneConfig, as published by Polyfrost, + * either version 1.0 of the Additional Terms, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License. If not, see . You should + * have also received a copy of the Additional Terms Applicable + * to OneConfig, as published by Polyfrost. If not, see + * + */ + +package org.polyfrost.oneconfig.internal.mixin.hidpi; + +import net.minecraft.client.LoadingScreenRenderer; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.ModifyVariable; + +@Mixin(LoadingScreenRenderer.class) +public abstract class LoadingScreenRendererMixin { + @ModifyVariable(method = "setLoadingProgress", at = @At("STORE"), ordinal = 1) + private int hiDpiFixSplashScale(int value) { + return (int) (value * org.lwjgl.opengl.Display.getPixelScaleFactor()); + } +} diff --git a/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/hidpi/ScaledResolutionMixin.java b/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/hidpi/ScaledResolutionMixin.java new file mode 100644 index 000000000..d579696ab --- /dev/null +++ b/versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/hidpi/ScaledResolutionMixin.java @@ -0,0 +1,49 @@ +/* + * This file is part of OneConfig. + * OneConfig - Next Generation Config Library for Minecraft: Java Edition + * Copyright (C) 2021~2024 Polyfrost. + * + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * OneConfig is licensed under the terms of version 3 of the GNU Lesser + * General Public License as published by the Free Software Foundation, AND + * under the Additional Terms Applicable to OneConfig, as published by Polyfrost, + * either version 1.0 of the Additional Terms, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License. If not, see . You should + * have also received a copy of the Additional Terms Applicable + * to OneConfig, as published by Polyfrost. If not, see + * + */ + +package org.polyfrost.oneconfig.internal.mixin.hidpi; + +//#if MC<11300 +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.ScaledResolution; +import org.objectweb.asm.Opcodes; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +@Mixin(ScaledResolution.class) +public abstract class ScaledResolutionMixin { + @Redirect(method = "", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayWidth:I", opcode = Opcodes.GETFIELD)) + private int hiDpiFixWidth(Minecraft mc) { + return (int) (mc.displayWidth / org.lwjgl.opengl.Display.getPixelScaleFactor()); + } + + @Redirect(method = "", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayHeight:I", opcode = Opcodes.GETFIELD)) + private int hiDpiFixHeight(Minecraft mc) { + return (int) (mc.displayHeight / org.lwjgl.opengl.Display.getPixelScaleFactor()); + } +} +//#endif