diff options
Diffstat (limited to 'src/main/java/de')
4 files changed, 305 insertions, 21 deletions
diff --git a/src/main/java/de/hysky/skyblocker/skyblock/FancyStatusBars.java b/src/main/java/de/hysky/skyblocker/skyblock/FancyStatusBars.java index c1632732..7744626b 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/FancyStatusBars.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/FancyStatusBars.java @@ -2,7 +2,10 @@ package de.hysky.skyblocker.skyblock; import de.hysky.skyblocker.SkyblockerMod; import de.hysky.skyblocker.config.SkyblockerConfigManager; +import de.hysky.skyblocker.skyblock.fancybars.BarGrid; +import de.hysky.skyblocker.skyblock.fancybars.StatusBar; import de.hysky.skyblocker.utils.Utils; +import de.hysky.skyblocker.utils.render.RenderHelper; import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext; @@ -10,6 +13,11 @@ import net.minecraft.client.texture.Sprite; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.util.Identifier; +import java.awt.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.function.Supplier; public class FancyStatusBars { @@ -18,11 +26,11 @@ public class FancyStatusBars { private final MinecraftClient client = MinecraftClient.getInstance(); private final StatusBarTracker statusBarTracker = SkyblockerMod.getInstance().statusBarTracker; - private final StatusBar[] bars = new StatusBar[]{ - new StatusBar(0, 16733525, 2), // Health Bar - new StatusBar(1, 5636095, 2), // Intelligence Bar - new StatusBar(2, 12106180, 1), // Defence Bar - new StatusBar(3, 8453920, 1), // Experience Bar + private final OldStatusBar[] bars = new OldStatusBar[]{ + new OldStatusBar(0, 16733525, 2, new Color[]{new Color(255, 0, 0), new Color(255, 220, 0)}), // Health Bar + new OldStatusBar(1, 5636095, 2, new Color[]{new Color(0, 255, 255), new Color(180, 0, 255)}), // Intelligence Bar + new OldStatusBar(2, 12106180, 1, new Color[]{new Color(255, 255, 255)}), // Defence Bar + new OldStatusBar(3, 8453920, 1, new Color[]{new Color(100, 220, 70)}), // Experience Bar }; // Positions to show the bars @@ -31,6 +39,16 @@ public class FancyStatusBars { private final int[] anchorsX = new int[3]; private final int[] anchorsY = new int[3]; + public static BarGrid barGrid = new BarGrid(); + public static Map<String, StatusBar> statusBars = new HashMap<>(); + + static { + statusBars.put("health", new StatusBar(new Identifier(SkyblockerMod.NAMESPACE, "temp"), new Color[]{new Color(255, 0, 0), new Color(255, 220, 0)}, true, null)); + statusBars.put("intelligence", new StatusBar(new Identifier(SkyblockerMod.NAMESPACE, "temp"), new Color[]{new Color(0, 255, 255), new Color(180, 0, 255)}, true, null)); + statusBars.put("defense", new StatusBar(new Identifier(SkyblockerMod.NAMESPACE, "temp"), new Color[]{new Color(255, 255, 255)}, false, null)); + statusBars.put("experience", new StatusBar(new Identifier(SkyblockerMod.NAMESPACE, "temp"), new Color[]{new Color(100, 220, 70)}, false, null)); + } + public FancyStatusBars() { moveBar(0, 0); moveBar(1, 0); @@ -42,8 +60,9 @@ public class FancyStatusBars { return (100 * value) / max; } - private static final Identifier TEST = new Identifier(SkyblockerMod.NAMESPACE, "bars/bar_test"); - private static final Supplier<Sprite> SUPPLIER = () -> MinecraftClient.getInstance().getGuiAtlasManager().getSprite(TEST); + private static final Identifier BAR_FILL = new Identifier(SkyblockerMod.NAMESPACE, "bars/bar_fill"); + private static final Identifier BAR_BACK = new Identifier(SkyblockerMod.NAMESPACE, "bars/bar_back"); + private static final Supplier<Sprite> SUPPLIER = () -> MinecraftClient.getInstance().getGuiAtlasManager().getSprite(BAR_FILL); public boolean render(DrawContext context, int scaledWidth, int scaledHeight) { var player = client.player; @@ -129,8 +148,9 @@ public class FancyStatusBars { } } - private class StatusBar { + private class OldStatusBar { public final int[] fill; + private final Color[] colors; public int offsetX; private final int v; private final int text_color; @@ -138,13 +158,14 @@ public class FancyStatusBars { public int bar_width; public Object text; - private StatusBar(int i, int textColor, int fillNum) { + private OldStatusBar(int i, int textColor, int fillNum, Color[] colors) { this.v = i * 9; this.text_color = textColor; this.fill = new int[fillNum]; this.fill[0] = 100; this.anchorNum = 0; this.text = ""; + this.colors = colors; } public void update(StatusBarTracker.Resource resource) { @@ -163,23 +184,13 @@ public class FancyStatusBars { context.drawTexture(BARS, anchorsX[anchorNum] + offsetX, anchorsY[anchorNum], 0, v, 9, 9); // Draw the background for the bar - context.drawTexture(BARS, anchorsX[anchorNum] + offsetX + 10, anchorsY[anchorNum], 10, v, 2, 9); - for (int i = 2; i < bar_width - 2; i += 58) { - context.drawTexture(BARS, anchorsX[anchorNum] + offsetX + 10 + i, anchorsY[anchorNum], 12, v, Math.min(58, bar_width - 2 - i), 9); - } - context.drawTexture(BARS, anchorsX[anchorNum] + offsetX + 10 + bar_width - 2, anchorsY[anchorNum], 70, v, 2, 9); + context.drawGuiTexture(BAR_BACK, anchorsX[anchorNum]+ offsetX+10, anchorsY[anchorNum]+1, bar_width, 7); // Draw the filled part of the bar for (int i = 0; i < fill.length; i++) { int fill_width = this.fill[i] * (bar_width - 2) / 100; if (fill_width >= 1) { - context.drawTexture(BARS, anchorsX[anchorNum] + offsetX + 11, anchorsY[anchorNum], 72 + i * 60, v, 1, 9); - } - for (int j = 1; j < fill_width - 1; j += 58) { - context.drawTexture(BARS, anchorsX[anchorNum] + offsetX + 11 + j, anchorsY[anchorNum], 73 + i * 60, v, Math.min(58, fill_width - 1 - j), 9); - } - if (fill_width == bar_width - 2) { - context.drawTexture(BARS, anchorsX[anchorNum] + offsetX + 11 + fill_width - 1, anchorsY[anchorNum], 131 + i * 60, v, 1, 9); + RenderHelper.renderNineSliceColored(context, BAR_FILL, anchorsX[anchorNum] + offsetX + 11, anchorsY[anchorNum]+2, fill_width, 5, colors[i]); } } } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/fancybars/BarGrid.java b/src/main/java/de/hysky/skyblocker/skyblock/fancybars/BarGrid.java new file mode 100644 index 00000000..b85f85e1 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/fancybars/BarGrid.java @@ -0,0 +1,93 @@ +package de.hysky.skyblocker.skyblock.fancybars; + +import java.util.LinkedList; +import java.util.List; + +public class BarGrid { + private final LinkedList<LinkedList<StatusBar>> top = new LinkedList<>(); + private final LinkedList<LinkedList<StatusBar>> bottomLeft = new LinkedList<>(); + private final LinkedList<LinkedList<StatusBar>> bottomRight = new LinkedList<>(); + + public BarGrid() {} + + public void add(int row, StatusBar bar, boolean right) { + if (row > 0) + top.get(row).add(bar); + else if (row < 0) { + if (right) { + bottomRight.get(Math.abs(row)).add(bar); + } else { + bottomLeft.get(Math.abs(row)).add(bar); + } + } + } + + public void add(int x, int y, StatusBar bar) { + if (y > 0) { + if (x < 1) throw new IllegalArgumentException("x can't be negative, x: " + x); + LinkedList<StatusBar> statusBars = top.get(y-1); + statusBars.add(Math.min(x-1, statusBars.size()), bar); + bar.gridY = y; + bar.gridX = statusBars.indexOf(bar)+1; + } else if (y < 0) { + LinkedList<StatusBar> statusBars = (x < 0? bottomLeft: bottomRight).get(Math.abs(y)-1); + statusBars.add(Math.min(Math.abs(x)-1, statusBars.size()), bar); + bar.gridY = y; + bar.gridX = -(statusBars.indexOf(bar)+1); + } + } + + public List<StatusBar> getRow(int row, boolean right) { + if (row > 0) { + return top.get(row-1); + } else { + return (right ? bottomRight: bottomLeft).get(Math.abs(row)-1); + } + } + + public void addRow(int row, boolean right) { + if (row>0) { + top.add(row-1, new LinkedList<>()); + } else if (row<0) { + (right ? bottomRight: bottomLeft).add(Math.abs(row)-1, new LinkedList<>()); + } + } + + public void remove(int x, int y) { + if (y > 0) { + if (x < 1) throw new IllegalArgumentException("x can't be negative, x: " + x); + LinkedList<StatusBar> statusBars = top.get(y-1); + StatusBar remove = statusBars.remove(x-1); + for (int i = x-1; i < statusBars.size(); i++) { + statusBars.get(i).gridX--; + } + remove.gridX = 0; + remove.gridY = 0; + if (statusBars.isEmpty()) { + top.remove(y - 1); + for (int i = y-1; i < top.size(); i++) { + for (StatusBar bar : top.get(i)) { + bar.gridY--; + } + } + } + } else if (y < 0) { + LinkedList<LinkedList<StatusBar>> bottom = x < 0 ? bottomLeft : bottomRight; + LinkedList<StatusBar> statusBars = bottom.get(Math.abs(y)-1); + StatusBar remove = statusBars.remove(Math.abs(x) - 1); + for (int i = Math.abs(x)-1; i < statusBars.size(); i++) { + statusBars.get(i).gridX--; + } + remove.gridX = 0; + remove.gridY = 0; + if (statusBars.isEmpty()) { + bottom.remove(Math.abs(y) - 1); + for (int i = Math.abs(y)-1; i < bottom.size(); i++) { + for (StatusBar bar : bottom.get(i)) { + bar.gridY--; + } + } + } + } + } +} diff --git a/src/main/java/de/hysky/skyblocker/skyblock/fancybars/StatusBar.java b/src/main/java/de/hysky/skyblocker/skyblock/fancybars/StatusBar.java new file mode 100644 index 00000000..9c688051 --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/fancybars/StatusBar.java @@ -0,0 +1,136 @@ +package de.hysky.skyblocker.skyblock.fancybars; + +import de.hysky.skyblocker.SkyblockerMod; +import de.hysky.skyblocker.utils.render.RenderHelper; +import net.minecraft.client.gui.*; +import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; +import net.minecraft.client.gui.widget.ClickableWidget; +import net.minecraft.client.gui.widget.Widget; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.Nullable; + +import java.awt.*; +import java.util.function.Consumer; + +public class StatusBar implements Widget, Drawable, Element, Selectable { + + private static final Identifier BAR_FILL = new Identifier(SkyblockerMod.NAMESPACE, "bars/bar_fill"); + private static final Identifier BAR_BACK = new Identifier(SkyblockerMod.NAMESPACE, "bars/bar_back"); + + private final Identifier icon; + private final Color[] colors; + private final boolean hasOverflow; + private final @Nullable Color textColor; + + private @Nullable Consumer<StatusBar> onClick = null; + public int gridX = 0; + public int gridY = 0; + + public float size = 1; + private int width = 0; + + public float fill = 0; + public float overflowFill = 0; + + private int x = 0; + private int y = 0; + + public StatusBar(Identifier icon, Color[] colors, boolean hasOverflow, @Nullable Color textColor) { + this.icon = icon; + this.colors = colors; + this.hasOverflow = hasOverflow; + this.textColor = textColor; + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + context.drawGuiTexture(icon, x, y, 9, 9); + context.drawGuiTexture(BAR_BACK, x + 10, y + 1, width - 10, 7); + RenderHelper.renderNineSliceColored(context, BAR_FILL, x + 11, y + 2, (int) ((width - 12) * fill), 5, colors[0]); + if (hasOverflow) { + RenderHelper.renderNineSliceColored(context, BAR_FILL, x + 11, y + 2, (int) ((width - 12) * overflowFill), 5, colors[1]); + } + } + + // GUI shenanigans + + @Override + public void setX(int x) { + this.x = x; + } + + @Override + public void setY(int y) { + this.y = y; + } + + @Override + public int getX() { + return x; + } + + @Override + public int getY() { + return y; + } + + @Override + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + @Override + public int getHeight() { + return 9; + } + + @Override + public ScreenRect getNavigationFocus() { + return Widget.super.getNavigationFocus(); + } + + @Override + public boolean isMouseOver(double mouseX, double mouseY) { + return mouseX >= x && mouseX <= x + getWidth() && mouseY >= y && mouseY <= y + getHeight(); + } + + @Override + public void forEachChild(Consumer<ClickableWidget> consumer) {} + + @Override + public void setFocused(boolean focused) { + + } + + @Override + public boolean isFocused() { + return false; + } + + @Override + public SelectionType getType() { + return SelectionType.NONE; + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (!isMouseOver(mouseX, mouseY)) return false; + if (onClick != null) { + onClick.accept(this); + } + return true; + } + + public void setOnClick(@Nullable Consumer<StatusBar> onClick) { + this.onClick = onClick; + } + + @Override + public void appendNarrations(NarrationMessageBuilder builder) { + + } +} diff --git a/src/main/java/de/hysky/skyblocker/skyblock/fancybars/StatusBarsConfigScreen.java b/src/main/java/de/hysky/skyblocker/skyblock/fancybars/StatusBarsConfigScreen.java new file mode 100644 index 00000000..1f0b7ffe --- /dev/null +++ b/src/main/java/de/hysky/skyblocker/skyblock/fancybars/StatusBarsConfigScreen.java @@ -0,0 +1,44 @@ +package de.hysky.skyblocker.skyblock.fancybars; + +import de.hysky.skyblocker.skyblock.FancyStatusBars; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; +import org.jetbrains.annotations.Nullable; + +public class StatusBarsConfigScreen extends Screen { + + private static final Identifier HOTBAR_TEXTURE = new Identifier("hud/hotbar"); + + private @Nullable StatusBar cursorBar = null; + protected StatusBarsConfigScreen(Text title) { + super(title); + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + super.render(context, mouseX, mouseY, delta); + context.drawGuiTexture(HOTBAR_TEXTURE, width/2 - 91, height-22, 182, 22); + if (cursorBar != null) { + cursorBar.setX(mouseX); + cursorBar.setY(mouseY); + cursorBar.render(context, mouseX, mouseY, delta); + } + } + + @Override + protected void init() { + super.init(); + FancyStatusBars.statusBars.values().forEach(this::setup); + } + + private void setup(StatusBar statusBar) { + this.addDrawableChild(statusBar); + statusBar.setOnClick(this::onClick); + } + + private void onClick(StatusBar statusBar) { + + } +} |