Rewrite the hud

This commit is contained in:
Sollace 2020-09-22 18:13:20 +02:00
parent 2672dc2206
commit c03899d7e2
4 changed files with 135 additions and 77 deletions

View file

@ -14,10 +14,13 @@ import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.options.KeyBinding;
class KeyBindingsHandler {
public class KeyBindingsHandler {
private final String KEY_CATEGORY = "unicopia.category.name";
public static final KeyBindingsHandler INSTANCE = new KeyBindingsHandler();
private final Map<KeyBinding, AbilitySlot> keys = new HashMap<>();
private final Map<AbilitySlot, KeyBinding> reverse = new HashMap<>();
private final Set<KeyBinding> pressed = new HashSet<>();
@ -27,8 +30,14 @@ class KeyBindingsHandler {
addKeybind(GLFW.GLFW_KEY_L, AbilitySlot.TERTIARY);
}
public KeyBinding getBinding(AbilitySlot slot) {
return reverse.get(slot);
}
public void addKeybind(int code, AbilitySlot slot) {
keys.put(KeyBindingHelper.registerKeyBinding(new KeyBinding("key.unicopia" + slot.name().toLowerCase(), code, KEY_CATEGORY)), slot);
KeyBinding binding = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.unicopia" + slot.name().toLowerCase(), code, KEY_CATEGORY));
reverse.put(slot, binding);
keys.put(binding, slot);
}
public void tick(MinecraftClient client) {

View file

@ -10,8 +10,6 @@ import net.minecraft.entity.player.PlayerEntity;
public class UnicopiaClient implements ClientModInitializer {
private final KeyBindingsHandler keyboard = new KeyBindingsHandler();
private Race lastPreferredRace = InteractionManager.instance().getPreferredRace();
@Override
@ -34,7 +32,7 @@ public class UnicopiaClient implements ClientModInitializer {
}
}
keyboard.tick(client);
KeyBindingsHandler.INSTANCE.tick(client);
});
}
}

View file

@ -0,0 +1,103 @@
package com.minelittlepony.unicopia.client.gui;
import com.minelittlepony.unicopia.ability.AbilityDispatcher;
import com.minelittlepony.unicopia.ability.AbilitySlot;
import com.minelittlepony.unicopia.client.KeyBindingsHandler;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.Text;
import net.minecraft.util.math.MathHelper;
class Slot {
private final UHud uHud;
private final AbilitySlot slot;
private int x;
private int y;
private float lastCooldown;
private final int slotPadding;
private final int labelOffset;
private final int size;
private final int iconSize;
private int textureU;
private int textureV;
private int backgroundU = 105;
private int backgroundV = 105;
public Slot(UHud uHud, AbilitySlot slot, int x, int y, int padding, int size, int labelOffset, int iconSize) {
this.uHud = uHud;
this.slot = slot;
this.x = x;
this.y = y;
this.slotPadding = padding;
this.labelOffset = labelOffset;
this.size = size;
this.iconSize = iconSize;
}
Slot background(int u, int v) {
textureU = u;
textureV = v;
return this;
}
Slot foreground(int u, int v) {
backgroundU = u;
backgroundV = v;
return this;
}
void renderBackground(MatrixStack matrices, AbilityDispatcher abilities, float tickDelta) {
matrices.push();
matrices.translate(x, y, 0);
AbilityDispatcher.Stat stat = abilities.getStat(slot);
float cooldown = 0.99999F;// stat.getFillProgress();
// background
UHud.drawTexture(matrices, 0, 0, textureU, textureV, size, size, 128, 128);
if (cooldown > 0 && cooldown < 1) {
float lerpCooldown = MathHelper.lerp(tickDelta, cooldown, lastCooldown);
lastCooldown = lerpCooldown;
int progressBottom = size - slotPadding;
int progressMax = size - slotPadding * 2;
int progressTop = progressBottom - (int)(progressMax * cooldown);
// progress
UHud.fill(matrices, slotPadding, progressTop, size - slotPadding, progressBottom, 0xCFFFFFFF);
RenderSystem.enableAlphaTest();
RenderSystem.enableBlend();
}
// contents
int middle = (int)(size / 2F - iconSize / 2F);
uHud.renderAbilityIcon(matrices, stat, middle, middle, iconSize, iconSize, iconSize, iconSize);
// foreground
UHud.drawTexture(matrices, 0, 0, backgroundU, backgroundV, size, size, 128, 128);
matrices.pop();
}
void renderForeground(MatrixStack matrices, AbilityDispatcher abilities, float tickDelta) {
Text label = KeyBindingsHandler.INSTANCE.getBinding(slot).getBoundKeyLocalizedText();
matrices.push();
matrices.translate(x + labelOffset, y + labelOffset, 0);
matrices.scale(0.5F, 0.5F, 0.5F);
UHud.drawTextWithShadow(matrices, uHud.font, label, 0, 0, 0xFFFFFF);
matrices.pop();
}
}

View file

@ -1,5 +1,8 @@
package com.minelittlepony.unicopia.client.gui;
import java.util.ArrayList;
import java.util.List;
import com.minelittlepony.unicopia.ability.Abilities;
import com.minelittlepony.unicopia.ability.AbilityDispatcher;
import com.minelittlepony.unicopia.ability.AbilitySlot;
@ -7,11 +10,12 @@ import com.minelittlepony.unicopia.entity.player.Pony;
import com.mojang.blaze3d.systems.RenderSystem;
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.util.math.MatrixStack;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.Util;
public class UHud extends DrawableHelper {
@ -19,55 +23,44 @@ public class UHud extends DrawableHelper {
public static final Identifier HUD_TEXTURE = new Identifier("unicopia", "textures/gui/hud.png");
private Slot secondarySlot = new Slot(AbilitySlot.SECONDARY, 26, 0);
private Slot tertiarySlot = new Slot(AbilitySlot.TERTIARY, 36, 24);
public TextRenderer font;
private final MinecraftClient client = MinecraftClient.getInstance();
private final List<Slot> slots = Util.make(new ArrayList<>(), slots -> {
slots.add(new Slot(this, AbilitySlot.PRIMARY, 0, 0, 8, 49, 38, 24).foreground(0, 59));
slots.add(new Slot(this, AbilitySlot.SECONDARY, 26, -5, 3, 22, 17, 18).background(80, 105));
slots.add(new Slot(this, AbilitySlot.TERTIARY, 36, 19, 3, 22, 17, 18).background(80, 105));
});
public void render(InGameHud hud, MatrixStack matrices, float tickDelta) {
if (client.player == null || client.player.isSpectator()) {
return;
}
font = client.textRenderer;
int scaledWidth = client.getWindow().getScaledWidth();
int scaledHeight = client.getWindow().getScaledHeight();
int x = 104 + (scaledWidth - 50) / 2;
int y = 20 + scaledHeight - 70;
matrices.push();
matrices.translate(104 + (scaledWidth - 50) / 2, 20 + scaledHeight - /*70*/ 80, 0);
RenderSystem.enableAlphaTest();
RenderSystem.enableBlend();
client.getTextureManager().bindTexture(HUD_TEXTURE);
int frameHeight = 54;
int frameWidth = 54;
AbilityDispatcher abilities = Pony.of(client.player).getAbilities();
drawTexture(matrices, x, y, 0, 0, frameWidth, frameHeight, 128, 128); // background
AbilityDispatcher.Stat stat = abilities.getStat(AbilitySlot.PRIMARY);
float progressPercent = stat.getFillProgress();
if (progressPercent > 0 && progressPercent < 1) {
int progressHeight = (int)(frameHeight * progressPercent);
drawTexture(matrices, x, y + (frameHeight - progressHeight),
61, frameHeight - progressHeight,
frameWidth, progressHeight, 128, 128); // progress
}
renderAbilityIcon(matrices, stat, x + 9, y + 15, 32, 32, 32, 32);
drawTexture(matrices, x, y, 0, 54, frameWidth, frameHeight, 128, 128); // frame
secondarySlot.render(matrices, abilities, x, y, tickDelta);
tertiarySlot.render(matrices, abilities, x, y, tickDelta);
slots.forEach(slot -> slot.renderBackground(matrices, abilities, tickDelta));
slots.forEach(slot -> slot.renderForeground(matrices, abilities, tickDelta));
RenderSystem.disableBlend();
RenderSystem.disableAlphaTest();
matrices.pop();
}
void renderAbilityIcon(MatrixStack matrices, AbilityDispatcher.Stat stat, int x, int y, int u, int v, int frameWidth, int frameHeight) {
@ -80,49 +73,4 @@ public class UHud extends DrawableHelper {
client.getTextureManager().bindTexture(HUD_TEXTURE);
});
}
class Slot {
private final AbilitySlot slot;
private int x;
private int y;
private float lastCooldown;
public Slot(AbilitySlot slot, int x, int y) {
this.slot = slot;
this.x = x;
this.y = y;
}
void render(MatrixStack matrices, AbilityDispatcher abilities, int x, int y, float tickDelta) {
x += this.x;
y += this.y;
AbilityDispatcher.Stat stat = abilities.getStat(slot);
float cooldown = stat.getFillProgress();
drawTexture(matrices, x, y, 80, 105, 25, 25, 128, 128);
if (cooldown > 0 && cooldown < 1) {
float lerpCooldown = MathHelper.lerp(tickDelta, cooldown, lastCooldown);
lastCooldown = lerpCooldown;
int slotPadding = 4;
int slotSize = 15;
int progressBottom = y + slotPadding + slotSize;
int progressTop = progressBottom - (int)(15F * cooldown);
fill(matrices, x + slotPadding, progressTop, x + slotPadding + slotSize, progressBottom, 0xFFFFFFFF);
RenderSystem.enableAlphaTest();
RenderSystem.enableBlend();
}
renderAbilityIcon(matrices, stat, x + 2, y + 2, 20, 20, 20, 20);
drawTexture(matrices, x, y, 105, 105, 25, 25, 128, 128);
}
}
}