Causes things to fade in as well as out.

This commit is contained in:
Markil3
2021-02-27 13:40:45 -07:00
parent c7680888c1
commit 0c1196bbd2
4 changed files with 47 additions and 28 deletions

View File

@@ -33,6 +33,7 @@ import org.apache.logging.log4j.Logger;
import java.util.Optional;
import static markil3.immersive_hud.TimerUtils.VISUAL_TIME;
import static markil3.immersive_hud.TimerUtils.resetAlpha;
/**
@@ -122,6 +123,8 @@ public class EventBus
public static void onGUIDraw(final RenderGameOverlayEvent event)
{
Minecraft mc = Minecraft.getInstance();
boolean fadeIn = false;
switch (event.getType())
{
case CROSSHAIRS:
@@ -159,7 +162,7 @@ public class EventBus
{
RenderUtils.renderHotbar(mc, mc.ingameGUI,
event.getMatrixStack(),
event.getPartialTicks(), TimerUtils.hotbarTime);
event.getPartialTicks(), TimerUtils.hotbarTime, VISUAL_TIME);
}
}
break;
@@ -249,7 +252,7 @@ public class EventBus
if (!TimerUtils.drawJumpbar())
{
RenderUtils.renderHorseJumpBar(mc, mc.ingameGUI,
event.getMatrixStack(), TimerUtils.jumpTime);
event.getMatrixStack(), TimerUtils.jumpTime, VISUAL_TIME);
}
}
break;
@@ -260,7 +263,7 @@ public class EventBus
if (!TimerUtils.drawExperience())
{
RenderUtils.renderExperience(mc, mc.ingameGUI,
event.getMatrixStack(), TimerUtils.experienceTime);
event.getMatrixStack(), TimerUtils.experienceTime, VISUAL_TIME);
}
}
break;

View File

@@ -35,6 +35,8 @@ import org.apache.logging.log4j.Logger;
@Mod("immersive_hud")
public class Main
{
public static final int FADE_IN_TIME = 5;
public static final int FADE_OUT_TIME = 20;
/**
* Class logger.
*/
@@ -56,12 +58,20 @@ public class Main
*
* @param renderTime - How many ticks until the element entirely
* disappears.
* @param maxTime - How many ticks the element can appear.
*
* @return The proper transparency to render something.
*/
static float getAlpha(int renderTime)
static float getAlpha(int renderTime, int maxTime)
{
return Math.min(renderTime / 20.0F,
1.0F);
if (renderTime <= FADE_OUT_TIME)
{
return renderTime / (float) FADE_OUT_TIME;
}
else if (renderTime > maxTime - FADE_IN_TIME)
{
return (maxTime - renderTime) / (float) FADE_IN_TIME;
}
return 1F;
}
}

View File

@@ -170,14 +170,14 @@ public class RenderUtils
public static void renderHorseJumpBar(Minecraft mc,
IngameGui gui,
MatrixStack matrixStack,
int renderTime)
int renderTime, int maxRenderTime)
{
if (renderTime == 0)
{
return;
}
float alpha = Main.getAlpha(renderTime);
float alpha = Main.getAlpha(renderTime, maxRenderTime);
int scaledWidth = mc.getMainWindow().getScaledWidth();
int scaledHeight = mc.getMainWindow().getScaledHeight();
int xPosition = scaledWidth / 2 - 91;
@@ -215,14 +215,14 @@ public class RenderUtils
public static void renderExperience(Minecraft mc,
IngameGui gui,
MatrixStack matrixStack,
int renderTime)
int renderTime, int maxRenderTime)
{
if (renderTime == 0)
{
return;
}
float alpha = Main.getAlpha(renderTime);
float alpha = Main.getAlpha(renderTime, maxRenderTime);
RenderSystem.color4f(1.0F, 1.0F, 1.0F, alpha);
if (mc.playerController.gameIsSurvivalOrAdventure())
@@ -256,7 +256,7 @@ public class RenderUtils
String s = "" + mc.player.experienceLevel;
int i1 = (scaledWidth - gui.getFontRenderer()
.getStringWidth(s)) / 2;
int j1 = scaledHeight - (int) ((22F * Main.getAlpha(TimerUtils.hotbarTime) + 9F) * alpha) - (int) (4F * alpha);
int j1 = scaledHeight - (int) ((22F * Main.getAlpha(TimerUtils.hotbarTime, maxRenderTime) + 9F) * alpha) - (int) (4F * alpha);
gui.getFontRenderer()
.drawString(matrixStack,
s,
@@ -309,7 +309,7 @@ public class RenderUtils
*/
public static void renderHotbar(Minecraft mc, IngameGui gui,
MatrixStack matrixStack,
float partialTicks, int renderTime)
float partialTicks, int renderTime, int maxRenderTime)
{
final ResourceLocation WIDGETS_TEX_PATH =
new ResourceLocation("textures/gui/widgets.png");
@@ -320,7 +320,7 @@ public class RenderUtils
return;
}
float alpha = Main.getAlpha(renderTime);
float alpha = Main.getAlpha(renderTime, maxRenderTime);
int scaledWidth = mc.getMainWindow().getScaledWidth();
int scaledHeight = mc.getMainWindow().getScaledHeight();

View File

@@ -32,6 +32,8 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Optional;
import static markil3.immersive_hud.Main.FADE_IN_TIME;
public class TimerUtils
{
/**
@@ -186,7 +188,7 @@ public class TimerUtils
*/
public static int getHotbarTranslation()
{
return (int) (22F * Main.getAlpha(hotbarTime));
return (int) (22F * Main.getAlpha(hotbarTime, VISUAL_TIME));
}
@@ -199,7 +201,7 @@ public class TimerUtils
public static int getExperienceTranslation()
{
return (int) ((getHotbarTranslation() + 10F) * Main.getAlpha(
experienceTime)) - 3;
experienceTime, VISUAL_TIME)) - 3;
}
@@ -211,7 +213,7 @@ public class TimerUtils
*/
public static int getJumpTranslation()
{
return (int) ((getHotbarTranslation() + 10F) * Main.getAlpha(jumpTime)) - 3;
return (int) ((getHotbarTranslation() + 10F) * Main.getAlpha(jumpTime, VISUAL_TIME)) - 3;
}
@@ -319,6 +321,7 @@ public class TimerUtils
public static void resetMountHealth()
{
mountTime = VISUAL_TIME;
jumpTime = 0;
}
/**
@@ -354,7 +357,7 @@ public class TimerUtils
{
setAlpha(Main.getAlpha(crosshairTime > 0 ?
crosshairTime :
VISUAL_TIME));
VISUAL_TIME - FADE_IN_TIME, VISUAL_TIME));
}
else
{
@@ -405,6 +408,7 @@ public class TimerUtils
final int BLINK_TIME = 200;
float effectAlpha = 0.0F;
Integer time = effectTime.get(effectinstance.getPotion());
if (time == null)
{
effectTime.put(effectinstance.getPotion(), (time = VISUAL_TIME));
@@ -425,7 +429,7 @@ public class TimerUtils
}
else
{
effectAlpha = Main.getAlpha(time);
effectAlpha = Main.getAlpha(time, VISUAL_TIME);
}
return effectAlpha;
}
@@ -627,7 +631,7 @@ public class TimerUtils
if (changed)
{
hotbarTime = VISUAL_TIME;
hotbarTime = VISUAL_TIME - (hotbarTime > 0 ? FADE_IN_TIME : 0);
}
else if (hotbarTime > 0)
{
@@ -671,7 +675,7 @@ public class TimerUtils
}
if (changed)
{
healthTime = HEALTH_TIME;
healthTime = HEALTH_TIME - (healthTime > 0 ? FADE_IN_TIME : 0);
}
else if (healthTime > 0)
{
@@ -689,7 +693,7 @@ public class TimerUtils
stack.translate(0F,
getHealthTranslation(),
0F);
setAlpha(Main.getAlpha(healthTime));
setAlpha(Main.getAlpha(healthTime, HEALTH_TIME));
return false;
}
return true;
@@ -734,7 +738,7 @@ public class TimerUtils
if (changed || hunger <= HUNGER_BOUNDARY)
{
hungerTime = HEALTH_TIME;
hungerTime = HEALTH_TIME - hungerTime == 0 ? FADE_IN_TIME : 0;
}
else if (hungerTime > 0)
{
@@ -750,7 +754,7 @@ public class TimerUtils
}
else
{
setAlpha(Main.getAlpha(hungerTime));
setAlpha(Main.getAlpha(hungerTime, HEALTH_TIME));
stack.push();
stack.translate(0F,
getHealthTranslation(),
@@ -776,7 +780,7 @@ public class TimerUtils
*/
if (healthTime > 0)
{
setAlpha(Main.getAlpha(healthTime));
setAlpha(Main.getAlpha(healthTime, HEALTH_TIME));
stack.push();
stack.translate(0F,
getHealthTranslation(),
@@ -819,6 +823,7 @@ public class TimerUtils
Minecraft mc = Minecraft.getInstance();
Entity tmp = mc.player.getRidingEntity();
boolean changed = false;
if (tmp == null)
{
mountHealth = -1;
@@ -838,9 +843,10 @@ public class TimerUtils
changed = true;
}
}
if (changed)
{
mountTime = VISUAL_TIME;
mountTime = VISUAL_TIME - (mountTime > 0 ? FADE_IN_TIME : 0);
}
else if (mountTime > 0)
{
@@ -853,7 +859,7 @@ public class TimerUtils
stack.translate(0F,
getHealthTranslation(),
0F);
setAlpha(Main.getAlpha(mountTime));
setAlpha(Main.getAlpha(mountTime, VISUAL_TIME));
return false;
}
return true;
@@ -872,7 +878,7 @@ public class TimerUtils
Minecraft mc = Minecraft.getInstance();
if (mc.player.getHorseJumpPower() > 0)
{
jumpTime = VISUAL_TIME;
jumpTime = VISUAL_TIME - (jumpTime > 0 ? FADE_IN_TIME : 0);
}
else if (jumpTime > 0)
{
@@ -901,7 +907,7 @@ public class TimerUtils
}
if (changed)
{
experienceTime = VISUAL_TIME;
experienceTime = VISUAL_TIME - (experienceTime > 0 ? FADE_IN_TIME : 0);
}
else if (experienceTime > 0)
{