| 1 | package net.minecraft.client.gui.components.toasts;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.Minecraft;
|
| 6 | import net.minecraft.client.Options;
|
| 7 | import net.minecraft.client.color.ColorLerper;
|
| 8 | import net.minecraft.client.gui.Font;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.renderer.RenderPipelines;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.resources.Identifier;
|
| 13 | import net.minecraft.world.item.DyeColor;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class NowPlayingToast implements Toast {
|
| 18 | private static final Identifier NOW_PLAYING_BACKGROUND_SPRITE = Identifier.withDefaultNamespace("toast/now_playing");
|
| 19 | private static final Identifier MUSIC_NOTES_SPRITE = Identifier.parse("icon/music_notes");
|
| 20 | private static final int PADDING = 7;
|
| 21 | private static final int MUSIC_NOTES_SIZE = 16;
|
| 22 | private static final int HEIGHT = 30;
|
| 23 | private static final int MUSIC_NOTES_SPACE = 30;
|
| 24 | private static final int VISIBILITY_DURATION = 5000;
|
| 25 | private static final int TEXT_COLOR = DyeColor.LIGHT_GRAY.getTextColor();
|
| 26 | private static final long MUSIC_COLOR_CHANGE_FREQUENCY_MS = 25L;
|
| 27 | private static int musicNoteColorTick;
|
| 28 | private static long lastMusicNoteColorChange;
|
| 29 | private static int musicNoteColor = -1;
|
| 30 | private boolean updateToast;
|
| 31 | private double notificationDisplayTimeMultiplier;
|
| 32 | private final Minecraft minecraft;
|
| 33 | private Toast.Visibility wantedVisibility = Toast.Visibility.HIDE;
|
| 34 |
|
| 35 | public NowPlayingToast() {
|
| 36 | this.minecraft = Minecraft.getInstance();
|
| 37 | }
|
| 38 |
|
| 39 | public static void extractToast(final GuiGraphicsExtractor graphics, final Font font) {
|
| 40 | String currentSong = getCurrentSongName();
|
| 41 | if (currentSong != null) {
|
| 42 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, NOW_PLAYING_BACKGROUND_SPRITE, 0, 0, getWidth(currentSong, font), 30);
|
| 43 | int notesOffset = 7;
|
| 44 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, MUSIC_NOTES_SPRITE, 7, 7, 16, 16, musicNoteColor);
|
| 45 | graphics.text(font, getNowPlayingString(currentSong), 30, 15 - 9 / 2, TEXT_COLOR);
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | private static @Nullable String getCurrentSongName() {
|
| 50 | return Minecraft.getInstance().getMusicManager().getCurrentMusicTranslationKey();
|
| 51 | }
|
| 52 |
|
| 53 | public static void tickMusicNotes() {
|
| 54 | if (getCurrentSongName() != null) {
|
| 55 | long now = System.currentTimeMillis();
|
| 56 | if (now > lastMusicNoteColorChange + 25L) {
|
| 57 | musicNoteColorTick++;
|
| 58 | lastMusicNoteColorChange = now;
|
| 59 | musicNoteColor = ColorLerper.getLerpedColor(ColorLerper.Type.MUSIC_NOTE, musicNoteColorTick);
|
| 60 | }
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | private static Component getNowPlayingString(final @Nullable String currentSongKey) {
|
| 65 | return currentSongKey == null ? Component.empty() : Component.translatable(currentSongKey.replace("/", "."));
|
| 66 | }
|
| 67 |
|
| 68 | public void showToast(final Options options) {
|
| 69 | this.updateToast = true;
|
| 70 | this.notificationDisplayTimeMultiplier = options.notificationDisplayTime().get();
|
| 71 | this.setWantedVisibility(Toast.Visibility.SHOW);
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public void update(final ToastManager manager, final long fullyVisibleForMs) {
|
| 76 | if (this.updateToast) {
|
| 77 | this.wantedVisibility = fullyVisibleForMs < 5000.0 * this.notificationDisplayTimeMultiplier ? Toast.Visibility.SHOW : Toast.Visibility.HIDE;
|
| 78 | tickMusicNotes();
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public void extractRenderState(final GuiGraphicsExtractor graphics, final Font font, final long fullyVisibleForMs) {
|
| 84 | extractToast(graphics, font);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public void onFinishedRendering() {
|
| 89 | this.updateToast = false;
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public int width() {
|
| 94 | return getWidth(getCurrentSongName(), this.minecraft.font);
|
| 95 | }
|
| 96 |
|
| 97 | private static int getWidth(final @Nullable String currentSong, final Font font) {
|
| 98 | return 30 + font.width(getNowPlayingString(currentSong)) + 7;
|
| 99 | }
|
| 100 |
|
| 101 | @Override
|
| 102 | public int height() {
|
| 103 | return 30;
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public float xPos(final int screenWidth, final float visiblePortion) {
|
| 108 | return this.width() * visiblePortion - this.width();
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public float yPos(final int firstSlotIndex) {
|
| 113 | return 0.0F;
|
| 114 | }
|
| 115 |
|
| 116 | @Override
|
| 117 | public Toast.Visibility getWantedVisibility() {
|
| 118 | return this.wantedVisibility;
|
| 119 | }
|
| 120 |
|
| 121 | public void setWantedVisibility(final Toast.Visibility visibility) {
|
| 122 | this.wantedVisibility = visibility;
|
| 123 | }
|
| 124 | }
|
| 125 |
|