generated from QuiltMC/quilt-template-mod
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Timer widget to the playing music screen
- Loading branch information
Showing
5 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/ui/widget/Timer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package net.pixaurora.kitten_heart.impl.ui.widget; | ||
|
||
import net.pixaurora.kitten_cube.impl.math.Point; | ||
import net.pixaurora.kitten_cube.impl.text.Color; | ||
import net.pixaurora.kitten_cube.impl.text.Component; | ||
import net.pixaurora.kitten_cube.impl.ui.controls.MouseButton; | ||
import net.pixaurora.kitten_cube.impl.ui.display.GuiDisplay; | ||
import net.pixaurora.kitten_cube.impl.ui.widget.Widget; | ||
import net.pixaurora.kitten_cube.impl.ui.widget.text.PushableTextLines; | ||
import net.pixaurora.kitten_heart.impl.ui.widget.progress.ProgressProvider; | ||
|
||
public class Timer implements Widget { | ||
private final PushableTextLines text; | ||
private final ProgressProvider progress; | ||
|
||
private long playedSeconds; | ||
private final long totalSeconds; | ||
|
||
public Timer(Point pos, ProgressProvider progress) { | ||
this.text = new PushableTextLines(pos); | ||
this.progress = progress; | ||
this.playedSeconds = -1; | ||
this.totalSeconds = this.progress.totalDuration().getSeconds(); | ||
} | ||
|
||
@Override | ||
public void draw(GuiDisplay gui, Point mousePos) { | ||
this.text.draw(gui, mousePos); | ||
} | ||
|
||
@Override | ||
public void tick() { | ||
long newPlayedSeconds = this.progress.playedDuration().getSeconds(); | ||
|
||
if (this.playedSeconds != newPlayedSeconds) { | ||
this.playedSeconds = newPlayedSeconds; | ||
|
||
this.text.clear(); | ||
this.text.push(Component.literal(this.display()), Color.WHITE); | ||
} | ||
} | ||
|
||
private String display() { | ||
StringBuilder display = new StringBuilder(); | ||
|
||
addTimer(display, this.playedSeconds); | ||
|
||
display.append(new char[] { ' ', '/', ' ' }); | ||
|
||
addTimer(display, this.totalSeconds); | ||
|
||
return display.toString(); | ||
} | ||
|
||
private void addTimer(StringBuilder builder, long totalSeconds) { | ||
long minutes = totalSeconds / 60; | ||
long seconds = totalSeconds % 60; | ||
|
||
builder.append(Long.toString(minutes)); | ||
|
||
builder.append(':'); | ||
|
||
if (seconds < 10) { | ||
builder.append('0'); | ||
} | ||
builder.append(Long.toString(seconds)); | ||
} | ||
|
||
@Override | ||
public void onClick(Point mousePos, MouseButton button) { | ||
} | ||
|
||
@Override | ||
public boolean isWithinBounds(Point mousePos) { | ||
return false; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...rt/src/main/java/net/pixaurora/kitten_heart/impl/ui/widget/progress/ProgressProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
package net.pixaurora.kitten_heart.impl.ui.widget.progress; | ||
|
||
import java.time.Duration; | ||
|
||
public interface ProgressProvider { | ||
double percentComplete(); | ||
|
||
Duration playedDuration(); | ||
|
||
Duration totalDuration(); | ||
} |