From 12cb6808e545d232c57b80420f0cb6d74337512a Mon Sep 17 00:00:00 2001 From: Pixirora Date: Wed, 6 Nov 2024 16:25:07 -0500 Subject: [PATCH] Fix relative alignment not updating when the window's size changes --- .../impl/ui/screen/ScreenTemplate.java | 9 ++-- .../impl/ui/screen/WidgetContainer.java | 42 ++++++++++--------- .../ui/screen/align/RelativeAlignment.java | 14 +++++-- .../impl/ui/screen/music/MusicScreen.java | 2 - 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/ScreenTemplate.java b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/ScreenTemplate.java index c042c0a..6c970df 100644 --- a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/ScreenTemplate.java +++ b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/ScreenTemplate.java @@ -12,7 +12,6 @@ import net.pixaurora.kitten_cube.impl.ui.screen.align.AlignmentStrategy; import net.pixaurora.kitten_cube.impl.ui.screen.align.PointManager; import net.pixaurora.kitten_cube.impl.ui.widget.Widget; -import net.pixaurora.kitten_heart.impl.KitTunes; public abstract class ScreenTemplate implements Screen { private boolean initializedWidgets = false; @@ -41,8 +40,6 @@ public final void draw(GuiDisplay gui, Point mousePos) { public final void init(Size window) { this.window = window; - KitTunes.LOGGER.info("Window: " + this.window); - if (!this.initializedWidgets) { this.initializedWidgets = true; this.addBackground(); @@ -77,6 +74,10 @@ public final void handleTick() { this.tick(); } + protected Size window() { + return this.window; + } + private void updateWindow(Size window) { this.defaultAligner = Optional.of(new PointManager(this.alignmentMethod(), window)); @@ -86,7 +87,7 @@ private void updateWindow(Size window) { } protected final WidgetContainer addWidget(W widget) { - WidgetContainer widgetContainer = new WidgetContainer<>(widget); + WidgetContainer widgetContainer = new WidgetContainer<>(widget, this); this.widgets.add(widgetContainer); widgetContainer.onWindowUpdate(window); diff --git a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/WidgetContainer.java b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/WidgetContainer.java index e1152e7..240be09 100644 --- a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/WidgetContainer.java +++ b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/WidgetContainer.java @@ -1,11 +1,10 @@ package net.pixaurora.kitten_cube.impl.ui.screen; import java.util.Optional; -import java.util.function.BiFunction; +import java.util.function.Function; import net.pixaurora.kitten_cube.impl.math.Point; import net.pixaurora.kitten_cube.impl.math.Size; -import net.pixaurora.kitten_cube.impl.ui.screen.align.Alignment; import net.pixaurora.kitten_cube.impl.ui.screen.align.AlignmentStrategy; import net.pixaurora.kitten_cube.impl.ui.screen.align.PointManager; import net.pixaurora.kitten_cube.impl.ui.screen.align.RelativeAlignment; @@ -13,13 +12,14 @@ public class WidgetContainer { private final T widget; - private Optional window; + private final ScreenTemplate screen; + private Optional customizedAlignment; private Optional aligner; - public WidgetContainer(T widget) { + public WidgetContainer(T widget, ScreenTemplate screen) { this.widget = widget; - this.window = Optional.empty(); + this.screen = screen; this.customizedAlignment = Optional.empty(); this.aligner = Optional.empty(); } @@ -40,35 +40,39 @@ public WidgetContainer customizedAlignment(AlignmentStrategy newAlignment) { } public AlignmentStrategy relativeAlignment(AlignedToCorner alignment) { - return new RelativeAlignment(this.customizedAlignment.orElse(Alignment.TOP_LEFT), - alignment.offset.apply(this.widget.pos(), this.widget.size())); + return new RelativeAlignment(this.customizedAlignment().orElse(this.screen.alignmentMethod()), alignment.offset, + this.widget); } public void onWindowUpdate(Size window) { - this.window = Optional.of(window); this.widget.onWindowUpdate(window); this.updateAlignment(); } - private void updateAlignment() { - Size window = this.window.orElseThrow(RuntimeException::new); + private Optional customizedAlignment() { + if (this.customizedAlignment.isPresent()) { + return this.customizedAlignment; + } else { + return this.widget.alignmentMethod(); + } + } - Optional alignment = this.customizedAlignment.isPresent() ? this.customizedAlignment - : this.widget.alignmentMethod(); - this.aligner = alignment.map(alignment0 -> new PointManager(alignment0, window)); + private void updateAlignment() { + Size window = this.screen.window(); + this.aligner = this.customizedAlignment().map(alignment -> new PointManager(alignment, window)); } public static enum AlignedToCorner { - TOP_LEFT((widgetPos, widgetSize) -> widgetPos), - TOP_RIGHT((widgetPos, widgetSize) -> widgetPos.offset(widgetSize.x(), 0)), - BOTTOM_LEFT((widgetPos, widgetSize) -> widgetPos.offset(0, widgetSize.y())), - BOTTOM_RIGHT((widgetPos, widgetSize) -> widgetPos.offset(widgetSize)), + TOP_LEFT(widget -> widget.pos()), + TOP_RIGHT(widget -> widget.pos().offset(widget.size().width(), 0)), + BOTTOM_LEFT(widget -> widget.pos().offset(0, widget.size().height())), + BOTTOM_RIGHT(widget -> widget.pos().offset(widget.size())), ; - private final BiFunction offset; + private final Function offset; - private AlignedToCorner(BiFunction offset) { + private AlignedToCorner(Function offset) { this.offset = offset; } diff --git a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/align/RelativeAlignment.java b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/align/RelativeAlignment.java index db3f14e..986b98a 100644 --- a/projects/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/align/RelativeAlignment.java +++ b/projects/kitten-heart/src/main/java/net/pixaurora/kitten_cube/impl/ui/screen/align/RelativeAlignment.java @@ -1,24 +1,30 @@ package net.pixaurora.kitten_cube.impl.ui.screen.align; +import java.util.function.Function; + import net.pixaurora.kitten_cube.impl.math.Point; import net.pixaurora.kitten_cube.impl.math.Size; +import net.pixaurora.kitten_cube.impl.ui.widget.Widget; public class RelativeAlignment implements AlignmentStrategy { private final AlignmentStrategy baseAlignment; - private final Point offset; - public RelativeAlignment(AlignmentStrategy baseAlignment, Point offset) { + private final Function offset; + private final Widget widget; + + public RelativeAlignment(AlignmentStrategy baseAlignment, Function offset, Widget widget) { this.baseAlignment = baseAlignment; this.offset = offset; + this.widget = widget; } @Override public Point align(Point original, Size window) { - return baseAlignment.align(original, window).offset(offset); + return this.baseAlignment.align(original, window).offset(this.offset.apply(this.widget)); } @Override public Point inverseAlign(Point aligned, Size window) { - return baseAlignment.inverseAlign(aligned, window).offset(offset.scaledBy(-1)); + return this.baseAlignment.inverseAlign(aligned, window).offset(this.offset.apply(this.widget).scaledBy(-1)); } } 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 bf29860..a83ed88 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 @@ -147,8 +147,6 @@ public DisplayMode createMusicDisplay(PlayingSong song) { Point.of(0, 0))) .customizedAlignment(progressBar.relativeAlignment(AlignedToCorner.BOTTOM_LEFT)); - KitTunes.LOGGER.info("" + pauseButton.customizedAligner().get().align(Point.ZERO)); - return new MusicDisplayMode(song, Arrays.asList(progressBar, timer, albumArt, songInfo, pauseButton)); }