-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.go
253 lines (220 loc) · 7.59 KB
/
control.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
package main
import (
"fmt"
"math"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
xlayout "fyne.io/x/fyne/layout"
)
// The ControlBar structure controls all aspects
// of the animaiton and manipulation of the
// running simulation. It allows the user to
// step the game forward (to the next generation
// of cells), backward (if the history has any
// previous generations), or to run automatically
// at a given speed. Some functions (like the
// zoom functions) have to be passed down to the
// LifeSim object that encapsulates the game display
// logic.
type ControlBar struct {
widget.BaseWidget
life *LifeSim
Clock *LifeSimClock
lastUpdateTime time.Time
updateCadence time.Duration
backwardStepButton *widget.Button
runStopButton *widget.Button
forwardStepButton *widget.Button
zoomOutButton *widget.Button
zoomInButton *widget.Button
glyphSelector *widget.Select
stateDisplay *widget.Label
speedSlider *widget.Slider
bar *fyne.Container
running bool
}
func (controlBar *ControlBar) IsRunning() bool {
return controlBar.running
}
func NewControlBar(sim *LifeSim) *ControlBar {
controlBar := &ControlBar{}
controlBar.life = sim
controlBar.Clock = NewLifeSimClock(sim)
controlBar.lastUpdateTime = time.Now()
controlBar.updateCadence = 100 * time.Millisecond
controlBar.backwardStepButton = widget.NewButtonWithIcon("", theme.MediaSkipPreviousIcon(), func() {
controlBar.StepBackward()
})
if len(controlBar.life.Game.History) == 0 {
controlBar.backwardStepButton.Disable()
}
controlBar.runStopButton = widget.NewButtonWithIcon("", theme.MediaPlayIcon(), func() {
if controlBar.IsRunning() {
controlBar.StopSim()
} else {
controlBar.StartSim()
}
})
controlBar.forwardStepButton = widget.NewButtonWithIcon("", theme.MediaSkipNextIcon(), func() {
if controlBar.IsRunning() {
controlBar.StopSim() // If we're running, we've probably already calculated the next step
} else {
controlBar.StepForward()
}
})
controlBar.zoomOutButton = widget.NewButtonWithIcon("", theme.ZoomOutIcon(), func() { controlBar.ZoomOut() })
controlBar.zoomInButton = widget.NewButtonWithIcon("", theme.ZoomInIcon(), func() { controlBar.ZoomIn() })
controlBar.glyphSelector = widget.NewSelect([]string{"Rectangle", "RoundedRectangle", "Circle"}, func(selection string) {
controlBar.life.GlyphStyle = selection
controlBar.life.Dirty = true
})
controlBar.glyphSelector.SetSelected(controlBar.life.GlyphStyle)
controlBar.stateDisplay = widget.NewLabel(controlBar.life.StateLabel())
controlBar.stateDisplay.Alignment = fyne.TextAlignCenter
controlBar.life.State.AddListener(binding.NewDataListener(func() {
controlBar.stateDisplay.Text = controlBar.life.StateLabel()
controlBar.Refresh()
}))
var minSpeed, maxSpeed, defaultSpeed float64
if fyne.CurrentDevice().IsMobile() {
maxSpeed = 1.2 // ~67 GPS
minSpeed = 3.0 // 1.0 GPS
defaultSpeed = 2.5 // 3.3 GPS
} else {
maxSpeed = 0.5 // 333 GPS
minSpeed = 3.0 // 1.0 GPS
defaultSpeed = 2.0 // 10.0 GPS
}
controlBar.speedSlider = widget.NewSlider(maxSpeed, minSpeed) // log_10 scale in milliseconds
controlBar.speedSlider.SetValue(defaultSpeed)
controlBar.speedSlider.Step = 0.1
fasterButton := widget.NewButton("faster", func() {
newVal := controlBar.speedSlider.Value - 0.1
controlBar.speedSlider.SetValue(max(maxSpeed, newVal))
})
fasterButton.Alignment = widget.ButtonAlignTrailing
slowerButton := widget.NewButton("slower", func() {
newVal := controlBar.speedSlider.Value + 0.1
controlBar.speedSlider.SetValue(min(minSpeed, newVal))
})
slowerButton.Alignment = widget.ButtonAlignLeading
controlBar.bar = container.New(layout.NewAdaptiveGridLayout(2),
container.New(layout.NewHBoxLayout(), controlBar.backwardStepButton, controlBar.runStopButton,
controlBar.forwardStepButton, controlBar.zoomOutButton, controlBar.zoomInButton,
controlBar.glyphSelector, layout.NewSpacer(), controlBar.stateDisplay, layout.NewSpacer()),
container.New(xlayout.NewHPortion([]float64{0.2, 0.6, 0.2}), fasterButton, controlBar.speedSlider, slowerButton))
// This is a bit of a hack... we want to stop the sim and prompt to zoom in
// when the edit mode is turned on, and we can only stop the sim in the control
// bar layer, not the LifeSim layer.
controlBar.life.EditMode.AddListener(binding.NewDataListener(func() {
if controlBar.life.IsEditable() {
controlBar.StopSim()
if controlBar.life.Scale > 0.0 && controlBar.life.Scale < 4.0 {
confirm := dialog.NewConfirm("Scale is very small",
"Each cell is very small on the screen. Would you like to zoom in?",
func(answer bool) {
if answer {
// if we don't disable auto-zoom, it will just zoom right back out
controlBar.life.SetAutoZoom(false)
controlBar.life.Zoom(controlBar.life.Scale / 5)
controlBar.life.Dirty = true
}
},
mainWindow)
confirm.Show()
}
controlBar.life.SetState(simEditing)
} else if controlBar.IsRunning() {
controlBar.life.SetState(simRunning)
} else {
controlBar.life.SetState(simPaused)
}
controlBar.life.Dirty = true
}))
controlBar.ExtendBaseWidget(controlBar)
return controlBar
}
func (controlBar *ControlBar) StopSim() {
if controlBar.IsRunning() {
controlBar.running = false
controlBar.setRunStopIcon(theme.MediaPlayIcon())
}
}
func (controlBar *ControlBar) StartSim() {
if !controlBar.IsRunning() {
controlBar.setRunStopIcon(theme.MediaPauseIcon())
controlBar.running = true
go controlBar.RunGame()
}
if controlBar.life.IsEditable() {
controlBar.life.EditMode.Set(false)
}
}
func (controlBar *ControlBar) DisableAutoZoom() {
// controlBar.autoZoomCheckBox.SetChecked(false)
controlBar.life.SetAutoZoom(false)
}
func (controlBar *ControlBar) ZoomIn() {
controlBar.DisableAutoZoom()
controlBar.life.Zoom(1.0 / zoomFactor)
controlBar.life.Dirty = true
}
func (controlBar *ControlBar) ZoomOut() {
controlBar.DisableAutoZoom()
controlBar.life.Zoom(zoomFactor)
controlBar.life.Dirty = true
}
func (controlBar *ControlBar) setRunStopIcon(icon fyne.Resource) {
controlBar.runStopButton.SetIcon(icon)
}
func (controlBar *ControlBar) CreateRenderer() fyne.WidgetRenderer {
return widget.NewSimpleRenderer(controlBar.bar)
}
func (controlBar *ControlBar) RunGame() {
controlBar.life.SetState(simRunning)
for controlBar.IsRunning() {
controlBar.StepForward()
if controlBar.life.Game.Size() == 0 {
controlBar.StopSim()
break
}
time.Sleep(time.Duration(math.Pow(10.0, controlBar.speedSlider.Value)) * time.Millisecond)
}
if controlBar.life.IsEditable() {
controlBar.life.SetState(simEditing)
} else {
controlBar.life.SetState(simPaused)
}
controlBar.life.Dirty = true
}
func (controlBar *ControlBar) StepForward() {
// controlBar.autoZoomCheckBox.SetChecked(controlBar.life.IsAutoZoom())
controlBar.updateCadence = time.Since(controlBar.lastUpdateTime)
controlBar.lastUpdateTime = time.Now()
controlBar.Clock.LifeTick()
if len(controlBar.life.Game.History) > 0 {
controlBar.backwardStepButton.Enable() // We might have history now
}
}
func (controlBar *ControlBar) StepBackward() {
if controlBar.IsRunning() {
controlBar.StopSim()
}
err := controlBar.life.Game.Previous()
if err != nil {
fmt.Println("Got error trying to step backwards", err)
}
if len(controlBar.life.Game.History) == 0 {
controlBar.backwardStepButton.Disable()
}
controlBar.life.Dirty = true
}
func (cb *ControlBar) StopClocks() {
cb.Clock.Running = false
}