diff options
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/hud')
3 files changed, 164 insertions, 0 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/hud/HudCore.java b/src/main/java/io/polyfrost/oneconfig/hud/HudCore.java new file mode 100644 index 0000000..c0e29d1 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/hud/HudCore.java @@ -0,0 +1,18 @@ +package io.polyfrost.oneconfig.hud; + +import io.polyfrost.oneconfig.hud.interfaces.BasicHud; +import net.minecraftforge.client.event.RenderGameOverlayEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +import java.util.ArrayList; + +public class HudCore { + public static ArrayList<BasicHud> huds = new ArrayList<>(); + + @SubscribeEvent + public void onRender(RenderGameOverlayEvent.Post event) { + if (event.type != RenderGameOverlayEvent.ElementType.ALL) return; + for (BasicHud hud : huds) + hud.drawAll(20,20, 5); + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/hud/interfaces/BasicHud.java b/src/main/java/io/polyfrost/oneconfig/hud/interfaces/BasicHud.java new file mode 100644 index 0000000..121c258 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/hud/interfaces/BasicHud.java @@ -0,0 +1,48 @@ +package io.polyfrost.oneconfig.hud.interfaces; + +import io.polyfrost.oneconfig.renderer.Renderer; + +import java.awt.*; + +public abstract class BasicHud { + public int normalX; + public int normalY; + public int paddingX = 5; + public int paddingY = 5; + public boolean background = true; + public boolean rounded = false; + + public abstract int getWidth(float scale); + + public abstract int getHeight(float scale); + + public abstract void draw(int x, int y, float scale); + + public int getExampleWidth(float scale) { + return getWidth(scale); + } + + public int getExampleHeight(float scale) { + return getHeight(scale); + } + + public void drawAll(int x, int y, float scale) { + drawBackGround(x, y, scale); + draw(x, y, scale); + } + + public void drawExampleAll(int x, int y, float scale) { + drawBackGround(x, y, scale); + drawExample(x, y, scale); + } + + public void drawExample(int x, int y, float scale) { + draw(x, y, scale); + } + + private void drawBackGround(int x, int y, float scale) { + Renderer.drawRoundRect(x - paddingX * scale / 2f, y - paddingY * scale / 2f, + getWidth(scale) + paddingX * scale, getHeight(scale) + paddingY * scale, + (2 * scale), new Color(0, 0, 0, 100).getRGB()); + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/hud/interfaces/TextHud.java b/src/main/java/io/polyfrost/oneconfig/hud/interfaces/TextHud.java new file mode 100644 index 0000000..33bbd79 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/hud/interfaces/TextHud.java @@ -0,0 +1,98 @@ +package io.polyfrost.oneconfig.hud.interfaces; + +import io.polyfrost.oneconfig.renderer.Renderer; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.FontRenderer; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.gameevent.TickEvent; + +import java.util.List; + +public class TextHud extends BasicHud { + /** + * Currently doesn't work because of double extend, will have to be redone somehow (I have no idea how yet) + */ + private final FontRenderer fb = Minecraft.getMinecraft().fontRendererObj; + boolean shadow = false; + private List<String> cachedLines; + private int cachedWidth; + private int cachedHeight; + boolean doExample = false; + private List<String> cachedExampleLines; + private int cachedExampleWidth; + private int cachedExampleHeight; + + protected List<String> update() { + return null; + } + + @SubscribeEvent + private void onTick(TickEvent.ClientTickEvent event) { + if (event.phase != TickEvent.Phase.START) return; + cachedLines = update(); + if (cachedLines != null) { + cachedHeight = cachedLines.size() * (fb.FONT_HEIGHT + 3); + cachedWidth = 0; + for (String line : cachedLines) { + int width = fb.getStringWidth(line); + if (width > cachedWidth) + cachedWidth = width; + } + } + if (doExample) { + cachedExampleLines = updateExample(); + if (cachedExampleLines != null) { + cachedExampleHeight = cachedExampleLines.size() * 12; + cachedExampleWidth = 0; + for (String line : cachedExampleLines) { + int width = fb.getStringWidth(line); + if (width > cachedExampleWidth) + cachedExampleWidth = width; + } + } + } + } + + protected List<String> updateExample() { + return update(); + } + + @Override + public void draw(int x, int y, float scale) { + if (cachedLines != null) + drawText(cachedLines, x, y, scale); + } + + @Override + public void drawExample(int x, int y, float scale) { + doExample = true; + if (cachedExampleLines != null) + drawText(cachedExampleLines, x, y, scale); + } + + private void drawText(List<String> lines, int x, int y, float scale) { + for (int i = 0; i < lines.size(); i++) { + Renderer.drawTextScale(lines.get(i), x, y + i * 12, 0xffffff, shadow, scale); + } + } + + @Override + public int getWidth(float scale) { + return (int) (cachedWidth * scale); + } + + @Override + public int getHeight(float scale) { + return (int) (cachedHeight * scale); + } + + @Override + public int getExampleWidth(float scale) { + return (int) (cachedExampleWidth * scale); + } + + @Override + public int getExampleHeight(float scale) { + return (int) (cachedExampleHeight * scale); + } +} |