1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*
* This file is part of OneConfig.
* OneConfig - Next Generation Config Library for Minecraft: Java Edition
* Copyright (C) 2021, 2022 Polyfrost.
* <https://polyfrost.cc> <https://github.com/Polyfrost/>
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* OneConfig is licensed under the terms of version 3 of the GNU Lesser
* General Public License as published by the Free Software Foundation, AND
* under the Additional Terms Applicable to OneConfig, as published by Polyfrost,
* either version 1.0 of the Additional Terms, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License. If not, see <https://www.gnu.org/licenses/>. You should
* have also received a copy of the Additional Terms Applicable
* to OneConfig, as published by Polyfrost. If not, see
* <https://polyfrost.cc/legal/oneconfig/additional-terms>
*/
package cc.polyfrost.oneconfig.internal.gui.impl;
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.internal.gui.BlurHandler;
import cc.polyfrost.oneconfig.internal.mixin.ShaderGroupAccessor;
//#if FABRIC==1
//$$ import cc.polyfrost.oneconfig.internal.mixin.GameRendererAccessor;
//#endif
import cc.polyfrost.oneconfig.libs.eventbus.Subscribe;
import cc.polyfrost.oneconfig.libs.universal.UMinecraft;
import cc.polyfrost.oneconfig.libs.universal.UScreen;
import cc.polyfrost.oneconfig.utils.gui.OneUIScreen;
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 <a href="https://github.com/tterrag1098/Blur/blob/1.8.9/src/main/java/com/tterrag/blur/Blur.java">...</a>
* For the public license, see <a href="https://github.com/tterrag1098/Blur/blob/1.8.9/LICENSE">...</a>
* <p>
* License available under <a href="https://github.com/boomboompower/ToggleChat/blob/master/src/main/resources/licenses/BlurMC-License.txt">...</a>
*
* @author tterrag1098, boomboompower
* <p>
* Taken from ToggleChat
* <a href="https://github.com/boomboompower/ToggleChat/blob/master/LICENSE">...</a>
*/
public class BlurHandlerImpl implements 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 progress = 0;
@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 (!isShaderActive()) {
return;
}
if (progress >= 5) return;
progress = getBlurStrengthProgress();
// 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) UMinecraft.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
*/
public void reloadBlur(Object 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 (!isShaderActive() && (gui instanceof OneUIScreen && ((OneUIScreen) gui).hasBackgroundBlur())) {
//#if FABRIC==1
//$$ ((GameRendererAccessor) UMinecraft.getMinecraft().gameRenderer).invokeLoadShader(this.blurShader);
//#else
UMinecraft.getMinecraft().entityRenderer.loadShader(this.blurShader);
//#endif
this.start = System.currentTimeMillis();
this.progress = 0;
// If a shader is active and the incoming UI is null or we have blur disabled, stop using the shader.
} else if (isShaderActive() && (gui == null || (gui instanceof OneUIScreen && !((OneUIScreen) gui).hasBackgroundBlur()))) {
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);
}
private boolean isShaderActive() {
return UMinecraft.getMinecraft().entityRenderer.getShaderGroup() != null
//#if MC<=11202
&& net.minecraft.client.renderer.OpenGlHelper.shadersSupported
//#endif
;
}
}
|