diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/service/KitTunesMinecraftUICompat.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/service/KitTunesMinecraftUICompat.java index 228e743..2ecd209 100644 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/service/KitTunesMinecraftUICompat.java +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/service/KitTunesMinecraftUICompat.java @@ -7,8 +7,8 @@ import net.pixaurora.kit_tunes.impl.ui.sound.SoundPlayer; import net.pixaurora.kit_tunes.impl.ui.text.Component; import net.pixaurora.kit_tunes.impl.ui.text.TextProcessor; -import net.pixaurora.kit_tunes.impl.ui.toast.KitTunesToastData; import net.pixaurora.kit_tunes.impl.ui.toast.MeowPlayingToast; +import net.pixaurora.kit_tunes.impl.ui.toast.Toast; /** * The version-specific implementation for pieces of the UI that are organized @@ -16,10 +16,10 @@ */ public interface KitTunesMinecraftUICompat extends SoundPlayer, TextProcessor { public static void sendNowPlayingNotification(Track track) { - KitTunes.UI_LAYER.sendToast(new MeowPlayingToast(track)); + KitTunes.UI_LAYER.sendToast(Toast.fromData(new MeowPlayingToast(track))); } - public void sendToast(KitTunesToastData toast); + public void sendToast(Toast toast); public Component translatable(String key); diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/AlignedGuiDisplay.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/AlignedGuiDisplay.java index 25020cd..0f34b54 100644 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/AlignedGuiDisplay.java +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/AlignedGuiDisplay.java @@ -26,12 +26,12 @@ public void drawTexture(ResourcePath path, Size size, Point pos) { } @Override - public void drawGuiTexture(ResourcePath path, Size size, Point pos) { - this.parent.drawGuiTexture(path, size, this.align(pos)); + public void drawGuiTextureSubsection(ResourcePath path, Size size, Point pos, Size subsection, Point offset) { + this.parent.drawGuiTextureSubsection(path, size, this.align(pos), subsection, offset); } @Override - public void drawText(Component text, Color color, Point pos) { - this.parent.drawText(text, color, this.align(pos)); + public void drawText(Component text, Color color, Point pos, boolean shadowed) { + this.parent.drawText(text, color, this.align(pos), shadowed); } } diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/GuiDisplay.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/GuiDisplay.java index 60f76db..2a51ee2 100644 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/GuiDisplay.java +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/GuiDisplay.java @@ -11,15 +11,23 @@ public interface GuiDisplay { public void drawTexture(ResourcePath path, Size size, Point pos); - public void drawGuiTexture(ResourcePath path, Size size, Point pos); + public void drawGuiTextureSubsection(ResourcePath path, Size size, Point pos, Size subsection, Point offset); - public void drawText(Component text, Color color, Point pos); + public void drawText(Component text, Color color, Point pos, boolean shadowed); - public default void drawTexture(Texture texture, Point pos) { + public default void drawText(Component text, Color color, Point pos) { + this.drawText(text, color, pos, true); + } + + public default void draw(Texture texture, Point pos) { this.drawTexture(texture.path(), texture.size(), pos); } - public default void drawGuiTexture(GuiTexture texture, Point pos) { - this.drawGuiTexture(texture.path(), texture.size(), pos); + public default void drawGui(GuiTexture texture, Point pos) { + this.drawGuiTextureSubsection(texture.path(), texture.size(), pos, texture.size(), Point.ZERO); + } + + public default void drawGui(GuiTexture texture, Point pos, Size subsection, Point offset) { + this.drawGuiTextureSubsection(texture.path(), texture.size(), pos, subsection, offset); } } diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/MinecraftClient.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/MinecraftClient.java index 098debd..41a9f54 100644 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/MinecraftClient.java +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/MinecraftClient.java @@ -1,5 +1,7 @@ package net.pixaurora.kit_tunes.impl.ui; +import java.util.List; + import net.pixaurora.kit_tunes.impl.KitTunes; import net.pixaurora.kit_tunes.impl.service.KitTunesMinecraftUICompat; import net.pixaurora.kit_tunes.impl.ui.math.Size; @@ -28,6 +30,10 @@ public static Size textSize(Component... text) { return impl().textSize(text); } + public static List splitText(Component text, int lineWidth) { + return impl().splitText(text, lineWidth); + } + public static void playSound(Sound sound) { impl().playSound(sound); } diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/text/TextProcessor.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/text/TextProcessor.java index b22d5fe..9b2aaa9 100644 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/text/TextProcessor.java +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/text/TextProcessor.java @@ -1,5 +1,7 @@ package net.pixaurora.kit_tunes.impl.ui.text; +import java.util.List; + import net.pixaurora.kit_tunes.impl.ui.math.Size; public interface TextProcessor { @@ -7,6 +9,8 @@ public interface TextProcessor { public int textWidth(Component text); + public List splitText(Component text, int lineWidth); + public default Size textSize(Component text) { return Size.of(this.textWidth(text), this.textHeight()); } diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/tile/InnerTile.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/tile/InnerTile.java new file mode 100644 index 0000000..f9a885c --- /dev/null +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/tile/InnerTile.java @@ -0,0 +1,33 @@ +package net.pixaurora.kit_tunes.impl.ui.tile; + +import net.pixaurora.kit_tunes.impl.ui.math.Point; +import net.pixaurora.kit_tunes.impl.ui.math.Size; +import net.pixaurora.kit_tunes.impl.ui.texture.GuiTexture; + +public class InnerTile { + private final GuiTexture texture; + private final Point textureOffset; + private final Size size; + + public InnerTile(GuiTexture texture, Point textureOffset, Size size) { + this.texture = texture; + this.textureOffset = textureOffset; + this.size = size; + } + + public GuiTexture texture() { + return this.texture; + } + + public Point textureOffset() { + return this.textureOffset; + } + + public Size size() { + return this.size; + } + + public PositionedInnerTile atPos(Point pos) { + return new PositionedInnerTile(pos, this); + } +} diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/tile/PositionedInnerTile.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/tile/PositionedInnerTile.java new file mode 100644 index 0000000..e936e9f --- /dev/null +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/tile/PositionedInnerTile.java @@ -0,0 +1,23 @@ +package net.pixaurora.kit_tunes.impl.ui.tile; + +import net.pixaurora.kit_tunes.impl.ui.GuiDisplay; +import net.pixaurora.kit_tunes.impl.ui.math.Point; +import net.pixaurora.kit_tunes.impl.ui.math.Size; + +public class PositionedInnerTile { + private final Point pos; + private final InnerTile tile; + + public PositionedInnerTile(Point pos, InnerTile tile) { + this.pos = pos; + this.tile = tile; + } + + public Size size() { + return this.tile.size(); + } + + public void draw(GuiDisplay gui) { + gui.drawGui(tile.texture(), this.pos, tile.size(), tile.textureOffset()); + } +} diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/DrawableToast.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/DrawableToast.java new file mode 100644 index 0000000..ca8c122 --- /dev/null +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/DrawableToast.java @@ -0,0 +1,76 @@ +package net.pixaurora.kit_tunes.impl.ui.toast; + +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; + +import net.pixaurora.kit_tunes.impl.ui.GuiDisplay; +import net.pixaurora.kit_tunes.impl.ui.MinecraftClient; +import net.pixaurora.kit_tunes.impl.ui.math.Point; +import net.pixaurora.kit_tunes.impl.ui.math.Size; +import net.pixaurora.kit_tunes.impl.ui.text.Component; +import net.pixaurora.kit_tunes.impl.ui.texture.Texture; +import net.pixaurora.kit_tunes.impl.ui.tile.PositionedInnerTile; +import net.pixaurora.kit_tunes.impl.ui.widget.text.PositionedText; +import net.pixaurora.kit_tunes.impl.util.Pair; + +public class DrawableToast implements Toast { + private final Texture icon; + private final Point iconPos; + + private final List tiles; + + private final List text; + + private final Size size; + + public DrawableToast(KitTunesToastData data) { + ToastBackground background = data.background(); + + this.icon = data.icon(); + this.iconPos = background.iconPos(); + + this.text = new ArrayList<>(); + this.text.add(new PositionedText(data.title(), data.titleColor(), background.titlePos())); + + Point textPos = background.bodyTextStartPos(); + for (Component line : data.messageLines()) { + for (Component splitLine : MinecraftClient.splitText(line, background.maxLineLength())) { + this.text.add(new PositionedText(splitLine, data.messageColor(), textPos)); + textPos = textPos.offset(0, MinecraftClient.textHeight()); + } + } + + Pair, Size> tilesAndSize = background.tilesAndSize(this.text); + + this.tiles = tilesAndSize.first(); + this.size = tilesAndSize.second(); + } + + @Override + public Duration timeShown() { + return Duration.ofSeconds(5); + } + + @Override + public void draw(GuiDisplay gui) { + this.drawBackground(gui); + + gui.draw(icon, iconPos); + + for (PositionedText line : this.text) { + gui.drawText(line.text(), line.color(), line.pos(), false); + } + } + + private void drawBackground(GuiDisplay gui) { + for (PositionedInnerTile tile : this.tiles) { + tile.draw(gui); + } + } + + @Override + public Size size() { + return this.size; + } +} diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/KitTunesToastData.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/KitTunesToastData.java index 4ac54ee..b568c6d 100644 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/KitTunesToastData.java +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/KitTunesToastData.java @@ -2,18 +2,23 @@ import java.util.List; -import net.pixaurora.kit_tunes.api.resource.ResourcePath; import net.pixaurora.kit_tunes.impl.ui.math.Size; +import net.pixaurora.kit_tunes.impl.ui.text.Color; import net.pixaurora.kit_tunes.impl.ui.text.Component; +import net.pixaurora.kit_tunes.impl.ui.texture.Texture; public interface KitTunesToastData { - public ResourcePath icon(); + public Texture icon(); public Size iconSize(); public Component title(); + public Color titleColor(); + public List messageLines(); + public Color messageColor(); + public ToastBackground background(); } diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/MeowPlayingToast.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/MeowPlayingToast.java index 83c0038..458bc0d 100644 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/MeowPlayingToast.java +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/MeowPlayingToast.java @@ -9,7 +9,10 @@ import net.pixaurora.kit_tunes.impl.KitTunes; import net.pixaurora.kit_tunes.impl.ui.math.Point; import net.pixaurora.kit_tunes.impl.ui.math.Size; +import net.pixaurora.kit_tunes.impl.ui.text.Color; import net.pixaurora.kit_tunes.impl.ui.text.Component; +import net.pixaurora.kit_tunes.impl.ui.texture.GuiTexture; +import net.pixaurora.kit_tunes.impl.ui.texture.Texture; public class MeowPlayingToast implements KitTunesToastData { public static final Component TITLE = Component.translatable("kit_tunes.toast.title"); @@ -19,8 +22,8 @@ public class MeowPlayingToast implements KitTunesToastData { public static final ResourcePath TEXTURE = KitTunes.resource("textures/gui/sprites/toast/loaf.png"); public static ToastBackground BACKGROUND = new ToastBackground( - new ToastBackgroundAppearance(TEXTURE, Size.of(43, 24), Point.of(32, 18), Size.of(2, 2)), Point.of(8, 1), - Point.of(34, 5), Point.of(34, 19), 180, 8, 4); + new ToastBackgroundAppearance(GuiTexture.of(TEXTURE, Size.of(43, 24)), Point.of(32, 18), Size.of(2, 2)), + Point.of(8, 1), Point.of(34, 5), Point.of(34, 19), 180, 8, 4); private final Track track; @@ -29,8 +32,9 @@ public MeowPlayingToast(Track track) { } @Override - public ResourcePath icon() { - return this.track.album().flatMap(Album::albumArtPath).orElse(DEFAULT_ICON); + public Texture icon() { + ResourcePath texturePath = this.track.album().flatMap(Album::albumArtPath).orElse(DEFAULT_ICON); + return Texture.of(texturePath, Size.of(16, 16)); } @Override @@ -43,6 +47,11 @@ public Component title() { return TITLE; } + @Override + public Color titleColor() { + return Color.YELLOW; + } + @Override public List messageLines() { List lines = new ArrayList<>(); @@ -61,6 +70,11 @@ public List messageLines() { return lines; } + @Override + public Color messageColor() { + return Color.WHITE; + } + @Override public ToastBackground background() { return BACKGROUND; diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/Toast.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/Toast.java new file mode 100644 index 0000000..bbf9199 --- /dev/null +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/Toast.java @@ -0,0 +1,18 @@ +package net.pixaurora.kit_tunes.impl.ui.toast; + +import java.time.Duration; + +import net.pixaurora.kit_tunes.impl.ui.GuiDisplay; +import net.pixaurora.kit_tunes.impl.ui.math.Size; + +public interface Toast { + public Duration timeShown(); + + public void draw(GuiDisplay gui); + + public Size size(); + + public static Toast fromData(KitTunesToastData data) { + return new DrawableToast(data); + } +} diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/ToastBackground.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/ToastBackground.java index 29c688e..d4e829a 100644 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/ToastBackground.java +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/ToastBackground.java @@ -3,8 +3,12 @@ import java.util.ArrayList; import java.util.List; +import net.pixaurora.kit_tunes.impl.ui.MinecraftClient; import net.pixaurora.kit_tunes.impl.ui.math.Point; import net.pixaurora.kit_tunes.impl.ui.math.Size; +import net.pixaurora.kit_tunes.impl.ui.tile.InnerTile; +import net.pixaurora.kit_tunes.impl.ui.tile.PositionedInnerTile; +import net.pixaurora.kit_tunes.impl.ui.widget.text.PositionedText; import net.pixaurora.kit_tunes.impl.util.Pair; public class ToastBackground { @@ -31,41 +35,52 @@ public ToastBackground(ToastBackgroundAppearance appearance, Point iconPos, Poin this.bottomPadding = bottomPadding; } - public Pair, Size> tilesAndSize(Size textSize) { - Size targetedSize = textSize.offset(linesStartPos).offset(Size.of(this.rightPadding, this.bottomPadding)); + public Pair, Size> tilesAndSize(List lines) { + Size minimumSize = Size.of(0, 0); + for (PositionedText line : lines) { + Size totalLineSize = MinecraftClient.textSize(line.text()).offset(line.pos()); + + minimumSize = Size.of(Math.max(minimumSize.width(), totalLineSize.width()), + Math.max(minimumSize.height(), totalLineSize.height())); + } + + return this.tilesAndSize(minimumSize.offset(this.rightPadding, this.bottomPadding)); + } + + public Pair, Size> tilesAndSize(Size minimumSize) { Size corners = this.appearance.topLeftSize().offset(this.appearance.bottomRightSize()); Size centerSegmentCounts = Size.of( (int) Math.ceil( - Math.max(1, (float) (targetedSize.width() - corners.width()) / this.appearance.middleWidth())), + Math.max(1, (float) (minimumSize.width() - corners.width()) / this.appearance.middleWidth())), (int) Math.ceil(Math.max(1, - (float) (targetedSize.height() - corners.height()) / this.appearance.middleHeight()))); + (float) (minimumSize.height() - corners.height()) / this.appearance.middleHeight()))); - List> columns = this.appearance.initColumns(); + List> columns = this.appearance.initColumns(); - List arrangedTiles = new ArrayList<>(); + List arrangedTiles = new ArrayList<>(); Point pos = Point.ZERO; for (int i = 0; i < columns.size(); i++) { - List column = columns.get(i); + List column = columns.get(i); int repetitionCount = i == 1 ? centerSegmentCounts.x() : 1; // If in the middle, draw the middle part // multiple times. for (int repetition = 0; repetition < repetitionCount; repetition++) { pos = pos.withY(0); - ToastBackgroundTile topTile = column.get(0); + InnerTile topTile = column.get(0); arrangedTiles.add(topTile.atPos(pos)); pos = pos.offset(0, topTile.size().height()); - ToastBackgroundTile middleTile = column.get(1); + InnerTile middleTile = column.get(1); for (int middlePart = 0; middlePart <= centerSegmentCounts.y(); middlePart++) { arrangedTiles.add(middleTile.atPos(pos)); pos = pos.offset(0, middleTile.size().height()); } - ToastBackgroundTile bottomTile = column.get(2); + InnerTile bottomTile = column.get(2); arrangedTiles.add(bottomTile.atPos(pos)); pos = pos.offset(bottomTile.size()); diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/ToastBackgroundAppearance.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/ToastBackgroundAppearance.java index 5057d4e..fed3013 100644 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/ToastBackgroundAppearance.java +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/ToastBackgroundAppearance.java @@ -3,51 +3,46 @@ import java.util.Arrays; import java.util.List; -import net.pixaurora.kit_tunes.api.resource.ResourcePath; import net.pixaurora.kit_tunes.impl.ui.math.Point; import net.pixaurora.kit_tunes.impl.ui.math.Size; +import net.pixaurora.kit_tunes.impl.ui.texture.GuiTexture; +import net.pixaurora.kit_tunes.impl.ui.tile.InnerTile; public class ToastBackgroundAppearance { - private final ResourcePath texture; - private final Size size; + private final GuiTexture texture; private final Point centerOffset; private final Size centerSize; - public ToastBackgroundAppearance(ResourcePath texture, Size size, Point centerTilePos, Size centerTileSize) { + public ToastBackgroundAppearance(GuiTexture texture, Point centerTilePos, Size centerTileSize) { this.texture = texture; - this.size = size; this.centerOffset = centerTilePos; this.centerSize = centerTileSize; } - public ResourcePath texture() { + public GuiTexture texture() { return this.texture; } - public Size size() { - return this.size; - } - public Point centerTilePos() { return this.centerOffset; } - public List> initColumns() { + public List> initColumns() { Point first = Point.ZERO; Point middle = this.centerOffset; Point last = this.centerOffset.offset(this.centerSize); return Arrays.asList( - Arrays.asList(new ToastBackgroundTile(first, this.topLeftSize()), - new ToastBackgroundTile(first.withYOf(middle), this.middleLeftSize()), - new ToastBackgroundTile(first.withYOf(last), this.bottomLeftSize())), - Arrays.asList(new ToastBackgroundTile(middle.withYOf(first), this.topMiddleSize()), - new ToastBackgroundTile(middle, this.centerSize()), - new ToastBackgroundTile(middle.withYOf(last), this.bottomMiddleSize())), - Arrays.asList(new ToastBackgroundTile(last.withYOf(first), this.topRightSize()), - new ToastBackgroundTile(last.withYOf(middle), this.middleRightSize()), - new ToastBackgroundTile(last, this.bottomRightSize()))); + Arrays.asList(new InnerTile(this.texture, first, this.topLeftSize()), + new InnerTile(this.texture, first.withYOf(middle), this.middleLeftSize()), + new InnerTile(this.texture, first.withYOf(last), this.bottomLeftSize())), + Arrays.asList(new InnerTile(this.texture, middle.withYOf(first), this.topMiddleSize()), + new InnerTile(this.texture, middle, this.centerSize()), + new InnerTile(this.texture, middle.withYOf(last), this.bottomMiddleSize())), + Arrays.asList(new InnerTile(this.texture, last.withYOf(first), this.topRightSize()), + new InnerTile(this.texture, last.withYOf(middle), this.middleRightSize()), + new InnerTile(this.texture, last, this.bottomRightSize()))); } public int leftWidth() { @@ -59,7 +54,7 @@ public int middleWidth() { } public int rightWidth() { - return size.x() - leftWidth() - middleWidth(); + return this.texture.size().x() - leftWidth() - middleWidth(); } public int topHeight() { @@ -71,7 +66,7 @@ public int middleHeight() { } public int bottomHeight() { - return size.y() - topHeight() - middleHeight(); + return this.texture.size().y() - topHeight() - middleHeight(); } public Size topLeftSize() { @@ -91,7 +86,7 @@ public Size topMiddleSize() { } public Size centerSize() { - return centerSize; + return this.centerSize; } public Size bottomMiddleSize() { diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/ToastBackgroundTile.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/ToastBackgroundTile.java deleted file mode 100644 index 43dd082..0000000 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/toast/ToastBackgroundTile.java +++ /dev/null @@ -1,37 +0,0 @@ -package net.pixaurora.kit_tunes.impl.ui.toast; - -import net.pixaurora.kit_tunes.impl.ui.math.Point; -import net.pixaurora.kit_tunes.impl.ui.math.Size; - -public class ToastBackgroundTile { - private final Point pos; - - private final Point textureOffset; - private final Size size; - - public ToastBackgroundTile(Point pos, Point textureOffset, Size size) { - this.pos = pos; - this.textureOffset = textureOffset; - this.size = size; - } - - public ToastBackgroundTile(Point textureOffset, Size size) { - this(Point.ZERO, textureOffset, size); - } - - public ToastBackgroundTile atPos(Point pos) { - return new ToastBackgroundTile(pos, this.textureOffset, this.size); - } - - public Point pos() { - return this.pos; - } - - public Point textureOffset() { - return this.textureOffset; - } - - public Size size() { - return this.size; - } -} diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/StaticIcon.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/StaticIcon.java index 508e019..42f1176 100644 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/StaticIcon.java +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/StaticIcon.java @@ -21,7 +21,7 @@ public StaticIcon(Texture icon, Point pos) { @Override public void draw(GuiDisplay gui, Point mousePos) { - gui.drawTexture(this.icon, this.surface.startPos()); + gui.draw(this.icon, this.surface.startPos()); } public WidgetSurface surface() { diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/button/RectangularButton.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/button/RectangularButton.java index 7292ea7..87c74f0 100644 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/button/RectangularButton.java +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/button/RectangularButton.java @@ -52,7 +52,7 @@ public static RectangularButton vanillaButton(Point pos, Component text, ClickEv @Override public void draw(GuiDisplay gui, Point mousePos) { GuiTexture background = this.background.texture(this.isDisabled, this.surface.isWithinBounds(mousePos)); - gui.drawGuiTexture(background, this.surface.startPos()); + gui.drawGui(background, this.surface.startPos()); gui.drawText(this.text, Color.WHITE, this.textPos); } diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/text/PositionedText.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/text/PositionedText.java index d945a58..b7ab293 100644 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/text/PositionedText.java +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/text/PositionedText.java @@ -6,13 +6,13 @@ public class PositionedText { private final Component text; - private final Point pos; private final Color color; + private final Point pos; - public PositionedText(Component text, Point pos, Color color) { + public PositionedText(Component text, Color color, Point pos) { this.text = text; - this.pos = pos; this.color = color; + this.pos = pos; } public Component text() { diff --git a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/text/PushableTextLines.java b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/text/PushableTextLines.java index da863c6..4fd14a8 100644 --- a/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/text/PushableTextLines.java +++ b/subprojects/core/src/main/java/net/pixaurora/kit_tunes/impl/ui/widget/text/PushableTextLines.java @@ -29,7 +29,7 @@ public Point endPos() { public void push(Component text, Color color) { Point newLinePos = MinecraftClient.textSize(text).centerOnVertical(startPos).offset(0, this.height()); - this.lines.add(new PositionedText(text, newLinePos, color)); + this.lines.add(new PositionedText(text, color, newLinePos)); } @Override diff --git a/subprojects/ui/r1.20.4/src/main/java/net/pixaurora/kit_tunes/impl/KitTunesUIImpl.java b/subprojects/ui/r1.20.4/src/main/java/net/pixaurora/kit_tunes/impl/KitTunesUIImpl.java index 320ca54..a88e600 100644 --- a/subprojects/ui/r1.20.4/src/main/java/net/pixaurora/kit_tunes/impl/KitTunesUIImpl.java +++ b/subprojects/ui/r1.20.4/src/main/java/net/pixaurora/kit_tunes/impl/KitTunesUIImpl.java @@ -1,7 +1,8 @@ package net.pixaurora.kit_tunes.impl; +import java.util.List; + import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.components.toasts.Toast; import net.minecraft.client.gui.screens.ConfirmLinkScreen; import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceLocation; @@ -14,7 +15,6 @@ import net.pixaurora.kit_tunes.impl.ui.screen.Screen; import net.pixaurora.kit_tunes.impl.ui.sound.Sound; import net.pixaurora.kit_tunes.impl.ui.text.Component; -import net.pixaurora.kit_tunes.impl.ui.toast.KitTunesToastData; public class KitTunesUIImpl implements KitTunesMinecraftUICompat { private final Minecraft client = Minecraft.getInstance(); @@ -38,11 +38,8 @@ public static MutableComponent componentToMinecraftType(Component component) { } @Override - public void sendToast(KitTunesToastData toastData) { - Minecraft client = Minecraft.getInstance(); - Toast toast = new KitTunesToastImpl(client.font, toastData); - - client.getToasts().addToast(toast); + public void sendToast(net.pixaurora.kit_tunes.impl.ui.toast.Toast toast) { + this.client.getToasts().addToast(new KitTunesToastImpl(toast)); } @Override @@ -100,4 +97,10 @@ public void setScreen(Screen screen) { public void confirmURL(String url) { ConfirmLinkScreen.confirmLinkNow(this.client.screen, url); } + + @Override + public List splitText(Component text, int lineWidth) { + // TODO: Actually split the text. + return List.of(text); + } } diff --git a/subprojects/ui/r1.20.4/src/main/java/net/pixaurora/kit_tunes/impl/gui/GuiDisplayImpl.java b/subprojects/ui/r1.20.4/src/main/java/net/pixaurora/kit_tunes/impl/gui/GuiDisplayImpl.java index 0a1db86..bb31eb7 100644 --- a/subprojects/ui/r1.20.4/src/main/java/net/pixaurora/kit_tunes/impl/gui/GuiDisplayImpl.java +++ b/subprojects/ui/r1.20.4/src/main/java/net/pixaurora/kit_tunes/impl/gui/GuiDisplayImpl.java @@ -28,14 +28,15 @@ public void drawTexture(ResourcePath path, Size size, Point pos) { } @Override - public void drawGuiTexture(ResourcePath path, Size size, Point pos) { - this.graphics.blitSprite(conversions.convert(path), pos.x(), pos.y(), size.width(), size.height()); + public void drawGuiTextureSubsection(ResourcePath path, Size size, Point pos, Size subsection, Point offset) { + this.graphics.blitSprite(conversions.convert(path), size.width(), size.height(), offset.x(), offset.y(), + pos.x(), pos.y(), subsection.width(), subsection.height()); } @SuppressWarnings("resource") @Override - public void drawText(Component text, Color color, Point pos) { - this.graphics.drawString(Minecraft.getInstance().font, conversions.convert(text), pos.x(), pos.y(), - color.hex()); + public void drawText(Component text, Color color, Point pos, boolean shadowed) { + this.graphics.drawString(Minecraft.getInstance().font, conversions.convert(text), pos.x(), pos.y(), color.hex(), + shadowed); } } diff --git a/subprojects/ui/r1.20.4/src/main/java/net/pixaurora/kit_tunes/impl/gui/KitTunesToastImpl.java b/subprojects/ui/r1.20.4/src/main/java/net/pixaurora/kit_tunes/impl/gui/KitTunesToastImpl.java index 60733ec..f1be7b2 100644 --- a/subprojects/ui/r1.20.4/src/main/java/net/pixaurora/kit_tunes/impl/gui/KitTunesToastImpl.java +++ b/subprojects/ui/r1.20.4/src/main/java/net/pixaurora/kit_tunes/impl/gui/KitTunesToastImpl.java @@ -1,83 +1,32 @@ package net.pixaurora.kit_tunes.impl.gui; -import java.util.ArrayList; -import java.util.List; - -import net.minecraft.ChatFormatting; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.Font; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.toasts.Toast; import net.minecraft.client.gui.components.toasts.ToastComponent; -import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.MutableComponent; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.util.FormattedCharSequence; -import net.pixaurora.kit_tunes.impl.KitTunesUIImpl; -import net.pixaurora.kit_tunes.impl.ui.math.Point; -import net.pixaurora.kit_tunes.impl.ui.math.Size; -import net.pixaurora.kit_tunes.impl.ui.toast.ToastBackground; -import net.pixaurora.kit_tunes.impl.ui.toast.ToastBackgroundAppearance; -import net.pixaurora.kit_tunes.impl.ui.toast.ToastBackgroundTile; -import net.pixaurora.kit_tunes.impl.util.Pair; -import net.pixaurora.kit_tunes.impl.ui.toast.KitTunesToastData; public class KitTunesToastImpl implements Toast { - private final KitTunesToastData toastData; - - private final ResourceLocation icon; - - private final ResourceLocation background; - private final List tiles; + private final net.pixaurora.kit_tunes.impl.ui.toast.Toast toast; + private final long millisecondsShown; - private final Component title; - private final List bodyLines; - private final Size toastSize; + private final ConversionCacheImpl conversions; - private boolean hasRendered = false; + private boolean hasRendered; private long firstRenderedTime; - public KitTunesToastImpl(Font font, KitTunesToastData toastData) { - this.toastData = toastData; - - this.icon = KitTunesUIImpl.resourceToMinecraftType(this.toastData.icon()); - - ToastBackground background = toastData.background(); - - this.background = KitTunesUIImpl.resourceToMinecraftGuiSprite(background.appearance().texture()); - - List lines = toastData.messageLines().stream().map(KitTunesUIImpl::componentToMinecraftType) - .toList(); - - this.title = KitTunesUIImpl.componentToMinecraftType(toastData.title()); - this.bodyLines = new ArrayList<>(); - - for (Component line : lines) { - this.bodyLines.addAll(font.split(line, background.maxLineLength())); - } - - int textWidth = font.width(this.title); - - for (FormattedCharSequence line : this.bodyLines) { - textWidth = Math.max(font.width(line), textWidth); - } - - Size textBox = Size.of(textWidth, this.bodyLines.size() * font.lineHeight); - - Pair, Size> tilesAndSize = background.tilesAndSize(textBox); - - this.tiles = tilesAndSize.first(); - this.toastSize = tilesAndSize.second(); + public KitTunesToastImpl(net.pixaurora.kit_tunes.impl.ui.toast.Toast toast) { + this.toast = toast; + this.millisecondsShown = toast.timeShown().toMillis(); + this.conversions = new ConversionCacheImpl(); } @Override public int width() { - return this.toastSize.width(); + return this.toast.size().width(); } @Override public int height() { - return this.toastSize.height(); + return this.toast.size().height(); } @Override @@ -87,51 +36,9 @@ public Toast.Visibility render(GuiGraphics graphics, ToastComponent manager, lon this.firstRenderedTime = frameTime; } - Point offset = Point.ZERO; - - this.drawToastBackground(graphics, offset); - - ToastBackground background = this.toastData.background(); - - this.drawTexture(graphics, this.icon, background.iconPos().offset(offset), this.toastData.iconSize()); - - Minecraft client = manager.getMinecraft(); - - Point titlePos = background.titlePos().offset(offset); - graphics.drawString(client.font, title, titlePos.x(), titlePos.y(), ChatFormatting.AQUA.getColor(), false); - - Point bodyTextStart = background.bodyTextStartPos().offset(offset); - int currentLineY = bodyTextStart.y(); - - for (FormattedCharSequence line : this.bodyLines) { - graphics.drawString(client.font, line, bodyTextStart.x(), currentLineY, ChatFormatting.BLACK.getColor(), - false); - currentLineY += client.font.lineHeight; - } - - return frameTime - this.firstRenderedTime < 5000 ? Toast.Visibility.SHOW : Toast.Visibility.HIDE; - } + this.toast.draw(new GuiDisplayImpl(graphics, conversions)); - public void drawTexture(GuiGraphics graphics, ResourceLocation texture, Point pos, Size size) { - int width = size.width(); - int height = size.height(); - - graphics.blit(texture, pos.x(), pos.y(), 0, 0.0F, 0.0F, width, height, width, height); - } - - private void drawToastBackground(GuiGraphics graphics, Point offset) { - ToastBackgroundAppearance appearance = this.toastData.background().appearance(); - - Size texture = appearance.size(); - - for (ToastBackgroundTile tile : this.tiles) { - Size tileSize = tile.size(); - Point textureOffset = tile.textureOffset(); - - Point tilePos = tile.pos().offset(offset); - - graphics.blitSprite(this.background, texture.width(), texture.height(), textureOffset.x(), - textureOffset.y(), tilePos.x(), tilePos.y(), tileSize.width(), tileSize.height()); - } + return frameTime - this.firstRenderedTime < this.millisecondsShown ? Toast.Visibility.SHOW + : Toast.Visibility.HIDE; } } diff --git a/subprojects/ui/r1.21.0/src/main/java/net/pixaurora/kit_tunes/impl/KitTunesUIImpl.java b/subprojects/ui/r1.21.0/src/main/java/net/pixaurora/kit_tunes/impl/KitTunesUIImpl.java index 50aeb3e..88ccdd4 100644 --- a/subprojects/ui/r1.21.0/src/main/java/net/pixaurora/kit_tunes/impl/KitTunesUIImpl.java +++ b/subprojects/ui/r1.21.0/src/main/java/net/pixaurora/kit_tunes/impl/KitTunesUIImpl.java @@ -1,7 +1,8 @@ package net.pixaurora.kit_tunes.impl; +import java.util.List; + import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.components.toasts.Toast; import net.minecraft.client.gui.screens.ConfirmLinkScreen; import net.minecraft.network.chat.MutableComponent; import net.minecraft.resources.ResourceLocation; @@ -14,7 +15,6 @@ import net.pixaurora.kit_tunes.impl.ui.screen.Screen; import net.pixaurora.kit_tunes.impl.ui.sound.Sound; import net.pixaurora.kit_tunes.impl.ui.text.Component; -import net.pixaurora.kit_tunes.impl.ui.toast.KitTunesToastData; public class KitTunesUIImpl implements KitTunesMinecraftUICompat { private final Minecraft client = Minecraft.getInstance(); @@ -38,11 +38,8 @@ public static MutableComponent componentToMinecraftType(Component component) { } @Override - public void sendToast(KitTunesToastData toastData) { - Minecraft client = Minecraft.getInstance(); - Toast toast = new KitTunesToastImpl(client.font, toastData); - - client.getToasts().addToast(toast); + public void sendToast(net.pixaurora.kit_tunes.impl.ui.toast.Toast toast) { + this.client.getToasts().addToast(new KitTunesToastImpl(toast)); } @Override @@ -100,4 +97,10 @@ public void setScreen(Screen screen) { public void confirmURL(String url) { ConfirmLinkScreen.confirmLinkNow(this.client.screen, url); } + + @Override + public List splitText(Component text, int lineWidth) { + // TODO: Actually split the text. + return List.of(text); + } } diff --git a/subprojects/ui/r1.21.0/src/main/java/net/pixaurora/kit_tunes/impl/gui/GuiDisplayImpl.java b/subprojects/ui/r1.21.0/src/main/java/net/pixaurora/kit_tunes/impl/gui/GuiDisplayImpl.java index 0a1db86..bb31eb7 100644 --- a/subprojects/ui/r1.21.0/src/main/java/net/pixaurora/kit_tunes/impl/gui/GuiDisplayImpl.java +++ b/subprojects/ui/r1.21.0/src/main/java/net/pixaurora/kit_tunes/impl/gui/GuiDisplayImpl.java @@ -28,14 +28,15 @@ public void drawTexture(ResourcePath path, Size size, Point pos) { } @Override - public void drawGuiTexture(ResourcePath path, Size size, Point pos) { - this.graphics.blitSprite(conversions.convert(path), pos.x(), pos.y(), size.width(), size.height()); + public void drawGuiTextureSubsection(ResourcePath path, Size size, Point pos, Size subsection, Point offset) { + this.graphics.blitSprite(conversions.convert(path), size.width(), size.height(), offset.x(), offset.y(), + pos.x(), pos.y(), subsection.width(), subsection.height()); } @SuppressWarnings("resource") @Override - public void drawText(Component text, Color color, Point pos) { - this.graphics.drawString(Minecraft.getInstance().font, conversions.convert(text), pos.x(), pos.y(), - color.hex()); + public void drawText(Component text, Color color, Point pos, boolean shadowed) { + this.graphics.drawString(Minecraft.getInstance().font, conversions.convert(text), pos.x(), pos.y(), color.hex(), + shadowed); } } diff --git a/subprojects/ui/r1.21.0/src/main/java/net/pixaurora/kit_tunes/impl/gui/KitTunesToastImpl.java b/subprojects/ui/r1.21.0/src/main/java/net/pixaurora/kit_tunes/impl/gui/KitTunesToastImpl.java index 60733ec..f1be7b2 100644 --- a/subprojects/ui/r1.21.0/src/main/java/net/pixaurora/kit_tunes/impl/gui/KitTunesToastImpl.java +++ b/subprojects/ui/r1.21.0/src/main/java/net/pixaurora/kit_tunes/impl/gui/KitTunesToastImpl.java @@ -1,83 +1,32 @@ package net.pixaurora.kit_tunes.impl.gui; -import java.util.ArrayList; -import java.util.List; - -import net.minecraft.ChatFormatting; -import net.minecraft.client.Minecraft; -import net.minecraft.client.gui.Font; import net.minecraft.client.gui.GuiGraphics; import net.minecraft.client.gui.components.toasts.Toast; import net.minecraft.client.gui.components.toasts.ToastComponent; -import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.MutableComponent; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.util.FormattedCharSequence; -import net.pixaurora.kit_tunes.impl.KitTunesUIImpl; -import net.pixaurora.kit_tunes.impl.ui.math.Point; -import net.pixaurora.kit_tunes.impl.ui.math.Size; -import net.pixaurora.kit_tunes.impl.ui.toast.ToastBackground; -import net.pixaurora.kit_tunes.impl.ui.toast.ToastBackgroundAppearance; -import net.pixaurora.kit_tunes.impl.ui.toast.ToastBackgroundTile; -import net.pixaurora.kit_tunes.impl.util.Pair; -import net.pixaurora.kit_tunes.impl.ui.toast.KitTunesToastData; public class KitTunesToastImpl implements Toast { - private final KitTunesToastData toastData; - - private final ResourceLocation icon; - - private final ResourceLocation background; - private final List tiles; + private final net.pixaurora.kit_tunes.impl.ui.toast.Toast toast; + private final long millisecondsShown; - private final Component title; - private final List bodyLines; - private final Size toastSize; + private final ConversionCacheImpl conversions; - private boolean hasRendered = false; + private boolean hasRendered; private long firstRenderedTime; - public KitTunesToastImpl(Font font, KitTunesToastData toastData) { - this.toastData = toastData; - - this.icon = KitTunesUIImpl.resourceToMinecraftType(this.toastData.icon()); - - ToastBackground background = toastData.background(); - - this.background = KitTunesUIImpl.resourceToMinecraftGuiSprite(background.appearance().texture()); - - List lines = toastData.messageLines().stream().map(KitTunesUIImpl::componentToMinecraftType) - .toList(); - - this.title = KitTunesUIImpl.componentToMinecraftType(toastData.title()); - this.bodyLines = new ArrayList<>(); - - for (Component line : lines) { - this.bodyLines.addAll(font.split(line, background.maxLineLength())); - } - - int textWidth = font.width(this.title); - - for (FormattedCharSequence line : this.bodyLines) { - textWidth = Math.max(font.width(line), textWidth); - } - - Size textBox = Size.of(textWidth, this.bodyLines.size() * font.lineHeight); - - Pair, Size> tilesAndSize = background.tilesAndSize(textBox); - - this.tiles = tilesAndSize.first(); - this.toastSize = tilesAndSize.second(); + public KitTunesToastImpl(net.pixaurora.kit_tunes.impl.ui.toast.Toast toast) { + this.toast = toast; + this.millisecondsShown = toast.timeShown().toMillis(); + this.conversions = new ConversionCacheImpl(); } @Override public int width() { - return this.toastSize.width(); + return this.toast.size().width(); } @Override public int height() { - return this.toastSize.height(); + return this.toast.size().height(); } @Override @@ -87,51 +36,9 @@ public Toast.Visibility render(GuiGraphics graphics, ToastComponent manager, lon this.firstRenderedTime = frameTime; } - Point offset = Point.ZERO; - - this.drawToastBackground(graphics, offset); - - ToastBackground background = this.toastData.background(); - - this.drawTexture(graphics, this.icon, background.iconPos().offset(offset), this.toastData.iconSize()); - - Minecraft client = manager.getMinecraft(); - - Point titlePos = background.titlePos().offset(offset); - graphics.drawString(client.font, title, titlePos.x(), titlePos.y(), ChatFormatting.AQUA.getColor(), false); - - Point bodyTextStart = background.bodyTextStartPos().offset(offset); - int currentLineY = bodyTextStart.y(); - - for (FormattedCharSequence line : this.bodyLines) { - graphics.drawString(client.font, line, bodyTextStart.x(), currentLineY, ChatFormatting.BLACK.getColor(), - false); - currentLineY += client.font.lineHeight; - } - - return frameTime - this.firstRenderedTime < 5000 ? Toast.Visibility.SHOW : Toast.Visibility.HIDE; - } + this.toast.draw(new GuiDisplayImpl(graphics, conversions)); - public void drawTexture(GuiGraphics graphics, ResourceLocation texture, Point pos, Size size) { - int width = size.width(); - int height = size.height(); - - graphics.blit(texture, pos.x(), pos.y(), 0, 0.0F, 0.0F, width, height, width, height); - } - - private void drawToastBackground(GuiGraphics graphics, Point offset) { - ToastBackgroundAppearance appearance = this.toastData.background().appearance(); - - Size texture = appearance.size(); - - for (ToastBackgroundTile tile : this.tiles) { - Size tileSize = tile.size(); - Point textureOffset = tile.textureOffset(); - - Point tilePos = tile.pos().offset(offset); - - graphics.blitSprite(this.background, texture.width(), texture.height(), textureOffset.x(), - textureOffset.y(), tilePos.x(), tilePos.y(), tileSize.width(), tileSize.height()); - } + return frameTime - this.firstRenderedTime < this.millisecondsShown ? Toast.Visibility.SHOW + : Toast.Visibility.HIDE; } }