| 1 | package net.minecraft.client.gui.components.toasts;
|
| 2 |
|
| 3 | import com.google.common.collect.Queues;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.BitSet;
|
| 6 | import java.util.Deque;
|
| 7 | import java.util.HashSet;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Set;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.client.Minecraft;
|
| 13 | import net.minecraft.client.MusicToastDisplayState;
|
| 14 | import net.minecraft.client.Options;
|
| 15 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 16 | import net.minecraft.client.gui.screens.PauseScreen;
|
| 17 | import net.minecraft.client.resources.sounds.SimpleSoundInstance;
|
| 18 | import net.minecraft.sounds.SoundEvent;
|
| 19 | import net.minecraft.sounds.SoundSource;
|
| 20 | import net.minecraft.util.Mth;
|
| 21 | import net.minecraft.util.Util;
|
| 22 | import org.apache.commons.lang3.mutable.MutableBoolean;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | @Environment(EnvType.CLIENT)
|
| 26 | public class ToastManager {
|
| 27 | private static final int SLOT_COUNT = 5;
|
| 28 | private static final int ALL_SLOTS_OCCUPIED = -1;
|
| 29 | private final Minecraft minecraft;
|
| 30 | private final List<ToastManager.ToastInstance<?>> visibleToasts = new ArrayList<>();
|
| 31 | private final BitSet occupiedSlots = new BitSet(5);
|
| 32 | private final Deque<Toast> queued = Queues.newArrayDeque();
|
| 33 | private final Set<SoundEvent> playedToastSounds = new HashSet<>();
|
| 34 | private ToastManager.@Nullable ToastInstance<NowPlayingToast> nowPlayingToast;
|
| 35 |
|
| 36 | public ToastManager(final Minecraft minecraft, final Options options) {
|
| 37 | this.minecraft = minecraft;
|
| 38 | this.initializeMusicToast(options.musicToast().get());
|
| 39 | }
|
| 40 |
|
| 41 | public void update() {
|
| 42 | MutableBoolean visibilityChangeSoundPlayed = new MutableBoolean(false);
|
| 43 | this.visibleToasts.removeIf(toast -> {
|
| 44 | Toast.Visibility previousVisibility = toast.visibility;
|
| 45 | toast.update();
|
| 46 | if (toast.visibility != previousVisibility && visibilityChangeSoundPlayed.isFalse()) {
|
| 47 | visibilityChangeSoundPlayed.setTrue();
|
| 48 | toast.visibility.playSound(this.minecraft.getSoundManager());
|
| 49 | }
|
| 50 |
|
| 51 | if (toast.hasFinishedRendering()) {
|
| 52 | this.occupiedSlots.clear(toast.firstSlotIndex, toast.firstSlotIndex + toast.occupiedSlotCount);
|
| 53 | return true;
|
| 54 | } else {
|
| 55 | return false;
|
| 56 | }
|
| 57 | });
|
| 58 | if (!this.queued.isEmpty() && this.freeSlotCount() > 0) {
|
| 59 | this.queued.removeIf(toast -> {
|
| 60 | int occcupiedSlotCount = toast.occcupiedSlotCount();
|
| 61 | int firstSlotIndex = this.findFreeSlotsIndex(occcupiedSlotCount);
|
| 62 | if (firstSlotIndex == -1) {
|
| 63 | return false;
|
| 64 | }
|
| 65 |
|
| 66 | this.visibleToasts.add(new ToastManager.ToastInstance<>(toast, firstSlotIndex, occcupiedSlotCount));
|
| 67 | this.occupiedSlots.set(firstSlotIndex, firstSlotIndex + occcupiedSlotCount);
|
| 68 | SoundEvent toastSound = toast.getSoundEvent();
|
| 69 | if (toastSound != null && this.playedToastSounds.add(toastSound)) {
|
| 70 | this.minecraft.getSoundManager().play(SimpleSoundInstance.forUI(toastSound, 1.0F, 1.0F));
|
| 71 | }
|
| 72 |
|
| 73 | return true;
|
| 74 | });
|
| 75 | }
|
| 76 |
|
| 77 | this.playedToastSounds.clear();
|
| 78 | if (this.nowPlayingToast != null) {
|
| 79 | this.nowPlayingToast.update();
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | public void extractRenderState(final GuiGraphicsExtractor graphics) {
|
| 84 | if (!this.minecraft.gui.hud.isHidden()) {
|
| 85 | int screenWidth = graphics.guiWidth();
|
| 86 | if (!this.visibleToasts.isEmpty()) {
|
| 87 | graphics.nextStratum();
|
| 88 | }
|
| 89 |
|
| 90 | for (ToastManager.ToastInstance<?> toast : this.visibleToasts) {
|
| 91 | toast.extractRenderState(graphics, screenWidth);
|
| 92 | }
|
| 93 |
|
| 94 | if (this.minecraft.options.musicToast().get().renderToast()
|
| 95 | && this.nowPlayingToast != null
|
| 96 | && (this.minecraft.gui.screen() == null || !(this.minecraft.gui.screen() instanceof PauseScreen))) {
|
| 97 | this.nowPlayingToast.extractRenderState(graphics, screenWidth);
|
| 98 | }
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | private int findFreeSlotsIndex(final int requiredCount) {
|
| 103 | if (this.freeSlotCount() >= requiredCount) {
|
| 104 | int consecutiveFreeSlotCount = 0;
|
| 105 |
|
| 106 | for (int i = 0; i < 5; i++) {
|
| 107 | if (this.occupiedSlots.get(i)) {
|
| 108 | consecutiveFreeSlotCount = 0;
|
| 109 | } else if (++consecutiveFreeSlotCount == requiredCount) {
|
| 110 | return i + 1 - consecutiveFreeSlotCount;
|
| 111 | }
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | return -1;
|
| 116 | }
|
| 117 |
|
| 118 | private int freeSlotCount() {
|
| 119 | return 5 - this.occupiedSlots.cardinality();
|
| 120 | }
|
| 121 |
|
| 122 | public <T extends Toast> @Nullable T getToast(final Class<? extends T> clazz, final Object token) {
|
| 123 | for (ToastManager.ToastInstance<?> instance : this.visibleToasts) {
|
| 124 | if (clazz.isAssignableFrom(instance.getToast().getClass()) && instance.getToast().getToken().equals(token)) {
|
| 125 | return (T)instance.getToast();
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | for (Toast toast : this.queued) {
|
| 130 | if (clazz.isAssignableFrom(toast.getClass()) && toast.getToken().equals(token)) {
|
| 131 | return (T)toast;
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | return null;
|
| 136 | }
|
| 137 |
|
| 138 | public void clear() {
|
| 139 | this.occupiedSlots.clear();
|
| 140 | this.visibleToasts.clear();
|
| 141 | this.queued.clear();
|
| 142 | }
|
| 143 |
|
| 144 | public void addToast(final Toast toast) {
|
| 145 | this.queued.add(toast);
|
| 146 | }
|
| 147 |
|
| 148 | public void showNowPlayingToast() {
|
| 149 | if (this.nowPlayingToast != null) {
|
| 150 | this.nowPlayingToast.resetToast();
|
| 151 | this.nowPlayingToast.getToast().showToast(this.minecraft.options);
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | public void hideNowPlayingToast() {
|
| 156 | if (this.nowPlayingToast != null) {
|
| 157 | this.nowPlayingToast.getToast().setWantedVisibility(Toast.Visibility.HIDE);
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | public Minecraft getMinecraft() {
|
| 162 | return this.minecraft;
|
| 163 | }
|
| 164 |
|
| 165 | public double getNotificationDisplayTimeMultiplier() {
|
| 166 | return this.minecraft.options.notificationDisplayTime().get();
|
| 167 | }
|
| 168 |
|
| 169 | private void initializeMusicToast(final MusicToastDisplayState state) {
|
| 170 | switch (state) {
|
| 171 | case PAUSE:
|
| 172 | case PAUSE_AND_TOAST:
|
| 173 | this.nowPlayingToast = new ToastManager.ToastInstance<>(new NowPlayingToast(), 0, 0);
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | public void setMusicToastDisplayState(final MusicToastDisplayState state) {
|
| 178 | switch (state) {
|
| 179 | case PAUSE:
|
| 180 | this.nowPlayingToast = new ToastManager.ToastInstance<>(new NowPlayingToast(), 0, 0);
|
| 181 | break;
|
| 182 | case PAUSE_AND_TOAST:
|
| 183 | this.nowPlayingToast = new ToastManager.ToastInstance<>(new NowPlayingToast(), 0, 0);
|
| 184 | if (this.minecraft.options.getFinalSoundSourceVolume(SoundSource.MUSIC) > 0.0F) {
|
| 185 | this.nowPlayingToast.getToast().showToast(this.minecraft.options);
|
| 186 | }
|
| 187 | break;
|
| 188 | case NEVER:
|
| 189 | this.nowPlayingToast = null;
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | @Environment(EnvType.CLIENT)
|
| 194 | private class ToastInstance<T extends Toast> {
|
| 195 | private static final long SLIDE_ANIMATION_DURATION_MS = 600L;
|
| 196 | private final T toast;
|
| 197 | private final int firstSlotIndex;
|
| 198 | private final int occupiedSlotCount;
|
| 199 | private long animationStartTime;
|
| 200 | private long becameFullyVisibleAt;
|
| 201 | private Toast.Visibility visibility;
|
| 202 | private long fullyVisibleFor;
|
| 203 | private float visiblePortion;
|
| 204 | protected boolean hasFinishedRendering;
|
| 205 |
|
| 206 | private ToastInstance(final T toast, final int firstSlotIndex, final int occupiedSlotCount) {
|
| 207 | this.toast = toast;
|
| 208 | this.firstSlotIndex = firstSlotIndex;
|
| 209 | this.occupiedSlotCount = occupiedSlotCount;
|
| 210 | this.resetToast();
|
| 211 | }
|
| 212 |
|
| 213 | public T getToast() {
|
| 214 | return this.toast;
|
| 215 | }
|
| 216 |
|
| 217 | public void resetToast() {
|
| 218 | this.animationStartTime = -1L;
|
| 219 | this.becameFullyVisibleAt = -1L;
|
| 220 | this.visibility = Toast.Visibility.HIDE;
|
| 221 | this.fullyVisibleFor = 0L;
|
| 222 | this.visiblePortion = 0.0F;
|
| 223 | this.hasFinishedRendering = false;
|
| 224 | }
|
| 225 |
|
| 226 | public boolean hasFinishedRendering() {
|
| 227 | return this.hasFinishedRendering;
|
| 228 | }
|
| 229 |
|
| 230 | private void calculateVisiblePortion(final long now) {
|
| 231 | float animationProgress = Mth.clamp((float)(now - this.animationStartTime) / 600.0F, 0.0F, 1.0F);
|
| 232 | animationProgress *= animationProgress;
|
| 233 | if (this.visibility == Toast.Visibility.HIDE) {
|
| 234 | this.visiblePortion = 1.0F - animationProgress;
|
| 235 | } else {
|
| 236 | this.visiblePortion = animationProgress;
|
| 237 | }
|
| 238 | }
|
| 239 |
|
| 240 | public void update() {
|
| 241 | long now = Util.getMillis();
|
| 242 | if (this.animationStartTime == -1L) {
|
| 243 | this.animationStartTime = now;
|
| 244 | this.visibility = Toast.Visibility.SHOW;
|
| 245 | }
|
| 246 |
|
| 247 | if (this.visibility == Toast.Visibility.SHOW && now - this.animationStartTime <= 600L) {
|
| 248 | this.becameFullyVisibleAt = now;
|
| 249 | }
|
| 250 |
|
| 251 | this.fullyVisibleFor = now - this.becameFullyVisibleAt;
|
| 252 | this.calculateVisiblePortion(now);
|
| 253 | this.toast.update(ToastManager.this, this.fullyVisibleFor);
|
| 254 | Toast.Visibility wantedVisibility = this.toast.getWantedVisibility();
|
| 255 | if (wantedVisibility != this.visibility) {
|
| 256 | this.animationStartTime = now - (int)((1.0F - this.visiblePortion) * 600.0F);
|
| 257 | this.visibility = wantedVisibility;
|
| 258 | }
|
| 259 |
|
| 260 | boolean wasAlreadyFinishedRendering = this.hasFinishedRendering;
|
| 261 | this.hasFinishedRendering = this.visibility == Toast.Visibility.HIDE && now - this.animationStartTime > 600L;
|
| 262 | if (this.hasFinishedRendering && !wasAlreadyFinishedRendering) {
|
| 263 | this.toast.onFinishedRendering();
|
| 264 | }
|
| 265 | }
|
| 266 |
|
| 267 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int screenWidth) {
|
| 268 | if (!this.hasFinishedRendering) {
|
| 269 | graphics.pose().pushMatrix();
|
| 270 | graphics.pose().translate(this.toast.xPos(screenWidth, this.visiblePortion), this.toast.yPos(this.firstSlotIndex));
|
| 271 | this.toast.extractRenderState(graphics, ToastManager.this.minecraft.font, this.fullyVisibleFor);
|
| 272 | graphics.pose().popMatrix();
|
| 273 | }
|
| 274 | }
|
| 275 | }
|
| 276 | }
|
| 277 |
|