-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_settings.v
152 lines (126 loc) · 3.46 KB
/
app_settings.v
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright(C) 2023 Lars Pontoppidan. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module main
import os
import toml
import shy.lib as shy
import shy.mth
pub struct UserSettings {
mut:
music_volume f32 = 1.0
sfx_volume f32 = 1.0
images []string
dimensions shy.Size = shy.size(3, 3)
game_mode GameMode = .relaxed
}
fn (mut us UserSettings) defaults() {
us.music_volume = 1.0
us.sfx_volume = 1.0
us.images.clear()
us.dimensions = shy.size(3, 3)
}
fn (a &App) ensure_settings_path() !string {
save_path := os.join_path(a.shy.config_dir()!, 'Black Grain', 'blackgrain.dk', 'puzzle_vibes')
if !os.exists(save_path) {
os.mkdir_all(save_path) or {
return error('could not make directory "${save_path}": ${err}')
}
}
return save_path
}
fn (a &App) settings_file() !string {
save_path := a.ensure_settings_path()!
return os.join_path(save_path, 'settings.toml')
}
fn (mut a App) save_settings_when_time_permits() {
a.save_settings_next = true
}
fn (mut a App) load_settings() ! {
save_file := a.settings_file()!
if os.is_file(save_file) {
// eprintln(os.read_file(save_file)!)
// eprintln('---')
toml_doc := toml.parse_file(save_file)!
if val := toml_doc.value_opt('music.volume') {
a.settings.music_volume = val.f32()
}
if val := toml_doc.value_opt('sfx.volume') {
a.settings.sfx_volume = val.f32()
}
if dim_width := toml_doc.value_opt('puzzle.dimensions.width') {
max := a.dim_selector.max.x
w := dim_width.f32()
a.settings.dimensions.width = mth.max(mth.min(w, max), 1)
}
if dim_height := toml_doc.value_opt('puzzle.dimensions.height') {
max := a.dim_selector.max.y
h := dim_height.f32()
a.settings.dimensions.height = mth.max(mth.min(h, max), 1)
}
a.dim_selector.dim = a.settings.dimensions
if game_mode := toml_doc.value_opt('app.game_mode') {
a.settings.game_mode = game_mode_from_string(game_mode as string)
}
mut images := []string{}
if toml_images := toml_doc.value_opt('app.images') {
toml_any_arr := toml_images.array()
images << toml_any_arr.as_strings()
}
// println(images)
// eprintln(toml_doc)
for image in images {
a.add_user_image(image)!
}
// println(a.settings.images)
}
}
fn (mut a App) save_settings() ! {
save_file := a.settings_file()!
mut toml_txt := '# Puzzle Vibes settings file
format_version = "1.0.0"
'
// TODO BUG workaround for V gcc compilation error on Windows?!
toml_txt += '
[music]
volume = ${a.settings.music_volume:.3f}'
toml_txt += '
[sfx]
volume = ${a.settings.sfx_volume:.3f}'
toml_txt += '
[puzzle]
dimensions.width = ${a.settings.dimensions.width}'
toml_txt += '
dimensions.height = ${a.settings.dimensions.height}'
toml_txt += '
[app]
game_mode = "${a.settings.game_mode}"
'
mut images_txt := '\timages = [\n'
for image in a.settings.images {
images_txt += "\t\t'${image}',\n"
}
images_txt = images_txt.trim_right(',\n')
images_txt += '\n\t]\n'
toml_txt += images_txt
os.write_file(save_file, toml_txt)!
// eprintln('${@FN}')
// a.show_toast(Toast{
// text: 'Settings saved (TODO)'
// duration: 2.5
// })
}
fn (mut a App) reset_settings() ! {
save_file := a.settings_file()!
if os.is_file(save_file) {
os.rm(save_file)!
for i, mut image in a.image_selector.images {
if image.removable {
a.image_selector.images.delete(i)
a.remove_user_image(image.source.str())
}
}
a.image_selector.selected = 0
a.settings.defaults()
}
}