fix: synced timer
All checks were successful
Linux arm64 / Build (push) Successful in 26s

This commit is contained in:
2025-02-01 11:22:59 +01:00
parent 3863c812c8
commit caf6569409
7 changed files with 50 additions and 24 deletions

View File

@@ -1,24 +1,26 @@
package gui.widget;
import java.time.Instant;
import imgui.ImGui;
public class TimerRenderer {
private float time;
private final float duration;
private final long endTime;
public TimerRenderer(float duration) {
this.time = 0;
this.duration = duration;
public TimerRenderer(Instant startTime, int duration) {
this.endTime = startTime.getEpochSecond() + duration;
}
private long getTimeRemaining() {
long currentTime = Instant.now().getEpochSecond();
return endTime - currentTime;
}
public void render() {
this.time += ImGui.getIO().getDeltaTime();
float remainingTime = this.duration - this.time;
int seconds = (int) remainingTime;
int minutes = seconds / 60;
seconds %= 60;
String text = String.format("%02d:%02d", minutes, seconds);
long seconds = getTimeRemaining();
long minutes = seconds / 60;
String text = String.format("%02d:%02d", minutes, seconds % 60);
var textSize = ImGui.calcTextSize(text);
ImGui.setCursorPosX(ImGui.getIO().getDisplaySizeX() / 2.0f - textSize.x / 2.0f);
ImGui.text(text);