diff --git a/src/main/java/com/tterrag/blurbg/BlurBG.java b/src/main/java/com/tterrag/blurbg/BlurBG.java index d5dc2aa..d18a6d8 100644 --- a/src/main/java/com/tterrag/blurbg/BlurBG.java +++ b/src/main/java/com/tterrag/blurbg/BlurBG.java @@ -20,6 +20,7 @@ import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.config.Configuration; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; +import net.minecraftforge.fml.common.Mod.Instance; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.TickEvent.Phase; @@ -29,12 +30,17 @@ import net.minecraftforge.fml.relauncher.ReflectionHelper; @Mod(modid = "blurbg", name = "BlurBG", version = "@VERSION@", acceptedMinecraftVersions = "[1.9, 1.12)") public class BlurBG { + @Instance + public static BlurBG instance; + private String[] blurExclusions; private Field _listShaders; private long start; private int fadeTime; + private int colorFirst, colorSecond; + @EventHandler public void preInit(FMLPreInitializationEvent event) { MinecraftForge.EVENT_BUS.register(this); @@ -47,6 +53,16 @@ public class BlurBG { fadeTime = config.getInt("fadeTime", Configuration.CATEGORY_GENERAL, 300, 0, Integer.MAX_VALUE, "The time it takes for the blur to fade in, in ms."); + colorFirst = Integer.parseUnsignedInt( + config.getString("gradientStartColor", Configuration.CATEGORY_GENERAL, "66000000", "The start color of the background gradient. Given in ARGB hex."), + 16 + ); + + colorSecond = Integer.parseUnsignedInt( + config.getString("gradientEndColor", Configuration.CATEGORY_GENERAL, "66000000", "The end color of the background gradient. Given in ARGB hex."), + 16 + ); + config.save(); } @@ -67,6 +83,10 @@ public class BlurBG { } } + private float getProgress() { + return Math.min((System.currentTimeMillis() - start) / (float) fadeTime, 1); + } + @SuppressWarnings("null") @SubscribeEvent public void onRenderTick(RenderTickEvent event) { @@ -78,7 +98,7 @@ public class BlurBG { for (Shader s : shaders) { ShaderUniform su = s.getShaderManager().getShaderUniform("Progress"); if (su != null) { - su.set(Math.min((System.currentTimeMillis() - start) / (float) fadeTime, 1)); + su.set(getProgress()); } } } catch (IllegalArgumentException | IllegalAccessException e) { @@ -86,5 +106,18 @@ public class BlurBG { } } } - + + public static int getBackgroundColor(boolean second) { + int color = second ? instance.colorSecond : instance.colorFirst; + int a = color >> 24; + int r = (color >> 16) & 0xFF; + int b = (color >> 8) & 0xFF; + int g = color & 0xFF; + float prog = instance.getProgress(); + a *= prog; + r *= prog; + g *= prog; + b *= prog; + return a << 24 | r << 16 | b << 8 | g; + } } diff --git a/src/main/java/com/tterrag/blurbg/BlurBGTransformer.java b/src/main/java/com/tterrag/blurbg/BlurBGTransformer.java index 25be93f..dfe76c8 100644 --- a/src/main/java/com/tterrag/blurbg/BlurBGTransformer.java +++ b/src/main/java/com/tterrag/blurbg/BlurBGTransformer.java @@ -7,7 +7,8 @@ import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.LdcInsnNode; +import org.objectweb.asm.tree.InsnNode; +import org.objectweb.asm.tree.MethodInsnNode; import org.objectweb.asm.tree.MethodNode; import net.minecraft.launchwrapper.IClassTransformer; @@ -18,6 +19,10 @@ public class BlurBGTransformer implements IClassTransformer { private static final String DRAW_WORLD_BAGKGROUND_METHOD = "drawWorldBackground"; private static final String DRAW_WORLD_BAGKGROUND_METHOD_OBF = "func_146270_b"; + + private static final String BLUR_MAIN_CLASS = "com/tterrag/blurbg/BlurBG"; + private static final String COLOR_HOOK_METHOD_NAME = "getBackgroundColor"; + private static final String COLOR_HOOK_METHOD_DESC = "(Z)I"; @Override public byte[] transform(String name, String transformedName, byte[] basicClass) { @@ -44,9 +49,17 @@ public class BlurBGTransformer implements IClassTransformer { // break; // } if (next.getOpcode() == Opcodes.LDC) { - // TODO make this configurable? System.out.println("Modifying GUI background darkness... "); - ((LdcInsnNode)next).cst = ((LdcInsnNode)next.getNext()).cst = 0x66000000; + AbstractInsnNode colorHook = new MethodInsnNode(Opcodes.INVOKESTATIC, BLUR_MAIN_CLASS, COLOR_HOOK_METHOD_NAME, COLOR_HOOK_METHOD_DESC, false); + AbstractInsnNode colorHook2 = colorHook.clone(null); + + // Replace LDC with hooks + m.instructions.set(next, colorHook); + m.instructions.set(colorHook.getNext(), colorHook2); + + // Load boolean constants for method param + m.instructions.insertBefore(colorHook, new InsnNode(Opcodes.ICONST_0)); + m.instructions.insertBefore(colorHook2, new InsnNode(Opcodes.ICONST_1)); break; } }