From b7503ceab6aed5f6b9ca26e9e2e95b0c9e6bc7de Mon Sep 17 00:00:00 2001 From: AlexIIL Date: Thu, 16 Apr 2015 16:25:04 +0100 Subject: [PATCH] Move all the data to an image.json file, to allow changing what is displayed on screen, and where. --- .../mods/load/BetterLoadingScreen.java | 2 +- .../alexiil/mods/load/MinecraftDisplayer.java | 255 ++++++++++++------ .../alexiil/mods/load/ProgressDisplayer.java | 43 ++- .../BetterLoadingScreenTransformer.java | 2 - .../load/coremod/LoadingScreenLoadPlugin.java | 9 +- .../java/alexiil/mods/load/json/Area.java | 20 ++ .../alexiil/mods/load/json/EPosition.java | 34 +++ .../java/alexiil/mods/load/json/EType.java | 5 + .../alexiil/mods/load/json/ImageRender.java | 62 +++++ .../alexiil/mods/load/json/JsonConfig.java | 67 +++++ 10 files changed, 406 insertions(+), 93 deletions(-) create mode 100644 src/main/java/alexiil/mods/load/json/Area.java create mode 100644 src/main/java/alexiil/mods/load/json/EPosition.java create mode 100644 src/main/java/alexiil/mods/load/json/EType.java create mode 100644 src/main/java/alexiil/mods/load/json/ImageRender.java create mode 100644 src/main/java/alexiil/mods/load/json/JsonConfig.java diff --git a/src/main/java/alexiil/mods/load/BetterLoadingScreen.java b/src/main/java/alexiil/mods/load/BetterLoadingScreen.java index e8117a0..567bc0f 100644 --- a/src/main/java/alexiil/mods/load/BetterLoadingScreen.java +++ b/src/main/java/alexiil/mods/load/BetterLoadingScreen.java @@ -22,7 +22,7 @@ import com.google.common.eventbus.EventBus; -@Mod(modid = Lib.Mod.ID, guiFactory = "alexiil.mods.load.ConfigGuiFactory", useMetadata = true, dependencies = "required-after:alexiillib", +@Mod(modid = Lib.Mod.ID, guiFactory = "alexiil.mods.load.ConfigGuiFactory", dependencies = "required-after:alexiillib", acceptableRemoteVersions = "*") public class BetterLoadingScreen extends AlexIILMod { @Instance(Lib.Mod.ID) diff --git a/src/main/java/alexiil/mods/load/MinecraftDisplayer.java b/src/main/java/alexiil/mods/load/MinecraftDisplayer.java index 6eafdc2..aff8fd2 100644 --- a/src/main/java/alexiil/mods/load/MinecraftDisplayer.java +++ b/src/main/java/alexiil/mods/load/MinecraftDisplayer.java @@ -1,5 +1,12 @@ package alexiil.mods.load; +import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import net.minecraft.client.Minecraft; import net.minecraft.client.audio.ISound; import net.minecraft.client.audio.PositionedSoundRecord; @@ -11,31 +18,35 @@ import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.WorldRenderer; import net.minecraft.client.renderer.texture.TextureManager; +import net.minecraft.client.resources.IResourcePack; import net.minecraft.client.resources.LanguageManager; -import net.minecraft.client.shader.Framebuffer; import net.minecraft.util.ResourceLocation; import net.minecraftforge.common.config.Configuration; -import net.minecraftforge.common.config.Property; +import net.minecraftforge.fml.client.FMLFileResourcePack; +import net.minecraftforge.fml.client.FMLFolderResourcePack; import org.lwjgl.opengl.GL11; import alexiil.mods.load.ProgressDisplayer.IDisplayer; +import alexiil.mods.load.json.Area; +import alexiil.mods.load.json.EPosition; +import alexiil.mods.load.json.EType; +import alexiil.mods.load.json.ImageRender; +import alexiil.mods.load.json.JsonConfig; public class MinecraftDisplayer implements IDisplayer { private static String sound; private static String defaultSound = "random.levelup"; - private String locationProgressBar = "textures/gui/icons.png"; + private ImageRender[] images; private TextureManager textureManager = null; + private Map fontRenderers = new HashMap(); private FontRenderer fontRenderer = null; private ScaledResolution resolution = null; - private Framebuffer framebuffer = null; private Minecraft mc = null; private boolean callAgain = false, isOpen = true; - private double startTexLocation = 74; private String lastText; private double lastPercent; - private int startTextLocation = 30; - private int startBarLocation = 40; + private IResourcePack myPack; public static void playFinishedSound() { SoundHandler soundHandler = Minecraft.getMinecraft().getSoundHandler(); @@ -54,26 +65,83 @@ public static void playFinishedSound() { soundHandler.playSound(sound); } - // Minecraft's display hasn't been created yet, so don't bother trying to open anything now + @SuppressWarnings("unchecked") + private List getOnlyList() { + Field[] flds = mc.getClass().getDeclaredFields(); + for (Field f : flds) { + if (f.getType().equals(List.class) && !Modifier.isStatic(f.getModifiers())) { + f.setAccessible(true); + try { + return (List) f.get(mc); + } + catch (Throwable e) { + e.printStackTrace(); + } + } + } + return null; + } + + // Because of the wrapper, we can actually use this to create stuffs that we need @Override public void open(Configuration cfg) { - String comment = - "The type of progress bar to display. Use either 0, 1 or 2. (0 is the experiance bar, 1 is the boss health bar, and 2 is the horse jump bar)"; - Property prop = cfg.get("general", "progressType", 1, comment, 0, 2); - startTexLocation = prop.getInt() * 10 + 64; - - String comment2 = - "The yPosition of the text, added to the centre (so, a value of 0 means its right in the middle of the screen, and negative numbers are higher up the screen). Default is 30"; - prop = cfg.get("general", "yPosText", 30, comment2, -500, 500); - startTextLocation = prop.getInt(); - - String comment3 = - "The yPosition of the bar, added to the centre (so, a value of 0 means its right in the middle of the screen, and negative numbers are higher up the screen). Default is 40"; - prop = cfg.get("general", "yPosBar", 50, comment3, -500, 500); - startBarLocation = prop.getInt(); - - String comment4 = "What sound to play when loading is complete. Default is the dispenser open (" + defaultSound + ")"; + mc = Minecraft.getMinecraft(); + // Open the normal config + String comment4 = "What sound to play when loading is complete. Default is the level up sound (" + defaultSound + ")"; sound = cfg.getString("sound", "general", defaultSound, comment4); + + // Add ourselves as a resource pack + if (!ProgressDisplayer.coreModLocation.isDirectory()) + myPack = new FMLFileResourcePack(ProgressDisplayer.modContainer); + else + myPack = new FMLFolderResourcePack(ProgressDisplayer.modContainer); + getOnlyList().add(myPack); + mc.refreshResources(); + + // Open the special config directory + File configDir = new File("./config/BetterLoadingScreen"); + if (!configDir.exists()) + configDir.mkdirs(); + + // Image Config + images = new ImageRender[5]; + String progress = "betterloadingscreen:textures/progressBars.png"; + String title = "textures/gui/title/mojang.png"; + String font = "textures/font/ascii.png"; + images[0] = new ImageRender(title, EPosition.CENTER, EType.STATIC, new Area(0, 0, 256, 256), new Area(0, 0, 256, 256)); + images[1] = new ImageRender(font, EPosition.CENTER, EType.DYNAMIC_TEXT_STATUS, null, new Area(0, -30, 0, 0), "000000", null); + images[2] = new ImageRender(font, EPosition.CENTER, EType.DYNAMIC_TEXT_PERCENTAGE, null, new Area(0, -40, 0, 0), "000000", null); + images[3] = new ImageRender(progress, EPosition.CENTER, EType.STATIC, new Area(0, 10, 182, 5), new Area(0, -50, 182, 5)); + images[4] = new ImageRender(progress, EPosition.CENTER, EType.DYNAMIC_PERCENTAGE, new Area(0, 15, 182, 5), new Area(0, -50, 182, 5)); + + ImageRender[] defaultImageRender = images; + + File imagesFile = new File(configDir, "images.json"); + JsonConfig imagesConfig = new JsonConfig(imagesFile, ImageRender[].class, images); + images = imagesConfig.load(); + + // Preset one is the default one + definePreset(configDir, "preset one", defaultImageRender); + + // Preset two uses something akin to minecraft's loading screen when loading a world + ImageRender[] presetData = new ImageRender[4]; + presetData[0] = + new ImageRender("textures/gui/options_background.png", EPosition.CENTER, EType.STATIC, new Area(0, 0, 65536, 65536), new Area(0, 0, + 8192, 8192), "404040", null); + presetData[1] = new ImageRender(font, EPosition.CENTER, EType.DYNAMIC_TEXT_STATUS, null, new Area(0, 0, 0, 0), "FFFFFF", null); + presetData[2] = new ImageRender(font, EPosition.CENTER, EType.DYNAMIC_TEXT_PERCENTAGE, null, new Area(0, -10, 0, 0), "FFFFFF", null); + presetData[3] = + new ImageRender(font, EPosition.BOTTOM_CENTER, EType.STATIC_TEXT, null, new Area(0, 10, 0, 0), "FFDD49", + "Better Loading Screen by AlexIIL"); + definePreset(configDir, "preset two", presetData); + + // Preset three uses... idk, TODO: Preset 3 etc + } + + private void definePreset(File configDir, String name, ImageRender... images) { + File presetFile = new File(configDir, name + ".json"); + JsonConfig presetConfig = new JsonConfig(presetFile, ImageRender[].class, images); + presetConfig.createNew(); } public void reDisplayProgress() { @@ -86,31 +154,16 @@ public void displayProgress(String text, double percent) { lastText = text; lastPercent = percent; - mc = Minecraft.getMinecraft(); resolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); preDisplayScreen(); - float sf = resolution.getScaleFactor(); - GL11.glScalef(sf, sf, sf); - - int centerX = resolution.getScaledWidth() / 2; - int centerY = resolution.getScaledHeight() / 2; - drawCenteredString(text, centerX, centerY + startTextLocation); - drawCenteredString((int) (percent * 100) + "%", centerX, centerY + startTextLocation + 10); + for (ImageRender image : images) + if (image != null) + drawImageRender(image, text, percent); - GL11.glColor4f(1, 1, 1, 1); - - textureManager.bindTexture(new ResourceLocation(locationProgressBar)); - - double texWidth = 182; - double startX = centerX - texWidth / 2; - drawTexturedModalRect(startX, centerY + startBarLocation, 0, startTexLocation, texWidth, 5); - drawTexturedModalRect(startX, centerY + startBarLocation, 0, startTexLocation + 5, percent * texWidth, 5); - - sf = 1 / sf; - GL11.glScalef(sf, sf, sf); postDisplayScreen(); + if (callAgain) { // For some reason, calling this again makes pre-init render properly. I have no idea why, it just does callAgain = false; @@ -118,25 +171,82 @@ public void displayProgress(String text, double percent) { } } - // Taken from net.minecraft.client.gui.Gui - public void drawTexturedModalRect(double x, double y, double u, double z, double width, double height) { - float f = 0.00390625F; - float f1 = 0.00390625F; + private FontRenderer fontRenderer(String fontTexture) { + if (fontRenderers.containsKey(fontTexture)) + return fontRenderers.get(fontTexture); + FontRenderer font = new FontRenderer(mc.gameSettings, new ResourceLocation(fontTexture), textureManager, false); + font.onResourceManagerReload(mc.getResourceManager()); + mc.refreshResources(); + font.onResourceManagerReload(mc.getResourceManager()); + fontRenderers.put(fontTexture, font); + return font; + } + + public void drawImageRender(ImageRender render, String text, double percent) { + int startX = render.transformX(resolution.getScaledWidth()); + int startY = render.transformY(resolution.getScaledHeight()); + GlStateManager.color(render.getRed(), render.getGreen(), render.getBlue()); + switch (render.type) { + case DYNAMIC_PERCENTAGE: { + ResourceLocation res = new ResourceLocation(render.resourceLocation); + textureManager.bindTexture(res); + double alteredWidth = render.position.width * percent; + drawRect(startX, startY, alteredWidth, render.position.height, render.texture.x, render.texture.y, alteredWidth, + render.texture.height); + break; + } + case DYNAMIC_TEXT_PERCENTAGE: { + FontRenderer font = fontRenderer(render.resourceLocation); + String percentage = (int) (percent * 100) + "%"; + int width = font.getStringWidth(percentage); + startX = render.positionType.transformX(render.position.x, resolution.getScaledWidth() - width); + startY = render.positionType.transformY(render.position.y, resolution.getScaledHeight() - font.FONT_HEIGHT); + drawString(font, percentage, startX, startY, render.getColour()); + break; + } + case DYNAMIC_TEXT_STATUS: { + FontRenderer font = fontRenderer(render.resourceLocation); + int width = font.getStringWidth(text); + startX = render.positionType.transformX(render.position.x, resolution.getScaledWidth() - width); + startY = render.positionType.transformY(render.position.y, resolution.getScaledHeight() - font.FONT_HEIGHT); + drawString(font, text, startX, startY, render.getColour()); + break; + } + case STATIC_TEXT: { + FontRenderer font = fontRenderer(render.resourceLocation); + int width = font.getStringWidth(render.text); + int startX1 = render.positionType.transformX(render.position.x, resolution.getScaledWidth() - width); + int startY1 = render.positionType.transformY(render.position.y, resolution.getScaledHeight() - font.FONT_HEIGHT); + drawString(font, render.text, startX1, startY1, render.getColour()); + break; + } + default: {// Assume STATIC + ResourceLocation res = new ResourceLocation(render.resourceLocation); + textureManager.bindTexture(res); + drawRect(startX, startY, render.position.width, render.position.height, render.texture.x, render.texture.y, render.texture.width, + render.texture.height); + break; + } + } + } + + public void drawString(FontRenderer font, String text, int x, int y, int colour) { + font.drawString(text, x, y, colour); + GlStateManager.color(1, 1, 1, 1); + } + + public void drawRect(double x, double y, double drawnWidth, double drawnHeight, double u, double v, double uWidth, double vHeight) { + float f = 1 / 256F; Tessellator tessellator = Tessellator.getInstance(); WorldRenderer wr = tessellator.getWorldRenderer(); wr.startDrawingQuads(); - wr.addVertexWithUV(x, y + height, 0, u * f, (z + height) * f1); - wr.addVertexWithUV(x + width, y + height, 0, (u + width) * f, (z + height) * f1); - wr.addVertexWithUV(x + width, y, 0, (u + width) * f, z * f1); - wr.addVertexWithUV(x, y, 0, u * f, z * f1); + wr.addVertexWithUV(x, y + drawnHeight, 0, u * f, (v + vHeight) * f); + wr.addVertexWithUV(x + drawnWidth, y + drawnHeight, 0, (u + uWidth) * f, (v + vHeight) * f); + wr.addVertexWithUV(x + drawnWidth, y, 0, (u + uWidth) * f, v * f); + wr.addVertexWithUV(x, y, 0, u * f, v * f); tessellator.draw(); } - private void drawCenteredString(String string, int xCenter, int yPos) { - int width = fontRenderer.getStringWidth(string); - fontRenderer.drawString(string, xCenter - width / 2, yPos, 0); - } - private void preDisplayScreen() { if (textureManager == null) { textureManager = mc.renderEngine = new TextureManager(mc.getResourceManager()); @@ -157,10 +267,7 @@ private void preDisplayScreen() { textureManager = mc.renderEngine; resolution = new ScaledResolution(mc, mc.displayWidth, mc.displayHeight); - int scaleFactor = resolution.getScaleFactor(); - if (framebuffer == null) - framebuffer = new Framebuffer(resolution.getScaledWidth() * scaleFactor, resolution.getScaledHeight() * scaleFactor, true); - framebuffer.bindFramebuffer(false); + GlStateManager.matrixMode(GL11.GL_PROJECTION); GlStateManager.loadIdentity(); GlStateManager.ortho(0.0D, (double) resolution.getScaledWidth(), (double) resolution.getScaledHeight(), 0.0D, 1000.0D, 3000.0D); @@ -172,38 +279,22 @@ private void preDisplayScreen() { GlStateManager.disableDepth(); GlStateManager.enableTexture2D(); - // This also means that you can override the mojang image :P - textureManager.bindTexture(new ResourceLocation("textures/gui/title/mojang.png")); + GlStateManager.clear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); + GlStateManager.clearColor(1, 1, 1, 1); - Tessellator tessellator = Tessellator.getInstance(); - WorldRenderer worldrenderer = tessellator.getWorldRenderer(); - worldrenderer.startDrawingQuads(); - worldrenderer.setColorOpaque_I(16777215); - worldrenderer.addVertexWithUV(0.0D, (double) mc.displayHeight, 0.0D, 0.0D, 0.0D); - worldrenderer.addVertexWithUV((double) mc.displayWidth, (double) mc.displayHeight, 0.0D, 0.0D, 0.0D); - worldrenderer.addVertexWithUV((double) mc.displayWidth, 0.0D, 0.0D, 0.0D, 0.0D); - worldrenderer.addVertexWithUV(0.0D, 0.0D, 0.0D, 0.0D, 0.0D); - tessellator.draw(); - GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F); - worldrenderer.setColorOpaque_I(16777215); - short short1 = 256; - short short2 = 256; - mc.scaledTessellator((resolution.getScaledWidth() - short1) / 2, (resolution.getScaledHeight() - short2) / 2, 0, 0, short1, short2); - GlStateManager.disableLighting(); - GlStateManager.disableFog(); - framebuffer.unbindFramebuffer(); - framebuffer.framebufferRender(resolution.getScaledWidth() * scaleFactor, resolution.getScaledHeight() * scaleFactor); + GlStateManager.enableAlpha(); + GlStateManager.alphaFunc(GL11.GL_GREATER, 0.1F); + GlStateManager.color(1, 1, 1, 1); } private void postDisplayScreen() { - GlStateManager.enableAlpha(); - GlStateManager.alphaFunc(GL11.GL_GREATER, 0.1F); mc.updateDisplay(); } @Override public void close() { isOpen = false; + getOnlyList().remove(myPack); } } diff --git a/src/main/java/alexiil/mods/load/ProgressDisplayer.java b/src/main/java/alexiil/mods/load/ProgressDisplayer.java index 5456532..5a6a984 100644 --- a/src/main/java/alexiil/mods/load/ProgressDisplayer.java +++ b/src/main/java/alexiil/mods/load/ProgressDisplayer.java @@ -4,6 +4,10 @@ import java.io.File; import net.minecraftforge.common.config.Configuration; +import net.minecraftforge.fml.client.FMLFileResourcePack; +import net.minecraftforge.fml.common.DummyModContainer; +import net.minecraftforge.fml.common.ModContainer; +import net.minecraftforge.fml.common.ModMetadata; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -66,6 +70,8 @@ public void close() {} private static int clientState = -1; public static Configuration cfg; public static boolean playSound; + public static File coreModLocation; + public static ModContainer modContainer; public static boolean isClient() { if (clientState != -1) @@ -81,8 +87,41 @@ public static boolean isClient() { return true; } - public static void start() { - Configuration cfg = new Configuration(new File("./config/betterloadingscreen.cfg")); + public static void start(File coremodLocation) { + coreModLocation = coremodLocation; + if (coreModLocation == null) + coreModLocation = new File("./../bin/"); + // Assume this is a dev environment, and that the build dir is in bin, and the test dir has the same parent as + // the bin dir... + ModMetadata md = new ModMetadata(); + md.name = Lib.Mod.NAME; + md.modId = Lib.Mod.ID; + modContainer = new DummyModContainer(md) { + @Override + public Class getCustomResourcePackClass() { + return FMLFileResourcePack.class; + } + + @Override + public File getSource() { + return coreModLocation; + } + + @Override + public String getModId() { + return Lib.Mod.ID; + } + }; + + File fileOld = new File("./config/betterloadingscreen.cfg"); + File fileNew = new File("./config/BetterLoadingScreen/config.cfg"); + + Configuration cfg; + if (fileOld.exists()) + cfg = new Configuration(fileOld); + else + cfg = new Configuration(fileNew); + boolean useMinecraft = isClient(); if (useMinecraft) { String comment = diff --git a/src/main/java/alexiil/mods/load/coremod/BetterLoadingScreenTransformer.java b/src/main/java/alexiil/mods/load/coremod/BetterLoadingScreenTransformer.java index 6146ee1..033e7ea 100644 --- a/src/main/java/alexiil/mods/load/coremod/BetterLoadingScreenTransformer.java +++ b/src/main/java/alexiil/mods/load/coremod/BetterLoadingScreenTransformer.java @@ -79,8 +79,6 @@ private byte[] transformMinecraft(byte[] before, boolean dev) { else if (method.owner.startsWith("com/mumfrey")) { System.out.println("Started with \"com/mumfrey\", was actually \"" + method.owner + "\""); } - else - System.out.println("Started with \"" + method.owner + "\""); } // LiteLoader removing end diff --git a/src/main/java/alexiil/mods/load/coremod/LoadingScreenLoadPlugin.java b/src/main/java/alexiil/mods/load/coremod/LoadingScreenLoadPlugin.java index c514ae4..111f94b 100644 --- a/src/main/java/alexiil/mods/load/coremod/LoadingScreenLoadPlugin.java +++ b/src/main/java/alexiil/mods/load/coremod/LoadingScreenLoadPlugin.java @@ -11,11 +11,6 @@ @IFMLLoadingPlugin.TransformerExclusions({ "alexiil.mods.load.coremod" }) @IFMLLoadingPlugin.SortingIndex(Integer.MAX_VALUE - 80) public class LoadingScreenLoadPlugin implements IFMLLoadingPlugin { - // The only reason this coremod exists is this static method: its the first time our code is called - static { - ProgressDisplayer.start(); - } - @Override public String[] getASMTransformerClass() { return new String[] { "alexiil.mods.load.coremod.BetterLoadingScreenTransformer" }; @@ -33,7 +28,9 @@ public String getSetupClass() { @Override public void injectData(Map data) { - Translation.addTranslations((File) data.get("coremodLocation")); + File coremodLocation = (File) data.get("coremodLocation"); + Translation.addTranslations(coremodLocation); + ProgressDisplayer.start(coremodLocation); } @Override diff --git a/src/main/java/alexiil/mods/load/json/Area.java b/src/main/java/alexiil/mods/load/json/Area.java new file mode 100644 index 0000000..9f0b4c6 --- /dev/null +++ b/src/main/java/alexiil/mods/load/json/Area.java @@ -0,0 +1,20 @@ +package alexiil.mods.load.json; + +public class Area { + public final int x; + public final int y; + public final int width; + public final int height; + + public Area(int x, int y, int width, int height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + @Override + public String toString() { + return "ImageTexture [x=" + x + ", y=" + y + ", width=" + width + ", height=" + height + "]"; + } +} diff --git a/src/main/java/alexiil/mods/load/json/EPosition.java b/src/main/java/alexiil/mods/load/json/EPosition.java new file mode 100644 index 0000000..b5eca8f --- /dev/null +++ b/src/main/java/alexiil/mods/load/json/EPosition.java @@ -0,0 +1,34 @@ +package alexiil.mods.load.json; + +public enum EPosition { + TOP_LEFT(-1, -1), TOP_CENTER(0, -1), TOP_RIGHT(1, -1), CENTER_LEFT(-1, 0), CENTER(0, 0), CENTER_RIGHT(1, 0), BOTTOM_LEFT(-1, 1), BOTTOM_CENTER(0, + 1), BOTTOM_RIGHT(1, 1); + + private final int x; + private final int y; + + private EPosition(int x, int y) { + this.x = x; + this.y = y; + } + + private int transform(int switcher, int coord, int screenThing) { + switch (switcher) { + case -1: + return coord; + case 0: + return screenThing / 2 - coord; + case 1: + return screenThing - coord; + } + throw new Error("switcher (" + switcher + ") != -1, 0 or 1 (" + this.toString() + ")"); + } + + public int transformX(int x, int screenWidth) { + return transform(this.x, x, screenWidth); + } + + public int transformY(int y, int screenHeight) { + return transform(this.y, y, screenHeight); + } +} diff --git a/src/main/java/alexiil/mods/load/json/EType.java b/src/main/java/alexiil/mods/load/json/EType.java new file mode 100644 index 0000000..5bf0064 --- /dev/null +++ b/src/main/java/alexiil/mods/load/json/EType.java @@ -0,0 +1,5 @@ +package alexiil.mods.load.json; + +public enum EType { + STATIC, STATIC_TEXT, DYNAMIC_TEXT_STATUS, DYNAMIC_TEXT_PERCENTAGE, DYNAMIC_PERCENTAGE; +} diff --git a/src/main/java/alexiil/mods/load/json/ImageRender.java b/src/main/java/alexiil/mods/load/json/ImageRender.java new file mode 100644 index 0000000..06e4ee4 --- /dev/null +++ b/src/main/java/alexiil/mods/load/json/ImageRender.java @@ -0,0 +1,62 @@ +package alexiil.mods.load.json; + +public class ImageRender { + public final String resourceLocation; + public final EPosition positionType; + public final EType type; + public final Area texture; + public final Area position; + public final String colour; + public final String text; + + public ImageRender(String resourceLocation, EPosition positionType, EType type, Area texture, Area position, String colour, String text) { + this.resourceLocation = resourceLocation; + this.positionType = positionType; + this.type = type; + this.texture = texture; + this.position = position; + this.colour = colour; + this.text = text; + } + + public ImageRender(String resourceLocation, EPosition positionType, EType type, Area texture, Area position) { + this(resourceLocation, positionType, type, texture, position, null, null); + } + + public int transformX(int screenWidth) { + return positionType.transformX(position.x, screenWidth - position.width); + } + + public int transformY(int screenWidth) { + return positionType.transformY(position.y, screenWidth - position.height); + } + + public int getColour() { + if (colour == null) + return 0xFFFFFF; + else { + try { + return Integer.parseInt(colour, 16); + } + catch (NumberFormatException nfe) { + return 0xFFFFFF; + } + } + } + + private float getColourPart(int bitStart) { + return ((getColour() >> bitStart) & 0xFF) / 256F; + } + + public float getRed() { + return getColourPart(16); + } + + public float getGreen() { + return getColourPart(8); + } + + public float getBlue() { + return getColourPart(0); + } +} diff --git a/src/main/java/alexiil/mods/load/json/JsonConfig.java b/src/main/java/alexiil/mods/load/json/JsonConfig.java new file mode 100644 index 0000000..0716db4 --- /dev/null +++ b/src/main/java/alexiil/mods/load/json/JsonConfig.java @@ -0,0 +1,67 @@ +package alexiil.mods.load.json; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +public class JsonConfig { + private final Class clazz; + private final File file; + private final T defaultConfig; + + public JsonConfig(File file, Class clazz, T defaultConfig) { + this.file = file; + this.clazz = clazz; + this.defaultConfig = defaultConfig; + } + + /** Overwrite any existing config: Treat it as a default config */ + public void createNew() { + BufferedWriter writer = null; + try { + writer = new BufferedWriter(new FileWriter(file)); + writer.write(new GsonBuilder().setPrettyPrinting().create().toJson(defaultConfig)); + writer.close(); + } + catch (IOException e1) { + e1.printStackTrace(); + } + finally { + if (writer != null) + try { + writer.close(); + } + catch (IOException e1) { + e1.printStackTrace(); + } + } + } + + public T load() { + BufferedReader reader = null; + try { + reader = new BufferedReader(new FileReader(file)); + return new Gson().fromJson(reader, clazz); + } + catch (FileNotFoundException e) { + createNew(); + } + finally { + if (reader != null) + try { + reader.close(); + } + catch (IOException e) { + e.printStackTrace(); + } + } + return defaultConfig; + } +}