Set getAlpha to use custom fade times.

This is in preparation for configurable times.
This commit is contained in:
Markil3
2021-02-27 13:48:48 -07:00
parent 0c1196bbd2
commit ea80abfcc0
3 changed files with 26 additions and 26 deletions

View File

@@ -34,10 +34,7 @@ import org.apache.logging.log4j.Logger;
*/ */
@Mod("immersive_hud") @Mod("immersive_hud")
public class Main public class Main
{ { /**
public static final int FADE_IN_TIME = 5;
public static final int FADE_OUT_TIME = 20;
/**
* Class logger. * Class logger.
*/ */
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger();
@@ -62,15 +59,15 @@ public class Main
* *
* @return The proper transparency to render something. * @return The proper transparency to render something.
*/ */
static float getAlpha(int renderTime, int maxTime) static float getAlpha(int renderTime, int maxTime, int fadeInTime, int fadeOutTime)
{ {
if (renderTime <= FADE_OUT_TIME) if (renderTime <= fadeOutTime)
{ {
return renderTime / (float) FADE_OUT_TIME; return renderTime / (float) fadeOutTime;
} }
else if (renderTime > maxTime - FADE_IN_TIME) else if (renderTime > maxTime - fadeInTime)
{ {
return (maxTime - renderTime) / (float) FADE_IN_TIME; return (maxTime - renderTime) / (float) fadeInTime;
} }
return 1F; return 1F;
} }

View File

@@ -49,6 +49,9 @@ import net.minecraft.world.World;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import static markil3.immersive_hud.TimerUtils.FADE_IN_TIME;
import static markil3.immersive_hud.TimerUtils.FADE_OUT_TIME;
/** /**
* Contains methods for rendering various HUD elements. Most of these methods * Contains methods for rendering various HUD elements. Most of these methods
* come from {@link IngameGui} and * come from {@link IngameGui} and
@@ -177,7 +180,7 @@ public class RenderUtils
return; return;
} }
float alpha = Main.getAlpha(renderTime, maxRenderTime); float alpha = Main.getAlpha(renderTime, maxRenderTime, FADE_IN_TIME, FADE_OUT_TIME);
int scaledWidth = mc.getMainWindow().getScaledWidth(); int scaledWidth = mc.getMainWindow().getScaledWidth();
int scaledHeight = mc.getMainWindow().getScaledHeight(); int scaledHeight = mc.getMainWindow().getScaledHeight();
int xPosition = scaledWidth / 2 - 91; int xPosition = scaledWidth / 2 - 91;
@@ -222,7 +225,7 @@ public class RenderUtils
return; return;
} }
float alpha = Main.getAlpha(renderTime, maxRenderTime); float alpha = Main.getAlpha(renderTime, maxRenderTime, FADE_IN_TIME, FADE_OUT_TIME);
RenderSystem.color4f(1.0F, 1.0F, 1.0F, alpha); RenderSystem.color4f(1.0F, 1.0F, 1.0F, alpha);
if (mc.playerController.gameIsSurvivalOrAdventure()) if (mc.playerController.gameIsSurvivalOrAdventure())
@@ -256,7 +259,7 @@ public class RenderUtils
String s = "" + mc.player.experienceLevel; String s = "" + mc.player.experienceLevel;
int i1 = (scaledWidth - gui.getFontRenderer() int i1 = (scaledWidth - gui.getFontRenderer()
.getStringWidth(s)) / 2; .getStringWidth(s)) / 2;
int j1 = scaledHeight - (int) ((22F * Main.getAlpha(TimerUtils.hotbarTime, maxRenderTime) + 9F) * alpha) - (int) (4F * alpha); int j1 = scaledHeight - (int) ((22F * Main.getAlpha(TimerUtils.hotbarTime, maxRenderTime, FADE_IN_TIME, FADE_OUT_TIME) + 9F) * alpha) - (int) (4F * alpha);
gui.getFontRenderer() gui.getFontRenderer()
.drawString(matrixStack, .drawString(matrixStack,
s, s,
@@ -320,7 +323,7 @@ public class RenderUtils
return; return;
} }
float alpha = Main.getAlpha(renderTime, maxRenderTime); float alpha = Main.getAlpha(renderTime, maxRenderTime, FADE_IN_TIME, FADE_OUT_TIME);
int scaledWidth = mc.getMainWindow().getScaledWidth(); int scaledWidth = mc.getMainWindow().getScaledWidth();
int scaledHeight = mc.getMainWindow().getScaledHeight(); int scaledHeight = mc.getMainWindow().getScaledHeight();

View File

@@ -32,15 +32,13 @@ import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Optional; import java.util.Optional;
import static markil3.immersive_hud.Main.FADE_IN_TIME;
public class TimerUtils public class TimerUtils
{ {
/** /**
* The number of ticks that the hand will be onscreen. Every tick is 1/20th * The number of ticks that the hand will be onscreen. Every tick is 1/20th
* of a second. * of a second.
*/ */
static final int HAND_TIME = 10 * 20; private static final int HAND_TIME = 10 * 20;
/** /**
* The number of ticks most elements will be onscreen. Every tick is 1/20th * The number of ticks most elements will be onscreen. Every tick is 1/20th
* of a second. * of a second.
@@ -50,7 +48,9 @@ public class TimerUtils
* The number of ticks that the health and hunger bars will be onscreen. * The number of ticks that the health and hunger bars will be onscreen.
* Every tick is 1/20th of a second. * Every tick is 1/20th of a second.
*/ */
static final int HEALTH_TIME = (int) ((float) VISUAL_TIME * 2.5); private static final int HEALTH_TIME = (int) ((float) VISUAL_TIME * 2.5);
public static final int FADE_IN_TIME = 5;
public static final int FADE_OUT_TIME = 20;
/** /**
* How many more ticks the hand will be onscreen. Every tick is 1/20th of a * How many more ticks the hand will be onscreen. Every tick is 1/20th of a
@@ -188,7 +188,7 @@ public class TimerUtils
*/ */
public static int getHotbarTranslation() public static int getHotbarTranslation()
{ {
return (int) (22F * Main.getAlpha(hotbarTime, VISUAL_TIME)); return (int) (22F * Main.getAlpha(hotbarTime, VISUAL_TIME, FADE_IN_TIME, FADE_OUT_TIME));
} }
@@ -201,7 +201,7 @@ public class TimerUtils
public static int getExperienceTranslation() public static int getExperienceTranslation()
{ {
return (int) ((getHotbarTranslation() + 10F) * Main.getAlpha( return (int) ((getHotbarTranslation() + 10F) * Main.getAlpha(
experienceTime, VISUAL_TIME)) - 3; experienceTime, VISUAL_TIME, FADE_IN_TIME, FADE_OUT_TIME)) - 3;
} }
@@ -213,7 +213,7 @@ public class TimerUtils
*/ */
public static int getJumpTranslation() public static int getJumpTranslation()
{ {
return (int) ((getHotbarTranslation() + 10F) * Main.getAlpha(jumpTime, VISUAL_TIME)) - 3; return (int) ((getHotbarTranslation() + 10F) * Main.getAlpha(jumpTime, VISUAL_TIME, FADE_IN_TIME, FADE_OUT_TIME)) - 3;
} }
@@ -357,7 +357,7 @@ public class TimerUtils
{ {
setAlpha(Main.getAlpha(crosshairTime > 0 ? setAlpha(Main.getAlpha(crosshairTime > 0 ?
crosshairTime : crosshairTime :
VISUAL_TIME - FADE_IN_TIME, VISUAL_TIME)); VISUAL_TIME, VISUAL_TIME, 0, 0));
} }
else else
{ {
@@ -429,7 +429,7 @@ public class TimerUtils
} }
else else
{ {
effectAlpha = Main.getAlpha(time, VISUAL_TIME); effectAlpha = Main.getAlpha(time, VISUAL_TIME, FADE_IN_TIME, FADE_OUT_TIME);
} }
return effectAlpha; return effectAlpha;
} }
@@ -693,7 +693,7 @@ public class TimerUtils
stack.translate(0F, stack.translate(0F,
getHealthTranslation(), getHealthTranslation(),
0F); 0F);
setAlpha(Main.getAlpha(healthTime, HEALTH_TIME)); setAlpha(Main.getAlpha(healthTime, HEALTH_TIME, FADE_IN_TIME, FADE_OUT_TIME));
return false; return false;
} }
return true; return true;
@@ -754,7 +754,7 @@ public class TimerUtils
} }
else else
{ {
setAlpha(Main.getAlpha(hungerTime, HEALTH_TIME)); setAlpha(Main.getAlpha(hungerTime, HEALTH_TIME, FADE_IN_TIME, FADE_OUT_TIME));
stack.push(); stack.push();
stack.translate(0F, stack.translate(0F,
getHealthTranslation(), getHealthTranslation(),
@@ -780,7 +780,7 @@ public class TimerUtils
*/ */
if (healthTime > 0) if (healthTime > 0)
{ {
setAlpha(Main.getAlpha(healthTime, HEALTH_TIME)); setAlpha(Main.getAlpha(healthTime, HEALTH_TIME, FADE_IN_TIME, FADE_OUT_TIME));
stack.push(); stack.push();
stack.translate(0F, stack.translate(0F,
getHealthTranslation(), getHealthTranslation(),
@@ -859,7 +859,7 @@ public class TimerUtils
stack.translate(0F, stack.translate(0F,
getHealthTranslation(), getHealthTranslation(),
0F); 0F);
setAlpha(Main.getAlpha(mountTime, VISUAL_TIME)); setAlpha(Main.getAlpha(mountTime, VISUAL_TIME, FADE_IN_TIME, FADE_OUT_TIME));
return false; return false;
} }
return true; return true;