Skip to content

Commit

Permalink
Add auto-updating and auto-adjusting to the history widget
Browse files Browse the repository at this point in the history
Signed-off-by: Rina Mary Shaw <[email protected]>
  • Loading branch information
Pixaurora committed Dec 14, 2024
1 parent 87aebc8 commit 48fe777
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<HistoryWidget> LISTENING_WIDGET = new AtomicReference<>();

public static List<ListenRecord> recentTracks(int limit) {
ArrayList<ListenRecord> recentTracks = new ArrayList<>();

Expand Down Expand Up @@ -50,4 +56,22 @@ private static Collection<ListenRecord> currentTracks() {
private static Collection<ListenRecord> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -29,28 +27,33 @@ 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> view;
private final AtomicReference<Size> 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
public void draw(GuiDisplay gui, Point mousePos) {
this.view.get().draw(gui, mousePos);
}

public static List<ListenRecord> getMaxAmount() {
List<ListenRecord> records = new ArrayList<>();

return records;
}

@Override
public void onClick(Point mousePos, MouseButton button) {
}
Expand All @@ -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<ListenRecord> recentMusic) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 48fe777

Please sign in to comment.