aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/polyfrost/oneconfig/themes/textures/TextureManager.java
blob: a2e96d4c0c481caeeca78538d4c4f0c6c0e529e6 (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
package io.polyfrost.oneconfig.themes.textures;

import io.polyfrost.oneconfig.themes.Theme;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.crash.CrashReport;
import net.minecraft.util.ReportedException;
import net.minecraft.util.ResourceLocation;
import org.jetbrains.annotations.NotNull;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static io.polyfrost.oneconfig.themes.Themes.activeTheme;
import static io.polyfrost.oneconfig.themes.Themes.themeLog;

public class TextureManager {
    private static final Minecraft mc = Minecraft.getMinecraft();
    private final List<ResourceLocation> resources = new ArrayList<>();
    private final List<TickableTexture> tickableTextures = new ArrayList<>();

    /**
     * Create a new texture manager for this theme, used for drawing of icons, etc.
     */
    public TextureManager(Theme theme) {
        for (ThemeElement element : ThemeElement.values()) {
            BufferedImage img;
            try {
                img = ImageIO.read(theme.getResource(element.location));
            } catch (Exception e) {
                themeLog.error("failed to get themed texture: " + element.location + ", having to fallback to default one. Is pack invalid?");
                try {
                    img = ImageIO.read(mc.getResourceManager().getResource(new ResourceLocation("oneconfig", element.location)).getInputStream());
                } catch (IOException ex) {
                    themeLog.fatal("failed to get fallback texture: " + element.location + ", game will crash :(");
                    throw new ReportedException(new CrashReport("TextureManager failure: FALLBACK_ERROR_OR_MISSING", ex));
                }
            }
            ResourceLocation location = mc.getTextureManager().getDynamicTextureLocation(element.location, new DynamicTexture(img));
            resources.add(location);
            if (img.getWidth() != element.size) {
                themeLog.warn("Theme element " + element.name() + " with size " + img.getWidth() + "px is not recommended, expected " + element.size + "px. Continuing anyway.");
            }
            if (element.ordinal() < 26) {
                if (img.getWidth() != img.getHeight()) {
                    themeLog.info("found tickable animated texture (" + element.name() + "). Loading texture");
                    try {
                        tickableTextures.add(new TickableTexture(element));
                    } catch (IOException e) {
                        themeLog.error("failed to create TickableTexture " + element.location + ". Just going to load it as a normal texture, this may break things!");
                        e.printStackTrace();
                    }
                }
            } else {
                if (element.ordinal() < 29) {
                    if (img.getHeight() != 144 || img.getWidth() != 758) {
                        themeLog.warn("found badly sized button texture " + element.location);
                    }
                }
            }
        }
    }

    /**
     * Draw the specified {@link ThemeElement} at the coordinates, scaled to the width and height.
     *
     * @param element element to draw
     * @param x       x coordinate (top left)
     * @param y       y coordinate (top left)
     * @param width   width of the image
     * @param height  height of the image
     */
    public void draw(@NotNull ThemeElement element, int x, int y, int width, int height) {
        if (activeTheme.isReady()) {
            ResourceLocation location = resources.get(element.ordinal());
            mc.getTextureManager().bindTexture(location);
            GlStateManager.enableBlend();
            try {
                if (!tickableTextures.isEmpty()) {
                    for (TickableTexture texture : tickableTextures) {
                        if (texture.getElement().equals(element)) {
                            texture.draw(x, y);
                        } else {
                            Gui.drawScaledCustomSizeModalRect(x, y, 0, 0, width, height, width, height, width, height);
                        }
                    }
                } else {
                    Gui.drawScaledCustomSizeModalRect(x, y, 0, 0, width, height, width, height, width, height);
                }
                GlStateManager.disableBlend();
                GlStateManager.color(1f, 1f, 1f, 1f);
            } catch (Exception e) {
                themeLog.error("Error occurred drawing texture " + element.name() + ", is theme invalid?", e);
            }
        }
    }

    /**
     * Draw the specified {@link ThemeElement} at the coordinates, using its recommended width and height.
     *
     * @param element element to draw
     * @param x       x coordinate (top left)
     * @param y       y coordinate (top left)
     */
    public void draw(@NotNull ThemeElement element, int x, int y) {
        this.draw(element, x, y, element.size, element.size);
    }
}