diff --git a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/listener/HistoryWidgetUpdater.java b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/listener/HistoryWidgetUpdater.java index cef5c49..7ed8920 100644 --- a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/listener/HistoryWidgetUpdater.java +++ b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/listener/HistoryWidgetUpdater.java @@ -4,15 +4,21 @@ import java.util.Arrays; import java.util.Collection; import java.util.List; +import java.util.concurrent.atomic.AtomicReference; +import net.pixaurora.kit_tunes.api.event.TrackEndEvent; +import net.pixaurora.kit_tunes.api.event.TrackStartEvent; import net.pixaurora.kit_tunes.api.listener.MusicEventListener; import net.pixaurora.kit_tunes.api.music.history.ListenRecord; import net.pixaurora.kitten_heart.impl.EventHandling; import net.pixaurora.kitten_heart.impl.KitTunes; import net.pixaurora.kitten_heart.impl.music.history.ListenHistory; import net.pixaurora.kitten_heart.impl.music.progress.PlayingSong; +import net.pixaurora.kitten_heart.impl.ui.widget.history.HistoryWidget; public class HistoryWidgetUpdater implements MusicEventListener { + public static final AtomicReference LISTENING_WIDGET = new AtomicReference<>(); + public static List recentTracks(int limit) { ArrayList recentTracks = new ArrayList<>(); @@ -50,4 +56,22 @@ private static Collection currentTracks() { private static Collection pastTracks() { return KitTunes.LISTEN_HISTORY.get(ListenHistory::getHistory); } + + @Override + public void onTrackStart(TrackStartEvent event) { + HistoryWidget listeningWidget = LISTENING_WIDGET.get(); + + if (listeningWidget != null) { + listeningWidget.update(); + } + } + + @Override + public void onTrackEnd(TrackEndEvent event) { + HistoryWidget listeningWidget = LISTENING_WIDGET.get(); + + if (listeningWidget != null) { + listeningWidget.update(); + } + } } diff --git a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/ui/screen/music/MusicScreen.java b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/ui/screen/music/MusicScreen.java index c6f458f..5276b97 100644 --- a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/ui/screen/music/MusicScreen.java +++ b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/ui/screen/music/MusicScreen.java @@ -59,7 +59,7 @@ protected Alignment alignmentMethod() { protected void firstInit() { this.setupMode(); - this.addWidget(new HistoryWidget()) + this.addWidget(new HistoryWidget(32)) .anchor(WidgetAnchor.MIDDLE_LEFT) .at(Point.of(10, 0)); } diff --git a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/ui/widget/history/HistoryWidget.java b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/ui/widget/history/HistoryWidget.java index 5ec5107..ccdbda7 100644 --- a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/ui/widget/history/HistoryWidget.java +++ b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/ui/widget/history/HistoryWidget.java @@ -18,8 +18,6 @@ import net.pixaurora.kitten_heart.impl.ui.widget.history.HistoryTileSet.Row; public class HistoryWidget implements Widget { - public static final int MAX_ITEMS = 10; - public static final GuiTexture BACKGROUND = GuiTexture .of(KitTunes.resource("textures/gui/sprites/widget/music/history/background.png"), Size.of(200, 64)); @@ -29,15 +27,26 @@ public class HistoryWidget implements Widget { Point.of(0, 21), Size.of(200, 21), Point.of(0, 42), Size.of(200, 22)); + private final int verticalPadding; private final HistoryTileSet tileSet = TILE_SET; private final AtomicReference view; + private final AtomicReference window; + + public HistoryWidget(int verticalPadding) { + this.verticalPadding = verticalPadding; + this.view = new AtomicReference<>(); + this.window = new AtomicReference<>(); + } - public HistoryWidget() { - this.view = new AtomicReference<>(this.initView()); + @Override + public void onWindowUpdate(Size window) { + this.window.set(window); + this.update(); + HistoryWidgetUpdater.LISTENING_WIDGET.set(this); } public void update() { - this.view.set(this.initView()); + this.view.set(this.initView(HistoryWidgetUpdater.recentTracks(this.maxItems()))); } @Override @@ -45,12 +54,6 @@ public void draw(GuiDisplay gui, Point mousePos) { this.view.get().draw(gui, mousePos); } - public static List getMaxAmount() { - List records = new ArrayList<>(); - - return records; - } - @Override public void onClick(Point mousePos, MouseButton button) { } @@ -65,8 +68,12 @@ public Size size() { return this.view.get().size; } - private View initView() { - return this.initView(HistoryWidgetUpdater.recentTracks(MAX_ITEMS)); + private int maxItems() { + int middleTilesHeight = this.window.get().height() - this.verticalPadding * 2 + - (this.tileSet.top().tile().height() + this.tileSet.bottom().tile().height()); + int middleEntries = middleTilesHeight / this.tileSet.middle().tile().height(); + + return middleEntries + 2; // Because the top and bottom were cancelled out before dividing } private View initView(List recentMusic) { diff --git a/projects/kitten-heart/src/main/resources/META-INF/services/net.pixaurora.kit_tunes.api.listener.MusicEventListener b/projects/kitten-heart/src/main/resources/META-INF/services/net.pixaurora.kit_tunes.api.listener.MusicEventListener index 2c484cc..43297ca 100644 --- a/projects/kitten-heart/src/main/resources/META-INF/services/net.pixaurora.kit_tunes.api.listener.MusicEventListener +++ b/projects/kitten-heart/src/main/resources/META-INF/services/net.pixaurora.kit_tunes.api.listener.MusicEventListener @@ -1,2 +1,3 @@ +net.pixaurora.kitten_heart.impl.listener.HistoryWidgetUpdater net.pixaurora.kitten_heart.impl.listener.NotificationSendingMusicListener net.pixaurora.kitten_heart.impl.listener.ScrobblingMusicListener