Skip to content

Commit 62b7959

Browse files
committed
implement MC text with MC renderer, move Platform.screen().glViewport, remove old code from LegacyHud
1 parent d72f971 commit 62b7959

File tree

14 files changed

+121
-99
lines changed

14 files changed

+121
-99
lines changed

minecraft/src/main/java/org/polyfrost/oneconfig/api/platform/v1/internal/GLPlatformImpl.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,17 @@
2727
package org.polyfrost.oneconfig.api.platform.v1.internal;
2828

2929
import dev.deftu.omnicore.api.client.render.state.OmniRenderStates;
30+
import org.jetbrains.annotations.NotNull;
31+
import org.jetbrains.annotations.Nullable;
32+
import org.lwjgl.BufferUtils;
3033
import org.polyfrost.oneconfig.api.platform.v1.GLPlatform;
3134
import org.polyfrost.oneconfig.utils.v1.MHUtils;
3235

36+
import java.nio.Buffer;
37+
import java.nio.IntBuffer;
38+
39+
import static org.lwjgl.opengl.GL11.*;
40+
3341
public class GLPlatformImpl implements GLPlatform {
3442
//@formatter:off
3543
//#if MC <= 1.12.2
@@ -38,6 +46,14 @@ public class GLPlatformImpl implements GLPlatform {
3846
.logIfErr().getOrElse(v -> 0L);
3947
//#endif
4048

49+
private final IntBuffer VIEWPORT = BufferUtils.createIntBuffer(
50+
//#if MC >= 1.13
51+
//$$ 4
52+
//#else
53+
16
54+
//#endif
55+
);
56+
4157
@Override
4258
public long getFunctionAddress(String addr) {
4359
//#if MC <= 1.12.2
@@ -53,7 +69,7 @@ public long getFunctionAddress(String addr) {
5369
* to match what NanoVG leaves dropped into the OpenGL context.
5470
*/
5571
@Override
56-
public void updateGameRenderStateAlongsideNanoVG() {
72+
public void syncOpenGLContext() {
5773
OmniRenderStates.syncBlend();
5874
OmniRenderStates.syncDepth();
5975
OmniRenderStates.syncCull();
@@ -63,4 +79,17 @@ public void updateGameRenderStateAlongsideNanoVG() {
6379
//$$ com.mojang.blaze3d.vertex.BufferUploader.reset();
6480
//#endif
6581
}
82+
83+
@Override
84+
public int @NotNull [] glViewport(int @Nullable [] in) {
85+
((Buffer) VIEWPORT).clear();
86+
//#if MC >= 1.13
87+
//$$ glGetIntegerv(GL_VIEWPORT, VIEWPORT);
88+
//#else
89+
glGetInteger(GL_VIEWPORT, VIEWPORT);
90+
//#endif
91+
int[] out = in != null ? in : new int[4];
92+
VIEWPORT.get(out, 0, 4);
93+
return out;
94+
}
6695
}

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

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,17 @@
2626

2727
package org.polyfrost.oneconfig.api.platform.v1.internal;
2828

29-
import dev.deftu.omnicore.api.client.render.OmniRenderingContext;
3029
import net.minecraft.client.Minecraft;
3130
import net.minecraft.client.gui.GuiScreen;
32-
import org.jetbrains.annotations.NotNull;
3331
import org.jetbrains.annotations.Nullable;
34-
import org.lwjgl.BufferUtils;
3532
import org.polyfrost.oneconfig.api.event.v1.EventDelay;
36-
import org.polyfrost.oneconfig.api.hud.v1.LegacyHud;
3733
import org.polyfrost.oneconfig.api.platform.v1.ScreenPlatform;
38-
import org.polyfrost.oneconfig.api.ui.v1.UIManager;
39-
import org.polyfrost.polyui.PolyUI;
40-
import org.polyfrost.polyui.component.Component;
41-
import org.polyfrost.polyui.component.Drawable;
42-
43-
import java.nio.Buffer;
44-
import java.nio.IntBuffer;
45-
import java.util.List;
4634

4735
public class ScreenPlatformImpl implements ScreenPlatform {
4836
// //#if MC > 1.13
4937
// //$$ private final float[] pixelScaleFactor = new float[1];
5038
// //#endif
5139

52-
private final IntBuffer VIEWPORT = BufferUtils.createIntBuffer(
53-
//#if MC >= 1.13
54-
//$$ 4
55-
//#else
56-
16
57-
//#endif
58-
);
59-
6040
@Override
6141
public int viewportWidth() {
6242
//#if MC>=11502
@@ -93,31 +73,6 @@ public int windowHeight() {
9373
//#endif
9474
}
9575

96-
@Override
97-
public int @NotNull [] glViewport(int @Nullable [] in) {
98-
((Buffer) VIEWPORT).clear();
99-
//#if MC >= 1.13
100-
//$$ org.lwjgl.opengl.GL11.glGetIntegerv(org.lwjgl.opengl.GL11.GL_VIEWPORT, VIEWPORT);
101-
//#else
102-
org.lwjgl.opengl.GL11.glGetInteger(org.lwjgl.opengl.GL11.GL_VIEWPORT, VIEWPORT);
103-
//#endif
104-
int[] out = in != null ? in : new int[4];
105-
VIEWPORT.get(out, 0, 4);
106-
return out;
107-
}
108-
109-
public void renderLegacyHuds(OmniRenderingContext ctx) {
110-
PolyUI defaultInstance = UIManager.INSTANCE.getDefaultInstance();
111-
Drawable master = defaultInstance.getMaster();
112-
List<Component> children = master.getChildren();
113-
if (children == null || children.isEmpty()) return;
114-
115-
for (Component child : children) {
116-
if (!(child instanceof LegacyHud.LegacyHudComponent)) continue;
117-
((LegacyHud.LegacyHudComponent) child).renderLegacy(ctx);
118-
}
119-
}
120-
12176
// todo: https://github.com/Polyfrost/OneConfig/issues/478
12277
// this override was removed to avoid the system receiving incorrect/inaccurate pixel ratio values
12378
// while the window was being resized (as it was calculated based on viewport / window size)

minecraft/src/main/java/org/polyfrost/oneconfig/api/ui/v1/internal/UIManagerImpl.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import dev.deftu.omnicore.api.client.render.GlCapabilities;
4242
import dev.deftu.omnicore.api.client.render.pipeline.OmniRenderPipeline;
4343
import dev.deftu.omnicore.api.client.render.pipeline.OmniRenderPipelines;
44+
import dev.deftu.omnicore.api.client.render.OmniRenderingContext;
4445
import dev.deftu.omnicore.api.client.render.state.OmniBlendState;
4546
import dev.deftu.omnicore.api.client.screen.OmniScreen;
4647
import net.minecraft.client.Minecraft;
@@ -54,6 +55,7 @@
5455
import org.polyfrost.oneconfig.api.ui.v1.internal.wrappers.MCWindow;
5556
import org.polyfrost.oneconfig.api.ui.v1.internal.wrappers.PolyUIScreen;
5657
import org.polyfrost.polyui.PolyUI;
58+
import org.polyfrost.polyui.data.Font;
5759
import org.polyfrost.polyui.renderer.Renderer;
5860
import org.polyfrost.polyui.renderer.Window;
5961

@@ -67,15 +69,18 @@ public class UIManagerImpl implements UIManager {
6769
private static final String LWJGL_API_PACKAGE = "org.polyfrost.oneconfig.api.ui.v1.api.";
6870
private static final String LWJGL_IMPL_PACKAGE = "org.polyfrost.oneconfig.api.ui.v1.internal.";
6971

72+
private static final Font MC_FONT = Font.of("minecraft/assets/fonts/nowhere", null, false, Font.Weight.Regular);
73+
7074
private static final Logger LOGGER = LogManager.getLogger("OneConfig/LWJGL");
7175

7276
private PolyUI ui;
7377
private OmniRenderPipeline pipeline;
78+
private OmniRenderingContext ctx;
7479

7580
private final Set<String> classLoaderInclude = new HashSet<>();
7681
private final Map<String, Class<?>> classCache = new HashMap<>();
7782

78-
private final NanoVgApi nanoVg;
83+
// private final NanoVgApi nanoVg;
7984
private final NanoSvgApi nanoSvg;
8085
private final StbApi stb;
8186
private final TinyFdApi tinyFD;
@@ -114,12 +119,12 @@ public UIManagerImpl() throws Throwable {
114119
boolean isGl3 = GlCapabilities.isGl3Available();
115120

116121
//#if MC >= 1.16.5
117-
//$$ nanoVg = new NanoVgImpl(isGl3);
122+
// //$$ nanoVg = new NanoVgImpl(isGl3);
118123
//$$ nanoSvg = new NanoSvgImpl();
119124
//$$ stb = new StbImpl();
120125
//$$ tinyFD = new TinyFdImpl();
121126
//#else
122-
nanoVg = Lwjgl3Manager.getIsolated(NanoVgApi.class, LWJGL_IMPL_PACKAGE + "NanoVgImpl", isGl3);
127+
// nanoVg = Lwjgl3Manager.getIsolated(NanoVgApi.class, LWJGL_IMPL_PACKAGE + "NanoVgImpl", isGl3);
123128
nanoSvg = Lwjgl3Manager.getIsolated(NanoSvgApi.class, LWJGL_IMPL_PACKAGE + "NanoSvgImpl");
124129
stb = Lwjgl3Manager.getIsolated(StbApi.class, LWJGL_IMPL_PACKAGE + "StbImpl");
125130
tinyFD = Lwjgl3Manager.getIsolated(TinyFdApi.class, LWJGL_IMPL_PACKAGE + "TinyFdImpl");
@@ -173,4 +178,19 @@ public OmniRenderPipeline getRenderPipeline() {
173178

174179
return pipeline;
175180
}
181+
182+
@Override
183+
public void __setRenderingContext(OmniRenderingContext renderingContext) {
184+
ctx = renderingContext;
185+
}
186+
187+
@Override
188+
public OmniRenderingContext getRenderingContext() {
189+
return ctx;
190+
}
191+
192+
@Override
193+
public Font getMCFont() {
194+
return MC_FONT;
195+
}
176196
}

minecraft/src/main/java/org/polyfrost/oneconfig/api/ui/v1/internal/wrappers/PolyUIScreen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void onRender(@NotNull OmniRenderingContext ctx, int mouseX, int mouseY,
121121
//#if MC >= 1.21.5
122122
//$$ kotlin.jvm.functions.Function0<kotlin.Unit> unbind = dev.deftu.omnicore.api.client.framebuffer.OmniFramebuffers.getMain().bind();
123123
//#endif
124-
Platform.screen().glViewport(viewport);
124+
Platform.gl().glViewport(viewport);
125125
float factor = Platform.screen().pixelRatio();
126126
int w = (int) (polyUI.getMaster().getWidth() * factor);
127127
int h = (int) (polyUI.getMaster().getHeight() * factor);

modules/hud/api/hud.api

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ public abstract class org/polyfrost/oneconfig/api/hud/v1/LegacyHud : org/polyfro
9494
public static synthetic fun createLegacy-Op45VSw$default (Lorg/polyfrost/oneconfig/api/hud/v1/LegacyHud;[Lorg/polyfrost/polyui/component/Component;Lorg/polyfrost/polyui/unit/Align;JZILjava/lang/Object;)Lorg/polyfrost/oneconfig/api/hud/v1/LegacyHud$LegacyHudComponent;
9595
public abstract fun getHeight ()F
9696
public abstract fun getWidth ()F
97-
public fun render (Lorg/polyfrost/polyui/renderer/Renderer;Lorg/polyfrost/polyui/component/Drawable;)V
98-
public abstract fun renderLegacy (Ldev/deftu/omnicore/api/client/render/OmniRenderingContext;FFFF)V
97+
public abstract fun renderLegacy (Lorg/polyfrost/polyui/renderer/Renderer;Ldev/deftu/omnicore/api/client/render/OmniRenderingContext;FFFFLorg/polyfrost/polyui/component/Drawable;)V
9998
public abstract fun setHeight (F)V
10099
public abstract fun setWidth (F)V
101100
}
@@ -106,7 +105,6 @@ public class org/polyfrost/oneconfig/api/hud/v1/LegacyHud$LegacyHudComponent : o
106105
public fun getHeight ()F
107106
public fun getWidth ()F
108107
protected fun render ()V
109-
public final fun renderLegacy (Ldev/deftu/omnicore/api/client/render/OmniRenderingContext;)V
110108
public fun setHeight (F)V
111109
public fun setWidth (F)V
112110
}

modules/hud/src/main/kotlin/org/polyfrost/oneconfig/api/hud/v1/LegacyHud.kt

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import dev.deftu.omnicore.api.client.render.OmniRenderingContext
3030
import dev.deftu.omnicore.api.client.render.OmniResolution
3131
import org.jetbrains.annotations.ApiStatus
3232
import org.polyfrost.oneconfig.api.platform.v1.Platform
33+
import org.polyfrost.oneconfig.api.ui.v1.UIManager
3334
import org.polyfrost.polyui.component.Component
3435
import org.polyfrost.polyui.component.Drawable
3536
import org.polyfrost.polyui.renderer.Renderer
@@ -38,7 +39,7 @@ import org.polyfrost.polyui.unit.AlignDefault
3839
import org.polyfrost.polyui.unit.Vec2
3940

4041
/**
41-
* [Hud] implementation that uses the old rendering system, with a standard [render] method.
42+
* [Hud] implementation that uses the old rendering system.
4243
*
4344
* **You must** ensure that the [width] and [height] properties accurately reflect the size of the HUD.
4445
* Note that they are only queried when the HUD is first created, and when the [update] method returns `true`.
@@ -69,18 +70,7 @@ abstract class LegacyHud(id: String, title: String, category: Category) : Hud<Dr
6970
*
7071
* **Note:** This method is called every frame, so you should not perform any heavy calculations here.
7172
*/
72-
abstract fun renderLegacy(ctx: OmniRenderingContext, x: Float, y: Float, scaleX: Float, scaleY: Float)
73-
74-
/**
75-
* Render extra things for your HUD using the PolyUI renderer.
76-
*
77-
* **This is called inside a PolyUI rendering context,** so you can use PolyUI components and methods here, but you **CANNOT** use
78-
* Minecraft rendering methods as they will not work correctly, cause visual glitches, or even crash.
79-
*
80-
* Due to differences in Minecraft versions, you *may not* experience issues, **but they will happen on some versions**.
81-
* So don't. Use [renderLegacy] for that.
82-
*/
83-
open fun render(renderer: Renderer, drawable: Drawable) {}
73+
abstract fun renderLegacy(renderer: Renderer, ctx: OmniRenderingContext, x: Float, y: Float, scaleX: Float, scaleY: Float, drawable: Drawable)
8474

8575
@ApiStatus.Experimental
8676
protected fun createLegacy(
@@ -112,13 +102,9 @@ abstract class LegacyHud(id: String, title: String, category: Category) : Hud<Dr
112102
hud.height = value
113103
}
114104

115-
fun renderLegacy(ctx: OmniRenderingContext) {
116-
val scale = if (HudManager.useGuiScale) 1f else Platform.screen().pixelRatio() / OmniResolution.scaleFactor.toFloat()
117-
hud.renderLegacy(ctx, x * scale, y * scale, scaleX * scale, scaleY * scale)
118-
}
119-
120105
override fun render() {
121-
hud.render(polyUI.renderer, this)
106+
val scale = if (HudManager.useGuiScale) 1f else Platform.screen().pixelRatio() / OmniResolution.scaleFactor.toFloat()
107+
hud.renderLegacy(polyUI.renderer, UIManager.INSTANCE.renderingContext, x * scale, y * scale, scaleX * scale, scaleY * scale, this)
122108
}
123109
}
124110
}

modules/hud/src/main/kotlin/org/polyfrost/oneconfig/api/hud/v1/internal/hudpages.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ package org.polyfrost.oneconfig.api.hud.v1.internal
3131
import org.polyfrost.oneconfig.api.hud.v1.Hud
3232
import org.polyfrost.oneconfig.api.hud.v1.HudManager
3333
import org.polyfrost.oneconfig.api.hud.v1.LegacyHud
34+
import org.polyfrost.oneconfig.api.ui.v1.UIManager
3435
import org.polyfrost.polyui.PolyUI
3536
import org.polyfrost.polyui.color.asMutable
3637
import org.polyfrost.polyui.color.rgba
@@ -39,7 +40,6 @@ import org.polyfrost.polyui.component.Drawable
3940
import org.polyfrost.polyui.component.extensions.*
4041
import org.polyfrost.polyui.component.impl.*
4142
import org.polyfrost.polyui.data.Font
42-
import org.polyfrost.polyui.data.FontFamily
4343
import org.polyfrost.polyui.event.Event
4444
import org.polyfrost.polyui.event.State
4545
import org.polyfrost.polyui.unit.Align
@@ -54,7 +54,6 @@ val alignC = Align(main = Align.Content.Center, cross = Align.Content.Center, li
5454
val alignNoPad = Align(pad = Vec2.ZERO)
5555
val alignHudDefault = Align(main = Align.Content.Center, cross = Align.Content.Center, pad = Vec2(8f, 8f))
5656
val BLACK_HALF = rgba(0, 0, 0, 0.5f)
57-
private val mcFont = FontFamily("Minecraft", "assets/oneconfig/fonts/minecraft/", FontFamily.Type.OpenType)
5857

5958
// const val angleSnapMargin = PI / 12.0
6059
const val minMargin = 4f
@@ -338,7 +337,7 @@ fun textOptions(text: Text): Drawable {
338337
val fontState = State(0).listen {
339338
text.font = when (it) {
340339
1 -> PolyUI.monospaceFont
341-
2 -> mcFont.get(text.fontWeight, text.italic)
340+
2 -> UIManager.INSTANCE.mcFont
342341
else -> text.polyUI.fonts.get(text.fontWeight, text.italic)
343342
}
344343
text._parent?.recalculate(false)

modules/ui/api/ui.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,16 @@ public final class org/polyfrost/oneconfig/api/ui/v1/OCPolyUIBuilder : org/polyf
3737

3838
public abstract interface class org/polyfrost/oneconfig/api/ui/v1/UIManager {
3939
public static final field INSTANCE Lorg/polyfrost/oneconfig/api/ui/v1/UIManager;
40+
public abstract fun __setRenderingContext (Ldev/deftu/omnicore/api/client/render/OmniRenderingContext;)V
4041
public fun createDefault ()Lorg/polyfrost/polyui/PolyUI;
4142
public abstract fun createPolyUIScreen (Lorg/polyfrost/polyui/PolyUI;FFZZLjava/util/function/Consumer;)Ldev/deftu/omnicore/api/client/screen/OmniScreen;
4243
public abstract fun createWindow ()Lorg/polyfrost/polyui/renderer/Window;
4344
public abstract fun getDefaultInstance ()Lorg/polyfrost/polyui/PolyUI;
45+
public abstract fun getMCFont ()Lorg/polyfrost/polyui/data/Font;
4446
public abstract fun getRenderPipeline ()Ldev/deftu/omnicore/api/client/render/pipeline/OmniRenderPipeline;
4547
public abstract fun getRenderer ()Lorg/polyfrost/polyui/renderer/Renderer;
4648
public abstract fun getRendererExt ()Lorg/polyfrost/oneconfig/api/ui/v1/api/RendererExt;
49+
public abstract fun getRenderingContext ()Ldev/deftu/omnicore/api/client/render/OmniRenderingContext;
4750
public abstract fun getTinyFD ()Lorg/polyfrost/oneconfig/api/ui/v1/api/TinyFdApi;
4851
}
4952

modules/ui/src/main/java/org/polyfrost/oneconfig/api/ui/v1/UIManager.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.polyfrost.polyui.Settings;
5757
import org.polyfrost.polyui.component.Component;
5858
import org.polyfrost.polyui.component.Drawable;
59+
import org.polyfrost.polyui.data.Font;
5960
import org.polyfrost.polyui.renderer.Renderer;
6061
import org.polyfrost.polyui.renderer.Window;
6162

@@ -65,6 +66,7 @@
6566
/**
6667
* Abstraction over the LWJGL3 implementation and loading.
6768
*/
69+
@SuppressWarnings("DeprecatedIsStillUsed")
6870
public interface UIManager {
6971
UIManager INSTANCE = ServiceLoader.load(
7072
UIManager.class,
@@ -113,6 +115,25 @@ public interface UIManager {
113115

114116
OmniRenderPipeline getRenderPipeline();
115117

118+
/**
119+
* Return the current rendering context of the default UI instance.
120+
* This method is internal as, well, you shouldn't have any reason to use it.
121+
* <br>
122+
* In fact, if you are using it, please let us know on the GitHub issues page.
123+
*/
124+
@ApiStatus.Internal
125+
OmniRenderingContext getRenderingContext();
126+
127+
/**
128+
* <h1>don't use this method!!</h1>
129+
* <br>if you do, I don't like you. you will probably break like, everything.
130+
*/
131+
@Deprecated
132+
@ApiStatus.Internal
133+
void __setRenderingContext(OmniRenderingContext renderingContext);
134+
135+
Font getMCFont();
136+
116137
/**
117138
* <h1>don't use this method!!</h1>
118139
*/
@@ -138,8 +159,8 @@ default PolyUI createDefault() {
138159
framebuffer.clearDepthStencil(1.0, 0);
139160
framebuffer.usingToRender((matrixStack, w, h) -> {
140161
ctx.pose().runReplacingGlobalState(() -> {
162+
__setRenderingContext(ctx);
141163
polyUI.render();
142-
Platform.screen().renderLegacyHuds(ctx);
143164
});
144165

145166
return Unit.INSTANCE;

0 commit comments

Comments
 (0)