From 79cd53e3a5a331747451ebab9cb11b1600ff2c2f Mon Sep 17 00:00:00 2001 From: 2008Choco Date: Wed, 14 Feb 2024 18:22:23 -0500 Subject: [PATCH] Fix pattern wheel fade in & out occasionally flickering --- .../veinminer/client/VeinMinerClient.java | 1 + .../client/render/hud/HudComponent.java | 5 ++ .../render/hud/HudComponentRenderer.java | 9 ++++ .../render/hud/PatternWheelHudComponent.java | 50 +++++++++++-------- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/VeinMinerClient.java b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/VeinMinerClient.java index dec3c7a..c803e3a 100644 --- a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/VeinMinerClient.java +++ b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/VeinMinerClient.java @@ -64,6 +64,7 @@ public void onInitializeClient() { ClientTickEvents.END_CLIENT_TICK.register(client -> { this.keyHandler.tick(); this.blockLookUpdateHandler.tick(client); + this.hudComponentRenderer.tick(); }); ClientPlayConnectionEvents.INIT.register((handler, client) -> serverState = new FabricServerState(this, client)); diff --git a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/hud/HudComponent.java b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/hud/HudComponent.java index ffb846c..f0a9ef2 100644 --- a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/hud/HudComponent.java +++ b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/hud/HudComponent.java @@ -37,4 +37,9 @@ public interface HudComponent { */ public boolean shouldRender(@NotNull ClientConfig config, @NotNull FabricServerState serverState); + /** + * Tick this component. + */ + public default void tick() {} + } diff --git a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/hud/HudComponentRenderer.java b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/hud/HudComponentRenderer.java index cfde9e2..de8a666 100644 --- a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/hud/HudComponentRenderer.java +++ b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/hud/HudComponentRenderer.java @@ -56,4 +56,13 @@ public void render(@NotNull GuiGraphics graphics, float tickDelta) { } } + /** + * Tick all hud components. + */ + public void tick() { + for (HudComponent component : hudComponents) { + component.tick(); + } + } + } diff --git a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/hud/PatternWheelHudComponent.java b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/hud/PatternWheelHudComponent.java index 5bcfb15..7bed6ce 100644 --- a/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/hud/PatternWheelHudComponent.java +++ b/veinminer-fabric/src/main/java/wtf/choco/veinminer/client/render/hud/PatternWheelHudComponent.java @@ -18,11 +18,11 @@ */ public final class PatternWheelHudComponent implements HudComponent { - private static final float STAY_TIME_MS = 3000F; - private static final float FADE_MS = 200F; - private static final float TOTAL_TIME_MS = STAY_TIME_MS + (FADE_MS * 2); + private static final int STAY_TICKS = 60; // 3 seconds + private static final int FADE_TICKS = 4; // 0.2 seconds + private static final int TOTAL_TICKS = STAY_TICKS + (FADE_TICKS * 2); - private float remainingMs = -1L; + private int remainingTicks = 0; @Override public void render(@NotNull Minecraft client, @NotNull FabricServerState serverState, @NotNull GuiGraphics graphics, float tickDelta) { @@ -31,20 +31,21 @@ public void render(@NotNull Minecraft client, @NotNull FabricServerState serverS RenderSystem.enableBlend(); RenderSystem.defaultBlendFunc(); + float actualRemainingMs = remainingTicks - tickDelta; + // Calculate alpha based on progress of fade in/out times - float alphaProgress = 1.0F; - if (remainingMs >= (STAY_TIME_MS + FADE_MS)) { // Fade in - alphaProgress = 1.0F - (remainingMs - (STAY_TIME_MS + FADE_MS)) / FADE_MS; + float alpha = 255.0F; + if (remainingTicks >= (STAY_TICKS + FADE_TICKS)) { // Fade in + float elapsed = TOTAL_TICKS - actualRemainingMs; + alpha = Math.min((int) (elapsed * 255.0F / FADE_TICKS), 255); } - else if (remainingMs < FADE_MS) { // Fade out - alphaProgress = (remainingMs / FADE_MS); + else if (remainingTicks < FADE_TICKS) { // Fade out + alpha = Math.min((int) (actualRemainingMs * 255.0F / FADE_TICKS), 255); } - alphaProgress = Mth.clamp(alphaProgress, 0.0F, 1.0F); - // Final colour with alpha included - int alpha = (Mth.floor(alphaProgress * 255) << 24) & 0xFF000000; - int colour = 0xFFFFFF | alpha; + int finalAlpha = (Mth.floor(alpha) << 24) & 0xFF000000; + int colour = 0xFFFFFF | finalAlpha; String before = serverState.getPreviousPattern().toString(); String selected = serverState.getSelectedPattern().toString(); @@ -62,14 +63,19 @@ else if (remainingMs < FADE_MS) { // Fade out RenderSystem.disableBlend(); - this.remainingMs -= (client.getDeltaFrameTime() * 50); - client.getProfiler().pop(); } @Override public boolean shouldRender(@NotNull ClientConfig config, @NotNull FabricServerState serverState) { - return config.isAllowPatternSwitchingKeybind() && serverState.hasPatternKeys() && remainingMs >= 0L; + return config.isAllowPatternSwitchingKeybind() && serverState.hasPatternKeys() && remainingTicks > 0; + } + + @Override + public void tick() { + if (remainingTicks > 0) { + this.remainingTicks--; + } } /** @@ -80,14 +86,14 @@ public boolean shouldRender(@NotNull ClientConfig config, @NotNull FabricServerS * period. If it is fading out, it will start fading in starting from the current fade out time. */ public void pushRender() { - if (remainingMs <= 0) { // If not rendered, set to max time - this.remainingMs = TOTAL_TIME_MS; + if (remainingTicks <= 0) { // If not rendered, set to max time + this.remainingTicks = TOTAL_TICKS; } - else if (remainingMs < FADE_MS) { // If fading out, fade back in starting at current fade out point - this.remainingMs = TOTAL_TIME_MS - remainingMs; + else if (remainingTicks < FADE_TICKS) { // If fading out, fade back in starting at current fade out point + this.remainingTicks = TOTAL_TICKS - remainingTicks; } - else if (remainingMs < (TOTAL_TIME_MS - FADE_MS)) { // If already faded in (during stay time), skip fade in time - this.remainingMs = TOTAL_TIME_MS - FADE_MS; + else if (remainingTicks < (TOTAL_TICKS - FADE_TICKS)) { // If already faded in (during stay time), skip fade in time + this.remainingTicks = TOTAL_TICKS - FADE_TICKS; } }