aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/overlays/TextOverlay.java
blob: c3f4ba9933757a30bff0995f1a3b434914e6418c (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package io.github.moulberry.notenoughupdates.overlays;

import io.github.moulberry.notenoughupdates.core.config.Position;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.GlStateManager;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL14;
import org.lwjgl.util.vector.Vector2f;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public abstract class TextOverlay {

    private Position position;
    protected Supplier<TextOverlayStyle> styleSupplier;
    public int overlayWidth = -1;
    public int overlayHeight = -1;
    public List<String> overlayStrings = null;
    private Supplier<List<String>> dummyStrings;

    public boolean shouldUpdateFrequent = false;

    private static final int PADDING_X = 5;
    private static final int PADDING_Y = 5;

    public TextOverlay(Position position, Supplier<List<String>> dummyStrings, Supplier<TextOverlayStyle> styleSupplier) {
        this.position = position;
        this.styleSupplier = styleSupplier;
        if(dummyStrings == null) {
            this.dummyStrings = () -> null;
        } else {
            this.dummyStrings = dummyStrings;
        }
    }

    public Vector2f getDummySize() {
        List<String> dummyStrings = this.dummyStrings.get();

        if(dummyStrings != null) {
            return getSize(dummyStrings);
        }
        return new Vector2f(100, 50);
    }

    public void tick() {
        update();
    }

    public void updateFrequent() {}
    public abstract void update();

    public void renderDummy() {
        List<String> dummyStrings = this.dummyStrings.get();
        render(dummyStrings, true);
    }

    public void render() {
        if(shouldUpdateFrequent) {
            updateFrequent();
            shouldUpdateFrequent = false;
        }
        render(overlayStrings, false);
    }

    protected Vector2f getSize(List<String> strings) {
        int overlayHeight = 0;
        int overlayWidth = 0;
        for(String s : strings) {
            if(s == null) {
                overlayHeight += 3;
                continue;
            }
            for(String s2 : s.split("\n")) {
                int sWidth = Minecraft.getMinecraft().fontRendererObj.getStringWidth(s2);
                if(sWidth > overlayWidth) {
                    overlayWidth = sWidth;
                }
                overlayHeight += 10;
            }
        }
        overlayHeight -= 2;

        int paddingX = 0;
        int paddingY = 0;
        if(styleSupplier.get() == TextOverlayStyle.BACKGROUND) {
            paddingX = PADDING_X;
            paddingY = PADDING_Y;
        }
        return new Vector2f(overlayWidth+paddingX*2, overlayHeight+paddingY*2);
    }

    protected Vector2f getTextOffset() {
        return new Vector2f();
    }

    protected Vector2f getPosition(int overlayWidth, int overlayHeight) {
        ScaledResolution scaledResolution = new ScaledResolution(Minecraft.getMinecraft());

        int x = position.getAbsX(scaledResolution, overlayWidth);
        int y = position.getAbsY(scaledResolution, overlayHeight);

        return new Vector2f(x, y);
    }

    protected void renderLine(String line, Vector2f position, boolean dummy) {}

    private void render(List<String> strings, boolean dummy) {
        if(strings == null) return;

        Vector2f size = getSize(strings);
        overlayHeight = (int)size.y;
        overlayWidth = (int)size.x;

        Vector2f position = getPosition(overlayWidth, overlayHeight);
        int x = (int)position.x;
        int y = (int)position.y;

        TextOverlayStyle style = styleSupplier.get();

        if(style == TextOverlayStyle.BACKGROUND) Gui.drawRect(x, y, x+overlayWidth, y+overlayHeight, 0x80000000);

        GlStateManager.enableBlend();
        GL14.glBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
        GlStateManager.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);

        int paddingX = 0;
        int paddingY = 0;
        if(styleSupplier.get() == TextOverlayStyle.BACKGROUND) {
            paddingX = PADDING_X;
            paddingY = PADDING_Y;
        }

        Vector2f textOffset = getTextOffset();
        paddingX += (int) textOffset.x;
        paddingY += (int) textOffset.y;

        int yOff = 0;
        for(String s : strings) {
            if(s == null) {
                yOff += 3;
            } else {
                for(String s2 : s.split("\n")) {
                    Vector2f pos = new Vector2f(x+paddingX, y+paddingY+yOff);
                    renderLine(s2, pos, dummy);

                    int xPad = (int)pos.x;
                    int yPad = (int)pos.y;

                    if(style == TextOverlayStyle.FULL_SHADOW) {
                        String clean = Utils.cleanColourNotModifiers(s2);
                        for(int xO=-2; xO<=2; xO++) {
                            for(int yO=-2; yO<=2; yO++) {
                                if(Math.abs(xO) != Math.abs(yO)) {
                                    Minecraft.getMinecraft().fontRendererObj.drawString(clean,
                                            xPad+xO/2f, yPad+yO/2f,
                                            new Color(0, 0, 0, 200/Math.max(Math.abs(xO), Math.abs(yO))).getRGB(), false);
                                }
                            }
                        }
                    }
                    Minecraft.getMinecraft().fontRendererObj.drawString(s2,
                            xPad, yPad, 0xffffff, style == TextOverlayStyle.MC_SHADOW);

                    yOff += 10;
                }
            }
        }
    }

}