~mil/mepo

ref: 4bb3b55aac499ba4f85180006c6b69976c6c0a14 mepo/src/util/utilprefs.zig -rw-r--r-- 2.5 KiB
4bb3b55aMiles Alan Move prefrences into global / utilprefs rather then object structure 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const std = @import("std");

const PrefValueEnum = enum { b, t, f, u };
const PrefValueUnion = union(PrefValueEnum) { b: bool, t: []const u8, f: f64, u: u8 };
const PrefMapping = struct {
    name: []const u8,
    value: PrefValueUnion,
};

const n_prefs = 13;
var prefs_mapping: [n_prefs]PrefMapping = mapping: {
    var mapping: [n_prefs]PrefMapping = undefined;
    mapping[0] = .{ .name = "debug_message_enabled", .value = .{ .b = true } };
    mapping[1] = .{ .name = "distance_unit_tf_km_mi", .value = .{ .b = false } };
    mapping[2] = .{ .name = "overlay_debugbar", .value = .{ .b = true } };
    mapping[3] = .{ .name = "help", .value = .{ .b = false } };
    mapping[4] = .{ .name = "overlay_pindetails", .value = .{ .b = true } };
    mapping[5] = .{ .name = "crosshair_size", .value = .{ .u = 15 } };
    mapping[6] = .{ .name = "drag_scale", .value = .{ .u = 2 } };
    mapping[7] = .{ .name = "fontsize_ui", .value = .{ .u = 20 } };
    mapping[8] = .{ .name = "zoom", .value = .{ .u = 14 } };
    mapping[9] = .{ .name = "debug_tiles", .value = .{ .b = false } };
    mapping[10] = .{ .name = "debug_stderr", .value = .{ .b = false } };
    mapping[11] = .{ .name = "tile_cache_max_n_transfers", .value = .{ .u = 20 } };
    mapping[12] = .{ .name = "tile_cache_expiry_seconds", .value = .{ .f = -1 } };
    break :mapping mapping;
};

pub fn get_u8(prefname: []const u8) f64 {
    for (prefs_mapping) |pref| {
        if (std.mem.eql(u8, pref.name, prefname)) return pref.value.u;
    }
    return 0;
}

pub fn get(prefname: []const u8) PrefValueUnion {
    for (prefs_mapping) |pref| {
        if (std.mem.eql(u8, pref.name, prefname)) {
            return pref.value;
        }
    }
    return .{ .b = false };
}

pub fn set_n(prefname: []const u8, value: f64) void {
    for (prefs_mapping) |*pref| {
        if (std.mem.eql(u8, pref.name, prefname)) {
            switch (pref.value) {
                .b => {
                    pref.value = .{ .b = value == 1 };
                },
                .u => {
                    pref.value = .{ .u = @floatToInt(u8, value) };
                },
                .f => {
                    pref.value = .{ .f = value };
                },
                .t => {},
            }
        }
    }
}

pub fn toggle_bool(prefname: []const u8) void {
    for (prefs_mapping) |*pref| {
        if (std.mem.eql(u8, pref.name, prefname)) {
            switch (pref.value) {
                .b => {
                    pref.value = .{ .b = !pref.value.b };
                },
                else => {},
            }
        }
    }
}