NowPlayingToast.java

net.minecraft.client.gui.components.toasts.NowPlayingToast

信息

  • 全限定名:net.minecraft.client.gui.components.toasts.NowPlayingToast
  • 类型:public class
  • 包:net.minecraft.client.gui.components.toasts
  • 源码路径:src/main/java/net/minecraft/client/gui/components/toasts/NowPlayingToast.java
  • 起始行号:L17
  • 实现:Toast
  • 职责:

    TODO

字段/常量

  • NOW_PLAYING_BACKGROUND_SPRITE

    • 类型: Identifier
    • 修饰符: private static final
    • 源码定位: L18
    • 说明:

      TODO

  • MUSIC_NOTES_SPRITE

    • 类型: Identifier
    • 修饰符: private static final
    • 源码定位: L19
    • 说明:

      TODO

  • PADDING

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L20
    • 说明:

      TODO

  • MUSIC_NOTES_SIZE

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L21
    • 说明:

      TODO

  • HEIGHT

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L22
    • 说明:

      TODO

  • MUSIC_NOTES_SPACE

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L23
    • 说明:

      TODO

  • VISIBILITY_DURATION

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L24
    • 说明:

      TODO

  • TEXT_COLOR

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L25
    • 说明:

      TODO

  • MUSIC_COLOR_CHANGE_FREQUENCY_MS

    • 类型: long
    • 修饰符: private static final
    • 源码定位: L26
    • 说明:

      TODO

  • musicNoteColorTick

    • 类型: int
    • 修饰符: private static
    • 源码定位: L27
    • 说明:

      TODO

  • lastMusicNoteColorChange

    • 类型: long
    • 修饰符: private static
    • 源码定位: L28
    • 说明:

      TODO

  • musicNoteColor

    • 类型: int
    • 修饰符: private static
    • 源码定位: L29
    • 说明:

      TODO

  • updateToast

    • 类型: boolean
    • 修饰符: private
    • 源码定位: L30
    • 说明:

      TODO

  • notificationDisplayTimeMultiplier

    • 类型: double
    • 修饰符: private
    • 源码定位: L31
    • 说明:

      TODO

  • minecraft

    • 类型: Minecraft
    • 修饰符: private final
    • 源码定位: L32
    • 说明:

      TODO

  • wantedVisibility

    • 类型: Toast.Visibility
    • 修饰符: private
    • 源码定位: L33
    • 说明:

      TODO

内部类/嵌套类型

构造器

public NowPlayingToast() @ L35

  • 构造器名:NowPlayingToast
  • 源码定位:L35
  • 修饰符:public

参数:

说明:

TODO

方法

下面的方法块按源码顺序生成。

public static void extractToast(GuiGraphicsExtractor graphics, Font font) @ L39

  • 方法名:extractToast
  • 源码定位:L39
  • 返回类型:void
  • 修饰符:public static

参数:

  • graphics: GuiGraphicsExtractor
  • font: Font

说明:

TODO

private static String getCurrentSongName() @ L49

  • 方法名:getCurrentSongName
  • 源码定位:L49
  • 返回类型:String
  • 修饰符:private static

参数:

说明:

TODO

public static void tickMusicNotes() @ L53

  • 方法名:tickMusicNotes
  • 源码定位:L53
  • 返回类型:void
  • 修饰符:public static

参数:

说明:

TODO

private static Component getNowPlayingString(String currentSongKey) @ L64

  • 方法名:getNowPlayingString
  • 源码定位:L64
  • 返回类型:Component
  • 修饰符:private static

参数:

  • currentSongKey: String

说明:

TODO

public void showToast(Options options) @ L68

  • 方法名:showToast
  • 源码定位:L68
  • 返回类型:void
  • 修饰符:public

参数:

  • options: Options

说明:

TODO

public void update(ToastManager manager, long fullyVisibleForMs) @ L74

  • 方法名:update
  • 源码定位:L74
  • 返回类型:void
  • 修饰符:public

参数:

  • manager: ToastManager
  • fullyVisibleForMs: long

说明:

TODO

public void extractRenderState(GuiGraphicsExtractor graphics, Font font, long fullyVisibleForMs) @ L82

  • 方法名:extractRenderState
  • 源码定位:L82
  • 返回类型:void
  • 修饰符:public

参数:

  • graphics: GuiGraphicsExtractor
  • font: Font
  • fullyVisibleForMs: long

说明:

TODO

public void onFinishedRendering() @ L87

  • 方法名:onFinishedRendering
  • 源码定位:L87
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

public int width() @ L92

  • 方法名:width
  • 源码定位:L92
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

private static int getWidth(String currentSong, Font font) @ L97

  • 方法名:getWidth
  • 源码定位:L97
  • 返回类型:int
  • 修饰符:private static

参数:

  • currentSong: String
  • font: Font

说明:

TODO

public int height() @ L101

  • 方法名:height
  • 源码定位:L101
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

public float xPos(int screenWidth, float visiblePortion) @ L106

  • 方法名:xPos
  • 源码定位:L106
  • 返回类型:float
  • 修饰符:public

参数:

  • screenWidth: int
  • visiblePortion: float

说明:

TODO

public float yPos(int firstSlotIndex) @ L111

  • 方法名:yPos
  • 源码定位:L111
  • 返回类型:float
  • 修饰符:public

参数:

  • firstSlotIndex: int

说明:

TODO

public Toast.Visibility getWantedVisibility() @ L116

  • 方法名:getWantedVisibility
  • 源码定位:L116
  • 返回类型:Toast.Visibility
  • 修饰符:public

参数:

说明:

TODO

public void setWantedVisibility(Toast.Visibility visibility) @ L121

  • 方法名:setWantedVisibility
  • 源码定位:L121
  • 返回类型:void
  • 修饰符:public

参数:

  • visibility: Toast.Visibility

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class NowPlayingToast implements Toast {
    private static final Identifier NOW_PLAYING_BACKGROUND_SPRITE = Identifier.withDefaultNamespace("toast/now_playing");
    private static final Identifier MUSIC_NOTES_SPRITE = Identifier.parse("icon/music_notes");
    private static final int PADDING = 7;
    private static final int MUSIC_NOTES_SIZE = 16;
    private static final int HEIGHT = 30;
    private static final int MUSIC_NOTES_SPACE = 30;
    private static final int VISIBILITY_DURATION = 5000;
    private static final int TEXT_COLOR = DyeColor.LIGHT_GRAY.getTextColor();
    private static final long MUSIC_COLOR_CHANGE_FREQUENCY_MS = 25L;
    private static int musicNoteColorTick;
    private static long lastMusicNoteColorChange;
    private static int musicNoteColor = -1;
    private boolean updateToast;
    private double notificationDisplayTimeMultiplier;
    private final Minecraft minecraft;
    private Toast.Visibility wantedVisibility = Toast.Visibility.HIDE;
 
    public NowPlayingToast() {
        this.minecraft = Minecraft.getInstance();
    }
 
    public static void extractToast(GuiGraphicsExtractor graphics, Font font) {
        String currentSong = getCurrentSongName();
        if (currentSong != null) {
            graphics.blitSprite(RenderPipelines.GUI_TEXTURED, NOW_PLAYING_BACKGROUND_SPRITE, 0, 0, getWidth(currentSong, font), 30);
            int notesOffset = 7;
            graphics.blitSprite(RenderPipelines.GUI_TEXTURED, MUSIC_NOTES_SPRITE, 7, 7, 16, 16, musicNoteColor);
            graphics.text(font, getNowPlayingString(currentSong), 30, 15 - 9 / 2, TEXT_COLOR);
        }
    }
 
    private static @Nullable String getCurrentSongName() {
        return Minecraft.getInstance().getMusicManager().getCurrentMusicTranslationKey();
    }
 
    public static void tickMusicNotes() {
        if (getCurrentSongName() != null) {
            long now = System.currentTimeMillis();
            if (now > lastMusicNoteColorChange + 25L) {
                musicNoteColorTick++;
                lastMusicNoteColorChange = now;
                musicNoteColor = ColorLerper.getLerpedColor(ColorLerper.Type.MUSIC_NOTE, musicNoteColorTick);
            }
        }
    }
 
    private static Component getNowPlayingString(@Nullable String currentSongKey) {
        return currentSongKey == null ? Component.empty() : Component.translatable(currentSongKey.replace("/", "."));
    }
 
    public void showToast(Options options) {
        this.updateToast = true;
        this.notificationDisplayTimeMultiplier = options.notificationDisplayTime().get();
        this.setWantedVisibility(Toast.Visibility.SHOW);
    }
 
    @Override
    public void update(ToastManager manager, long fullyVisibleForMs) {
        if (this.updateToast) {
            this.wantedVisibility = fullyVisibleForMs < 5000.0 * this.notificationDisplayTimeMultiplier ? Toast.Visibility.SHOW : Toast.Visibility.HIDE;
            tickMusicNotes();
        }
    }
 
    @Override
    public void extractRenderState(GuiGraphicsExtractor graphics, Font font, long fullyVisibleForMs) {
        extractToast(graphics, font);
    }
 
    @Override
    public void onFinishedRendering() {
        this.updateToast = false;
    }
 
    @Override
    public int width() {
        return getWidth(getCurrentSongName(), this.minecraft.font);
    }
 
    private static int getWidth(@Nullable String currentSong, Font font) {
        return 30 + font.width(getNowPlayingString(currentSong)) + 7;
    }
 
    @Override
    public int height() {
        return 30;
    }
 
    @Override
    public float xPos(int screenWidth, float visiblePortion) {
        return this.width() * visiblePortion - this.width();
    }
 
    @Override
    public float yPos(int firstSlotIndex) {
        return 0.0F;
    }
 
    @Override
    public Toast.Visibility getWantedVisibility() {
        return this.wantedVisibility;
    }
 
    public void setWantedVisibility(Toast.Visibility visibility) {
        this.wantedVisibility = visibility;
    }
}

引用的其他类

  • Minecraft

    • 引用位置: 字段/方法调用
    • 关联成员: Minecraft.getInstance()
  • Options

    • 引用位置: 参数
  • ColorLerper

    • 引用位置: 方法调用
    • 关联成员: ColorLerper.getLerpedColor()
  • Font

    • 引用位置: 参数
  • GuiGraphicsExtractor

    • 引用位置: 参数
  • Toast

    • 引用位置: 参数/字段/实现/返回值
  • ToastManager

    • 引用位置: 参数
  • LoggedChatMessage

    • 引用位置: 方法调用
    • 关联成员: System.currentTimeMillis()
  • Component

    • 引用位置: 方法调用/返回值
    • 关联成员: Component.empty(), Component.translatable()
  • Identifier

    • 引用位置: 字段/方法调用
    • 关联成员: Identifier.parse(), Identifier.withDefaultNamespace()