From a6d083584aa3f14a738f881033a4826d7a76ca21 Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 1 Oct 2020 19:55:51 +0200 Subject: [PATCH] Fixed layering on the hud --- .../unicopia/client/gui/ManaRingSlot.java | 111 ++++++++++++++++++ .../unicopia/client/gui/Slot.java | 21 ++-- .../unicopia/client/gui/UHud.java | 100 +--------------- 3 files changed, 122 insertions(+), 110 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/client/gui/ManaRingSlot.java diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/ManaRingSlot.java b/src/main/java/com/minelittlepony/unicopia/client/gui/ManaRingSlot.java new file mode 100644 index 00000000..9173f938 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/ManaRingSlot.java @@ -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(); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/Slot.java b/src/main/java/com/minelittlepony/unicopia/client/gui/Slot.java index 5af2794a..189e8933 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/Slot.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/Slot.java @@ -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) { diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/UHud.java b/src/main/java/com/minelittlepony/unicopia/client/gui/UHud.java index 45ed4df1..c312ca06 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/UHud.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/UHud.java @@ -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 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(); - } }