Skip to content

Commit

Permalink
NumberPart and example
Browse files Browse the repository at this point in the history
  • Loading branch information
MeiNanziiii committed Nov 3, 2024
1 parent d9abf2e commit b1534f8
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/main/java/ua/mei/mgui/api/hud/ServerHudRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import net.minecraft.util.Identifier;

import java.util.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class ServerHudRegistry {
private static Map<Identifier, ServerHud> registeredHuds = new LinkedHashMap<>();
private static final Map<Identifier, ServerHud> registeredHuds = new LinkedHashMap<>();

public static ServerHud register(Identifier identifier, Supplier<ServerHud> hudSupplier) {
if (registeredHuds.containsKey(identifier)) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/ua/mei/mgui/api/hud/part/GlyphPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.text.MutableText;
import ua.mei.mgui.api.hud.ServerHud;
import ua.mei.pfu.api.font.BitmapGlyph;
import ua.mei.pfu.api.font.FontResourceManager;

@SuppressWarnings({"unused"})
public class GlyphPart extends HudPart<GlyphPart> {
Expand All @@ -26,6 +27,11 @@ public static GlyphPart create(ServerHud hud, String path, int height, int y) {
return new GlyphPart(hud, glyph);
}

public static GlyphPart create(ServerHud hud, String path, int y, FontResourceManager manager) {
BitmapGlyph glyph = manager.requestGlyph(path, y - 64);
return new GlyphPart(hud, glyph);
}

@Override
public MutableText render() {
return this.glyph.value;
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/ua/mei/mgui/api/hud/part/NumberPart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ua.mei.mgui.api.hud.part;

import ua.mei.mgui.api.hud.ServerHud;
import ua.mei.pfu.api.font.FontResourceManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class NumberPart extends HudGroup {
public static final FontResourceManager manager = FontResourceManager.create("mgui", "mgui", "numbers");

public final Map<Character, GlyphPart> glyphs;
public final int y;

protected NumberPart(ServerHud hud, int y) {
super(hud, List.of());

this.glyphs = new HashMap<>();

for (int i = 0; i < 10; i++) {
glyphs.put((char) ('0' + i), GlyphPart.create(hud, "hud/five/number_" + i + ".png", y, manager));
}
glyphs.put('-', GlyphPart.create(hud, "hud/five/number_minus.png", y, manager));

this.y = y;

setNumber(0);
}

public static NumberPart create(ServerHud hud, int y) {
return new NumberPart(hud, y);
}

public NumberPart setNumber(int number) {
this.parts.clear();

int offset = 0;

for (Character digitChar : String.valueOf(number).toCharArray()) {
GlyphPart glyph = glyphs.get(digitChar);
glyph = new GlyphPart(glyph.hud, glyph.glyph).xOffset(offset);
parts.add(glyph);
offset += glyph.getWidth() + 1;
}

return this;
}
}
5 changes: 4 additions & 1 deletion src/main/java/ua/mei/mgui/impl/ServerHudRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import ua.mei.mgui.api.hud.ServerHudRegistry;
import ua.mei.mgui.api.hud.part.HudGroup;

import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ApiStatus.Internal
@SuppressWarnings({"unused"})
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 1 addition & 6 deletions src/test/java/ua/mei/mgui/test/MGuiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import eu.pb4.polymer.resourcepack.api.PolymerResourcePackUtils;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.util.Identifier;
import ua.mei.mgui.api.hud.ServerHud;
Expand All @@ -12,23 +11,19 @@
public class MGuiTest implements ModInitializer {
public static FontResourceManager manager;
public static ServerHud hud;
public static ServerHud hud2;

@Override
public void onInitialize() {
PolymerResourcePackUtils.addModAssets("mgui_test");

manager = FontResourceManager.create("mgui_test", "mgui_test", "test");
hud2 = ServerHudRegistry.register(Identifier.of("mgui_test:test_hud_2"), YetAnotherTestHud::new);
hud = ServerHudRegistry.register(Identifier.of("mgui_test:test_hud"), TestHud::new);
hud = ServerHudRegistry.register(Identifier.of("mgui_test:test_hud"), YetAnotherTestHud::new);

ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
hud.show(handler.player);
hud2.show(handler.player);
});
ServerPlayConnectionEvents.DISCONNECT.register((handler, server) -> {
hud.hide(handler.player);
hud2.show(handler.player);
});
}
}
20 changes: 18 additions & 2 deletions src/test/java/ua/mei/mgui/test/YetAnotherTestHud.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
package ua.mei.mgui.test;

import net.minecraft.server.network.ServerPlayerEntity;
import ua.mei.mgui.api.hud.HudAlign;
import ua.mei.mgui.api.hud.ServerHud;
import ua.mei.mgui.api.hud.part.GlyphPart;
import ua.mei.mgui.api.hud.part.NumberPart;
import ua.mei.pfu.api.font.FontResourceManager;

public class YetAnotherTestHud extends ServerHud {
public final NumberPart ticks;

public YetAnotherTestHud() {
root.addPart(GlyphPart.create(this, "gui/sprite_2.png", 192));
ticks = NumberPart.create(this, 8);

root.addPart(ticks).align(HudAlign.LEFT).xOffset(96);
}

@Override
public void update(ServerPlayerEntity player) {
ticks.setNumber(player.server.getTicks());
}

@Override
public void tick(ServerPlayerEntity player) {
update(player);
}

@Override
Expand Down

0 comments on commit b1534f8

Please sign in to comment.