Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package de.rettichlp.pkutils.common.gui.overlay;

import lombok.Builder;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;

import java.awt.Color;
import java.time.Duration;
import java.time.temporal.Temporal;
import java.util.function.Supplier;

import static de.rettichlp.pkutils.PKUtils.renderService;
import static de.rettichlp.pkutils.common.services.RenderService.TEXT_BOX_MARGIN;
import static de.rettichlp.pkutils.common.services.RenderService.TEXT_BOX_PADDING;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.time.Duration.between;
import static java.time.LocalDateTime.now;

@Builder
public class ProgressTextOverlay extends OverlayEntry {

private final Supplier<Text> textSupplier;
private double progress;

@Builder.Default
private Color backgroundColor = new Color(127, 127, 127, 100);

@Builder.Default
private Color borderColor = new Color(255, 255, 255, 255);

@Override
public int getWidth() {
Text text = this.textSupplier.get();
return renderService.getTextBoxSizeX(text);
}

@Override
public int getHeight() {
return renderService.getTextBoxSizeY();
}

@Override
public void draw(@NotNull DrawContext drawContext, int x, int y, Alignment alignment) {
int innerX = x + TEXT_BOX_MARGIN;
int innerY = y + TEXT_BOX_MARGIN;

drawContext.fill(innerX, innerY, innerX + getContentWidth(), innerY + getContentHeight(), this.backgroundColor.getRGB());
drawContext.drawBorder(innerX, innerY, getContentWidth(), getContentHeight(), this.borderColor.getRGB());
drawContext.drawHorizontalLine(innerX + 2, (int) (innerX + ((getContentWidth() - 4) * this.progress)), innerY + getContentHeight() - 3, this.borderColor.getRGB());
drawContext.drawTextWithShadow(this.textRenderer, this.textSupplier.get(), innerX + TEXT_BOX_PADDING, innerY + TEXT_BOX_PADDING, 0xFFFFFF);

// debug: draw outline
if (renderService.isDebugEnabled()) {
drawContext.drawBorder(x, y, getWidth(), getHeight(), new Color(0, 0, 255).getRGB());
}
}

public static double calculateProgress(Temporal startTime, @NotNull Duration duration) {
long elapsedMillis = between(startTime, now()).toMillis();
double progress = (double) elapsedMillis / duration.toMillis();
return min(1.0, max(0.0, progress));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package de.rettichlp.pkutils.common.models;

import de.rettichlp.pkutils.common.gui.overlay.TextOverlay;
import de.rettichlp.pkutils.common.gui.overlay.ProgressTextOverlay;
import de.rettichlp.pkutils.common.registry.PKUtilsBase;
import lombok.RequiredArgsConstructor;
import net.minecraft.text.Text;

import java.time.Duration;
import java.time.LocalDateTime;

import static de.rettichlp.pkutils.common.gui.overlay.ProgressTextOverlay.calculateProgress;
import static java.time.Duration.between;
import static java.time.LocalDateTime.now;
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Expand Down Expand Up @@ -39,16 +40,17 @@ public Duration getRemainingDuration() {
return between(now(), this.startTime.plus(this.duration));
}

public TextOverlay toTextWidget() {
public ProgressTextOverlay toTextWidget() {
String millisToFriendlyString = millisToFriendlyString(getRemainingDuration().toMillis());

Text text = empty()
.append(of(this.title).copy().formatted(GRAY))
.append(of(":").copy().formatted(DARK_GRAY)).append(" ")
.append(of(millisToFriendlyString));

return TextOverlay.builder()
return ProgressTextOverlay.builder()
.textSupplier(() -> text)
.progress(calculateProgress(this.startTime, this.duration))
.build();
}
}