Skip to content

Commit

Permalink
implemented void in test mod
Browse files Browse the repository at this point in the history
  • Loading branch information
Hobbyshop committed Mar 31, 2024
1 parent ec25487 commit 9d701f6
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ class Text(private val label: String) : Element<TextStyleSheet>() {
}

override fun render(renderer: Renderer) {
// println("Rendering at $offset")
renderer.text(offset.x, offset.y, label, font, styles.color)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TestScreen(void: VoidUI) : Screen(void) {
gap = 10,
children = arrayOf(
Text("Title"),
Text("Bitte fick mich äöü"),
Text("Hello World!")
)
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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!");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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<Float, Float> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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")
)
)
)
)
}

}
Original file line number Diff line number Diff line change
@@ -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
)
)
)
1 change: 0 additions & 1 deletion test-mod/src/main/resources/void.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
"defaultRequire": 1
},
"client": [
"MixinInGameHud"
]
}

0 comments on commit 9d701f6

Please sign in to comment.