Skip to content

Commit

Permalink
rework
Browse files Browse the repository at this point in the history
  • Loading branch information
MeiNanziiii committed Nov 3, 2024
1 parent 66f8ab6 commit 4ec2630
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 24 deletions.
28 changes: 16 additions & 12 deletions src/main/java/ua/mei/mgui/api/hud/HudAlign.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
package ua.mei.mgui.api.hud;

import java.util.function.BiFunction;

@SuppressWarnings({"unused"})
public enum HudAlign {
LEFT(
(firstElement, element) -> -element.glyph.glyphWidth,
(firstElement, element) -> -(firstElement.glyph.glyphWidth - element.glyph.glyphWidth)
(firstElement, element, context) -> -element.getWidth(context),
(firstElement, element, context) -> -firstElement.getWidth(context) + element.getWidth(context)
),
CENTER(
(firstElement, element) -> -element.glyph.glyphWidth,
(firstElement, element) -> -((firstElement.glyph.glyphWidth - element.glyph.glyphWidth) / 2)
(firstElement, element, context) -> -element.getWidth(context),
(firstElement, element, context) -> (firstElement.getWidth(context) - element.getWidth(context)) / -2
),
RIGHT(
(firstElement, element) -> -element.glyph.glyphWidth,
(firstElement, element) -> 0
(firstElement, element, context) -> -element.getWidth(context),
(firstElement, element, context) -> 0
);

public final BiFunction<HudElement, HudElement, Integer> spaceBefore;
public final BiFunction<HudElement, HudElement, Integer> spaceAfter;
public final HudAlignFunction spaceBefore;
public final HudAlignFunction offset;

HudAlign(BiFunction<HudElement, HudElement, Integer> spaceBefore, BiFunction<HudElement, HudElement, Integer> spaceAfter) {
HudAlign(HudAlignFunction spaceBefore, HudAlignFunction offset) {
this.spaceBefore = spaceBefore;
this.spaceAfter = spaceAfter;
this.offset = offset;
}

@FunctionalInterface
public interface HudAlignFunction {
Integer apply(HudElement firstElement, HudElement element, HudDrawContext context);
}
}
2 changes: 1 addition & 1 deletion src/main/java/ua/mei/mgui/api/hud/HudDrawContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public MutableText draw() {
if (i != 0) {
HudElement firstElement = elements.getFirst();

text.append(formatter.spaceBefore(element.align.spaceBefore.apply(firstElement, element) - 1).offset(element.x + element.align.spaceAfter.apply(firstElement, element)).value);
text.append(formatter.spaceBefore(element.align.spaceBefore.apply(firstElement, element, this) - 1).offset(element.x + element.align.offset.apply(firstElement, element, this)).value);
} else {
text.append(formatter.offset(element.x).value);
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/ua/mei/mgui/api/hud/HudElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
@SuppressWarnings({"unused"})
public class HudElement {
public final BitmapGlyph glyph;

public final int y;
public int x = 0;

public HudAlign align = HudAlign.LEFT;
public HudAlign align = HudAlign.CENTER;
public int x = 0;

private HudElement(BitmapGlyph glyph) {
this.glyph = glyph;
Expand All @@ -23,4 +22,8 @@ public static HudElement create(ServerHud hud, String path, int y) {
public static HudElement create(ServerHud hud, String path, int height, int y) {
return new HudElement(hud.getResourceManager().requestGlyph(path, height, y - 64));
}

public int getWidth(HudDrawContext context) {
return this.glyph.glyphWidth;
}
}
15 changes: 9 additions & 6 deletions src/main/java/ua/mei/mgui/api/hud/ServerHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import ua.mei.pfu.api.font.FontResourceManager;

Expand All @@ -12,27 +11,31 @@
public abstract class ServerHud {
private final List<ServerPlayerEntity> players = new ArrayList<>();

public MutableText draw(HudDrawContext context) {
return context.draw();
}
public abstract void draw(HudDrawContext context);

public void tick(MinecraftServer server) {
if (server.getTicks() % 5 == 0) {
for (ServerPlayerEntity player : players) {
player.sendMessageToClient(draw(new HudDrawContext((player))), true);
drawToPlayer(player);
}
}
}

public void show(ServerPlayerEntity player) {
players.add(player);
player.sendMessageToClient(draw(new HudDrawContext((player))), true);
drawToPlayer(player);
}

public void hide(ServerPlayerEntity player) {
players.remove(player);
player.sendMessageToClient(Text.empty(), true);
}

public void drawToPlayer(ServerPlayerEntity player) {
HudDrawContext context = new HudDrawContext((player));
draw(context);
player.sendMessageToClient(context.draw(), true);
}

public abstract FontResourceManager getResourceManager();
}
7 changes: 5 additions & 2 deletions src/test/java/ua/mei/mgui/test/TestHud.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ua.mei.mgui.test;

import net.minecraft.text.MutableText;
import ua.mei.mgui.api.hud.HudAlign;
import ua.mei.mgui.api.hud.HudElement;
import ua.mei.mgui.api.hud.HudDrawContext;
import ua.mei.mgui.api.hud.ServerHud;
Expand All @@ -15,17 +16,19 @@ public TestHud() {
this.firstElement = HudElement.create(this, "gui/sprite_2.png", 96);
this.secondElement = HudElement.create(this, "gui/sprite_0.png", 64);
this.thirdElement = HudElement.create(this, "gui/sprite_1.png", 32);

this.firstElement.align = HudAlign.LEFT;
this.thirdElement.align = HudAlign.RIGHT;
}

@Override
public MutableText draw(HudDrawContext context) {
public void draw(HudDrawContext context) {
firstElement.x = (int) Math.clamp(context.player.getX(), -256, 256);
secondElement.x = -firstElement.x;
thirdElement.x = (int) Math.clamp(context.player.getY(), -256, 256);
context.drawElement(firstElement);
context.drawElement(secondElement);
context.drawElement(thirdElement);
return super.draw(context);
}

@Override
Expand Down

0 comments on commit 4ec2630

Please sign in to comment.