Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add resolution control option to the config screen #2296

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.common.collect.ImmutableList;
import com.mojang.blaze3d.pipeline.RenderTarget;
import com.mojang.blaze3d.platform.Monitor;
import com.mojang.blaze3d.platform.VideoMode;
import com.mojang.blaze3d.platform.Window;
import net.caffeinemc.mods.sodium.client.gl.arena.staging.MappedStagingBuffer;
import net.caffeinemc.mods.sodium.client.gl.device.RenderDevice;
Expand All @@ -25,13 +27,16 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

// TODO: Rename in Sodium 0.6
public class SodiumGameOptionPages {
private static final SodiumOptionsStorage sodiumOpts = new SodiumOptionsStorage();
private static final MinecraftOptionsStorage vanillaOpts = new MinecraftOptionsStorage();
private static final Window window = Minecraft.getInstance().getWindow();

public static OptionPage general() {
Monitor monitor = window.findBestMonitor();
List<OptionGroup> groups = new ArrayList<>();

groups.add(OptionGroup.createBuilder()
Expand Down Expand Up @@ -89,6 +94,26 @@ public static OptionPage general() {
}
}, (opts) -> opts.fullscreen().get())
.build())
.add(OptionImpl.createBuilder(int.class, vanillaOpts)
.setName(Component.translatable("options.fullscreen.resolution"))
.setTooltip(Component.translatable("options.fullscreen.resolution"))
.setControl(option -> new SliderControl(option, 0, null != monitor? monitor.getModeCount(): 0, 1, ControlValueFormatter.resolution()))
.setBinding((options, value) -> {
if (null != monitor) {
window.setPreferredFullscreenVideoMode(0 == value? Optional.empty(): Optional.of(monitor.getMode(value - 1)));
}
}, options -> {
if (null == monitor) {
return 0;
}
else {
Optional<VideoMode> optional = window.getPreferredFullscreenVideoMode();
return optional.map((videoMode) -> monitor.getVideoModeIndex(videoMode) + 1).orElse(0);
}
})
.setImpact(OptionImpact.HIGH)
.setFlags(OptionFlag.REQUIRES_VIDEOMODE_RELOAD)
.build())
.add(OptionImpl.createBuilder(boolean.class, vanillaOpts)
.setName(Component.translatable("options.vsync"))
.setTooltip(Component.translatable("sodium.options.v_sync.tooltip"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ private void rebuildGUIOptions() {
// Add each option's control element
for (Option<?> option : group.getOptions()) {
Control<?> control = option.getControl();
ControlElement<?> element = control.createElement(new Dim2i(x, y, 200, 18));
ControlElement<?> element = control.createElement(new Dim2i(x, y, 206, 18));

this.addRenderableWidget(element);

Expand Down Expand Up @@ -355,6 +355,10 @@ private void applyChanges() {
client.delayTextureReload();
}

if (flags.contains(OptionFlag.REQUIRES_VIDEOMODE_RELOAD)) {
client.getWindow().changeFullscreenVideoMode();
}

if (flags.contains(OptionFlag.REQUIRES_GAME_RESTART)) {
Console.instance().logMessage(MessageLevel.WARN,
Component.translatable("sodium.console.game_restart"), 10.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public enum OptionFlag {
REQUIRES_RENDERER_RELOAD,
REQUIRES_RENDERER_UPDATE,
REQUIRES_ASSET_RELOAD,
REQUIRES_VIDEOMODE_RELOAD,
REQUIRES_GAME_RESTART
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package net.caffeinemc.mods.sodium.client.gui.options.control;

import com.mojang.blaze3d.platform.Monitor;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;

public interface ControlValueFormatter {
static ControlValueFormatter guiScale() {
return (v) -> (v == 0) ? Component.translatable("options.guiScale.auto") : Component.literal(v + "x");
}

static ControlValueFormatter resolution() {
Monitor monitor = Minecraft.getInstance().getWindow().findBestMonitor();
return (v) -> {
if (null == monitor) {
return Component.translatable("options.fullscreen.unavailable");
} else if (0 == v) {
return Component.translatable("options.fullscreen.current");
} else {
return Component.literal(monitor.getMode(v - 1).toString().replace(" (24bit)",""));
}
};
}
static ControlValueFormatter fpsLimit() {
return (v) -> (v == 260) ? Component.translatable("options.framerateLimit.max") : Component.translatable("options.framerate", v);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ public Button(Option<Integer> option, Dim2i dim, int min, int max, int interval,
this.thumbPosition = this.getThumbPositionForValue(option.getValue());
this.formatter = formatter;

this.sliderBounds = new Rect2i(dim.getLimitX() - 96, dim.getCenterY() - 5, 90, 10);
this.sliderBounds = new Rect2i(dim.getLimitX() - 56, dim.getCenterY() - 5, 50, 10);
this.sliderHeld = false;
}

@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
super.render(graphics, mouseX, mouseY, delta);

if (this.option.isAvailable() && (this.hovered || this.isFocused())) {
if (this.option.isAvailable()) { // && (this.hovered || this.isFocused())) {
this.renderSlider(graphics);
} else {
this.renderStandaloneValue(graphics);
Expand All @@ -102,21 +102,20 @@ private void renderSlider(GuiGraphics graphics) {
int sliderWidth = this.sliderBounds.getWidth();
int sliderHeight = this.sliderBounds.getHeight();

String label = this.formatter.format(this.option.getValue()).getString();
int labelWidth = this.font.width(label);

this.drawString(graphics, label, sliderX + sliderWidth - labelWidth, sliderY + (sliderHeight / 2) - 4, 0xFFFFFFFF);

this.thumbPosition = this.getThumbPositionForValue(this.option.getValue());

double thumbOffset = Mth.clamp((double) (this.getIntValue() - this.min) / this.range * sliderWidth, 0, sliderWidth);

int thumbX = (int) (sliderX + thumbOffset - THUMB_WIDTH);
int trackY = (int) (sliderY + (sliderHeight / 2f) - ((double) TRACK_HEIGHT / 2));
int trackY = sliderY + sliderHeight;

this.drawRect(graphics, thumbX, sliderY, thumbX + (THUMB_WIDTH * 2), sliderY + sliderHeight, 0xFFFFFFFF);
this.drawRect(graphics, thumbX, trackY + 2*TRACK_HEIGHT,thumbX + (THUMB_WIDTH * 2), trackY - TRACK_HEIGHT, 0xFFFFFFFF);
this.drawRect(graphics, sliderX, trackY, sliderX + sliderWidth, trackY + TRACK_HEIGHT, 0xFFFFFFFF);

String label = String.valueOf(this.getIntValue());

int labelWidth = this.font.width(label);

this.drawString(graphics, label, sliderX - labelWidth - 6, sliderY + (sliderHeight / 2) - 4, 0xFFFFFFFF);
}

public int getIntValue() {
Expand Down