aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/polyfrost/oneconfig/hud/interfaces/TextHud.java
blob: 299e7b89866b129bb63084a781b200435aac5ea1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package io.polyfrost.oneconfig.hud.interfaces;

import io.polyfrost.oneconfig.lwjgl.RenderManager;
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;
    boolean doExample = false;
    private List<String> cachedLines;
    private int cachedWidth;
    private int cachedHeight;
    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++) {
            RenderManager.drawScaledString(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);
    }
}