| 1 | package net.minecraft.client.gui.screens.options;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.mojang.blaze3d.platform.MacosUtil;
|
| 6 | import com.mojang.blaze3d.platform.Monitor;
|
| 7 | import com.mojang.blaze3d.platform.VideoMode;
|
| 8 | import com.mojang.blaze3d.platform.Window;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Optional;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import net.minecraft.ChatFormatting;
|
| 14 | import net.minecraft.client.Minecraft;
|
| 15 | import net.minecraft.client.OptionInstance;
|
| 16 | import net.minecraft.client.Options;
|
| 17 | import net.minecraft.client.TextureFilteringMethod;
|
| 18 | import net.minecraft.client.gui.components.AbstractSliderButton;
|
| 19 | import net.minecraft.client.gui.components.AbstractWidget;
|
| 20 | import net.minecraft.client.gui.components.CycleButton;
|
| 21 | import net.minecraft.client.gui.components.StringWidget;
|
| 22 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 23 | import net.minecraft.client.gui.screens.Screen;
|
| 24 | import net.minecraft.client.input.MouseButtonEvent;
|
| 25 | import net.minecraft.client.renderer.GpuWarnlistManager;
|
| 26 | import net.minecraft.network.chat.CommonComponents;
|
| 27 | import net.minecraft.network.chat.Component;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public class VideoSettingsScreen extends OptionsSubScreen {
|
| 32 | private static final Component TITLE = Component.translatable("options.videoTitle");
|
| 33 | private static final Component IMPROVED_TRANSPARENCY = Component.translatable("options.improvedTransparency").withStyle(ChatFormatting.ITALIC);
|
| 34 | private static final Component WARNING_MESSAGE = Component.translatable("options.graphics.warning.message", IMPROVED_TRANSPARENCY, IMPROVED_TRANSPARENCY);
|
| 35 | private static final Component WARNING_TITLE = Component.translatable("options.graphics.warning.title").withStyle(ChatFormatting.RED);
|
| 36 | private static final Component BUTTON_ACCEPT = Component.translatable("options.graphics.warning.accept");
|
| 37 | private static final Component BUTTON_CANCEL = Component.translatable("options.graphics.warning.cancel");
|
| 38 | private static final Component DISPLAY_HEADER = Component.translatable("options.video.display.header")
|
| 39 | .withStyle(ChatFormatting.UNDERLINE, ChatFormatting.BOLD);
|
| 40 | private static final Component QUALITY_HEADER = Component.translatable("options.video.quality.header")
|
| 41 | .withStyle(ChatFormatting.UNDERLINE, ChatFormatting.BOLD);
|
| 42 | private static final Component PREFERENCES_HEADER = Component.translatable("options.video.preferences.header")
|
| 43 | .withStyle(ChatFormatting.UNDERLINE, ChatFormatting.BOLD);
|
| 44 | private static final Component RESTART_REQUIRED = Component.translatable("options.restartRequired").withColor(-2142128);
|
| 45 | private static final Component EXCLUSIVE_FULLSCREEN_MODE_TOOLTIP = Component.translatable("options.fullscreen.exclusive.mode.tooltip");
|
| 46 | private final GpuWarnlistManager gpuWarnlistManager;
|
| 47 | private final int oldMipmaps;
|
| 48 | private final int oldAnisotropyBit;
|
| 49 | private final TextureFilteringMethod oldTextureFiltering;
|
| 50 | private final LinearLayout header = LinearLayout.vertical().spacing(2);
|
| 51 | private @Nullable StringWidget restartWarning;
|
| 52 | private @Nullable Monitor fullscreenModeMonitor;
|
| 53 |
|
| 54 | private static OptionInstance<?>[] qualityOptions(final Options options) {
|
| 55 | return new OptionInstance[]{
|
| 56 | options.biomeBlendRadius(),
|
| 57 | options.renderDistance(),
|
| 58 | options.prioritizeChunkUpdates(),
|
| 59 | options.simulationDistance(),
|
| 60 | options.ambientOcclusion(),
|
| 61 | options.cloudStatus(),
|
| 62 | options.particles(),
|
| 63 | options.mipmapLevels(),
|
| 64 | options.entityShadows(),
|
| 65 | options.entityDistanceScaling(),
|
| 66 | options.menuBackgroundBlurriness(),
|
| 67 | options.cloudRange(),
|
| 68 | options.cutoutLeaves(),
|
| 69 | options.improvedTransparency(),
|
| 70 | options.textureFiltering(),
|
| 71 | options.maxAnisotropyBit(),
|
| 72 | options.weatherRadius()
|
| 73 | };
|
| 74 | }
|
| 75 |
|
| 76 | private static OptionInstance<?>[] displayOptions(final Options options) {
|
| 77 | List<OptionInstance<?>> result = Lists.newArrayList(
|
| 78 | options.framerateLimit(),
|
| 79 | options.enableVsync(),
|
| 80 | options.inactivityFpsLimit(),
|
| 81 | options.guiScale(),
|
| 82 | options.fullscreen(),
|
| 83 | options.exclusiveFullscreen(),
|
| 84 | options.gamma(),
|
| 85 | options.preferredGraphicsBackend()
|
| 86 | );
|
| 87 | if (MacosUtil.IS_MACOS) {
|
| 88 | result.add(options.macFullscreenMenuVisibility());
|
| 89 | }
|
| 90 |
|
| 91 | return result.toArray(OptionInstance[]::new);
|
| 92 | }
|
| 93 |
|
| 94 | private static OptionInstance<?>[] preferenceOptions(final Options options) {
|
| 95 | return new OptionInstance[]{options.showAutosaveIndicator(), options.vignette(), options.attackIndicator(), options.chunkSectionFadeInTime()};
|
| 96 | }
|
| 97 |
|
| 98 | public VideoSettingsScreen(final Screen lastScreen, final Minecraft minecraft, final Options options) {
|
| 99 | super(lastScreen, options, TITLE);
|
| 100 | this.gpuWarnlistManager = minecraft.getGpuWarnlistManager();
|
| 101 | this.gpuWarnlistManager.resetWarnings();
|
| 102 | this.oldMipmaps = options.mipmapLevels().get();
|
| 103 | this.oldAnisotropyBit = options.maxAnisotropyBit().get();
|
| 104 | this.oldTextureFiltering = options.textureFiltering().get();
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | protected void addOptions() {
|
| 109 | Window window = this.minecraft.getWindow();
|
| 110 | this.list.addHeader(DISPLAY_HEADER);
|
| 111 | this.fullscreenModeMonitor = window.findBestMonitor();
|
| 112 | int initialValue;
|
| 113 | if (this.fullscreenModeMonitor == null) {
|
| 114 | initialValue = -1;
|
| 115 | } else {
|
| 116 | initialValue = window.getPreferredFullscreenVideoMode().map(this.fullscreenModeMonitor::indexOfMode).orElse(-1);
|
| 117 | }
|
| 118 |
|
| 119 | OptionInstance<Integer> exclusiveFullscreenModeOption = new OptionInstance<>(
|
| 120 | "options.fullscreen.exclusive.mode",
|
| 121 | OptionInstance.cachedConstantTooltip(EXCLUSIVE_FULLSCREEN_MODE_TOOLTIP),
|
| 122 | (caption, value) -> {
|
| 123 | if (this.fullscreenModeMonitor == null) {
|
| 124 | return Component.translatable("options.fullscreen.unavailable");
|
| 125 | }
|
| 126 |
|
| 127 | if (value == -1) {
|
| 128 | return Options.genericValueLabel(caption, Component.translatable("options.fullscreen.current"));
|
| 129 | }
|
| 130 |
|
| 131 | VideoMode mode = this.fullscreenModeMonitor.mode(value);
|
| 132 | return Options.genericValueLabel(
|
| 133 | caption,
|
| 134 | Component.translatable(
|
| 135 | "options.fullscreen.entry", mode.getWidth(), mode.getHeight(), mode.refreshRateLabel(), mode.getRedBits() + mode.getGreenBits() + mode.getBlueBits()
|
| 136 | )
|
| 137 | );
|
| 138 | },
|
| 139 | new OptionInstance.IntRange(-1, this.fullscreenModeMonitor != null ? this.fullscreenModeMonitor.modeCount() - 1 : -1),
|
| 140 | initialValue,
|
| 141 | value -> {
|
| 142 | if (this.fullscreenModeMonitor != null) {
|
| 143 | window.setPreferredFullscreenVideoMode(value == -1 ? Optional.empty() : Optional.of(this.fullscreenModeMonitor.mode(value)));
|
| 144 | }
|
| 145 | }
|
| 146 | );
|
| 147 | this.list.addBig(exclusiveFullscreenModeOption);
|
| 148 | this.list.addSmall(displayOptions(this.options));
|
| 149 | this.list.addHeader(QUALITY_HEADER);
|
| 150 | this.list.addBig(this.options.graphicsPreset());
|
| 151 | this.list.addSmall(qualityOptions(this.options));
|
| 152 | this.list.addHeader(PREFERENCES_HEADER);
|
| 153 | this.list.addSmall(preferenceOptions(this.options));
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | protected void addTitle() {
|
| 158 | this.header.defaultCellSetting().alignHorizontallyCenter().alignVerticallyMiddle();
|
| 159 | this.header.addChild(new StringWidget(this.title, this.font));
|
| 160 | if (this.options.isRestartRequiredToApplyVideoSettings()) {
|
| 161 | this.restartWarning = new StringWidget(RESTART_REQUIRED, this.font);
|
| 162 | this.header.addChild(this.restartWarning);
|
| 163 | }
|
| 164 |
|
| 165 | this.layout.addToHeader(this.header);
|
| 166 | }
|
| 167 |
|
| 168 | @Override
|
| 169 | public void tick() {
|
| 170 | if (this.list != null && this.list.findOption(this.options.maxAnisotropyBit()) instanceof AbstractSliderButton maxAnisotropy) {
|
| 171 | maxAnisotropy.active = this.options.textureFiltering().get() == TextureFilteringMethod.ANISOTROPIC;
|
| 172 | }
|
| 173 |
|
| 174 | boolean restartRequired = this.options.isRestartRequiredToApplyVideoSettings();
|
| 175 | if (restartRequired && (this.restartWarning == null || !this.restartWarning.visible)) {
|
| 176 | if (this.restartWarning == null) {
|
| 177 | this.restartWarning = new StringWidget(RESTART_REQUIRED, this.font);
|
| 178 | this.header.addChild(this.restartWarning);
|
| 179 | this.addRenderableWidget(this.restartWarning);
|
| 180 | }
|
| 181 |
|
| 182 | this.restartWarning.visible = true;
|
| 183 | this.repositionElements();
|
| 184 | } else if (!restartRequired && this.restartWarning != null && this.restartWarning.visible) {
|
| 185 | this.restartWarning.visible = false;
|
| 186 | this.repositionElements();
|
| 187 | }
|
| 188 |
|
| 189 | super.tick();
|
| 190 | }
|
| 191 |
|
| 192 | @Override
|
| 193 | public void onClose() {
|
| 194 | this.minecraft.getWindow().changeFullscreenVideoMode();
|
| 195 | super.onClose();
|
| 196 | }
|
| 197 |
|
| 198 | @Override
|
| 199 | public void removed() {
|
| 200 | if (this.options.mipmapLevels().get() != this.oldMipmaps
|
| 201 | || this.options.maxAnisotropyBit().get() != this.oldAnisotropyBit
|
| 202 | || this.options.textureFiltering().get() != this.oldTextureFiltering) {
|
| 203 | this.minecraft.updateMaxMipLevel(this.options.mipmapLevels().get());
|
| 204 | this.minecraft.delayTextureReload();
|
| 205 | }
|
| 206 |
|
| 207 | super.removed();
|
| 208 | }
|
| 209 |
|
| 210 | @Override
|
| 211 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 212 | if (super.mouseClicked(event, doubleClick)) {
|
| 213 | if (this.gpuWarnlistManager.isShowingWarning()) {
|
| 214 | List<Component> warningMessage = Lists.newArrayList(WARNING_MESSAGE, CommonComponents.NEW_LINE);
|
| 215 | String rendererWarnings = this.gpuWarnlistManager.getRendererWarnings();
|
| 216 | if (rendererWarnings != null) {
|
| 217 | warningMessage.add(CommonComponents.NEW_LINE);
|
| 218 | warningMessage.add(Component.translatable("options.graphics.warning.renderer", rendererWarnings).withStyle(ChatFormatting.GRAY));
|
| 219 | }
|
| 220 |
|
| 221 | String vendorWarnings = this.gpuWarnlistManager.getVendorWarnings();
|
| 222 | if (vendorWarnings != null) {
|
| 223 | warningMessage.add(CommonComponents.NEW_LINE);
|
| 224 | warningMessage.add(Component.translatable("options.graphics.warning.vendor", vendorWarnings).withStyle(ChatFormatting.GRAY));
|
| 225 | }
|
| 226 |
|
| 227 | String versionWarnings = this.gpuWarnlistManager.getVersionWarnings();
|
| 228 | if (versionWarnings != null) {
|
| 229 | warningMessage.add(CommonComponents.NEW_LINE);
|
| 230 | warningMessage.add(Component.translatable("options.graphics.warning.version", versionWarnings).withStyle(ChatFormatting.GRAY));
|
| 231 | }
|
| 232 |
|
| 233 | this.minecraft
|
| 234 | .gui
|
| 235 | .setScreen(
|
| 236 | new UnsupportedGraphicsWarningScreen(
|
| 237 | WARNING_TITLE, warningMessage, ImmutableList.of(new UnsupportedGraphicsWarningScreen.ButtonOption(BUTTON_ACCEPT, btn -> {
|
| 238 | this.options.improvedTransparency().set(true);
|
| 239 | Minecraft.getInstance().levelExtractor.allChanged();
|
| 240 | this.gpuWarnlistManager.dismissWarning();
|
| 241 | this.minecraft.gui.setScreen(this);
|
| 242 | }), new UnsupportedGraphicsWarningScreen.ButtonOption(BUTTON_CANCEL, btn -> {
|
| 243 | this.gpuWarnlistManager.dismissWarning();
|
| 244 | this.options.improvedTransparency().set(false);
|
| 245 | this.updateTransparencyButton();
|
| 246 | this.minecraft.gui.setScreen(this);
|
| 247 | }))
|
| 248 | )
|
| 249 | );
|
| 250 | }
|
| 251 |
|
| 252 | return true;
|
| 253 | } else {
|
| 254 | return false;
|
| 255 | }
|
| 256 | }
|
| 257 |
|
| 258 | @Override
|
| 259 | public boolean mouseScrolled(final double x, final double y, final double scrollX, final double scrollY) {
|
| 260 | if (this.minecraft.hasControlDown()) {
|
| 261 | OptionInstance<Integer> guiScale = this.options.guiScale();
|
| 262 | if (guiScale.values() instanceof OptionInstance.ClampingLazyMaxIntRange clampingLazyMaxIntRange) {
|
| 263 | int oldValue = guiScale.get();
|
| 264 | int adjustedOldValue = oldValue == 0 ? clampingLazyMaxIntRange.maxInclusive() + 1 : oldValue;
|
| 265 | int newValue = adjustedOldValue + (int)Math.signum(scrollY);
|
| 266 | if (newValue != 0 && newValue <= clampingLazyMaxIntRange.maxInclusive() && newValue >= clampingLazyMaxIntRange.minInclusive()) {
|
| 267 | CycleButton<Integer> cycleButton = (CycleButton<Integer>)this.list.findOption(guiScale);
|
| 268 | if (cycleButton != null) {
|
| 269 | guiScale.set(newValue);
|
| 270 | cycleButton.setValue(newValue);
|
| 271 | this.list.setScrollAmount(0.0);
|
| 272 | return true;
|
| 273 | }
|
| 274 | }
|
| 275 | }
|
| 276 |
|
| 277 | return false;
|
| 278 | } else {
|
| 279 | return super.mouseScrolled(x, y, scrollX, scrollY);
|
| 280 | }
|
| 281 | }
|
| 282 |
|
| 283 | public void updateFullscreenButton(final boolean fullscreen) {
|
| 284 | if (this.list != null) {
|
| 285 | AbstractWidget fullscreenWidget = this.list.findOption(this.options.fullscreen());
|
| 286 | if (fullscreenWidget != null) {
|
| 287 | CycleButton<Boolean> fullscreenButton = (CycleButton<Boolean>)fullscreenWidget;
|
| 288 | fullscreenButton.setValue(fullscreen);
|
| 289 | }
|
| 290 | }
|
| 291 | }
|
| 292 |
|
| 293 | @Override
|
| 294 | protected void repositionElements() {
|
| 295 | super.repositionElements();
|
| 296 | Monitor monitor = this.minecraft.getWindow().findBestMonitor();
|
| 297 | if (monitor != this.fullscreenModeMonitor) {
|
| 298 | if (this.list != null) {
|
| 299 | this.list.applyUnsavedChanges();
|
| 300 | }
|
| 301 |
|
| 302 | this.layout.removeChildren();
|
| 303 | this.header.removeChildren();
|
| 304 | this.restartWarning = null;
|
| 305 | this.rebuildWidgets();
|
| 306 | }
|
| 307 | }
|
| 308 |
|
| 309 | public void updateTransparencyButton() {
|
| 310 | if (this.list != null) {
|
| 311 | OptionInstance<Boolean> option = this.options.improvedTransparency();
|
| 312 | AbstractWidget widget = this.list.findOption(option);
|
| 313 | if (widget != null) {
|
| 314 | CycleButton<Boolean> button = (CycleButton<Boolean>)widget;
|
| 315 | button.setValue(option.get());
|
| 316 | }
|
| 317 | }
|
| 318 | }
|
| 319 | }
|
| 320 |
|