aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/polyfrost/oneconfig/themes/Theme.java
blob: 59b5767423a30516dffa7affa102d9d28016c6b8 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package io.polyfrost.oneconfig.themes;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.polyfrost.oneconfig.renderer.Renderer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.FileResourcePack;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

@SuppressWarnings("unused")
public class Theme extends FileResourcePack {
    private final File themeFile;
    private final File themeConfigFile;
    private static final Minecraft mc = Minecraft.getMinecraft();
    private final JsonObject packMetadata;
    private final JsonObject packConfig;

    /**
     * Create a new theme instance for the window.
     * @param themePack file of the pack
     * @throws IOException if an error occurs reading metadata or unpacking, etc.
     */
    protected Theme(File themePack) throws IOException {
        super(themePack);
        themeFile = themePack;
        themeConfigFile = new File(themeFile.getPath() + ".json");
        packMetadata = new JsonParser().parse(new InputStreamReader(getInputStreamByName("pack.json"))).getAsJsonObject();
        try {
            unpackConfig();
        } catch (Exception e) {
            Themes.themeLog.error("failed to unpack config!", e);
            unpackConfig();
        }
        packConfig = new JsonParser().parse(new FileReader(themeConfigFile)).getAsJsonObject();
    }

    /**
     * Attempt to unpack the theme default config if it doesn't already exist.
     */
    private void unpackConfig() throws IOException {
        if (themeConfigFile.createNewFile()) {
            Themes.themeLog.warn("Creating config file for theme " + themeFile.getName() + ", assuming it has never been opened before.");
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(getInputStreamByName("default_config.json")));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                responseStrBuilder.append(inputStr);
            FileWriter fileWriter = new FileWriter(themeConfigFile);
            fileWriter.write(responseStrBuilder.toString());
            fileWriter.close();
        }

    }

    /**
     * Get the accent color for the window, used on separators, lines, etc.
     */
    public Color getAccentColor() {
        JsonObject colors = packConfig.getAsJsonObject("colors");
        int accentColor = colors.get("accent_color").getAsInt();
        return Renderer.getColorFromInt(accentColor);
    }
    /**
     * Get the base color for the window, used for the main background.
     */
    public Color getBaseColor() {
        JsonObject colors = packConfig.getAsJsonObject("colors");
        int accentColor = colors.get("base_color").getAsInt();
        return Renderer.getColorFromInt(accentColor);
    }
    /**
     * Get the base color for the buttons, items, and most elements.
     */
    public Color getElementColor() {
        JsonObject colors = packConfig.getAsJsonObject("colors");
        int accentColor = colors.get("element_color").getAsInt();
        return Renderer.getColorFromInt(accentColor);
    }
    /**
     * Get the accent color for elements when they are hovered.
     */
    public Color getHoverColor() {
        JsonObject colors = packConfig.getAsJsonObject("colors");
        int accentColor = colors.get("hover_color").getAsInt();
        return Renderer.getColorFromInt(accentColor);
    }
    /**
     * Get the accent color for elements when they are clicked.
     */
    public Color getClickColor() {
        JsonObject colors = packConfig.getAsJsonObject("colors");
        int accentColor = colors.get("click_color").getAsInt();
        return Renderer.getColorFromInt(accentColor);
    }
    /**
     * Get the color for the close/destroy buttons.
     */
    public Color getCloseColor() {
        JsonObject colors = packConfig.getAsJsonObject("colors");
        int accentColor = colors.get("base_color").getAsInt();
        return Renderer.getColorFromInt(accentColor);
    }
    /**
     * Get the color for the main text, like titles, config element items, etc.
     */
    public Color getTextColor() {
        JsonObject colors = packConfig.getAsJsonObject("colors");
        int accentColor = colors.get("title_color").getAsInt();
        return Renderer.getColorFromInt(accentColor);
    }
    /**
     * Get the accent color for the text, used for subtitles, etc.
     */
    public Color getAccentTextColor() {
        JsonObject colors = packConfig.getAsJsonObject("colors");
        int accentColor = colors.get("subtitle_color").getAsInt();
        return Renderer.getColorFromInt(accentColor);
    }
    /**
     * Weather or not to round off the corners of pretty much every element.
     */
    public boolean shouldRoundCorners() {
        return packConfig.get("round_corners").getAsBoolean();
    }


    /**
     * Get the InputStream of a resource in the pack.
     * @param name name of the resource
     * @throws IOException an error occurs reading
     */
    public InputStream getResource(String name) throws IOException {
        return getInputStreamByName(name);
    }

    /**
     * Get this pack's metadata json.
     */
    public JsonObject getPackMetaData() {
        return packMetadata;
    }

    /**
     * Get the pack's config file.
     */
    public JsonObject getPackConfig() {
        return packConfig;
    }

    /**
     * Get the pack's description.
     */
    public String getDescription() {
        try {
            return packMetadata.get("description").getAsString();
        } catch (Exception e) {
            Themes.themeLog.error("Failed to get pack name. Defaulting to 'error occurred', is pack invalid??");
        }
        return "Couldn't get description!";
    }

    /**
     * Get the friendly name of the pack.
     */
    public String getName() {
        try {
            return packMetadata.get("name").getAsString();
        } catch (Exception e) {
            Themes.themeLog.error("Failed to get pack name. Defaulting to 'null', is pack invalid??");
        }
        return "null";

    }

    /**
     * Get the pack's title image.
     */
    public BufferedImage getImage() {
        try {
            return getPackImage();
        } catch (IOException e) {
            Themes.themeLog.error("Failed to parse pack image. Is pack invalid??");
            //e.printStackTrace();
        }
        return null;
    }

    /**
     * Get the pack's version. Not used yet, but will be when more features are added for theme compatability.
     */
    public int getVersion() {
        try {
            return packMetadata.get("version").getAsInt();
        } catch (Exception e) {
            Themes.themeLog.error("Failed to get pack version. Is pack invalid?");
        }
        return 0;
    }

    /**
     * Get the source file of this theme.
     */
    public File getThemeFile() {
        return themeFile;
    }


}