Skip to content

Commit

Permalink
Fix pattern wheel fade in & out occasionally flickering
Browse files Browse the repository at this point in the history
  • Loading branch information
2008Choco committed Feb 14, 2024
1 parent b9796a2 commit 79cd53e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ public interface HudComponent {
*/
public boolean shouldRender(@NotNull ClientConfig config, @NotNull FabricServerState serverState);

/**
* Tick this component.
*/
public default void tick() {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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--;
}
}

/**
Expand All @@ -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;
}
}

Expand Down

0 comments on commit 79cd53e

Please sign in to comment.