Skip to content

Commit

Permalink
Fix relative alignment not updating when the window's size changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixaurora committed Nov 6, 2024
1 parent 4649a01 commit 12cb680
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));

Expand All @@ -86,7 +87,7 @@ private void updateWindow(Size window) {
}

protected final <W extends Widget> WidgetContainer<W> addWidget(W widget) {
WidgetContainer<W> widgetContainer = new WidgetContainer<>(widget);
WidgetContainer<W> widgetContainer = new WidgetContainer<>(widget, this);
this.widgets.add(widgetContainer);
widgetContainer.onWindowUpdate(window);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
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;
import net.pixaurora.kitten_cube.impl.ui.widget.Widget;

public class WidgetContainer<T extends Widget> {
private final T widget;
private Optional<Size> window;
private final ScreenTemplate screen;

private Optional<AlignmentStrategy> customizedAlignment;
private Optional<PointManager> 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();
}
Expand All @@ -40,35 +40,39 @@ public WidgetContainer<T> 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<AlignmentStrategy> customizedAlignment() {
if (this.customizedAlignment.isPresent()) {
return this.customizedAlignment;
} else {
return this.widget.alignmentMethod();
}
}

Optional<AlignmentStrategy> 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<Point, Size, Point> offset;
private final Function<Widget, Point> offset;

private AlignedToCorner(BiFunction<Point, Size, Point> offset) {
private AlignedToCorner(Function<Widget, Point> offset) {
this.offset = offset;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Widget, Point> offset;
private final Widget widget;

public RelativeAlignment(AlignmentStrategy baseAlignment, Function<Widget, Point> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down

0 comments on commit 12cb680

Please sign in to comment.