Store settings and gui layout whenever it gets changed.

fixes #469
This commit is contained in:
IntegratedQuantum 2024-07-04 10:24:57 +02:00
parent ac3847db79
commit 337e014d88
8 changed files with 37 additions and 8 deletions

View File

@ -160,6 +160,7 @@ pub fn mainButtonReleased(self: *GuiWindow, mousePosition: Vec2f) void {
}
self.scale = @max(self.scale, 0.25);
gui.updateWindowPositions();
gui.save();
}
} else {
// Zoom in
@ -169,6 +170,7 @@ pub fn mainButtonReleased(self: *GuiWindow, mousePosition: Vec2f) void {
self.scale += 0.25;
}
gui.updateWindowPositions();
gui.save();
}
}
}
@ -334,6 +336,7 @@ pub fn updateSelected(self: *GuiWindow, mousePosition: Vec2f) void {
self.pos = @max(self.pos, Vec2f{0, 0});
self.pos = @min(self.pos, windowSize - self.size);
gui.updateWindowPositions();
gui.save();
};
if(self.rootComponent) |*component| {
component.updateSelected();

View File

@ -183,7 +183,7 @@ pub fn deinit() void {
GuiCommandQueue.deinit();
}
fn save() void {
pub fn save() void {
const guiJson = JsonElement.initObject(main.stackAllocator);
defer guiJson.free(main.stackAllocator);
for(windowList.items) |window| {

View File

@ -23,6 +23,7 @@ fn apply(_: usize) void {
const oldName = settings.playerName;
main.globalAllocator.free(settings.playerName);
settings.playerName = main.globalAllocator.dupe(u8, textComponent.currentString.items);
settings.save();
gui.closeWindow(&window);
if(oldName.len == 0) {

View File

@ -32,10 +32,12 @@ fn keypressListener(key: c_int, mouseButton: c_int, scancode: c_int) void {
selectedKey.?.scancode = scancode;
selectedKey = null;
needsUpdate = true;
main.settings.save();
}
fn updateSensitivity(sensitivity: f32) void {
main.settings.mouseSensitivity = sensitivity;
main.settings.save();
}
fn sensitivityFormatter(allocator: main.utils.NeverFailingAllocator, value: f32) []const u8 {

View File

@ -43,14 +43,17 @@ fn fpsCapFormatter(allocator: main.utils.NeverFailingAllocator, value: f32) []co
fn fpsCapCallback(newValue: f32) void {
settings.fpsCap = fpsCapRound(newValue);
settings.save();
}
fn renderDistanceCallback(newValue: u16) void {
settings.renderDistance = newValue + renderDistances[0];
settings.save();
}
fn fovCallback(newValue: f32) void {
settings.fov = newValue;
settings.save();
main.Window.GLFWCallbacks.framebufferSize(undefined, main.Window.width, main.Window.height);
}
@ -60,19 +63,23 @@ fn fovFormatter(allocator: main.utils.NeverFailingAllocator, value: f32) []const
fn LODFactorCallback(newValue: u16) void {
settings.LODFactor = @as(f32, @floatFromInt(newValue + 1))/2;
settings.save();
}
fn bloomCallback(newValue: bool) void {
settings.bloom = newValue;
settings.save();
}
fn vsyncCallback(newValue: bool) void {
settings.vsync = newValue;
settings.save();
main.Window.reloadSettings();
}
fn anisotropicFilteringCallback(newValue: u16) void {
settings.anisotropicFiltering = anisotropy[newValue];
settings.save();
if(main.game.world != null) {
main.blocks.meshes.reloadTextures(undefined);
}
@ -80,6 +87,7 @@ fn anisotropicFilteringCallback(newValue: u16) void {
fn resolutionScaleCallback(newValue: u16) void {
settings.resolutionScale = std.math.pow(f32, 2.0, @as(f32, @floatFromInt(newValue)) - 2.0);
settings.save();
main.Window.GLFWCallbacks.framebufferSize(null, main.Window.width, main.Window.height);
}

View File

@ -59,6 +59,7 @@ fn join(_: usize) void {
_connection.world = &main.game.testWorld;
main.globalAllocator.free(settings.lastUsedIPAddress);
settings.lastUsedIPAddress = main.globalAllocator.dupe(u8, ipAddressEntry.currentString.items);
settings.save();
main.game.testWorld.init(ipAddressEntry.currentString.items, _connection) catch |err| {
std.log.err("Encountered error while opening world: {s}", .{@errorName(err)});
};

View File

@ -18,6 +18,7 @@ pub var window = GuiWindow {
fn musicCallback(newValue: f32) void {
settings.musicVolume = deziBelToLinear(newValue);
settings.save();
}
fn deziBelToLinear(x: f32) f32 {

View File

@ -96,6 +96,26 @@ pub fn init() void {
}
pub fn deinit() void {
save();
inline for(@typeInfo(@This()).Struct.decls) |decl| {
const is_const = @typeInfo(@TypeOf(&@field(@This(), decl.name))).Pointer.is_const; // Sadly there is no direct way to check if a declaration is const.
if(!is_const) {
const declType = @TypeOf(@field(@This(), decl.name));
if(@typeInfo(declType) == .Struct) {
@compileError("Not implemented yet.");
}
if(@typeInfo(declType) == .Pointer) {
if(@typeInfo(declType).Pointer.size == .Slice) {
main.globalAllocator.free(@field(@This(), decl.name));
} else {
@compileError("Not implemented yet.");
}
}
}
}
}
pub fn save() void {
const jsonObject = JsonElement.initObject(main.stackAllocator);
defer jsonObject.free(main.stackAllocator);
@ -111,13 +131,6 @@ pub fn deinit() void {
} else {
jsonObject.put(decl.name, @field(@This(), decl.name));
}
if(@typeInfo(declType) == .Pointer) {
if(@typeInfo(declType).Pointer.size == .Slice) {
main.globalAllocator.free(@field(@This(), decl.name));
} else {
@compileError("Not implemented yet.");
}
}
}
}