diff --git a/src/main/java/codechicken/lib/gui/modular/elements/GuiProgressIcon.java b/src/main/java/codechicken/lib/gui/modular/elements/GuiProgressIcon.java index 3e56ce61..47a0caf0 100644 --- a/src/main/java/codechicken/lib/gui/modular/elements/GuiProgressIcon.java +++ b/src/main/java/codechicken/lib/gui/modular/elements/GuiProgressIcon.java @@ -25,26 +25,46 @@ */ public class GuiProgressIcon extends GuiElement implements BackgroundRender { - private Material background = null; - private Material animated; + private Supplier background = null; + private Supplier animated; private Supplier progress = () -> 0D; + private boolean rotateToDirection = true; private Direction direction = Direction.RIGHT; - public GuiProgressIcon(@NotNull GuiParent parent, Material animated) { + public GuiProgressIcon(@NotNull GuiParent parent, Supplier animated) { super(parent); this.animated = animated; } - public GuiProgressIcon(@NotNull GuiParent parent, Material background, Material animated) { + public GuiProgressIcon(@NotNull GuiParent parent, Material animated) { + this(parent, () -> animated); + } + + public GuiProgressIcon(@NotNull GuiParent parent, Supplier background, Supplier animated) { super(parent); this.background = background; this.animated = animated; } + public GuiProgressIcon(@NotNull GuiParent parent, Material background, Material animated) { + this(parent, () -> background, () -> animated); + } + + public GuiProgressIcon(@NotNull GuiParent parent) { super(parent); } + /** + * The expected default direction for a progress texture is left-to-right e.g. furnace arrow. + * The texture will then be rotated to the direction specified via {@link #setDirection(Direction)} + * If you do not want the texture to be rotated then set this to false. + */ + public GuiProgressIcon setRotateToDirection(boolean rotateToDirection) { + this.rotateToDirection = rotateToDirection; + return this; + } + /** * Set the direction this progress icon is pointing, Default is RIGHT */ @@ -57,6 +77,14 @@ public GuiProgressIcon setDirection(Direction direction) { * Sets the background texture, aka the "empty" texture. */ public GuiProgressIcon setBackground(@Nullable Material background) { + this.background = () -> background; + return this; + } + + /** + * Sets the background texture, aka the "empty" texture. + */ + public GuiProgressIcon setBackground(@Nullable Supplier background) { this.background = background; return this; } @@ -65,6 +93,14 @@ public GuiProgressIcon setBackground(@Nullable Material background) { * Sets the texture that will be animated. */ public GuiProgressIcon setAnimated(Material animated) { + this.animated = () -> animated; + return this; + } + + /** + * Sets the texture that will be animated. + */ + public GuiProgressIcon setAnimated(Supplier animated) { this.animated = animated; return this; } @@ -92,24 +128,38 @@ public double getProgress() { @Override public void renderBackground(GuiRender render, double mouseX, double mouseY, float partialTicks) { - render.pose().pushPose(); - - double width = direction.getAxis() == Axis.X ? xSize() : ySize(); - double height = direction.getAxis() == Axis.X ? ySize() : xSize(); - - render.pose().translate(xMin() + (xSize() / 2), yMin() + (ySize() / 2), 0); - render.pose().mulPose(com.mojang.math.Axis.ZP.rotationDegrees((float) Direction.RIGHT.rotationTo(direction))); - - double halfWidth = width / 2; - double halfHeight = height / 2; - if (background != null) { - render.tex(background, -halfWidth, -halfHeight, halfWidth, halfHeight, 0xFFFFFFFF); + if (rotateToDirection) { + render.pose().pushPose(); + + double width = direction.getAxis() == Axis.X ? xSize() : ySize(); + double height = direction.getAxis() == Axis.X ? ySize() : xSize(); + + render.pose().translate(xMin() + (xSize() / 2), yMin() + (ySize() / 2), 0); + render.pose().mulPose(com.mojang.math.Axis.ZP.rotationDegrees((float) Direction.RIGHT.rotationTo(direction))); + + double halfWidth = width / 2; + double halfHeight = height / 2; + if (background != null && background.get() != null) { + render.tex(background.get(), -halfWidth, -halfHeight, halfWidth, halfHeight, 0xFFFFFFFF); + } + + if (animated == null || animated.get() == null) return; + float progress = (float) getProgress(); + render.partialSprite(animated.get().renderType(GuiRender::texColType), -halfWidth, -halfHeight, -halfWidth + (width * progress), -halfHeight + height, animated.get().sprite(), 0F, 0F, progress, 1F, 0xFFFFFFFF); + + render.pose().popPose(); + } else { + if (background != null && background.get() != null) { + render.texRect(background.get(), getRectangle()); + } + if (animated == null || animated.get() == null) return; + float progress = (float) getProgress(); + switch (direction) { + case UP -> render.partialSprite(animated.get().renderType(GuiRender::texColType), xMin(), yMax() - (ySize() * progress), xMax(), yMax(), animated.get().sprite(), 0F, 1F - progress, 1F, 1F, 0xFFFFFFFF); + case LEFT -> render.partialSprite(animated.get().renderType(GuiRender::texColType), xMax() - (xSize() * progress), yMin(), xMax(), yMax(), animated.get().sprite(), 1F - progress, 0F, 1F, 1F, 0xFFFFFFFF); + case DOWN -> render.partialSprite(animated.get().renderType(GuiRender::texColType), xMin(), yMin(), xMax(), yMin() + (ySize() * progress), animated.get().sprite(), 0F, 0F, 1F, progress, 0xFFFFFFFF); + case RIGHT -> render.partialSprite(animated.get().renderType(GuiRender::texColType), xMin(), yMin(), xMin() + (xSize() * progress), yMax(), animated.get().sprite(), 0F, 0F, progress, 1F, 0xFFFFFFFF); + } } - - if (animated == null) return; - float progress = (float) getProgress(); - render.partialSprite(animated.renderType(GuiRender::texColType), -halfWidth, -halfHeight, -halfWidth + (width* progress), -halfHeight + height, animated.sprite(), 0F, 0F, progress, 1F, 0xFFFFFFFF); - - render.pose().popPose(); } }