Fixed layering on the hud

This commit is contained in:
Sollace 2020-10-01 19:55:51 +02:00
parent f4bffad815
commit a6d083584a
3 changed files with 122 additions and 110 deletions

View file

@ -0,0 +1,111 @@
package com.minelittlepony.unicopia.client.gui;
import com.minelittlepony.unicopia.ability.AbilityDispatcher;
import com.minelittlepony.unicopia.ability.AbilitySlot;
import com.minelittlepony.unicopia.entity.player.MagicReserves;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.entity.player.MagicReserves.Bar;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.BufferRenderer;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Matrix4f;
class ManaRingSlot extends Slot {
public ManaRingSlot(UHud uHud, AbilitySlot normalSlot, AbilitySlot backupSlot, int x, int y, int padding, int size,
int labelOffset, int iconSize) {
super(uHud, normalSlot, backupSlot, x, y, padding, size, labelOffset, iconSize);
}
@Override
protected void renderContents(MatrixStack matrices, AbilityDispatcher abilities, boolean bSwap, float tickDelta) {
matrices.push();
matrices.translate(24.5, 25.5, 0);
MagicReserves mana = Pony.of(uHud.client.player).getMagicalReserves();
double arcBegin = 0;
arcBegin = renderRing(matrices, 17, 13, 0, mana.getMana(), 0xFF88FF99);
arcBegin = renderRing(matrices, 17, 13, arcBegin, mana.getEnergy(), 0xFF002299);
matrices.pop();
super.renderContents(matrices, abilities, bSwap, tickDelta);
}
private double renderRing(MatrixStack matrices, double outerRadius, double innerRadius, double offsetAngle, Bar bar, int color) {
double fill = bar.getPercentFill() * Math.PI * 2;
renderArc(matrices, innerRadius, outerRadius, offsetAngle, fill, color, false);
return offsetAngle + fill;
}
/**
* Renders a colored arc.
*
* @param mirrorHorizontally Whether or not the arc must be mirrored across the horizontal plane. Will produce a bar that grows from the middle filling both sides.
*/
static void renderArc(MatrixStack matrices, double innerRadius, double outerRadius, double startAngle, double arcAngle, int color, boolean mirrorHorizontally) {
float f = (color >> 24 & 255) / 255F;
float g = (color >> 16 & 255) / 255F;
float h = (color >> 8 & 255) / 255F;
float k = (color & 255) / 255F;
final double num_rings = 300;
final double twoPi = Math.PI * 2;
final double increment = twoPi / num_rings;
if (arcAngle < increment) {
return;
}
final double maxAngle = MathHelper.clamp(startAngle + arcAngle, 0, twoPi - increment);
BufferBuilder bufferBuilder = Tessellator.getInstance().getBuffer();
RenderSystem.enableBlend();
RenderSystem.disableTexture();
RenderSystem.defaultBlendFunc();
bufferBuilder.begin(7, VertexFormats.POSITION_COLOR);
Matrix4f model = matrices.peek().getModel();
if (!mirrorHorizontally) {
startAngle = -startAngle;
}
for (double angle = startAngle; angle >= -maxAngle; angle -= increment) {
// center
bufferBuilder.vertex(model,
(float)(innerRadius * Math.sin(angle)),
(float)(innerRadius * Math.cos(angle)), 0).color(f, g, h, k).next();
// point one
bufferBuilder.vertex(model,
(float)(outerRadius * Math.sin(angle)),
(float)(outerRadius * Math.cos(angle)), 0).color(f, g, h, k).next();
// point two
bufferBuilder.vertex(model,
(float)(outerRadius * Math.sin(angle + increment)),
(float)(outerRadius * Math.cos(angle + increment)), 0).color(f, g, h, k).next();
// back to center
bufferBuilder.vertex(model,
(float)(innerRadius * Math.sin(angle + increment)),
(float)(innerRadius * Math.cos(angle + increment)), 0).color(f, g, h, k).next();
}
bufferBuilder.end();
BufferRenderer.draw(bufferBuilder);
RenderSystem.enableTexture();
}
}

View file

@ -10,13 +10,13 @@ import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;
class Slot {
private final UHud uHud;
protected final UHud uHud;
private final AbilitySlot aSlot;
private final AbilitySlot bSlot;
private int x;
private int y;
protected int x;
protected int y;
private float lastCooldown;
@ -59,8 +59,7 @@ class Slot {
matrices.push();
matrices.translate(x, y, 0);
AbilityDispatcher.Stat stat = abilities.getStat(bSwap ? bSlot : aSlot);
float cooldown = stat.getFillProgress();
float cooldown = abilities.getStat(bSwap ? bSlot : aSlot).getFillProgress();
// background
UHud.drawTexture(matrices, 0, 0, textureU, textureV, size, size, 128, 128);
@ -80,17 +79,15 @@ class Slot {
RenderSystem.enableBlend();
}
// contents
uHud.renderAbilityIcon(matrices, stat, slotPadding / 2, slotPadding / 2, iconSize, iconSize, iconSize, iconSize);
renderContents(matrices, abilities, bSwap, tickDelta);
matrices.pop();
}
void renderForeground(MatrixStack matrices, AbilityDispatcher abilities, float tickDelta) {
matrices.push();
matrices.translate(x, y, 0);
protected void renderContents(MatrixStack matrices, AbilityDispatcher abilities, boolean bSwap, float tickDelta) {
// contents
uHud.renderAbilityIcon(matrices, abilities.getStat(bSwap ? bSlot : aSlot), slotPadding / 2, slotPadding / 2, iconSize, iconSize, iconSize, iconSize);
UHud.drawTexture(matrices, 0, 0, backgroundU, backgroundV, size, size, 128, 128);
matrices.pop();
}
void renderLabel(MatrixStack matrices, AbilityDispatcher abilities, float tickDelta) {

View file

@ -6,8 +6,6 @@ import java.util.List;
import com.minelittlepony.unicopia.ability.Abilities;
import com.minelittlepony.unicopia.ability.AbilityDispatcher;
import com.minelittlepony.unicopia.ability.AbilitySlot;
import com.minelittlepony.unicopia.entity.player.MagicReserves;
import com.minelittlepony.unicopia.entity.player.MagicReserves.Bar;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.mojang.blaze3d.systems.RenderSystem;
@ -15,15 +13,9 @@ import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawableHelper;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.render.BufferBuilder;
import net.minecraft.client.render.BufferRenderer;
import net.minecraft.client.render.Tessellator;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Matrix4f;
public class UHud extends DrawableHelper {
@ -33,10 +25,10 @@ public class UHud extends DrawableHelper {
public TextRenderer font;
private final MinecraftClient client = MinecraftClient.getInstance();
final MinecraftClient client = MinecraftClient.getInstance();
private final List<Slot> slots = Util.make(new ArrayList<>(), slots -> {
slots.add(new Slot(this, AbilitySlot.PRIMARY, AbilitySlot.PASSIVE, 0, 0, 8, 49, 38, 42).background(0, 5).foreground(0, 59));
slots.add(new ManaRingSlot(this, AbilitySlot.PRIMARY, AbilitySlot.PASSIVE, 0, 0, 8, 49, 38, 42).background(0, 5).foreground(0, 59));
slots.add(new Slot(this, AbilitySlot.SECONDARY, AbilitySlot.SECONDARY, 26, -5, 3, 22, 17, 19).background(80, 105));
slots.add(new Slot(this, AbilitySlot.TERTIARY, AbilitySlot.TERTIARY, 36, 19, 3, 22, 17, 19).background(80, 105));
});
@ -65,11 +57,6 @@ public class UHud extends DrawableHelper {
boolean swap = client.options.keySneak.isPressed();
slots.forEach(slot -> slot.renderBackground(matrices, abilities, swap, tickDelta));
renderManaRings(matrices);
slots.forEach(slot -> slot.renderForeground(matrices, abilities, tickDelta));
slots.forEach(slot -> slot.renderLabel(matrices, abilities, tickDelta));
RenderSystem.disableBlend();
@ -89,87 +76,4 @@ public class UHud extends DrawableHelper {
});
}
void renderManaRings(MatrixStack matrices) {
matrices.push();
matrices.translate(24.5, 25.5, 0);
MagicReserves mana = Pony.of(client.player).getMagicalReserves();
double arcBegin = 0;
arcBegin = renderRing(matrices, 17, 13, 0, mana.getMana(), 0xFF88FF99);
arcBegin = renderRing(matrices, 17, 13, arcBegin, mana.getEnergy(), 0xFF002299);
matrices.pop();
}
private double renderRing(MatrixStack matrices, double outerRadius, double innerRadius, double offsetAngle, Bar bar, int color) {
double fill = bar.getPercentFill() * Math.PI * 2;
renderArc(matrices, innerRadius, outerRadius, offsetAngle, fill, color, false);
return offsetAngle + fill;
}
/**
* Renders a colored arc.
*
* @param mirrorHorizontally Whether or not the arc must be mirrored across the horizontal plane. Will produce a bar that grows from the middle filling both sides.
*/
static void renderArc(MatrixStack matrices, double innerRadius, double outerRadius, double startAngle, double arcAngle, int color, boolean mirrorHorizontally) {
float f = (color >> 24 & 255) / 255F;
float g = (color >> 16 & 255) / 255F;
float h = (color >> 8 & 255) / 255F;
float k = (color & 255) / 255F;
final double num_rings = 300;
final double twoPi = Math.PI * 2;
final double increment = twoPi / num_rings;
if (arcAngle < increment) {
return;
}
final double maxAngle = MathHelper.clamp(startAngle + arcAngle, 0, twoPi - increment);
BufferBuilder bufferBuilder = Tessellator.getInstance().getBuffer();
RenderSystem.enableBlend();
RenderSystem.disableTexture();
RenderSystem.defaultBlendFunc();
bufferBuilder.begin(7, VertexFormats.POSITION_COLOR);
Matrix4f model = matrices.peek().getModel();
if (!mirrorHorizontally) {
startAngle = -startAngle;
}
for (double angle = startAngle; angle >= -maxAngle; angle -= increment) {
// center
bufferBuilder.vertex(model,
(float)(innerRadius * Math.sin(angle)),
(float)(innerRadius * Math.cos(angle)), 0).color(f, g, h, k).next();
// point one
bufferBuilder.vertex(model,
(float)(outerRadius * Math.sin(angle)),
(float)(outerRadius * Math.cos(angle)), 0).color(f, g, h, k).next();
// point two
bufferBuilder.vertex(model,
(float)(outerRadius * Math.sin(angle + increment)),
(float)(outerRadius * Math.cos(angle + increment)), 0).color(f, g, h, k).next();
// back to center
bufferBuilder.vertex(model,
(float)(innerRadius * Math.sin(angle + increment)),
(float)(innerRadius * Math.cos(angle + increment)), 0).color(f, g, h, k).next();
}
bufferBuilder.end();
BufferRenderer.draw(bufferBuilder);
RenderSystem.enableTexture();
}
}