| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.NativeImage;
|
| 4 | import com.mojang.blaze3d.platform.Window;
|
| 5 | import java.io.IOException;
|
| 6 | import java.io.InputStream;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.function.Consumer;
|
| 9 | import java.util.function.IntSupplier;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.client.Minecraft;
|
| 13 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 14 | import net.minecraft.client.renderer.RenderPipelines;
|
| 15 | import net.minecraft.client.renderer.texture.MipmapStrategy;
|
| 16 | import net.minecraft.client.renderer.texture.ReloadableTexture;
|
| 17 | import net.minecraft.client.renderer.texture.TextureContents;
|
| 18 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 19 | import net.minecraft.client.resources.metadata.texture.TextureMetadataSection;
|
| 20 | import net.minecraft.resources.Identifier;
|
| 21 | import net.minecraft.server.packs.resources.ReloadInstance;
|
| 22 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 23 | import net.minecraft.server.packs.resources.ResourceProvider;
|
| 24 | import net.minecraft.util.ARGB;
|
| 25 | import net.minecraft.util.Mth;
|
| 26 | import net.minecraft.util.Util;
|
| 27 |
|
| 28 | @Environment(EnvType.CLIENT)
|
| 29 | public class LoadingOverlay extends Overlay {
|
| 30 | public static final Identifier MOJANG_STUDIOS_LOGO_LOCATION = Identifier.withDefaultNamespace("textures/gui/title/mojangstudios.png");
|
| 31 | private static final int LOGO_BACKGROUND_COLOR = ARGB.color(255, 239, 50, 61);
|
| 32 | private static final int LOGO_BACKGROUND_COLOR_DARK = ARGB.color(255, 0, 0, 0);
|
| 33 | private static final IntSupplier BRAND_BACKGROUND = () -> Minecraft.getInstance().options.darkMojangStudiosBackground().get()
|
| 34 | ? LOGO_BACKGROUND_COLOR_DARK
|
| 35 | : LOGO_BACKGROUND_COLOR;
|
| 36 | private static final int LOGO_SCALE = 240;
|
| 37 | private static final float LOGO_QUARTER_FLOAT = 60.0F;
|
| 38 | private static final int LOGO_QUARTER = 60;
|
| 39 | private static final int LOGO_HALF = 120;
|
| 40 | private static final float LOGO_OVERLAP = 0.0625F;
|
| 41 | private static final float SMOOTHING = 0.95F;
|
| 42 | public static final long FADE_OUT_TIME = 1000L;
|
| 43 | public static final long FADE_IN_TIME = 500L;
|
| 44 | private final Minecraft minecraft;
|
| 45 | private final ReloadInstance reload;
|
| 46 | private final Consumer<Optional<Throwable>> onFinish;
|
| 47 | private final boolean fadeIn;
|
| 48 | private float currentProgress;
|
| 49 | private long fadeOutStart = -1L;
|
| 50 | private long fadeInStart = -1L;
|
| 51 |
|
| 52 | public LoadingOverlay(final Minecraft minecraft, final ReloadInstance reload, final Consumer<Optional<Throwable>> onFinish, final boolean fadeIn) {
|
| 53 | this.minecraft = minecraft;
|
| 54 | this.reload = reload;
|
| 55 | this.onFinish = onFinish;
|
| 56 | this.fadeIn = fadeIn;
|
| 57 | }
|
| 58 |
|
| 59 | public static void registerTextures(final TextureManager textureManager) {
|
| 60 | textureManager.registerAndLoad(MOJANG_STUDIOS_LOGO_LOCATION, new LoadingOverlay.LogoTexture());
|
| 61 | }
|
| 62 |
|
| 63 | private static int replaceAlpha(final int color, final int alpha) {
|
| 64 | return color & 16777215 | alpha << 24;
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 69 | int width = graphics.guiWidth();
|
| 70 | int height = graphics.guiHeight();
|
| 71 | long now = Util.getMillis();
|
| 72 | if (this.fadeIn && this.fadeInStart == -1L) {
|
| 73 | this.fadeInStart = now;
|
| 74 | }
|
| 75 |
|
| 76 | float fadeOutAnim = this.fadeOutStart > -1L ? (float)(now - this.fadeOutStart) / 1000.0F : -1.0F;
|
| 77 | float fadeInAnim = this.fadeInStart > -1L ? (float)(now - this.fadeInStart) / 500.0F : -1.0F;
|
| 78 | float logoAlpha;
|
| 79 | if (fadeOutAnim >= 1.0F) {
|
| 80 | if (this.minecraft.gui.screen() != null) {
|
| 81 | this.minecraft.gui.screen().extractRenderStateWithTooltipAndSubtitles(graphics, 0, 0, a);
|
| 82 | } else {
|
| 83 | this.minecraft.gui.hud.extractDeferredSubtitles();
|
| 84 | }
|
| 85 |
|
| 86 | int alpha = Mth.ceil((1.0F - Mth.clamp(fadeOutAnim - 1.0F, 0.0F, 1.0F)) * 255.0F);
|
| 87 | graphics.nextStratum();
|
| 88 | graphics.fill(0, 0, width, height, replaceAlpha(BRAND_BACKGROUND.getAsInt(), alpha));
|
| 89 | logoAlpha = 1.0F - Mth.clamp(fadeOutAnim - 1.0F, 0.0F, 1.0F);
|
| 90 | } else if (this.fadeIn) {
|
| 91 | if (this.minecraft.gui.screen() != null && fadeInAnim < 1.0F) {
|
| 92 | this.minecraft.gui.screen().extractRenderStateWithTooltipAndSubtitles(graphics, mouseX, mouseY, a);
|
| 93 | } else {
|
| 94 | this.minecraft.gui.hud.extractDeferredSubtitles();
|
| 95 | }
|
| 96 |
|
| 97 | int alpha = Mth.ceil(Mth.clamp(fadeInAnim, 0.15, 1.0) * 255.0);
|
| 98 | graphics.nextStratum();
|
| 99 | graphics.fill(0, 0, width, height, replaceAlpha(BRAND_BACKGROUND.getAsInt(), alpha));
|
| 100 | logoAlpha = Mth.clamp(fadeInAnim, 0.0F, 1.0F);
|
| 101 | } else {
|
| 102 | ARGB.setVector4fFromARGB32(this.minecraft.gameRenderer.gameRenderState().guiRenderState.clearColorOverride, BRAND_BACKGROUND.getAsInt());
|
| 103 | logoAlpha = 1.0F;
|
| 104 | }
|
| 105 |
|
| 106 | int contentX = (int)(graphics.guiWidth() * 0.5);
|
| 107 | int logoY = (int)(graphics.guiHeight() * 0.5);
|
| 108 | double logoHeight = Math.min(graphics.guiWidth() * 0.75, graphics.guiHeight()) * 0.25;
|
| 109 | int logoHeightHalf = (int)(logoHeight * 0.5);
|
| 110 | double contentWidth = logoHeight * 4.0;
|
| 111 | int logoWidthHalf = (int)(contentWidth * 0.5);
|
| 112 | int color = ARGB.white(logoAlpha);
|
| 113 | graphics.blit(
|
| 114 | RenderPipelines.MOJANG_LOGO,
|
| 115 | MOJANG_STUDIOS_LOGO_LOCATION,
|
| 116 | contentX - logoWidthHalf,
|
| 117 | logoY - logoHeightHalf,
|
| 118 | -0.0625F,
|
| 119 | 0.0F,
|
| 120 | logoWidthHalf,
|
| 121 | (int)logoHeight,
|
| 122 | 120,
|
| 123 | 60,
|
| 124 | 120,
|
| 125 | 120,
|
| 126 | color
|
| 127 | );
|
| 128 | graphics.blit(
|
| 129 | RenderPipelines.MOJANG_LOGO,
|
| 130 | MOJANG_STUDIOS_LOGO_LOCATION,
|
| 131 | contentX,
|
| 132 | logoY - logoHeightHalf,
|
| 133 | 0.0625F,
|
| 134 | 60.0F,
|
| 135 | logoWidthHalf,
|
| 136 | (int)logoHeight,
|
| 137 | 120,
|
| 138 | 60,
|
| 139 | 120,
|
| 140 | 120,
|
| 141 | color
|
| 142 | );
|
| 143 | int barY = (int)(graphics.guiHeight() * 0.8325);
|
| 144 | float actualProgress = this.reload.getActualProgress();
|
| 145 | this.currentProgress = Mth.clamp(this.currentProgress * 0.95F + actualProgress * 0.050000012F, 0.0F, 1.0F);
|
| 146 | if (fadeOutAnim < 1.0F) {
|
| 147 | this.extractProgressBar(graphics, width / 2 - logoWidthHalf, barY - 5, width / 2 + logoWidthHalf, barY + 5, 1.0F - Mth.clamp(fadeOutAnim, 0.0F, 1.0F));
|
| 148 | }
|
| 149 |
|
| 150 | if (fadeOutAnim >= 2.0F) {
|
| 151 | this.minecraft.gui.setOverlay(null);
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public void tick() {
|
| 157 | if (this.fadeOutStart == -1L && this.reload.isDone() && this.isReadyToFadeOut()) {
|
| 158 | try {
|
| 159 | this.reload.checkExceptions();
|
| 160 | this.onFinish.accept(Optional.empty());
|
| 161 | } catch (Throwable t) {
|
| 162 | this.onFinish.accept(Optional.of(t));
|
| 163 | }
|
| 164 |
|
| 165 | this.fadeOutStart = Util.getMillis();
|
| 166 | if (this.minecraft.gui.screen() != null) {
|
| 167 | Window window = this.minecraft.getWindow();
|
| 168 | this.minecraft.gui.screen().init(window.getGuiScaledWidth(), window.getGuiScaledHeight());
|
| 169 | }
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | private boolean isReadyToFadeOut() {
|
| 174 | return !this.fadeIn || this.fadeInStart > -1L && Util.getMillis() - this.fadeInStart >= 1000L;
|
| 175 | }
|
| 176 |
|
| 177 | private void extractProgressBar(final GuiGraphicsExtractor graphics, final int x0, final int y0, final int x1, final int y1, final float fade) {
|
| 178 | int width = Mth.ceil((x1 - x0 - 2) * this.currentProgress);
|
| 179 | int alpha = Math.round(fade * 255.0F);
|
| 180 | int white = ARGB.color(alpha, 255, 255, 255);
|
| 181 | graphics.fill(x0 + 2, y0 + 2, x0 + width, y1 - 2, white);
|
| 182 | graphics.fill(x0 + 1, y0, x1 - 1, y0 + 1, white);
|
| 183 | graphics.fill(x0 + 1, y1, x1 - 1, y1 - 1, white);
|
| 184 | graphics.fill(x0, y0, x0 + 1, y1, white);
|
| 185 | graphics.fill(x1, y0, x1 - 1, y1, white);
|
| 186 | }
|
| 187 |
|
| 188 | @Environment(EnvType.CLIENT)
|
| 189 | private static class LogoTexture extends ReloadableTexture {
|
| 190 | public LogoTexture() {
|
| 191 | super(LoadingOverlay.MOJANG_STUDIOS_LOGO_LOCATION);
|
| 192 | }
|
| 193 |
|
| 194 | @Override
|
| 195 | public TextureContents loadContents(final ResourceManager resourceManager) throws IOException {
|
| 196 | ResourceProvider vanillaProvider = Minecraft.getInstance().getVanillaPackResources().asResourceManager();
|
| 197 |
|
| 198 | try (InputStream resource = vanillaProvider.open(LoadingOverlay.MOJANG_STUDIOS_LOGO_LOCATION)) {
|
| 199 | return new TextureContents(NativeImage.read(resource), new TextureMetadataSection(true, true, MipmapStrategy.MEAN, 0.0F));
|
| 200 | }
|
| 201 | }
|
| 202 | }
|
| 203 | }
|
| 204 |
|