| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.blaze3d.audio.ListenerTransform;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.Comparator;
|
| 7 | import java.util.List;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.client.Minecraft;
|
| 11 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 12 | import net.minecraft.client.resources.sounds.SoundInstance;
|
| 13 | import net.minecraft.client.sounds.SoundEventListener;
|
| 14 | import net.minecraft.client.sounds.SoundManager;
|
| 15 | import net.minecraft.client.sounds.WeighedSoundEvents;
|
| 16 | import net.minecraft.network.chat.Component;
|
| 17 | import net.minecraft.util.ARGB;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.util.Util;
|
| 20 | import net.minecraft.world.phys.Vec3;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class SubtitleOverlay implements SoundEventListener {
|
| 25 | private static final int LOOPING_DISPLAY_FULL_BRIGHTNESS_TIME = 500;
|
| 26 | private static final long DISPLAY_TIME = 3000L;
|
| 27 | private final Minecraft minecraft;
|
| 28 | private final List<SubtitleOverlay.Subtitle> subtitles = Lists.newArrayList();
|
| 29 | private boolean isListening;
|
| 30 | private final List<SubtitleOverlay.Subtitle> audibleSubtitles = new ArrayList<>();
|
| 31 |
|
| 32 | public SubtitleOverlay(final Minecraft minecraft) {
|
| 33 | this.minecraft = minecraft;
|
| 34 | }
|
| 35 |
|
| 36 | public void extractRenderState(final GuiGraphicsExtractor graphics) {
|
| 37 | SoundManager soundManager = this.minecraft.getSoundManager();
|
| 38 | if (!this.isListening && this.minecraft.options.showSubtitles().get()) {
|
| 39 | soundManager.addListener(this);
|
| 40 | this.isListening = true;
|
| 41 | } else if (this.isListening && !this.minecraft.options.showSubtitles().get()) {
|
| 42 | soundManager.removeListener(this);
|
| 43 | this.isListening = false;
|
| 44 | }
|
| 45 |
|
| 46 | if (this.isListening) {
|
| 47 | ListenerTransform listener = soundManager.getListenerTransform();
|
| 48 | Vec3 position = listener.position();
|
| 49 | Vec3 forwards = listener.forward();
|
| 50 | Vec3 right = listener.right();
|
| 51 | double displayTimeMultiplier = this.minecraft.options.notificationDisplayTime().get();
|
| 52 | this.audibleSubtitles.clear();
|
| 53 | int width = 0;
|
| 54 |
|
| 55 | for (SubtitleOverlay.Subtitle subtitle : this.subtitles) {
|
| 56 | if (!subtitle.playedAt.isEmpty()) {
|
| 57 | subtitle.purgeOldInstances(3000.0 * displayTimeMultiplier);
|
| 58 | }
|
| 59 |
|
| 60 | if (subtitle.isAudibleFrom(position) && subtitle.isStillActive()) {
|
| 61 | this.audibleSubtitles.add(subtitle);
|
| 62 | width = Math.max(width, this.minecraft.font.width(subtitle.getText()));
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | if (!this.audibleSubtitles.isEmpty()) {
|
| 67 | width += this.minecraft.font.width("<") + this.minecraft.font.width(" ") + this.minecraft.font.width(">") + this.minecraft.font.width(" ");
|
| 68 | if (!this.audibleSubtitles.isEmpty()) {
|
| 69 | graphics.nextStratum();
|
| 70 | }
|
| 71 |
|
| 72 | int row = 0;
|
| 73 |
|
| 74 | for (SubtitleOverlay.Subtitle subtitle : this.audibleSubtitles) {
|
| 75 | int alpha = 255;
|
| 76 | Component text = subtitle.getText();
|
| 77 | SubtitleOverlay.SoundPlayedAt closestRecentLocation = subtitle.getBestSubtitleCandidate(position);
|
| 78 | if (closestRecentLocation != null) {
|
| 79 | Vec3 delta = closestRecentLocation.location.subtract(position).normalize();
|
| 80 | double rightness = right.dot(delta);
|
| 81 | double forwardness = forwards.dot(delta);
|
| 82 | boolean inView = forwardness > 0.5;
|
| 83 | int halfWidth = width / 2;
|
| 84 | int height = 9;
|
| 85 | int halfHeight = height / 2;
|
| 86 | float scale = 1.0F;
|
| 87 | int textWidth = this.minecraft.font.width(text);
|
| 88 | int brightness = subtitle.getBrightness(closestRecentLocation, displayTimeMultiplier);
|
| 89 | graphics.pose().pushMatrix();
|
| 90 | graphics.pose().translate(graphics.guiWidth() - halfWidth * 1.0F - 2.0F, graphics.guiHeight() - 35 - row * (height + 1) * 1.0F);
|
| 91 | graphics.pose().scale(1.0F, 1.0F);
|
| 92 | graphics.fill(-halfWidth - 1, -halfHeight - 1, halfWidth + 1, halfHeight + 1, this.minecraft.options.getBackgroundColor(0.8F));
|
| 93 | int textColor = ARGB.color(255, brightness, brightness, brightness);
|
| 94 | if (!inView) {
|
| 95 | if (rightness > 0.0) {
|
| 96 | graphics.text(this.minecraft.font, ">", halfWidth - this.minecraft.font.width(">"), -halfHeight, textColor);
|
| 97 | } else if (rightness < 0.0) {
|
| 98 | graphics.text(this.minecraft.font, "<", -halfWidth, -halfHeight, textColor);
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | graphics.text(this.minecraft.font, text, -textWidth / 2, -halfHeight, textColor);
|
| 103 | graphics.pose().popMatrix();
|
| 104 | row++;
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public void onPlaySound(final SoundInstance sound, final WeighedSoundEvents soundEvent, final float range) {
|
| 113 | if (soundEvent.getSubtitle() != null) {
|
| 114 | Component text = soundEvent.getSubtitle();
|
| 115 | if (!this.subtitles.isEmpty()) {
|
| 116 | for (SubtitleOverlay.Subtitle subtitle : this.subtitles) {
|
| 117 | if (subtitle.getText().equals(text)) {
|
| 118 | subtitle.refresh(new Vec3(sound.getX(), sound.getY(), sound.getZ()));
|
| 119 | return;
|
| 120 | }
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | this.subtitles.add(new SubtitleOverlay.Subtitle(text, range, new Vec3(sound.getX(), sound.getY(), sound.getZ()), sound.isLooping()));
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | @Environment(EnvType.CLIENT)
|
| 129 | private record SoundPlayedAt(Vec3 location, long time) {
|
| 130 | }
|
| 131 |
|
| 132 | @Environment(EnvType.CLIENT)
|
| 133 | private static class Subtitle {
|
| 134 | private final Component text;
|
| 135 | private final float range;
|
| 136 | private final List<SubtitleOverlay.SoundPlayedAt> playedAt = new ArrayList<>();
|
| 137 | private final boolean looping;
|
| 138 |
|
| 139 | public Subtitle(final Component text, final float range, final Vec3 location, final boolean looping) {
|
| 140 | this.text = text;
|
| 141 | this.range = range;
|
| 142 | this.looping = looping;
|
| 143 | this.playedAt.add(new SubtitleOverlay.SoundPlayedAt(location, Util.getMillis()));
|
| 144 | }
|
| 145 |
|
| 146 | private int getBrightness(final SubtitleOverlay.SoundPlayedAt closestRecentLocation, final double displayTimeMultiplier) {
|
| 147 | int displayFullBrightnessTime = this.looping ? 500 : 0;
|
| 148 | return Mth.floor(
|
| 149 | Mth.clampedLerp(
|
| 150 | (float)Math.max(0L, Util.getMillis() - closestRecentLocation.time - displayFullBrightnessTime)
|
| 151 | / (float)((3000L - displayFullBrightnessTime) * displayTimeMultiplier),
|
| 152 | 255.0F,
|
| 153 | 75.0F
|
| 154 | )
|
| 155 | );
|
| 156 | }
|
| 157 |
|
| 158 | public Component getText() {
|
| 159 | return this.text;
|
| 160 | }
|
| 161 |
|
| 162 | public SubtitleOverlay.@Nullable SoundPlayedAt getBestSubtitleCandidate(final Vec3 position) {
|
| 163 | if (this.playedAt.isEmpty()) {
|
| 164 | return null;
|
| 165 | } else {
|
| 166 | return this.playedAt.size() == 1
|
| 167 | ? this.playedAt.getFirst()
|
| 168 | : this.playedAt
|
| 169 | .stream()
|
| 170 | .filter(soundPlayedAt -> soundPlayedAt.location.closerThan(position, this.range))
|
| 171 | .max(Comparator.comparingLong(SubtitleOverlay.SoundPlayedAt::time).thenComparingDouble(sound -> sound.location.distanceTo(position)))
|
| 172 | .orElse(null);
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | public void refresh(final Vec3 location) {
|
| 177 | this.playedAt.removeIf(soundPlayedAt -> location.equals(soundPlayedAt.location()));
|
| 178 | this.playedAt.add(new SubtitleOverlay.SoundPlayedAt(location, Util.getMillis()));
|
| 179 | }
|
| 180 |
|
| 181 | public boolean isAudibleFrom(final Vec3 camera) {
|
| 182 | if (Float.isInfinite(this.range)) {
|
| 183 | return true;
|
| 184 | }
|
| 185 |
|
| 186 | if (this.playedAt.isEmpty()) {
|
| 187 | return false;
|
| 188 | }
|
| 189 |
|
| 190 | SubtitleOverlay.SoundPlayedAt closest = this.getBestSubtitleCandidate(camera);
|
| 191 | return closest == null ? false : camera.closerThan(closest.location, this.range);
|
| 192 | }
|
| 193 |
|
| 194 | public void purgeOldInstances(final double maxAge) {
|
| 195 | long currentTime = Util.getMillis();
|
| 196 | this.playedAt.removeIf(soundPlayedAt -> currentTime - soundPlayedAt.time() > maxAge);
|
| 197 | }
|
| 198 |
|
| 199 | public boolean isStillActive() {
|
| 200 | return !this.playedAt.isEmpty();
|
| 201 | }
|
| 202 | }
|
| 203 | }
|
| 204 |
|