Skip to content

Commit

Permalink
move the toast logic to the core of the mod
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixaurora committed Jul 8, 2024
1 parent c93cdd0 commit 6665761
Show file tree
Hide file tree
Showing 24 changed files with 312 additions and 330 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
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
* in the core of the mod.
*/
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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -28,6 +30,10 @@ public static Size textSize(Component... text) {
return impl().textSize(text);
}

public static List<Component> splitText(Component text, int lineWidth) {
return impl().splitText(text, lineWidth);
}

public static void playSound(Sound sound) {
impl().playSound(sound);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package net.pixaurora.kit_tunes.impl.ui.text;

import java.util.List;

import net.pixaurora.kit_tunes.impl.ui.math.Size;

public interface TextProcessor {
public int textHeight();

public int textWidth(Component text);

public List<Component> splitText(Component text, int lineWidth);

public default Size textSize(Component text) {
return Size.of(this.textWidth(text), this.textHeight());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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<PositionedInnerTile> tiles;

private final List<PositionedText> 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<List<PositionedInnerTile>, 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Component> messageLines();

public Color messageColor();

public ToastBackground background();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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;

Expand All @@ -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
Expand All @@ -43,6 +47,11 @@ public Component title() {
return TITLE;
}

@Override
public Color titleColor() {
return Color.YELLOW;
}

@Override
public List<Component> messageLines() {
List<Component> lines = new ArrayList<>();
Expand All @@ -61,6 +70,11 @@ public List<Component> messageLines() {
return lines;
}

@Override
public Color messageColor() {
return Color.WHITE;
}

@Override
public ToastBackground background() {
return BACKGROUND;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit 6665761

Please sign in to comment.