From 9d701f6b79eadb13d0bb6587f639077dfa3c477a Mon Sep 17 00:00:00 2001 From: marc Date: Sun, 31 Mar 2024 01:38:42 +0100 Subject: [PATCH] implemented void in test mod --- .../neptuneclient/voidui/widgets/elements.kt | 1 - .../neptuneclient/voidui/tests/ScreenTest.kt | 2 +- .../voidui/testmod/ModEntry.java | 31 ++++++--- .../voidui/testmod/impl/RendererImpl.java | 67 ++++++++++++++++++- .../voidui/testmod/impl/ScreenBridge.java | 26 +++++++ .../voidui/testmod/mixins/MixinInGameHud.java | 29 -------- .../voidui/testmod/example/TestScreen.kt | 24 +++++++ .../voidui/testmod/example/TestTheme.kt | 52 ++++++++++++++ test-mod/src/main/resources/void.mixins.json | 1 - 9 files changed, 191 insertions(+), 42 deletions(-) create mode 100644 test-mod/src/main/java/com/neptuneclient/voidui/testmod/impl/ScreenBridge.java delete mode 100644 test-mod/src/main/java/com/neptuneclient/voidui/testmod/mixins/MixinInGameHud.java create mode 100644 test-mod/src/main/kotlin/com/neptuneclient/voidui/testmod/example/TestScreen.kt create mode 100644 test-mod/src/main/kotlin/com/neptuneclient/voidui/testmod/example/TestTheme.kt diff --git a/src/main/kotlin/com/neptuneclient/voidui/widgets/elements.kt b/src/main/kotlin/com/neptuneclient/voidui/widgets/elements.kt index e51f808..63c115e 100644 --- a/src/main/kotlin/com/neptuneclient/voidui/widgets/elements.kt +++ b/src/main/kotlin/com/neptuneclient/voidui/widgets/elements.kt @@ -138,7 +138,6 @@ class Text(private val label: String) : Element() { } override fun render(renderer: Renderer) { -// println("Rendering at $offset") renderer.text(offset.x, offset.y, label, font, styles.color) } diff --git a/src/test/kotlin/com/neptuneclient/voidui/tests/ScreenTest.kt b/src/test/kotlin/com/neptuneclient/voidui/tests/ScreenTest.kt index d8e92bc..d6b7057 100644 --- a/src/test/kotlin/com/neptuneclient/voidui/tests/ScreenTest.kt +++ b/src/test/kotlin/com/neptuneclient/voidui/tests/ScreenTest.kt @@ -20,7 +20,7 @@ class TestScreen(void: VoidUI) : Screen(void) { gap = 10, children = arrayOf( Text("Title"), - Text("Bitte fick mich äöü"), + Text("Hello World!") ) ) ) diff --git a/test-mod/src/main/java/com/neptuneclient/voidui/testmod/ModEntry.java b/test-mod/src/main/java/com/neptuneclient/voidui/testmod/ModEntry.java index 88d078e..d77a812 100644 --- a/test-mod/src/main/java/com/neptuneclient/voidui/testmod/ModEntry.java +++ b/test-mod/src/main/java/com/neptuneclient/voidui/testmod/ModEntry.java @@ -1,29 +1,44 @@ package com.neptuneclient.voidui.testmod; import com.neptuneclient.voidui.VoidUI; +import com.neptuneclient.voidui.testmod.example.TestScreen; +import com.neptuneclient.voidui.testmod.example.TestTheme; import com.neptuneclient.voidui.testmod.impl.RendererImpl; -import com.neptuneclient.voidui.utils.Font; +import com.neptuneclient.voidui.testmod.impl.ScreenBridge; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents; +import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback; +import net.minecraft.client.MinecraftClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.nio.file.Path; +import static net.minecraft.server.command.CommandManager.literal; public class ModEntry implements ClientModInitializer { public static final Logger LOGGER = LoggerFactory.getLogger("TestMod"); + public static VoidUI voidUI; + @Override public void onInitializeClient() { - LOGGER.info("VoidUI is best!"); - - ClientLifecycleEvents.CLIENT_STARTED.register((client) -> { - VoidUI vui = new VoidUI(new RendererImpl()); + ClientLifecycleEvents.CLIENT_STARTED.register((mc) -> { + voidUI = new VoidUI(new RendererImpl(), new TestTheme()); + }); - // testing if nanovg will handle the font buffer - new Font(vui, "testFont", Path.of("fonts/WorkSans-Regular.ttf"), 28); + CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> { + dispatcher.register(literal("examplemod").executes(context -> { + try { + MinecraftClient.getInstance().submit(() -> { + MinecraftClient.getInstance().setScreen(new ScreenBridge(new TestScreen())); + }); + } catch (Exception e) { + e.printStackTrace(); + } + return 1; + })); }); + LOGGER.info("VoidUI is best!"); } } diff --git a/test-mod/src/main/java/com/neptuneclient/voidui/testmod/impl/RendererImpl.java b/test-mod/src/main/java/com/neptuneclient/voidui/testmod/impl/RendererImpl.java index 351b0ac..aa6f128 100644 --- a/test-mod/src/main/java/com/neptuneclient/voidui/testmod/impl/RendererImpl.java +++ b/test-mod/src/main/java/com/neptuneclient/voidui/testmod/impl/RendererImpl.java @@ -2,13 +2,18 @@ import com.neptuneclient.voidui.rendering.Renderer; import com.neptuneclient.voidui.utils.Font; +import com.neptuneclient.voidui.widgets.objects.EdgeInsets; +import com.neptuneclient.voidui.widgets.objects.Size; import kotlin.Pair; import net.minecraft.client.MinecraftClient; import org.jetbrains.annotations.NotNull; +import org.lwjgl.BufferUtils; import org.lwjgl.nanovg.NVGColor; import org.lwjgl.nanovg.NanoVGGL3; import java.awt.Color; +import java.nio.ByteBuffer; +import java.nio.FloatBuffer; import static org.lwjgl.nanovg.NanoVG.*; @@ -80,12 +85,70 @@ public int windowHeight() { @Override public void text(float x, float y, @NotNull String text, @NotNull Font font, @NotNull Color color) { + NVGColor c = NVGColor.calloc(); + c.r(color.getRed()); + c.g(color.getGreen()); + c.b(color.getBlue()); + c.a(color.getAlpha()); + + nvgFontFace(vg, font.getIdentifier()); + nvgFontSize(vg, font.getSize()); + nvgTextMetrics(vg, null, null, BufferUtils.createFloatBuffer(1)); + + FloatBuffer bounds = BufferUtils.createFloatBuffer(4); + nvgTextBounds(vg, x, y, text, bounds); + + nvgFillColor(vg, c); + nvgText(vg, x, y + (y - bounds.get(1)), text); + nvgClosePath(vg); + c.free(); } @NotNull @Override - public Pair getTextBounds(@NotNull String text, @NotNull Font font) { - return null; + public Size getTextBounds(@NotNull String text, @NotNull Font font) { + FloatBuffer buffer = BufferUtils.createFloatBuffer(4); + + nvgFontSize(vg, font.getSize()); + nvgFontFace(vg, font.getIdentifier()); + nvgTextBounds(vg, 0f, 0f, text, buffer); + return new Size(buffer.get(2) - buffer.get(0), buffer.get(3) - buffer.get(1)); + } + + @Override + public void rectangleFrame(float x, float y, float width, float height, float thickness, @NotNull Color color) { + NVGColor c = NVGColor.calloc(); + c.r(color.getRed()); + c.g(color.getGreen()); + c.b(color.getBlue()); + c.a(color.getAlpha()); + + nvgBeginPath(vg); + nvgRect(vg, x, y, width, height); + nvgStrokeColor(vg, c); + nvgStrokeWidth(vg, thickness); + nvgStroke(vg); + nvgClosePath(vg); + + c.free(); + } + + @Override + public void roundedRectangleFrame(float x, float y, float width, float height, @NotNull EdgeInsets thickness, float radius, @NotNull Color color) { + NVGColor c = NVGColor.calloc(); + c.r(color.getRed()); + c.g(color.getGreen()); + c.b(color.getBlue()); + c.a(color.getAlpha()); + + nvgBeginPath(vg); + nvgRect(vg, x, y, width, height); + nvgStrokeColor(vg, c); + nvgStrokeWidth(vg, thickness.component1()); + nvgStroke(vg); + nvgClosePath(vg); + + c.free(); } } diff --git a/test-mod/src/main/java/com/neptuneclient/voidui/testmod/impl/ScreenBridge.java b/test-mod/src/main/java/com/neptuneclient/voidui/testmod/impl/ScreenBridge.java new file mode 100644 index 0000000..7c33e8b --- /dev/null +++ b/test-mod/src/main/java/com/neptuneclient/voidui/testmod/impl/ScreenBridge.java @@ -0,0 +1,26 @@ +package com.neptuneclient.voidui.testmod.impl; + +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; + +public class ScreenBridge extends Screen { + + private com.neptuneclient.voidui.widgets.Screen voidScreen; + + public ScreenBridge(com.neptuneclient.voidui.widgets.Screen voidScreen) { + super(Text.of(voidScreen.getClass().getSimpleName())); + this.voidScreen = voidScreen; + } + + @Override + protected void init() { + voidScreen.init(); + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + voidScreen.render(); + } + +} diff --git a/test-mod/src/main/java/com/neptuneclient/voidui/testmod/mixins/MixinInGameHud.java b/test-mod/src/main/java/com/neptuneclient/voidui/testmod/mixins/MixinInGameHud.java deleted file mode 100644 index 9f327ea..0000000 --- a/test-mod/src/main/java/com/neptuneclient/voidui/testmod/mixins/MixinInGameHud.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.neptuneclient.voidui.testmod.mixins; - -import com.neptuneclient.voidui.VoidUI; -import com.neptuneclient.voidui.testmod.impl.RendererImpl; -import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.gui.hud.InGameHud; -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.callback.CallbackInfo; - -import java.awt.Color; - -@Mixin(InGameHud.class) -public class MixinInGameHud { - - private VoidUI v = new VoidUI(new RendererImpl()); - - @Inject(method = "render", at = @At("RETURN")) - public void mixin$render(DrawContext context, float tickDelta, CallbackInfo ci) { - // i have looked at oneconfig to see how they render with nanovg - // but it didnt make me smarter - - v.getRenderer().beginFrame(); - v.getRenderer().roundedRectangle(100, 100, 200, 120, 15, Color.RED); - v.getRenderer().endFrame(); - } - -} diff --git a/test-mod/src/main/kotlin/com/neptuneclient/voidui/testmod/example/TestScreen.kt b/test-mod/src/main/kotlin/com/neptuneclient/voidui/testmod/example/TestScreen.kt new file mode 100644 index 0000000..d274d1d --- /dev/null +++ b/test-mod/src/main/kotlin/com/neptuneclient/voidui/testmod/example/TestScreen.kt @@ -0,0 +1,24 @@ +package com.neptuneclient.voidui.testmod.example + +import com.neptuneclient.voidui.testmod.ModEntry +import com.neptuneclient.voidui.widgets.* +import com.neptuneclient.voidui.widgets.objects.EdgeInsets + +class TestScreen : Screen(ModEntry.voidUI) { + + override fun build(): Widget { + return BackgroundPanel( + child = Padding( + padding = EdgeInsets.all(20F), + child = Column( + gap = 10, + children = arrayOf( + Text("Hello World"), + Text("Heh") + ) + ) + ) + ) + } + +} \ No newline at end of file diff --git a/test-mod/src/main/kotlin/com/neptuneclient/voidui/testmod/example/TestTheme.kt b/test-mod/src/main/kotlin/com/neptuneclient/voidui/testmod/example/TestTheme.kt new file mode 100644 index 0000000..1164d58 --- /dev/null +++ b/test-mod/src/main/kotlin/com/neptuneclient/voidui/testmod/example/TestTheme.kt @@ -0,0 +1,52 @@ +package com.neptuneclient.voidui.testmod.example + +import com.neptuneclient.voidui.themes.Styles +import com.neptuneclient.voidui.themes.Theme +import com.neptuneclient.voidui.themes.objects.Border +import com.neptuneclient.voidui.themes.styles.PanelStyleSheet +import com.neptuneclient.voidui.themes.styles.TextStyleSheet +import com.neptuneclient.voidui.widgets.objects.EdgeInsets +import java.awt.Color +import java.nio.file.Path + +class TestTheme : Theme( + backgroundPanel = Styles( + normal = PanelStyleSheet( + color = Color(0x101216), + radius = 10 + ) + ), + accentBackgroundPanel = Styles( + normal = PanelStyleSheet( + color = Color(26, 31, 41, 220), + radius = 10 + ) + ), + panel = Styles( + normal = PanelStyleSheet( + color = Color(0x272935), + radius = 10, + border = Border( + sides = EdgeInsets.all(1f), + color = Color(255, 255, 255, 40) + ) + ) + ), + accentPanel = Styles( + normal = PanelStyleSheet( + color = Color(0), + radius = 10, + border = Border( + sides = EdgeInsets.all(1f), + color = Color(255, 255, 255, 40) + ) + ) + ), + text = Styles( + normal = TextStyleSheet( + color = Color.WHITE, + font = Path.of("fonts/WorkSans-Regular.ttf"), + size = 16 + ) + ) +) \ No newline at end of file diff --git a/test-mod/src/main/resources/void.mixins.json b/test-mod/src/main/resources/void.mixins.json index a40bead..685e4e4 100644 --- a/test-mod/src/main/resources/void.mixins.json +++ b/test-mod/src/main/resources/void.mixins.json @@ -7,6 +7,5 @@ "defaultRequire": 1 }, "client": [ - "MixinInGameHud" ] } \ No newline at end of file