Skip to content

Commit

Permalink
Clean up toast
Browse files Browse the repository at this point in the history
  • Loading branch information
OreCruncher committed Feb 11, 2024
1 parent d9fb713 commit 4f5041e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.orecruncher.dsurround.gui.sound;

import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.Music;
import org.orecruncher.dsurround.config.libraries.ISoundLibrary;
import org.orecruncher.dsurround.lib.GameUtils;
import org.orecruncher.dsurround.lib.di.ContainerManager;
import org.orecruncher.dsurround.lib.gui.ColorPalette;
import org.orecruncher.dsurround.lib.gui.WarmToast;

public class SoundToast {

private static final WarmToast.Profile SOUND_TOAST_PROFILE = new WarmToast.Profile(new ResourceLocation("toast/advancement"), 5000, ColorPalette.PUMPKIN_ORANGE, ColorPalette.WHEAT);

public static void create(Music music) {
var soundLibrary = ContainerManager.resolve(ISoundLibrary.class);
var metadata = soundLibrary.getSoundMetadata(music.getEvent().value().getLocation());
Expand All @@ -18,7 +22,7 @@ public static void create(Music music) {
var author = metadata.getCredits().get(0).author();
var titleLine = Component.translatable("dsurround.text.toast.music.title", title);
var authorLine = Component.translatable("dsurround.text.toast.music.author", author);
var toast = WarmToast.multiline(GameUtils.getMC(), titleLine, authorLine);
var toast = WarmToast.multiline(GameUtils.getMC(), SOUND_TOAST_PROFILE, titleLine, authorLine);
GameUtils.getMC().getToasts().addToast(toast);
}
}
Expand Down
39 changes: 21 additions & 18 deletions src/main/java/org/orecruncher/dsurround/lib/gui/WarmToast.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
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.TextColor;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.FormattedCharSequence;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

@SuppressWarnings("unused")
public class WarmToast implements Toast {
private static final ResourceLocation BACKGROUND_SPRITE = new ResourceLocation("toast/advancement");
private static final int DISPLAY_TIME = 5000;
private static final Profile DEFAULT_PROFILE = new Profile(new ResourceLocation("toast/advancement"), 5000, ColorPalette.GOLD, ColorPalette.WHITE);

private static final int MAX_LINE_SIZE = 200;
private static final int MIN_LINE_SIZE = 100;
private static final int LINE_SPACING = 12;
private static final int MARGIN = 10;

private final ResourceLocation backgroundSprite;
private final int displayTime;
private final Profile profile;

private Component title;
private List<FormattedCharSequence> messageLines;
Expand All @@ -32,21 +32,20 @@ public class WarmToast implements Toast {
private final int width;

public static WarmToast multiline(Minecraft minecraft, Component title, Component body) {
return multiline(minecraft, BACKGROUND_SPRITE, DISPLAY_TIME, title, body);
return multiline(minecraft, DEFAULT_PROFILE, title, body);
}

public static WarmToast multiline(Minecraft minecraft, ResourceLocation backgroundSprite, int displayTime, Component title, Component body) {
public static WarmToast multiline(Minecraft minecraft, Profile profile, Component title, Component body) {
var font = minecraft.font;
var list = font.split(body, MAX_LINE_SIZE);
var titleSize = Math.min(MAX_LINE_SIZE, Math.max(MIN_LINE_SIZE, font.width(title)));
var lineSize = list.stream().mapToInt(font::width).max().orElse(MIN_LINE_SIZE);
int width = Math.max(titleSize, lineSize) + MARGIN * 3;
return new WarmToast(backgroundSprite, displayTime, title, list, width);
return new WarmToast(profile, title, list, width);
}

private WarmToast(ResourceLocation background, int displayTime, Component title, List<FormattedCharSequence> body, int width) {
this.backgroundSprite = background;
this.displayTime = displayTime;
private WarmToast(Profile profile, Component title, List<FormattedCharSequence> body, int width) {
this.profile = profile;
this.title = title;
this.messageLines = body;
this.width = width;
Expand Down Expand Up @@ -74,7 +73,7 @@ public void reset(Component component, @Nullable Component component2) {

int i = this.width();
if (i == 160 && this.messageLines.size() <= 1) {
guiGraphics.blitSprite(this.backgroundSprite, 0, 0, i, this.height());
guiGraphics.blitSprite(this.profile.sprite, 0, 0, i, this.height());
} else {
int renderHeight = this.height();
int lineRenderCount = Math.min(4, renderHeight - 28);
Expand All @@ -88,33 +87,37 @@ public void reset(Component component, @Nullable Component component2) {
}

if (this.messageLines.isEmpty()) {
guiGraphics.drawString(toastComponent.getMinecraft().font, this.title, 18, LINE_SPACING, -256, false);
guiGraphics.drawString(toastComponent.getMinecraft().font, this.title, 18, LINE_SPACING, this.profile.titleColor.getValue(), false);
} else {
guiGraphics.drawString(toastComponent.getMinecraft().font, this.title, 18, 7, -256, false);
guiGraphics.drawString(toastComponent.getMinecraft().font, this.title, 18, 7, this.profile.titleColor.getValue(), false);

for(int j = 0; j < this.messageLines.size(); ++j) {
guiGraphics.drawString(toastComponent.getMinecraft().font, this.messageLines.get(j), 18, 18 + j * LINE_SPACING, -1, false);
guiGraphics.drawString(toastComponent.getMinecraft().font, this.messageLines.get(j), 18, 18 + j * LINE_SPACING, this.profile.bodyColor.getValue(), false);
}
}

double d = (double)this.displayTime * toastComponent.getNotificationDisplayTimeMultiplier();
double d = (double)this.profile.displayTime * toastComponent.getNotificationDisplayTimeMultiplier();
long o = lastChanged - this.lastChanged;
return (double)o < d ? Visibility.SHOW : Visibility.HIDE;
}

private void renderBackgroundRow(GuiGraphics guiGraphics, int i, int j, int k, int l) {
int m = j == 0 ? 20 : 5;
int n = Math.min(60, i - m);
guiGraphics.blitSprite(this.backgroundSprite, 160, 32, 0, j, 0, k, m, l);
guiGraphics.blitSprite(this.profile.sprite, 160, 32, 0, j, 0, k, m, l);

for(int o = m; o < i - n; o += 64) {
guiGraphics.blitSprite(this.backgroundSprite, 160, 32, 32, j, o, k, Math.min(64, i - o - n), l);
guiGraphics.blitSprite(this.profile.sprite, 160, 32, 32, j, o, k, Math.min(64, i - o - n), l);
}

guiGraphics.blitSprite(this.backgroundSprite, 160, 32, 160 - n, j, i - n, k, n, l);
guiGraphics.blitSprite(this.profile.sprite, 160, 32, 160 - n, j, i - n, k, n, l);
}

private static ImmutableList<FormattedCharSequence> nullToEmpty(@Nullable Component component) {
return component == null ? ImmutableList.of() : ImmutableList.of(component.getVisualOrderText());
}

public record Profile(ResourceLocation sprite, int displayTime, TextColor titleColor, TextColor bodyColor) {

}
}

0 comments on commit 4f5041e

Please sign in to comment.