Skip to content

Commit 0df0214

Browse files
committed
enable HiDPI support on legacy
1 parent c830f6c commit 0df0214

File tree

10 files changed

+205
-7
lines changed

10 files changed

+205
-7
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name=OneConfig
33
mod_id=oneconfig
44
version_major=1
55
version_minor=0
6-
version_patch=0-alpha.39
6+
version_patch=0-alpha.40
77

88
polyfrost.defaults.loom=3
99

modules/ui/src/main/kotlin/org/polyfrost/oneconfig/api/ui/v1/keybind/OCKeybindHelper.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class OCKeybindHelper : KeybindHelper() {
4646
) else super.build()
4747
}
4848

49+
fun inScreens(): OCKeybindHelper {
50+
inScreens = true
51+
return this
52+
}
53+
4954
fun register() = build().register()
5055

5156
fun KeyBinder.Bind.register() = KeybindManager.registerKeybind(this)

versions/src/main/java/org/polyfrost/oneconfig/api/platform/v1/internal/ScreenPlatformImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public int windowWidth() {
9696
//#if MC>=11502
9797
//$$ return Minecraft.getInstance().getMainWindow().getWidth();
9898
//#else
99-
return Minecraft.getMinecraft().displayWidth;
99+
return (int) (Minecraft.getMinecraft().displayWidth / org.lwjgl.opengl.Display.getPixelScaleFactor());
100100
//#endif
101101
}
102102

@@ -105,7 +105,7 @@ public int windowHeight() {
105105
//#if MC>=11502
106106
//$$ return Minecraft.getInstance().getMainWindow().getHeight();
107107
//#else
108-
return Minecraft.getMinecraft().displayHeight;
108+
return (int) (Minecraft.getMinecraft().displayHeight / org.lwjgl.opengl.Display.getPixelScaleFactor());
109109
//#endif
110110
}
111111

versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ private static void registerCommands() {
111111

112112
private static void registerKeybinds() {
113113
OCKeybindHelper builder = OCKeybindHelper.builder();
114+
if (Platform.loader().isDevelopmentEnvironment()) builder.inScreens();
114115
builder.mods(KeyModifiers.RSHIFT).does((s) -> {
115116
if (s) OneConfigUI.INSTANCE.open();
116117
return Unit.INSTANCE;

versions/src/main/java/org/polyfrost/oneconfig/internal/OneConfigMixinInit.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ public List<String> getMixins() {
9696
} else {
9797
// legacy
9898
mixins.add("GuiScreenMixin");
99+
100+
mixins.add("hidpi.ScaledResolutionMixin");
101+
mixins.add("hidpi.EntityRendererMixin");
102+
mixins.add("hidpi.LoadingScreenRendererMixin");
99103
}
100104

101105
return mixins;

versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/GuiScreenMixin.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@
2727
package org.polyfrost.oneconfig.internal.mixin;
2828
//#if MC<=11202
2929

30+
import net.minecraft.client.Minecraft;
3031
import net.minecraft.client.gui.GuiScreen;
3132
import org.lwjgl.input.Keyboard;
3233
import org.lwjgl.input.Mouse;
34+
import org.lwjgl.opengl.Display;
3335
import org.polyfrost.oneconfig.api.event.v1.EventManager;
3436
import org.polyfrost.oneconfig.api.event.v1.events.KeyInputEvent;
3537
import org.polyfrost.oneconfig.api.event.v1.events.MouseInputEvent;
3638
import org.spongepowered.asm.mixin.Mixin;
3739
import org.spongepowered.asm.mixin.injection.At;
3840
import org.spongepowered.asm.mixin.injection.Inject;
41+
import org.spongepowered.asm.mixin.injection.Redirect;
3942
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
4043

4144
@Mixin(GuiScreen.class)
@@ -78,5 +81,18 @@ public abstract class GuiScreenMixin {
7881
//$$ }
7982
//$$ }
8083
//#endif
84+
85+
86+
// HiDPI fixes
87+
88+
@Redirect(method = "handleMouseInput", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayWidth:I", ordinal = 0))
89+
private int hiDpiFixMouseX(Minecraft mc) {
90+
return (int) (mc.displayWidth / Display.getPixelScaleFactor());
91+
}
92+
93+
@Redirect(method = "handleMouseInput", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayHeight:I", ordinal = 0))
94+
private int hiDpiFixMouseY(Minecraft mc) {
95+
return (int) (mc.displayHeight / Display.getPixelScaleFactor());
96+
}
8197
}
8298
//#endif

versions/src/main/java/org/polyfrost/oneconfig/internal/mixin/MinecraftMixin.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@
3333
import org.spongepowered.asm.mixin.Mixin;
3434
import org.spongepowered.asm.mixin.Shadow;
3535
import org.spongepowered.asm.mixin.Unique;
36-
import org.spongepowered.asm.mixin.injection.At;
37-
import org.spongepowered.asm.mixin.injection.Inject;
38-
import org.spongepowered.asm.mixin.injection.ModifyArg;
36+
import org.spongepowered.asm.mixin.injection.*;
3937
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
4038

4139
//#if FORGE
@@ -50,6 +48,11 @@ public abstract class MinecraftMixin {
5048
@Shadow
5149
private Timer timer;
5250

51+
@Shadow public int displayWidth;
52+
@Shadow public int displayHeight;
53+
@Shadow private int tempDisplayWidth;
54+
@Shadow private int tempDisplayHeight;
55+
5356
//@formatter:off
5457
@Unique
5558
private static final String UPDATE_CAMERA_AND_RENDER =
@@ -75,7 +78,7 @@ public abstract class MinecraftMixin {
7578
}
7679

7780
//#if MC<=11300
78-
@Inject(method = "resize", at = @At("HEAD"))
81+
@Inject(method = "resize", at = @At("TAIL"))
7982
private void ocfg$resizeCallback(int width, int height, CallbackInfo ci) {
8083
EventManager.INSTANCE.post(new ResizeEvent(width, height));
8184
}
@@ -210,4 +213,37 @@ public abstract class MinecraftMixin {
210213
}
211214

212215
//#endif
216+
217+
// HiDPI fixes
218+
219+
//#if MC<11300
220+
@Inject(method = "startGame", at = @At("HEAD"))
221+
private void hiDpiFixInit(CallbackInfo ci) {
222+
System.setProperty("org.lwjgl.opengl.Display.enableHighDPI", "true");
223+
}
224+
225+
@ModifyVariable(method = "resize", at = @At(value = "HEAD"), ordinal = 0, argsOnly = true)
226+
private int hiDpiFixResizeW(int value) {
227+
return (int) (value * org.lwjgl.opengl.Display.getPixelScaleFactor());
228+
}
229+
230+
@ModifyVariable(method = "resize", at = @At(value = "HEAD"), ordinal = 1, argsOnly = true)
231+
private int hiDpiFixResizeH(int value) {
232+
return (int) (value * org.lwjgl.opengl.Display.getPixelScaleFactor());
233+
}
234+
235+
@ModifyVariable(method = "drawSplashScreen", at = @At("STORE"), ordinal = 0)
236+
private int hiDpiFixSplashScale(int value) {
237+
return (int) (value * org.lwjgl.opengl.Display.getPixelScaleFactor());
238+
}
239+
240+
@Inject(method = "startGame", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Minecraft;createDisplay()V", shift = At.Shift.AFTER))
241+
private void hiDpiFixDisplaySizes(CallbackInfo ci) {
242+
float scale = org.lwjgl.opengl.Display.getPixelScaleFactor();
243+
this.displayWidth = (int) (this.displayWidth * scale);
244+
this.displayHeight = (int) (this.displayHeight * scale);
245+
this.tempDisplayWidth = (int) (this.tempDisplayWidth * scale);
246+
this.tempDisplayHeight = (int) (this.tempDisplayHeight * scale);
247+
}
248+
//#endif
213249
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This file is part of OneConfig.
3+
* OneConfig - Next Generation Config Library for Minecraft: Java Edition
4+
* Copyright (C) 2021~2024 Polyfrost.
5+
* <https://polyfrost.org> <https://github.com/Polyfrost/>
6+
*
7+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8+
*
9+
* OneConfig is licensed under the terms of version 3 of the GNU Lesser
10+
* General Public License as published by the Free Software Foundation, AND
11+
* under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
12+
* either version 1.0 of the Additional Terms, or (at your option) any later
13+
* version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
* Lesser General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Lesser General Public
21+
* License. If not, see <https://www.gnu.org/licenses/>. You should
22+
* have also received a copy of the Additional Terms Applicable
23+
* to OneConfig, as published by Polyfrost. If not, see
24+
* <https://polyfrost.org/legal/oneconfig/additional-terms>
25+
*/
26+
27+
package org.polyfrost.oneconfig.internal.mixin.hidpi;
28+
29+
import net.minecraft.client.Minecraft;
30+
import net.minecraft.client.renderer.EntityRenderer;
31+
import org.lwjgl.opengl.Display;
32+
import org.spongepowered.asm.mixin.Mixin;
33+
import org.spongepowered.asm.mixin.injection.At;
34+
import org.spongepowered.asm.mixin.injection.Redirect;
35+
36+
@Mixin(EntityRenderer.class)
37+
public abstract class EntityRendererMixin {
38+
@Redirect(method = "updateCameraAndRender", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayWidth:I", ordinal = 0))
39+
private int hiDpiFixMouseX(Minecraft mc) {
40+
return (int) (mc.displayWidth / Display.getPixelScaleFactor());
41+
}
42+
43+
@Redirect(method = "updateCameraAndRender", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayHeight:I", ordinal = 0))
44+
private int hiDpiFixMouseY(Minecraft mc) {
45+
return (int) (mc.displayHeight / Display.getPixelScaleFactor());
46+
}
47+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of OneConfig.
3+
* OneConfig - Next Generation Config Library for Minecraft: Java Edition
4+
* Copyright (C) 2021~2024 Polyfrost.
5+
* <https://polyfrost.org> <https://github.com/Polyfrost/>
6+
*
7+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8+
*
9+
* OneConfig is licensed under the terms of version 3 of the GNU Lesser
10+
* General Public License as published by the Free Software Foundation, AND
11+
* under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
12+
* either version 1.0 of the Additional Terms, or (at your option) any later
13+
* version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
* Lesser General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Lesser General Public
21+
* License. If not, see <https://www.gnu.org/licenses/>. You should
22+
* have also received a copy of the Additional Terms Applicable
23+
* to OneConfig, as published by Polyfrost. If not, see
24+
* <https://polyfrost.org/legal/oneconfig/additional-terms>
25+
*/
26+
27+
package org.polyfrost.oneconfig.internal.mixin.hidpi;
28+
29+
import net.minecraft.client.LoadingScreenRenderer;
30+
import org.spongepowered.asm.mixin.Mixin;
31+
import org.spongepowered.asm.mixin.injection.At;
32+
import org.spongepowered.asm.mixin.injection.ModifyVariable;
33+
34+
@Mixin(LoadingScreenRenderer.class)
35+
public abstract class LoadingScreenRendererMixin {
36+
@ModifyVariable(method = "setLoadingProgress", at = @At("STORE"), ordinal = 1)
37+
private int hiDpiFixSplashScale(int value) {
38+
return (int) (value * org.lwjgl.opengl.Display.getPixelScaleFactor());
39+
}
40+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of OneConfig.
3+
* OneConfig - Next Generation Config Library for Minecraft: Java Edition
4+
* Copyright (C) 2021~2024 Polyfrost.
5+
* <https://polyfrost.org> <https://github.com/Polyfrost/>
6+
*
7+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8+
*
9+
* OneConfig is licensed under the terms of version 3 of the GNU Lesser
10+
* General Public License as published by the Free Software Foundation, AND
11+
* under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
12+
* either version 1.0 of the Additional Terms, or (at your option) any later
13+
* version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18+
* Lesser General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Lesser General Public
21+
* License. If not, see <https://www.gnu.org/licenses/>. You should
22+
* have also received a copy of the Additional Terms Applicable
23+
* to OneConfig, as published by Polyfrost. If not, see
24+
* <https://polyfrost.org/legal/oneconfig/additional-terms>
25+
*/
26+
27+
package org.polyfrost.oneconfig.internal.mixin.hidpi;
28+
29+
//#if MC<11300
30+
import net.minecraft.client.Minecraft;
31+
import net.minecraft.client.gui.ScaledResolution;
32+
import org.objectweb.asm.Opcodes;
33+
import org.spongepowered.asm.mixin.Mixin;
34+
import org.spongepowered.asm.mixin.injection.At;
35+
import org.spongepowered.asm.mixin.injection.Redirect;
36+
37+
@Mixin(ScaledResolution.class)
38+
public abstract class ScaledResolutionMixin {
39+
@Redirect(method = "<init>", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayWidth:I", opcode = Opcodes.GETFIELD))
40+
private int hiDpiFixWidth(Minecraft mc) {
41+
return (int) (mc.displayWidth / org.lwjgl.opengl.Display.getPixelScaleFactor());
42+
}
43+
44+
@Redirect(method = "<init>", at = @At(value = "FIELD", target = "Lnet/minecraft/client/Minecraft;displayHeight:I", opcode = Opcodes.GETFIELD))
45+
private int hiDpiFixHeight(Minecraft mc) {
46+
return (int) (mc.displayHeight / org.lwjgl.opengl.Display.getPixelScaleFactor());
47+
}
48+
}
49+
//#endif

0 commit comments

Comments
 (0)