Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Try adding a render
  • Loading branch information
sddsd2332 committed Dec 31, 2024
1 parent fa8e478 commit 14658ca
Show file tree
Hide file tree
Showing 25 changed files with 358 additions and 49 deletions.
39 changes: 38 additions & 1 deletion src/main/java/mekanism/client/gui/element/GuiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import mekanism.client.render.MekanismRenderer;
import mekanism.common.InfuseStorage;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
Expand All @@ -25,7 +27,7 @@
@SideOnly(Side.CLIENT)
public class GuiUtils {

public static void renderExtendedTexture(ResourceLocation resource, int sideWidth, int sideHeight, int left, int top, int width, int height) {
public static void renderExtendedTexture(ResourceLocation resource, int sideWidth, int sideHeight, int left, int top, int width, int height) {
int textureWidth = 2 * sideWidth + 1;
int textureHeight = 2 * sideHeight + 1;
blitNineSlicedSized(resource, left, top, width, height, sideWidth, sideHeight, textureWidth, textureHeight, 0, 0, textureWidth, textureHeight);
Expand Down Expand Up @@ -332,4 +334,39 @@ public static Iterable<Integer> asIterable(int pNumerator, int pDenominator) {
}


public static int drawString(FontRenderer font, String component, float x, float y, int color, boolean drawShadow) {
return font.drawString(component, x, y, color, drawShadow);
}

public static void drawBackdrop(Minecraft minecraft, int x, int y, int width, int height, int alpha) {
int argb = 0xFFFFFF | alpha << 24;
Gui.drawRect(x - 2, y - 2, x + width + 2, y + height + 2, multiply(0xFFFFFF, argb));
}

public static int multiply(int pPackedColourOne, int pPackedColorTwo) {
return color(alpha(pPackedColourOne) * alpha(pPackedColorTwo) / 255, red(pPackedColourOne) * red(pPackedColorTwo) / 255, green(pPackedColourOne) * green(pPackedColorTwo) / 255, blue(pPackedColourOne) * blue(pPackedColorTwo) / 255);
}


public static int alpha(int pPackedColor) {
return pPackedColor >>> 24;
}

public static int red(int pPackedColor) {
return pPackedColor >> 16 & 255;
}

public static int green(int pPackedColor) {
return pPackedColor >> 8 & 255;
}

public static int blue(int pPackedColor) {
return pPackedColor & 255;
}


public static int color(int pAlpha, int pRed, int pGreen, int pBlue) {
return pAlpha << 24 | pRed << 16 | pGreen << 8 | pBlue;
}

}
159 changes: 159 additions & 0 deletions src/main/java/mekanism/client/render/HUDRenderer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package mekanism.client.render;

import mekanism.client.gui.element.GuiUtils;
import mekanism.common.config.MekanismConfig;
import mekanism.common.util.LangUtils;
import mekanism.common.util.MekanismUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import static mekanism.client.render.hud.MekaSuitEnergyLevel.blit;

public class HUDRenderer {

private static final EntityEquipmentSlot[] EQUIPMENT_ORDER = {EntityEquipmentSlot.HEAD, EntityEquipmentSlot.CHEST, EntityEquipmentSlot.LEGS, EntityEquipmentSlot.FEET, EntityEquipmentSlot.MAINHAND,
EntityEquipmentSlot.OFFHAND};


private static final ResourceLocation[] ARMOR_ICONS = {MekanismUtils.getResource(MekanismUtils.ResourceType.GUI_HUD, "hud_mekasuit_helmet.png"),
MekanismUtils.getResource(MekanismUtils.ResourceType.GUI_HUD, "hud_mekasuit_chest.png"),
MekanismUtils.getResource(MekanismUtils.ResourceType.GUI_HUD, "hud_mekasuit_leggings.png"),
MekanismUtils.getResource(MekanismUtils.ResourceType.GUI_HUD, "hud_mekasuit_boots.png")};
private static final ResourceLocation TOOL_ICON = MekanismUtils.getResource(MekanismUtils.ResourceType.GUI_HUD, "hud_mekatool.png");

private static final ResourceLocation COMPASS = MekanismUtils.getResource(MekanismUtils.ResourceType.GUI, "Compass.png");

private long lastTick = -1;
private float prevRotationYaw;
private float prevRotationPitch;

public void renderHUD(Minecraft minecraft, FontRenderer font, float partialTick, int screenWidth, int screenHeight, int maxTextHeight,
boolean reverseHud) {
EntityPlayer player = minecraft.player;
update(minecraft.world, player);
if (MekanismConfig.current().client.hudOpacity.val() < 0.05F) {
return;
}
int color = 0x40F5F0;
GlStateManager.pushMatrix();
float yawJitter = -absSqrt(player.rotationYawHead - prevRotationYaw);
float pitchJitter = -absSqrt(player.rotationPitch - prevRotationPitch);
GlStateManager.translate(yawJitter, pitchJitter, 0);
if (MekanismConfig.current().client.hudCompassEnabled.val()) {
renderCompass(player, font, partialTick, screenWidth, screenHeight, maxTextHeight, reverseHud, color);
minecraft.renderEngine.bindTexture(Gui.ICONS);
}
// renderMekaSuitEnergyIcons(player, font, color);
GlStateManager.popMatrix();
}


private void update(World level, EntityPlayer player) {
// if we're just now rendering the HUD after a pause, reset the pitch/yaw trackers
if (lastTick == -1 || level.getWorldTime() - lastTick > 1) {
prevRotationYaw = player.rotationYaw;
prevRotationPitch = player.rotationPitch;
}
lastTick = level.getWorldTime();
float yawDiff = player.rotationYawHead - prevRotationYaw;
float pitchDiff = player.rotationPitch - prevRotationPitch;
float jitter = MekanismConfig.current().client.hudJitter.val();
prevRotationYaw += yawDiff / jitter;
prevRotationPitch += pitchDiff / jitter;
}

private static float absSqrt(float val) {
float ret = (float) Math.sqrt(Math.abs(val));
return val < 0 ? -ret : ret;
}

/*
public static final EntityEquipmentSlot[] ARMOR_SLOTS = {EntityEquipmentSlot.HEAD, EntityEquipmentSlot.CHEST, EntityEquipmentSlot.LEGS, EntityEquipmentSlot.FEET};
public static final EntityEquipmentSlot[] HAND_SLOTS = {EntityEquipmentSlot.MAINHAND, EntityEquipmentSlot.OFFHAND};
private void renderMekaSuitEnergyIcons(EntityPlayer player, FontRenderer font, int color) {
GlStateManager.pushMatrix();
GlStateManager.translate(10, 10, 0);
int posX = 0;
Predicate<Item> showArmorPercent = item -> item instanceof ItemMekaSuitArmor;
for (int i = 0; i < ARMOR_SLOTS.length; i++) {
posX += renderEnergyIcon(player, font, posX, color, ARMOR_ICONS[i],ARMOR_SLOTS[i], showArmorPercent);
}
Predicate<Item> showToolPercent = item -> item instanceof ItemMekTool;
for (EntityEquipmentSlot hand : HAND_SLOTS) {
posX += renderEnergyIcon(player, font, posX, color, TOOL_ICON, hand, showToolPercent);
}
GlStateManager.popMatrix();
}
private int renderEnergyIcon(EntityPlayer player, FontRenderer font, int posX, int color, ResourceLocation icon, EntityEquipmentSlot slot,
Predicate<Item> showPercent) {
ItemStack stack = player.getItemStackFromSlot(slot);
if (showPercent.test(stack.getItem())) {
renderHUDElement(font, posX, 0, IModuleHelper.INSTANCE.hudElementPercent(icon, StorageUtils.getEnergyRatio(stack)), color, false);
return 48;
}
return 0;
}
*/

private void renderCompass(EntityPlayer player, FontRenderer font, float partialTick, int screenWidth, int screenHeight, int maxTextHeight, boolean reverseHud,
int color) {
//Reversed hud causes the compass to render on the right side of the screen
int posX = reverseHud ? screenWidth - 125 : 25;
//Pin the compass above the bottom of the screen and also above the text hud that may render below it
int posY = Math.min(screenHeight - 20, maxTextHeight) - 80;
GlStateManager.pushMatrix();
GlStateManager.translate(posX + 50, posY + 50, 0);

GlStateManager.scale(0.7F, 0.7F, 0.7F);
BlockPos pos = new BlockPos(player.posX, player.posY, player.posZ);
String coords = LangUtils.localize("x: " + pos.getX() + ",y: " + pos.getY() + ",z: " + pos.getZ());
font.drawString(coords, -font.getStringWidth(coords) / 2F, -4, color, false);

float angle = 180 - getViewYRot(player, partialTick);
GlStateManager.rotate(-60, 1, 0, 0);
GlStateManager.rotate(angle, 0, 0, 1);
GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA, GlStateManager.SourceFactor.ONE, GlStateManager.DestFactor.ZERO);
MekanismRenderer.color(color);
blit(COMPASS, -50, -50, 100, 100, 0, 0, 256, 256, 256, 256);
rotateStr(font, LangUtils.localize("direction.north.short"), angle, 0, color);
rotateStr(font, LangUtils.localize("direction.east.short"), angle, 90, color);
rotateStr(font, LangUtils.localize("direction.south.short"), angle, 180, color);
rotateStr(font, LangUtils.localize("direction.west.short"), angle, 270, color);
MekanismRenderer.resetColor();
;
GlStateManager.popMatrix();
}

private void rotateStr(FontRenderer font, String langEntry, float rotation, float shift, int color) {
GlStateManager.pushMatrix();
GlStateManager.rotate(shift, 0, 0, 1);
GlStateManager.translate(0, -50, 0);
GlStateManager.rotate((-rotation - shift), 0, 0, 1);
GuiUtils.drawString(font, langEntry, -2.5F, -4, color, false);
GlStateManager.popMatrix();
}


public float getViewYRot(EntityPlayer player, float pPartialTicks) {
return pPartialTicks == 1.0F ? player.rotationYawHead : lerp(pPartialTicks, player.prevRotationYawHead, player.rotationYawHead);
}

public static float lerp(float pDelta, float pStart, float pEnd) {
return pStart + pDelta * (pEnd - pStart);
}


}
45 changes: 1 addition & 44 deletions src/main/java/mekanism/client/render/RenderTickHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import mekanism.common.util.MekanismUtils;
import net.minecraft.block.Block;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.AbstractClientPlayer;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.particle.Particle;
Expand All @@ -28,7 +27,6 @@
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderHandEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent.Phase;
import net.minecraftforge.fml.common.gameevent.TickEvent.RenderTickEvent;
Expand Down Expand Up @@ -103,39 +101,6 @@ public void tickEnd(RenderTickEvent event) {
ClientTickHandler.wheelStatus = 0;
}

if (mc.currentScreen == null && !mc.gameSettings.hideGUI && !player.isSpectator() && MekanismConfig.current().client.enableHUD.val()) {
ScaledResolution scaledresolution = new ScaledResolution(mc);
int count = 0;
List<List<String>> renderStrings = new ArrayList<>();
for (EntityEquipmentSlot slotType : EQUIPMENT_ORDER) {
ItemStack stack = player.getItemStackFromSlot(slotType);
if (stack.getItem() instanceof IItemHUDProvider hudProvider) {
count += makeComponent(list -> hudProvider.addHUDStrings(list, player, stack, slotType), renderStrings);
}
}
boolean reverseHud = !MekanismConfig.current().client.alignHUDLeft.val();
if (count > 0) {
float hudScale = MekanismConfig.current().client.hudScale.val();
int xScale = (int) (scaledresolution.getScaledWidth() / hudScale);
int yScale = (int) (scaledresolution.getScaledHeight() / hudScale);
int start = (renderStrings.size() * 2) + (count * 9);
int y = yScale - start;
GlStateManager.pushMatrix();
GlStateManager.scale(hudScale, hudScale, hudScale);
for (List<String> group : renderStrings) {
for (String text : group) {
int textWidth = font.getStringWidth(text);
//Align text to right if hud is reversed, otherwise align to the left
//Note: that we always offset by 2 pixels from the edge of the screen regardless of how it is aligned
int x = reverseHud ? xScale - textWidth - 2 : 2;
font.drawStringWithShadow(text, MekanismConfig.current().client.hudX.val() + x, MekanismConfig.current().client.hudY.val() + y, 0xFFC8C8C8);
y += 9;
}
y += 2;
}
GlStateManager.popMatrix();
}
}
// Traverse a copy of jetpack state and do animations
for (UUID uuid : Mekanism.playerState.getActiveJetpacks()) {
EntityPlayer p = mc.world.getPlayerEntityByUUID(uuid);
Expand Down Expand Up @@ -259,14 +224,6 @@ private void drawString(ScaledResolution res, String s, boolean leftSide, int y,
}
}

private int makeComponent(Consumer<List<String>> adder, List<List<String>> initial) {
List<String> list = new ArrayList<>();
adder.accept(list);
int size = list.size();
if (size > 0) {
initial.add(list);
}
return size;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -46,11 +47,11 @@ public static void blit(ResourceLocation pAtlasLocation, int pX, int pY, int pWi
}


static void blit(ResourceLocation pAtlasLocation, int pX1, int pX2, int pY1, int pY2, int pBlitOffset, int pUWidth, int pVHeight, float pUOffset, float pVOffset, int pTextureWidth, int pTextureHeight) {
public static void blit(ResourceLocation pAtlasLocation, int pX1, int pX2, int pY1, int pY2, int pBlitOffset, int pUWidth, int pVHeight, float pUOffset, float pVOffset, int pTextureWidth, int pTextureHeight) {
innerBlit(pAtlasLocation, pX1, pX2, pY1, pY2, pBlitOffset, (pUOffset + 0.0F) / (float) pTextureWidth, (pUOffset + (float) pUWidth) / (float) pTextureWidth, (pVOffset + 0.0F) / (float) pTextureHeight, (pVOffset + (float) pVHeight) / (float) pTextureHeight);
}

static void innerBlit(ResourceLocation pAtlasLocation, int pX1, int pX2, int pY1, int pY2, int pBlitOffset, float pMinU, float pMaxU, float pMinV, float pMaxV) {
public static void innerBlit(ResourceLocation pAtlasLocation, int pX1, int pX2, int pY1, int pY2, int pBlitOffset, float pMinU, float pMaxU, float pMinV, float pMaxV) {
Minecraft mc = Minecraft.getMinecraft();
mc.renderEngine.bindTexture(pAtlasLocation);
Tessellator tessellator = Tessellator.getInstance();
Expand All @@ -61,6 +62,7 @@ static void innerBlit(ResourceLocation pAtlasLocation, int pX1, int pX2, int pY1
bufferbuilder.pos((float) pX2, (float) pY2, (float) pBlitOffset).tex(pMaxU, pMaxV).endVertex();
bufferbuilder.pos((float) pX2, (float) pY1, (float) pBlitOffset).tex(pMaxU, pMinV).endVertex();
tessellator.draw();
GlStateManager.enableAlpha();
}


Expand Down
Loading

0 comments on commit 14658ca

Please sign in to comment.