| 1 | package net.minecraft.client.gui.components.toasts;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.gui.Font;
|
| 8 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 9 | import net.minecraft.client.renderer.RenderPipelines;
|
| 10 | import net.minecraft.network.chat.Component;
|
| 11 | import net.minecraft.resources.Identifier;
|
| 12 | import net.minecraft.util.FormattedCharSequence;
|
| 13 | import net.minecraft.util.Mth;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | @Environment(EnvType.CLIENT)
|
| 17 | public class TutorialToast implements Toast {
|
| 18 | private static final Identifier BACKGROUND_SPRITE = Identifier.withDefaultNamespace("toast/tutorial");
|
| 19 | public static final int PROGRESS_BAR_WIDTH = 154;
|
| 20 | public static final int PROGRESS_BAR_HEIGHT = 1;
|
| 21 | public static final int PROGRESS_BAR_X = 3;
|
| 22 | public static final int PROGRESS_BAR_MARGIN_BOTTOM = 4;
|
| 23 | private static final int PADDING_TOP = 7;
|
| 24 | private static final int PADDING_BOTTOM = 3;
|
| 25 | private static final int LINE_SPACING = 11;
|
| 26 | private static final int TEXT_LEFT = 30;
|
| 27 | private static final int TEXT_WIDTH = 126;
|
| 28 | private final TutorialToast.Icons icon;
|
| 29 | private final List<FormattedCharSequence> lines;
|
| 30 | private Toast.Visibility visibility = Toast.Visibility.SHOW;
|
| 31 | private long lastSmoothingTime;
|
| 32 | private float smoothedProgress;
|
| 33 | private float progress;
|
| 34 | private final boolean progressable;
|
| 35 | private final int timeToDisplayMs;
|
| 36 |
|
| 37 | public TutorialToast(
|
| 38 | final Font font,
|
| 39 | final TutorialToast.Icons icon,
|
| 40 | final Component title,
|
| 41 | final @Nullable Component message,
|
| 42 | final boolean progressable,
|
| 43 | final int timeToDisplayMs
|
| 44 | ) {
|
| 45 | this.icon = icon;
|
| 46 | this.lines = new ArrayList<>(2);
|
| 47 | this.lines.addAll(font.split(title.copy().withColor(-11534256), 126));
|
| 48 | if (message != null) {
|
| 49 | this.lines.addAll(font.split(message, 126));
|
| 50 | }
|
| 51 |
|
| 52 | this.progressable = progressable;
|
| 53 | this.timeToDisplayMs = timeToDisplayMs;
|
| 54 | }
|
| 55 |
|
| 56 | public TutorialToast(final Font font, final TutorialToast.Icons icon, final Component title, final @Nullable Component message, final boolean progressable) {
|
| 57 | this(font, icon, title, message, progressable, 0);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public Toast.Visibility getWantedVisibility() {
|
| 62 | return this.visibility;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public void update(final ToastManager manager, final long fullyVisibleForMs) {
|
| 67 | if (this.timeToDisplayMs > 0) {
|
| 68 | this.progress = Math.min((float)fullyVisibleForMs / this.timeToDisplayMs, 1.0F);
|
| 69 | this.smoothedProgress = this.progress;
|
| 70 | this.lastSmoothingTime = fullyVisibleForMs;
|
| 71 | if (fullyVisibleForMs > this.timeToDisplayMs) {
|
| 72 | this.hide();
|
| 73 | }
|
| 74 | } else if (this.progressable) {
|
| 75 | this.smoothedProgress = Mth.clampedLerp((float)(fullyVisibleForMs - this.lastSmoothingTime) / 100.0F, this.smoothedProgress, this.progress);
|
| 76 | this.lastSmoothingTime = fullyVisibleForMs;
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public int height() {
|
| 82 | return 7 + this.contentHeight() + 3;
|
| 83 | }
|
| 84 |
|
| 85 | private int contentHeight() {
|
| 86 | return Math.max(this.lines.size(), 2) * 11;
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public void extractRenderState(final GuiGraphicsExtractor graphics, final Font font, final long fullyVisibleForMs) {
|
| 91 | int height = this.height();
|
| 92 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, BACKGROUND_SPRITE, 0, 0, this.width(), height);
|
| 93 | this.icon.extractRenderState(graphics, 6, 6);
|
| 94 | int textHeight = this.lines.size() * 11;
|
| 95 | int textTop = 7 + (this.contentHeight() - textHeight) / 2;
|
| 96 |
|
| 97 | for (int i = 0; i < this.lines.size(); i++) {
|
| 98 | graphics.text(font, this.lines.get(i), 30, textTop + i * 11, -16777216, false);
|
| 99 | }
|
| 100 |
|
| 101 | if (this.progressable) {
|
| 102 | int progressBarY = height - 4;
|
| 103 | graphics.fill(3, progressBarY, 157, progressBarY + 1, -1);
|
| 104 | int col;
|
| 105 | if (this.progress >= this.smoothedProgress) {
|
| 106 | col = -16755456;
|
| 107 | } else {
|
| 108 | col = -11206656;
|
| 109 | }
|
| 110 |
|
| 111 | graphics.fill(3, progressBarY, (int)(3.0F + 154.0F * this.smoothedProgress), progressBarY + 1, col);
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | public void hide() {
|
| 116 | this.visibility = Toast.Visibility.HIDE;
|
| 117 | }
|
| 118 |
|
| 119 | public void updateProgress(final float progress) {
|
| 120 | this.progress = progress;
|
| 121 | }
|
| 122 |
|
| 123 | @Environment(EnvType.CLIENT)
|
| 124 | public enum Icons {
|
| 125 | MOVEMENT_KEYS(Identifier.withDefaultNamespace("toast/movement_keys")),
|
| 126 | MOUSE(Identifier.withDefaultNamespace("toast/mouse")),
|
| 127 | TREE(Identifier.withDefaultNamespace("toast/tree")),
|
| 128 | RECIPE_BOOK(Identifier.withDefaultNamespace("toast/recipe_book")),
|
| 129 | WOODEN_PLANKS(Identifier.withDefaultNamespace("toast/wooden_planks")),
|
| 130 | SOCIAL_INTERACTIONS(Identifier.withDefaultNamespace("toast/social_interactions")),
|
| 131 | RIGHT_CLICK(Identifier.withDefaultNamespace("toast/right_click"));
|
| 132 |
|
| 133 | private final Identifier sprite;
|
| 134 |
|
| 135 | Icons(final Identifier sprite) {
|
| 136 | this.sprite = sprite;
|
| 137 | }
|
| 138 |
|
| 139 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int x, final int y) {
|
| 140 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, this.sprite, x, y, 20, 20);
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 |
|