Skip to content

Commit

Permalink
GuiProgressIcon texture improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon3055 committed Apr 14, 2024
1 parent d712280 commit a5887ec
Showing 1 changed file with 72 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,46 @@
*/
public class GuiProgressIcon extends GuiElement<GuiProgressIcon> implements BackgroundRender {

private Material background = null;
private Material animated;
private Supplier<Material> background = null;
private Supplier<Material> animated;
private Supplier<Double> progress = () -> 0D;
private boolean rotateToDirection = true;
private Direction direction = Direction.RIGHT;

public GuiProgressIcon(@NotNull GuiParent<?> parent, Material animated) {
public GuiProgressIcon(@NotNull GuiParent<?> parent, Supplier<Material> 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<Material> background, Supplier<Material> 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
*/
Expand All @@ -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<Material> background) {
this.background = background;
return this;
}
Expand All @@ -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<Material> animated) {
this.animated = animated;
return this;
}
Expand Down Expand Up @@ -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();
}
}

0 comments on commit a5887ec

Please sign in to comment.