-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreferences.go
269 lines (237 loc) · 8.41 KB
/
preferences.go
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package main
import (
"fmt"
"image/color"
"strconv"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/data/validation"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
const (
gameHistorySizeKey = "io.patenaude.gooeylife.history_size"
autoZoomDefaultKey = "io.patenaude.gooeylife.auto_zoom_default"
displayRefreshRateKey = "io.patenaude.gooeylife.display_refresh_rate"
lastUsedDirectoryKey = "io.patenaude.gooeylife.last_dir_url"
pausedCellColorKey = "io.patenaude.gooeylife.paused_color"
runningCellColorKey = "io.patenaude.gooeylife.running_color"
editCellColorKey = "io.patenaude.gooeyLife.edit_color"
backgroundColorKey = "io.patenaude.gooeyLife.background_color"
showGuidedTourKey = "io.patenaude.gooeylife.guided_tour"
scrollAsZoomKey = "io.patenaude.gooeylife.scroll_as_zoom"
defaultHistorySize = 10
)
var (
defaultPausedColor color.Color = color.NRGBA{R: 0, G: 0, B: 255, A: 255}
defaultRunningColor color.Color = color.NRGBA{R: 0, G: 255, B: 0, A: 255}
defaultEditColor color.Color = color.NRGBA{R: 255, G: 255, B: 0, A: 255}
defaultBGColor color.Color = color.NRGBA{R: 0, G: 0, B: 0, A: 255}
)
type ConfigT struct {
app fyne.App
}
var Config ConfigT
func InitConfig(app fyne.App) {
Config.app = app
}
func (c ConfigT) HistorySize() int {
return c.app.Preferences().IntWithFallback(gameHistorySizeKey, defaultHistorySize)
}
func (c ConfigT) SetHistorySize(value int) {
c.app.Preferences().SetInt(gameHistorySizeKey, value)
}
func (c ConfigT) AutoZoomDefault() bool {
return c.app.Preferences().BoolWithFallback(autoZoomDefaultKey, true)
}
func (c ConfigT) SetAutoZoomDefault(value bool) {
c.app.Preferences().SetBool(autoZoomDefaultKey, value)
}
func (c ConfigT) ShowGuidedTour() bool {
return c.app.Preferences().BoolWithFallback(showGuidedTourKey, true)
}
func (c ConfigT) SetShowGuidedTour(show bool) {
c.app.Preferences().SetBool(showGuidedTourKey, show)
}
func (c ConfigT) ScrollAsZoom() bool {
return c.app.Preferences().BoolWithFallback(scrollAsZoomKey, true)
}
func (c ConfigT) SetScrollAsZoom(saz bool) {
c.app.Preferences().SetBool(scrollAsZoomKey, saz)
}
func (c ConfigT) DisplayRefreshRate() int {
return c.app.Preferences().IntWithFallback(displayRefreshRateKey, 60)
}
func (c ConfigT) SetDisplayRefreshRate(rate int) {
c.app.Preferences().SetInt(displayRefreshRateKey, rate)
}
func (c ConfigT) PausedCellColor() color.Color {
return c.fetchColor(pausedCellColorKey, defaultPausedColor)
}
func (c ConfigT) SetPausedCellColor(clr color.Color) {
c.setColor(pausedCellColorKey, clr)
}
func (c ConfigT) RunningCellColor() color.Color {
return c.fetchColor(runningCellColorKey, defaultRunningColor)
}
func (c ConfigT) SetRunningCellColor(clr color.Color) {
c.setColor(runningCellColorKey, clr)
}
func (c ConfigT) EditCellColor() color.Color {
return c.fetchColor(editCellColorKey, defaultEditColor)
}
func (c ConfigT) SetEditCellColor(clr color.Color) {
c.setColor(editCellColorKey, clr)
}
func (c ConfigT) BackgroundColor() color.Color {
return c.fetchColor(backgroundColorKey, defaultBGColor)
}
func (c ConfigT) SetBackgroundColor(clr color.Color) {
c.setColor(backgroundColorKey, clr)
}
func (c ConfigT) fetchColor(key string, def color.Color) color.Color {
attr := c.app.Preferences().IntListWithFallback(key, make([]int, 0))
if len(attr) != 4 {
return def
}
// the values we get back at 16 bit scaled, but we neeed 8 bit values
col := color.NRGBA{R: uint8(attr[0]), G: uint8(attr[1]), B: uint8(attr[2]), A: uint8(attr[3])}
return col
}
func (c ConfigT) setColor(key string, clr color.Color) {
r, g, b, a := clr.RGBA()
attr := []int{int(r), int(g), int(b), int(a)}
c.app.Preferences().SetIntList(key, attr)
}
func (c ConfigT) ShowPreferencesDialog(tabs *LifeTabs, clk *DisplayUpdateClock) {
historySizeEntry := widget.NewEntry()
historySizeEntry.Validator = validation.NewRegexp(`^\d+$`, "non-negative integers only")
historySizeEntry.SetText(fmt.Sprintf("%d", c.HistorySize()))
autoZoomDefaultCheck := widget.NewCheck("Auto Zoom by default", func(_ bool) {})
autoZoomDefaultCheck.SetChecked(c.AutoZoomDefault())
displayRefreshRateSelector := widget.NewSelect([]string{"30Hz", "60Hz"}, func(_ string) {})
if clk.DisplayUpdateHz == 60 {
displayRefreshRateSelector.SetSelectedIndex(1)
} else {
displayRefreshRateSelector.SetSelectedIndex(0)
}
scrollAsZoomRadioGroup := widget.NewRadioGroup([]string{"scroll", "zoom"}, nil)
if c.ScrollAsZoom() {
scrollAsZoomRadioGroup.SetSelected("zoom")
} else {
scrollAsZoomRadioGroup.SetSelected("scroll")
}
pausedColorPickerButton := widget.NewButtonWithIcon("Paused cells", theme.ColorPaletteIcon(), func() {
picker := dialog.NewColorPicker("Paused Cell Color", "", func(clr color.Color) {
c.SetPausedCellColor(clr)
mainWindow.Canvas().Content().Refresh()
}, mainWindow)
picker.Advanced = true
picker.SetColor(c.PausedCellColor())
picker.Show()
})
runningColorPickerButton := widget.NewButtonWithIcon("Running cells", theme.ColorPaletteIcon(), func() {
picker := dialog.NewColorPicker("Running Cell Color", "", func(clr color.Color) {
c.SetRunningCellColor(clr)
mainWindow.Canvas().Content().Refresh()
}, mainWindow)
picker.Advanced = true
picker.SetColor(c.RunningCellColor())
picker.Show()
})
editColorPickerButton := widget.NewButtonWithIcon("Editing cells", theme.ColorPaletteIcon(), func() {
picker := dialog.NewColorPicker("Editing Cell Color", "", func(clr color.Color) {
c.SetEditCellColor(clr)
mainWindow.Canvas().Content().Refresh()
}, mainWindow)
picker.Advanced = true
picker.SetColor(c.EditCellColor())
picker.Show()
})
backgroundColorPickerButton := widget.NewButtonWithIcon("Background", theme.ColorPaletteIcon(), func() {
picker := dialog.NewColorPicker("Background Color", "", func(clr color.Color) {
c.SetBackgroundColor(clr)
mainWindow.Canvas().Content().Refresh()
}, mainWindow)
picker.Advanced = true
picker.SetColor(c.BackgroundColor())
picker.Show()
})
entries := []*widget.FormItem{
widget.NewFormItem("Saved Generations", historySizeEntry),
widget.NewFormItem("Auto-zoom enabled by default", autoZoomDefaultCheck),
widget.NewFormItem("Diplay refresh rate", displayRefreshRateSelector),
widget.NewFormItem("Mouse wheel function", scrollAsZoomRadioGroup),
widget.NewFormItem("Paused Cell Color", pausedColorPickerButton),
widget.NewFormItem("Running Cell Color", runningColorPickerButton),
widget.NewFormItem("Editing Cell Color", editColorPickerButton),
widget.NewFormItem("Background Color", backgroundColorPickerButton)}
dialog.ShowForm("Preferences", "Save", "Cancel", entries, func(save bool) {
if save {
historySize, err := strconv.Atoi(historySizeEntry.Text)
if err != nil {
fmt.Println("Prefernces let through a bad value: ", historySizeEntry.Text, err)
} else {
if historySize != c.HistorySize() {
// update all existing games
for _, lc := range tabs.GetLifeContainters() {
lc.Sim.Game.SetHistorySize(historySize)
}
// persist the new value
c.SetHistorySize(historySize)
}
}
c.SetAutoZoomDefault(autoZoomDefaultCheck.Checked)
if displayRefreshRateSelector.SelectedIndex() == 0 {
c.SetDisplayRefreshRate(30)
} else {
c.SetDisplayRefreshRate(60)
}
clk.DisplayUpdateHz = c.DisplayRefreshRate()
c.SetScrollAsZoom(scrollAsZoomRadioGroup.Selected == "zoom")
}
}, mainWindow)
}
func (c *ConfigT) LastUsedDirURI() fyne.ListableURI {
lastUriString := c.app.Preferences().StringWithFallback(lastUsedDirectoryKey, "")
if lastUriString == "" {
return nil
}
uri := storage.NewURI(lastUriString)
listable, err := storage.CanList(uri)
if err != nil {
return nil
}
for !listable {
uri, err = storage.Parent(uri)
if err != nil {
fyne.LogError("Unable to walk up file tree", err)
return nil
}
listable, err = storage.CanList(uri)
}
listableUri, err := storage.ListerForURI(uri)
if err != nil {
fyne.LogError("Can't make listable URI:", err)
return nil
}
return listableUri
}
func (c *ConfigT) SetLastUsedDirURI(uri fyne.URI) {
// parent := uri // make a copy of the URI
listable, err := storage.CanList(uri)
if err != nil {
fyne.LogError("Can't check listability of URI:", err)
return
}
for !listable {
uri, err = storage.Parent(uri)
if err != nil {
fyne.LogError("Unable to walk up file tree", err)
return
}
listable, _ = storage.CanList(uri)
}
c.app.Preferences().SetString(lastUsedDirectoryKey, uri.String())
}