diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-06-05 17:43:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-05 17:43:23 +0200 |
commit | ab7256dff5d6d37488081ba7a01b36d3ee9ef563 (patch) | |
tree | 8207341e6c402848cdbe7b2f2297f5f975e0e083 /src/main/java/cc/polyfrost/oneconfig/internal/gui | |
parent | a903cfc4d3f76cf3db24749b65156d126fa714e7 (diff) | |
download | OneConfig-ab7256dff5d6d37488081ba7a01b36d3ee9ef563.tar.gz OneConfig-ab7256dff5d6d37488081ba7a01b36d3ee9ef563.tar.bz2 OneConfig-ab7256dff5d6d37488081ba7a01b36d3ee9ef563.zip |
refactor (#36)
* refactor
* fix vig compat
* fix nanovg thingy
* e
* finalize
* gui utils package thingy
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/internal/gui')
-rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/internal/gui/BlurHandler.java | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/internal/gui/BlurHandler.java b/src/main/java/cc/polyfrost/oneconfig/internal/gui/BlurHandler.java new file mode 100644 index 0000000..f5c332f --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/internal/gui/BlurHandler.java @@ -0,0 +1,148 @@ +package cc.polyfrost.oneconfig.internal.gui; + +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.RenderEvent; +import cc.polyfrost.oneconfig.events.event.ScreenOpenEvent; +import cc.polyfrost.oneconfig.events.event.Stage; +import cc.polyfrost.oneconfig.gui.OneConfigGui; +import cc.polyfrost.oneconfig.internal.mixin.ShaderGroupAccessor; +import gg.essential.universal.UMinecraft; +import gg.essential.universal.UScreen; +import me.kbrewster.eventbus.Subscribe; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.shader.Shader; +import net.minecraft.client.shader.ShaderUniform; +import net.minecraft.util.ResourceLocation; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.List; + +/** + * An implementation of the BlurMC mod by tterrag1098. + * <p> + * For the original source see https://github.com/tterrag1098/Blur/blob/1.8.9/src/main/java/com/tterrag/blur/Blur.java + * For the public license, see https://github.com/tterrag1098/Blur/blob/1.8.9/LICENSE + * <p> + * License available under https://github.com/boomboompower/ToggleChat/blob/master/src/main/resources/licenses/BlurMC-License.txt + * + * @author tterrag1098, boomboompower + * <p> + * Taken from ToggleChat + * https://github.com/boomboompower/ToggleChat/blob/master/LICENSE + */ +public class BlurHandler { + public static BlurHandler INSTANCE = new BlurHandler(); + private final ResourceLocation blurShader = new ResourceLocation("shaders/post/fade_in_blur.json"); + private final Logger logger = LogManager.getLogger("OneConfig - Blur"); + private long start; + private float lastProgress = 0; + + /** + * Simply initializes the blur mod so events are properly handled by forge. + */ + public void load() { + EventManager.INSTANCE.register(this); + } + + @Subscribe + private void onGuiChange(ScreenOpenEvent event) { + reloadBlur(event.screen); + } + + @Subscribe + private void onRenderTick(RenderEvent event) { + if (event.stage != Stage.END) { + return; + } + + // Only blur on our own menus + if (UScreen.getCurrentScreen() == null) { + return; + } + + // Only update the shader if one is active + if (!UMinecraft.getMinecraft().entityRenderer.isShaderActive()) { + return; + } + + float progress = getBlurStrengthProgress(); + + // If the new progress value matches the old one this + // will skip the frame update, which (hopefully) resolves the issue + // with the heavy computations after the "animation" is complete. + if (progress == this.lastProgress) { + return; + } + + // Store it for the next iteration! + this.lastProgress = progress; + + // This is hilariously bad, and could cause frame issues on low-end computers. + // Why is this being computed every tick? Surely there is a better way? + // This needs to be optimized. + try { + final List<Shader> listShaders = ((ShaderGroupAccessor) Minecraft.getMinecraft().entityRenderer.getShaderGroup()).getListShaders(); + + // Should not happen. Something bad happened. + if (listShaders == null) { + return; + } + + // Iterate through the list of shaders. + for (Shader shader : listShaders) { + ShaderUniform su = shader.getShaderManager().getShaderUniform("Progress"); + + if (su == null) { + continue; + } + + // All this for this. + su.set(progress); + } + } catch (IllegalArgumentException ex) { + this.logger.error("An error.png occurred while updating OneConfig's blur. Please report this!", ex); + } + } + + /** + * Activates/deactivates the blur in the current world if + * one of many conditions are met, such as no current other shader + * is being used, we actually have the blur setting enabled + */ + private void reloadBlur(GuiScreen gui) { + // Don't do anything if no world is loaded + if (UMinecraft.getWorld() == null) { + return; + } + + // If a shader is not already active and the UI is + // a one of ours, we should load our own blur! + if (!UMinecraft.getMinecraft().entityRenderer.isShaderActive() && gui instanceof OneConfigGui) { + UMinecraft.getMinecraft().entityRenderer.loadShader(this.blurShader); + + this.start = System.currentTimeMillis(); + + // If a shader is active and the incoming UI is null or we have blur disabled, stop using the shader. + } else if (UMinecraft.getMinecraft().entityRenderer.isShaderActive() && (gui == null)) { + String name = UMinecraft.getMinecraft().entityRenderer.getShaderGroup().getShaderGroupName(); + + // Only stop our specific blur ;) + if (!name.endsWith("fade_in_blur.json")) { + return; + } + + UMinecraft.getMinecraft().entityRenderer.stopUseShader(); + } + } + + /** + * Returns the strength of the blur as determined by the duration the effect of the blur. + * <p> + * The strength of the blur does not go below 5.0F. + */ + private float getBlurStrengthProgress() { + return Math.min((System.currentTimeMillis() - this.start) / 50F, 5.0F); + } +} |