Skip to content

Commit 6db60fa

Browse files
authored
Update settings screen (#801)
* update settings screen
1 parent da1d92f commit 6db60fa

30 files changed

Lines changed: 1518 additions & 768 deletions

src/main/java/net/vulkanmod/Initializer.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import net.fabricmc.api.ClientModInitializer;
44
import net.fabricmc.fabric.api.renderer.v1.Renderer;
55
import net.fabricmc.loader.api.FabricLoader;
6+
import net.minecraft.network.chat.Component;
67
import net.vulkanmod.config.Config;
78
import net.vulkanmod.config.Platform;
89
import net.vulkanmod.config.UpdateChecker;
10+
import net.vulkanmod.config.option.Option;
11+
import net.vulkanmod.config.option.OptionRegistry;
12+
import net.vulkanmod.config.option.Options;
913
import net.vulkanmod.config.video.VideoModeManager;
1014
import net.vulkanmod.render.chunk.build.frapi.VulkanModRenderer;
1115
import org.apache.logging.log4j.LogManager;
@@ -44,14 +48,7 @@ public void onInitializeClient() {
4448
}
4549

4650
private static Config loadConfig(Path path) {
47-
Config config = Config.load(path);
48-
49-
if(config == null) {
50-
config = new Config();
51-
config.write();
52-
}
53-
54-
return config;
51+
return Config.load(path);
5552
}
5653

5754
public static String getVersion() {
Lines changed: 104 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,142 @@
11
package net.vulkanmod.config;
22

3-
import com.google.gson.Gson;
4-
import com.google.gson.GsonBuilder;
3+
import com.google.gson.*;
4+
import com.google.gson.annotations.JsonAdapter;
5+
import net.vulkanmod.Initializer;
6+
import net.vulkanmod.config.video.VideoMode;
57
import net.vulkanmod.config.video.VideoModeManager;
6-
import net.vulkanmod.config.video.VideoModeSet;
78

8-
import java.io.FileReader;
99
import java.io.IOException;
10-
import java.lang.reflect.Modifier;
1110
import java.nio.file.Files;
1211
import java.nio.file.Path;
13-
import java.util.Collections;
1412

13+
@JsonAdapter(Config.GsonAdapter.class)
1514
public class Config {
16-
public VideoModeSet.VideoMode videoMode = VideoModeManager.getFirstAvailable().getVideoMode();
15+
16+
public VideoMode videoMode;
1717
public int windowMode = 0;
1818

1919
public int advCulling = 2;
2020
public boolean indirectDraw = true;
21-
2221
public boolean uniqueOpaqueLayer = true;
2322
public boolean entityCulling = true;
24-
public int device = -1;
2523

2624
public int ambientOcclusion = 1;
2725
public int frameQueueSize = 2;
2826
public int builderThreads = 0;
29-
3027
public boolean backFaceCulling = true;
3128
public boolean textureAnimations = true;
3229

33-
public void write() {
30+
public int device = -1;
3431

35-
if(!Files.exists(CONFIG_PATH.getParent())) {
36-
try {
37-
Files.createDirectories(CONFIG_PATH);
38-
} catch (IOException e) {
39-
e.printStackTrace();
40-
}
41-
}
32+
private static Path CONFIG_PATH;
33+
private static final Gson GSON = new GsonBuilder()
34+
.setPrettyPrinting()
35+
.registerTypeAdapter(Config.class, new GsonAdapter())
36+
.create();
4237

38+
public void save() {
4339
try {
44-
Files.write(CONFIG_PATH, Collections.singleton(GSON.toJson(this)));
40+
Files.createDirectories(CONFIG_PATH.getParent());
41+
Files.writeString(CONFIG_PATH, GSON.toJson(this));
4542
} catch (IOException e) {
46-
e.printStackTrace();
43+
Initializer.LOGGER.error("Error saving config file!", e);
4744
}
4845
}
4946

50-
private static Path CONFIG_PATH;
51-
52-
private static final Gson GSON = new GsonBuilder()
53-
.setPrettyPrinting()
54-
.excludeFieldsWithModifiers(Modifier.PRIVATE)
55-
.create();
56-
5747
public static Config load(Path path) {
58-
Config config;
59-
Config.CONFIG_PATH = path;
48+
CONFIG_PATH = path;
6049

6150
if (Files.exists(path)) {
62-
try (FileReader fileReader = new FileReader(path.toFile())) {
63-
config = GSON.fromJson(fileReader, Config.class);
51+
try {
52+
String content = Files.readString(path);
53+
Config config = GSON.fromJson(content, Config.class);
54+
55+
if (config.videoMode == null ||
56+
VideoModeManager.findSetFor(config.videoMode) == null) {
57+
config.videoMode = VideoModeManager.currentOsMode();
58+
}
59+
60+
return config;
61+
} catch (IOException | JsonSyntaxException e) {
62+
System.err.println("Failed to load config, using defaults: " + e.getMessage());
6463
}
65-
catch (IOException exception) {
66-
throw new RuntimeException(exception.getMessage());
64+
}
65+
66+
Config config = new Config();
67+
config.videoMode = VideoModeManager.currentOsMode();
68+
return config;
69+
}
70+
71+
public static class GsonAdapter implements JsonSerializer<Config>, JsonDeserializer<Config> {
72+
73+
@Override
74+
public JsonElement serialize(Config src, java.lang.reflect.Type typeOfSrc, JsonSerializationContext context) {
75+
JsonObject obj = new JsonObject();
76+
77+
if (src.videoMode != null) {
78+
JsonObject vm = new JsonObject();
79+
vm.addProperty("width", src.videoMode.width());
80+
vm.addProperty("height", src.videoMode.height());
81+
vm.addProperty("bitDepth", src.videoMode.bitDepth());
82+
vm.addProperty("refreshRate", src.videoMode.refreshRate());
83+
obj.add("videoMode", vm);
6784
}
85+
86+
obj.addProperty("windowMode", src.windowMode);
87+
obj.addProperty("advCulling", src.advCulling);
88+
obj.addProperty("indirectDraw", src.indirectDraw);
89+
obj.addProperty("uniqueOpaqueLayer", src.uniqueOpaqueLayer);
90+
obj.addProperty("entityCulling", src.entityCulling);
91+
obj.addProperty("ambientOcclusion", src.ambientOcclusion);
92+
obj.addProperty("frameQueueSize", src.frameQueueSize);
93+
obj.addProperty("builderThreads", src.builderThreads);
94+
obj.addProperty("backFaceCulling", src.backFaceCulling);
95+
obj.addProperty("textureAnimations", src.textureAnimations);
96+
obj.addProperty("device", src.device);
97+
98+
return obj;
6899
}
69-
else {
70-
config = null;
100+
101+
@Override
102+
public Config deserialize(JsonElement json, java.lang.reflect.Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
103+
Config config = new Config();
104+
JsonObject obj = json.getAsJsonObject();
105+
106+
if (obj.has("videoMode")) {
107+
JsonObject vm = obj.getAsJsonObject("videoMode");
108+
int w = getInt(vm, "width", 1920);
109+
int h = getInt(vm, "height", 1080);
110+
int bd = getInt(vm, "bitDepth", 8);
111+
int rr = getInt(vm, "refreshRate", 60);
112+
config.videoMode = new VideoMode(w, h, bd, rr);
113+
} else {
114+
config.videoMode = VideoModeManager.currentOsMode();
115+
}
116+
117+
config.windowMode = getInt(obj, "windowMode", 0);
118+
config.advCulling = getInt(obj, "advCulling", 2);
119+
config.indirectDraw = getBoolean(obj, "indirectDraw");
120+
config.uniqueOpaqueLayer = getBoolean(obj, "uniqueOpaqueLayer");
121+
config.entityCulling = getBoolean(obj, "entityCulling");
122+
config.ambientOcclusion = getInt(obj, "ambientOcclusion", 1);
123+
config.frameQueueSize = getInt(obj, "frameQueueSize", 2);
124+
config.builderThreads = getInt(obj, "builderThreads", 0);
125+
config.backFaceCulling = getBoolean(obj, "backFaceCulling");
126+
config.textureAnimations = getBoolean(obj, "textureAnimations");
127+
config.device = getInt(obj, "device", -1);
128+
129+
return config;
71130
}
72131

73-
return config;
132+
private int getInt(JsonObject obj, String key, int def) {
133+
JsonElement el = obj.get(key);
134+
return el != null && el.isJsonPrimitive() ? el.getAsInt() : def;
135+
}
136+
137+
private boolean getBoolean(JsonObject obj, String key) {
138+
JsonElement el = obj.get(key);
139+
return el == null || !el.isJsonPrimitive() || el.getAsBoolean();
140+
}
74141
}
75-
}
142+
}

src/main/java/net/vulkanmod/config/Platform.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public abstract class Platform {
1313

1414
public static void init() {
1515
GLFW.glfwInitHint(GLFW_PLATFORM, activePlat);
16-
LOGGER.info("Selecting Platform: {}", getStringFromPlat(activePlat));
16+
LOGGER.info("Selecting Platform: {}", getStringFromPlat());
1717
LOGGER.info("GLFW: {}", GLFW.glfwGetVersionString());
1818
GLFW.glfwInit();
1919
}
@@ -40,14 +40,14 @@ private static int getSupportedPlat() {
4040
return GLFW_ANY_PLATFORM; //Unknown platform
4141
}
4242

43-
private static String getStringFromPlat(int plat) {
44-
return switch (plat) {
43+
private static String getStringFromPlat() {
44+
return switch (Platform.activePlat) {
4545
case GLFW_PLATFORM_WIN32 -> "WIN32";
4646
case GLFW_PLATFORM_WAYLAND -> "WAYLAND";
4747
case GLFW_PLATFORM_X11 -> "X11";
4848
case GLFW_PLATFORM_COCOA -> "MACOS";
4949
case GLFW_ANY_PLATFORM -> "ANDROID";
50-
default -> throw new IllegalStateException("Unexpected value: " + plat);
50+
default -> throw new IllegalStateException("Unexpected value: " + Platform.activePlat);
5151
};
5252
}
5353

src/main/java/net/vulkanmod/config/gui/GuiElement.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import net.minecraft.client.gui.narration.NarrationElementOutput;
88
import net.minecraft.client.gui.navigation.FocusNavigationEvent;
99
import net.minecraft.client.gui.navigation.ScreenRectangle;
10-
import net.minecraft.client.input.MouseButtonEvent;
10+
import org.jetbrains.annotations.NotNull;
1111
import org.jetbrains.annotations.Nullable;
1212

1313
public abstract class GuiElement implements GuiEventListener, NarratableEntry {
@@ -22,6 +22,7 @@ public abstract class GuiElement implements GuiEventListener, NarratableEntry {
2222
protected int hoverTime;
2323
protected long hoverStopTime;
2424

25+
@SuppressWarnings("unused") // this will surely be used some day
2526
public void setPosition(int x, int y) {
2627
this.x = x;
2728
this.y = y;
@@ -34,6 +35,7 @@ public void setPosition(int x, int y, int width, int height) {
3435
this.height = height;
3536
}
3637

38+
@SuppressWarnings("unused") // this will surely be used someday
3739
public void resize(int width, int height) {
3840
this.width = width;
3941
this.height = height;
@@ -102,7 +104,7 @@ public ComponentPath getCurrentFocusPath() {
102104
}
103105

104106
@Override
105-
public ScreenRectangle getRectangle() {
107+
public @NotNull ScreenRectangle getRectangle() {
106108
return GuiEventListener.super.getRectangle();
107109
}
108110

@@ -117,7 +119,7 @@ public boolean isFocused() {
117119
}
118120

119121
@Override
120-
public NarrationPriority narrationPriority() {
122+
public @NotNull NarrationPriority narrationPriority() {
121123
return NarrationPriority.NONE;
122124
}
123125

0 commit comments

Comments
 (0)