From 2231da6af9982840fa9d3bd24c4333bdbbe19cc2 Mon Sep 17 00:00:00 2001 From: nextdaydelivery <12willettsh@gmail.com> Date: Sat, 12 Feb 2022 11:13:52 +0000 Subject: theme things --- .../java/io/polyfrost/oneconfig/OneConfig.java | 16 ++ .../java/io/polyfrost/oneconfig/gui/Window.java | 3 +- .../io/polyfrost/oneconfig/renderer/Renderer.java | 10 + .../java/io/polyfrost/oneconfig/themes/Theme.java | 211 +++++++++++++++++++++ .../java/io/polyfrost/oneconfig/themes/Themes.java | 43 +++++ 5 files changed, 281 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/polyfrost/oneconfig/themes/Theme.java (limited to 'src/main/java/io') diff --git a/src/main/java/io/polyfrost/oneconfig/OneConfig.java b/src/main/java/io/polyfrost/oneconfig/OneConfig.java index 4b916d9..6f58f52 100644 --- a/src/main/java/io/polyfrost/oneconfig/OneConfig.java +++ b/src/main/java/io/polyfrost/oneconfig/OneConfig.java @@ -1,13 +1,29 @@ package io.polyfrost.oneconfig; import io.polyfrost.oneconfig.command.OneConfigCommand; +import net.minecraft.client.Minecraft; import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.event.FMLInitializationEvent; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; + +import java.io.File; @Mod(modid = "oneconfig", name = "OneConfig", version = "${version}") public class OneConfig { + private static Minecraft mc = Minecraft.getMinecraft(); + public static File jarFile; + public static File oneConfigDir = new File(mc.mcDataDir, "OneConfig/"); + public static File themesDir = new File(oneConfigDir, "themes/"); + + + @Mod.EventHandler + public void onPreFMLInit(FMLPreInitializationEvent event) { + jarFile = event.getSourceFile(); + oneConfigDir.mkdirs(); + themesDir.mkdirs(); + } @Mod.EventHandler public void onFMLInitialization(FMLInitializationEvent event) { diff --git a/src/main/java/io/polyfrost/oneconfig/gui/Window.java b/src/main/java/io/polyfrost/oneconfig/gui/Window.java index ed420aa..ed4e0cd 100644 --- a/src/main/java/io/polyfrost/oneconfig/gui/Window.java +++ b/src/main/java/io/polyfrost/oneconfig/gui/Window.java @@ -34,8 +34,7 @@ public class Window extends GuiScreen { } public void drawWindow() { - Renderer.drawScaledImage(location, 10, 10, 64 ,64); - Renderer.drawRoundRectangle(50,50,100,100,10,-1); + } public static Window getWindow() { diff --git a/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java b/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java index 05e2731..19d8710 100644 --- a/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java +++ b/src/main/java/io/polyfrost/oneconfig/renderer/Renderer.java @@ -5,6 +5,8 @@ import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.Gui; import net.minecraft.util.ResourceLocation; +import java.awt.*; + public class Renderer extends Gui { private static final Minecraft mc = Minecraft.getMinecraft(); private static final FontRenderer fr = mc.fontRendererObj; @@ -36,4 +38,12 @@ public class Renderer extends Gui { } } + public static Color getColorFromInt(int color) { + float f = (float)(color >> 16 & 255) / 255.0F; + float f1 = (float)(color >> 8 & 255) / 255.0F; + float f2 = (float)(color & 255) / 255.0F; + float f3 = (float)(color >> 24 & 255) / 255.0F; + return new Color(f, f1, f2, f3); + } + } diff --git a/src/main/java/io/polyfrost/oneconfig/themes/Theme.java b/src/main/java/io/polyfrost/oneconfig/themes/Theme.java new file mode 100644 index 0000000..59b5767 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/themes/Theme.java @@ -0,0 +1,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; + } + + +} diff --git a/src/main/java/io/polyfrost/oneconfig/themes/Themes.java b/src/main/java/io/polyfrost/oneconfig/themes/Themes.java index 7ba8097..7bff29c 100644 --- a/src/main/java/io/polyfrost/oneconfig/themes/Themes.java +++ b/src/main/java/io/polyfrost/oneconfig/themes/Themes.java @@ -1,4 +1,47 @@ package io.polyfrost.oneconfig.themes; +import io.polyfrost.oneconfig.OneConfig; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + public class Themes { + public static Theme activeTheme; + public static final Logger themeLog = LogManager.getLogger("OneConfig Themes"); + + /** + * Return a list of all available themes in the directory. + * @return list of themes + */ + public static List getThemes() { + FilenameFilter filter = (dir, name) -> name.endsWith(".zip"); + return Arrays.asList(Objects.requireNonNull(OneConfig.themesDir.listFiles(filter))); + } + + /** + * Return the active theme instance. + */ + public static Theme getActiveTheme() { + return activeTheme; + } + + /** + * Open a new theme in the window, and restart the GUI. + * @param theme Theme file to open + */ + public static void openTheme(File theme) { + try { + activeTheme = new Theme(theme); + } catch (IOException e) { + e.printStackTrace(); + } + // TODO restart gui + } + } -- cgit