aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/polyfrost/oneconfig/themes
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig/themes')
-rw-r--r--src/main/java/io/polyfrost/oneconfig/themes/TextureManager.java61
-rw-r--r--src/main/java/io/polyfrost/oneconfig/themes/Theme.java38
-rw-r--r--src/main/java/io/polyfrost/oneconfig/themes/ThemeElement.java8
-rw-r--r--src/main/java/io/polyfrost/oneconfig/themes/TickableTexture.java90
4 files changed, 170 insertions, 27 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/themes/TextureManager.java b/src/main/java/io/polyfrost/oneconfig/themes/TextureManager.java
index fd636f2..6406e1b 100644
--- a/src/main/java/io/polyfrost/oneconfig/themes/TextureManager.java
+++ b/src/main/java/io/polyfrost/oneconfig/themes/TextureManager.java
@@ -4,12 +4,14 @@ 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 javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
+import java.io.IOException;
import java.util.ArrayList;
-import java.util.HashMap;
import java.util.List;
import static io.polyfrost.oneconfig.themes.Themes.themeLog;
@@ -17,43 +19,54 @@ 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<ThemeElement> tickableTextureLocations = new ArrayList<>();
- private final HashMap<Integer, Integer> tickableTextures = new HashMap<>();
- private int tick = 0;
+ 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()) {
+ 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?");
- img = new BufferedImage(128,128,BufferedImage.TYPE_INT_ARGB);
- // TODO add fallback
+ 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) {
+ 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(img.getWidth() != img.getHeight()) {
- themeLog.info("found tickable animated texture (" + element.name() + "). Loading texture");
- tickableTextureLocations.add(element);
- tickableTextures.put(img.getWidth(), img.getHeight());
+ 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 {
+
}
}
}
/**
* Draw the specified icon 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
+ * @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(ThemeElement element, int x, int y, int width, int height) {
GlStateManager.enableBlend();
@@ -61,20 +74,18 @@ public class TextureManager {
ResourceLocation location = resources.get(element.ordinal());
mc.getTextureManager().bindTexture(location);
try {
- if(tickableTextureLocations.contains(element)) {
- int texWidth = tickableTextures.keySet().stream().findFirst().get(); // TODO unsure if this works safe
- int texHeight = tickableTextures.values().stream().findFirst().get();
- int frames = texHeight / texWidth;
- while(tick < frames) {
- Gui.drawModalRectWithCustomSizedTexture(x, y, 0, (tick * texWidth), texWidth, texWidth, texWidth, texWidth);
- tick++;
- if(tick == frames) {
- tick = 0;
+ 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();
} catch (Exception e) {
themeLog.error("Error occurred drawing texture " + element.name() + ", is theme invalid?", e);
}
diff --git a/src/main/java/io/polyfrost/oneconfig/themes/Theme.java b/src/main/java/io/polyfrost/oneconfig/themes/Theme.java
index bd6ee80..9e67799 100644
--- a/src/main/java/io/polyfrost/oneconfig/themes/Theme.java
+++ b/src/main/java/io/polyfrost/oneconfig/themes/Theme.java
@@ -3,9 +3,12 @@ package io.polyfrost.oneconfig.themes;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.polyfrost.oneconfig.renderer.Renderer;
+import io.polyfrost.oneconfig.renderer.TrueTypeFont;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.resources.FileResourcePack;
+import net.minecraft.crash.CrashReport;
+import net.minecraft.util.ReportedException;
import net.minecraft.util.ResourceLocation;
import javax.imageio.ImageIO;
@@ -34,6 +37,8 @@ public class Theme extends FileResourcePack {
private final String title;
private final int version;
private final TextureManager manager;
+ private final TrueTypeFont boldFont;
+ private final TrueTypeFont normalFont;
@@ -81,7 +86,24 @@ public class Theme extends FileResourcePack {
this.title = title;
this.description = description;
this.version = version;
-
+ TrueTypeFont normalFontTemp;
+ TrueTypeFont boldFontTemp;
+ try {
+ boldFontTemp = new TrueTypeFont(Font.createFont(Font.TRUETYPE_FONT, getResource("textures/fonts/font_bold.ttf")).deriveFont(30f), true);
+ normalFontTemp = new TrueTypeFont(Font.createFont(Font.TRUETYPE_FONT, getResource("textures/fonts/font.ttf")).deriveFont(12f), true);
+ } catch (FontFormatException e) {
+ Themes.themeLog.error("failed to derive fonts, is theme invalid?",e);
+ e.printStackTrace();
+ try {
+ normalFontTemp = new TrueTypeFont(Font.createFont(Font.TRUETYPE_FONT, mc.getResourceManager().getResource(new ResourceLocation("oneconfig", "textures/fonts/font.ttf")).getInputStream()).deriveFont(12f), true);
+ boldFontTemp = new TrueTypeFont(Font.createFont(Font.TRUETYPE_FONT, mc.getResourceManager().getResource(new ResourceLocation("oneconfig", "textures/fonts/font_bold.ttf")).getInputStream()).deriveFont(30f), true);
+ } catch (FontFormatException ex) {
+ ex.printStackTrace();
+ throw new ReportedException(new CrashReport("Failed to get fallback fonts! game will crash :(", ex));
+ }
+ }
+ normalFont = normalFontTemp;
+ boldFont = boldFontTemp;
manager = new TextureManager(this);
if(Themes.VERSION != version) {
Themes.themeLog.warn("Theme was made for a different version of OneConfig! This may cause issues in the GUI.");
@@ -253,4 +275,18 @@ public class Theme extends FileResourcePack {
return manager;
}
+ /**
+ * Get the font from this theme.
+ */
+ public TrueTypeFont getFont() {
+ return normalFont;
+ }
+
+ /**
+ * Get the bold, larger font from this theme.
+ */
+ public TrueTypeFont getBoldFont() {
+ return boldFont;
+ }
+
}
diff --git a/src/main/java/io/polyfrost/oneconfig/themes/ThemeElement.java b/src/main/java/io/polyfrost/oneconfig/themes/ThemeElement.java
index f0be82a..818bfdf 100644
--- a/src/main/java/io/polyfrost/oneconfig/themes/ThemeElement.java
+++ b/src/main/java/io/polyfrost/oneconfig/themes/ThemeElement.java
@@ -29,7 +29,13 @@ public enum ThemeElement {
UTILITIES("textures/mod/utilities.png", 32),
LOGO("textures/logos/logo.png", 128),
- SMALL_LOGO("textures/logos/logo_small.png", 32);
+ SMALL_LOGO("textures/logos/logo_small.png", 64),
+
+ BUTTON_OFF("textures/window/button_off.png", 512),
+ BUTTON_HOVER("textures/window/button_hover.png", 512),
+ BUTTON_CLICK("textures/window/button_click.png", 512),
+
+ BACKGROUND("textures/window/background.png", 1600);
public final String location;
diff --git a/src/main/java/io/polyfrost/oneconfig/themes/TickableTexture.java b/src/main/java/io/polyfrost/oneconfig/themes/TickableTexture.java
new file mode 100644
index 0000000..86022fb
--- /dev/null
+++ b/src/main/java/io/polyfrost/oneconfig/themes/TickableTexture.java
@@ -0,0 +1,90 @@
+package io.polyfrost.oneconfig.themes;
+
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+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.util.ResourceLocation;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+import static io.polyfrost.oneconfig.themes.Themes.getActiveTheme;
+import static io.polyfrost.oneconfig.themes.Themes.themeLog;
+
+@SuppressWarnings("unused")
+public class TickableTexture {
+ private final int framesToSkip;
+ private final BufferedImage image;
+ private final int sizeX, sizeY, frames;
+ private int tick;
+ private int tick2;
+ private final ThemeElement thisElement;
+ private final ResourceLocation location;
+
+ public TickableTexture(ThemeElement element) throws IOException {
+ thisElement = element;
+ InputStream inputStream = getActiveTheme().getResource(element.location);
+ image = ImageIO.read(inputStream);
+ location = Minecraft.getMinecraft().getTextureManager().getDynamicTextureLocation(element.location, new DynamicTexture(image));
+ sizeX = image.getWidth();
+ sizeY = image.getHeight();
+ frames = sizeY / sizeX;
+ int frametime;
+ try {
+ JsonObject jsonObject = new JsonParser().parse(new InputStreamReader(getActiveTheme().getResource(element.location + ".json"))).getAsJsonObject();
+ frametime = jsonObject.get("frametime").getAsInt();
+ if (frametime == 0) {
+ frametime = 1;
+ themeLog.warn("You cannot have a frame tick time of 0. This will mean there is no animation as it will happen way too fast. Defaulting to 1, as we assume you wanted it fast.");
+ }
+ } catch (Exception e) {
+ themeLog.error("failed to load metadata for tickable texture (" + element.location + "). Setting default (5)");
+ frametime = 5;
+ }
+ framesToSkip = frametime;
+ }
+
+ public void draw(int x, int y) {
+ GlStateManager.enableBlend();
+ GlStateManager.color(1f,1f,1f,1f);
+ Minecraft.getMinecraft().getTextureManager().bindTexture(location);
+ if (tick < frames) {
+ Gui.drawModalRectWithCustomSizedTexture(x, y, 0, (tick * sizeX), sizeX, sizeX, sizeX, sizeX);
+ tick2++;
+ if (tick2 == framesToSkip) {
+ tick2 = 0;
+ tick++;
+ }
+ }
+ if (tick == frames) {
+ tick = 0;
+ }
+ GlStateManager.disableBlend();
+ }
+
+ public BufferedImage getImage() {
+ return image;
+ }
+
+ public int getFrameTime() {
+ return framesToSkip;
+ }
+
+ public int getSizeX() {
+ return sizeX;
+ }
+
+ public int getSizeY() {
+ return sizeY;
+ }
+
+ public ThemeElement getElement() {
+ return thisElement;
+ }
+}