/
menu.go
381 lines (307 loc) · 10.1 KB
/
menu.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package main
import (
"fmt"
"math"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/text"
"golang.org/x/image/colornames"
"golang.org/x/image/font/basicfont"
)
type mainMenu struct {
text *text.Text
}
func (menu *mainMenu) NumOptionIndexes() int {
additionalOptions := 1
return (len(feederContexts) - 1) + additionalOptions
}
func (menu *mainMenu) ShowMainMenu(win *pixelgl.Window, canvas *pixelgl.Canvas, imd *imdraw.IMDraw, currentBirds *[]*bird) {
imd.Clear()
menuClosed := false
selectedOptionNumber := 0
// Set the selected context number to the index of the current context.
for index, name := range feederContexts {
if context.Name() == name {
selectedOptionNumber = index
break
}
}
xValue := canvas.Bounds().Min.X + 100
yValue := canvas.Bounds().Max.Y - 100
atlas := text.NewAtlas(basicfont.Face7x13, text.ASCII)
menu.text = text.New(pixel.Vec{X: xValue, Y: yValue}, atlas)
menu.PrintMenuText(selectedOptionNumber, context.Name())
for !win.Closed() && !menuClosed {
// Increment/decrement selected feeder context selector, then re-print with the selection highlighted.
if win.JustPressed(pixelgl.KeyDown) {
if selectedOptionNumber == menu.NumOptionIndexes() {
selectedOptionNumber = 0
} else {
selectedOptionNumber++
}
} else if win.JustPressed(pixelgl.KeyUp) {
if selectedOptionNumber == 0 {
selectedOptionNumber = menu.NumOptionIndexes()
} else {
selectedOptionNumber--
}
}
menu.PrintMenuText(selectedOptionNumber, context.Name())
canvas.Clear(colornames.Rosybrown)
menu.text.Draw(canvas, pixel.IM.Scaled(menu.text.Orig, 3))
win.SetMatrix(pixel.IM.Scaled(pixel.ZV,
math.Min(
win.Bounds().W()/canvas.Bounds().W(),
win.Bounds().H()/canvas.Bounds().H(),
),
).Moved(win.Bounds().Center()))
canvas.Draw(win, pixel.IM.Moved(canvas.Bounds().Center()))
win.Update()
// Leave the menu when enter or escape is pressed.
if win.JustPressed(pixelgl.KeyEnter) {
// Only re-initialize the feeder context if a *different* one is selected.
if len(feederContexts) > selectedOptionNumber && context.Name() != feederContexts[selectedOptionNumber] {
context = feederContextMappings[feederContexts[selectedOptionNumber]]
context.Initialize()
*currentBirds = nil
} else if selectedOptionNumber == menu.NumOptionIndexes() {
if soundDisabled {
enableSounds()
} else {
disableSounds()
}
// Don't close the menu if muting/unmuting.
continue
}
menuClosed = true
} else if win.JustPressed(pixelgl.KeyEscape) {
menuClosed = true
}
}
// Set the next frame to be a 'loading' screen.
if !win.Closed() {
menu.ShowLoadingScreen(win, canvas, imd)
}
}
func (menu *mainMenu) ShowLoadingScreen(win *pixelgl.Window, canvas *pixelgl.Canvas, imd *imdraw.IMDraw) {
imd.Clear()
xValue := canvas.Bounds().Min.X + 100
yValue := canvas.Bounds().Max.Y - 100
atlas := text.NewAtlas(basicfont.Face7x13, text.ASCII)
menu.text = text.New(pixel.Vec{X: xValue, Y: yValue}, atlas)
menu.PrintLoadingText()
canvas.Clear(colornames.Rosybrown)
menu.text.Draw(canvas, pixel.IM.Scaled(menu.text.Orig, 3))
win.SetMatrix(pixel.IM.Scaled(pixel.ZV,
math.Min(
win.Bounds().W()/canvas.Bounds().W(),
win.Bounds().H()/canvas.Bounds().H(),
),
).Moved(win.Bounds().Center()))
canvas.Draw(win, pixel.IM.Moved(canvas.Bounds().Center()))
win.Update()
}
func (menu *mainMenu) PrintMenuText(selectedContextNumber int, currentContextName string) {
menu.text.Clear()
menu.text.LineHeight = .9 * menu.text.Atlas().LineHeight()
menu.text.Color = colornames.White
fmt.Fprintln(menu.text, menuTitleText)
fmt.Fprintln(menu.text, "------------------------------")
menu.text.Color = colornames.Pink
fmt.Fprintln(menu.text, menuSubtitleText)
for index, name := range feederContexts {
// Show the current feeder in red.
if index == selectedContextNumber {
menu.text.Color = colornames.Red
} else {
menu.text.Color = colornames.Blue
}
if currentContextName == name {
name = name + " (current)"
}
fmt.Fprintln(menu.text, name)
}
fmt.Fprintln(menu.text) // New line.
menu.text.Color = colornames.White
fmt.Fprintln(menu.text, "------------------------------")
// Print the mute/unmute button.
if selectedContextNumber == len(feederContexts) {
menu.text.Color = colornames.Red
} else {
menu.text.Color = colornames.Blue
}
soundOption := "Mute"
if soundDisabled {
soundOption = "Unmute"
}
fmt.Fprintln(menu.text, soundOption)
menu.text.Color = colornames.White
fmt.Fprintln(menu.text, "------------------------------")
menu.text.Color = colornames.Pink
fmt.Fprintln(menu.text, menuAdditionalText)
if needsUpdate() {
fmt.Fprintln(menu.text, updateRequiredText)
}
menu.text.Color = colornames.White
fmt.Fprintln(menu.text, "------------------------------")
menu.text.Color = colornames.Pink
fmt.Fprintln(menu.text, "\nVersion: "+fmt.Sprint(versionNumber))
fmt.Fprintln(menu.text, creditText)
}
func (menu *mainMenu) PrintLoadingText() {
menu.text.Clear()
menu.text.Color = colornames.Pink
fmt.Fprintln(menu.text, "Loading...")
}
type pauseMenu struct {
width float64
height float64
center pixel.Vec
text *text.Text
selectedOptionNumber int
open bool
justOpenedMenu bool
upperYBound float64
lowerYBound float64
}
func (menu *pauseMenu) NumOptionIndexes() int {
additionalOptions := 1
return (len(feederContexts) - 1) + additionalOptions
}
func (menu *pauseMenu) Show(canvas *pixelgl.Canvas) {
menu.text.Draw(canvas, pixel.IM.Scaled(menu.text.Orig, 2))
}
func (menu *pauseMenu) ShowLoadingScreen(win *pixelgl.Window, imd *imdraw.IMDraw, canvas *pixelgl.Canvas) {
menu.PrintLoadingText()
// Draw to the canvas.
imd.Draw(canvas)
// Draw the pause menu when open.
menu.Show(canvas)
// Stretch the canvas to the window.
win.Clear(colornames.White)
win.SetMatrix(pixel.IM.Scaled(pixel.ZV,
math.Min(
win.Bounds().W()/canvas.Bounds().W(),
win.Bounds().H()/canvas.Bounds().H(),
),
).Moved(win.Bounds().Center()))
canvas.Draw(win, pixel.IM.Moved(canvas.Bounds().Center()))
win.Update()
}
func (menu *pauseMenu) Render(win *pixelgl.Window, imd *imdraw.IMDraw, canvas *pixelgl.Canvas, currentBirds *[]*bird) {
imd.Color = colornames.Rosybrown
// Top left point.
imd.Push(pixel.Vec{X: menu.center.X - menu.width/2, Y: menu.upperYBound})
// Top right point.
imd.Push(pixel.Vec{X: menu.center.X + menu.width/2, Y: menu.upperYBound})
// Bottom left point.
imd.Push(pixel.Vec{X: menu.center.X - menu.width/2, Y: menu.lowerYBound})
// Bottom right point.
imd.Push(pixel.Vec{X: menu.center.X + menu.width/2, Y: menu.lowerYBound})
imd.Rectangle(0)
if win.JustPressed(pixelgl.KeyDown) {
if menu.selectedOptionNumber == menu.NumOptionIndexes() {
menu.selectedOptionNumber = 0
} else {
menu.selectedOptionNumber++
}
} else if win.JustPressed(pixelgl.KeyUp) {
if menu.selectedOptionNumber == 0 {
menu.selectedOptionNumber = menu.NumOptionIndexes()
} else {
menu.selectedOptionNumber--
}
}
// Print to the text.
menu.PrintMenuText(menu.selectedOptionNumber, context.Name())
if win.JustPressed(pixelgl.KeyEnter) {
// Only re-initialize the feeder context if a *different* one is selected.
if len(feederContexts) > menu.selectedOptionNumber && context.Name() != feederContexts[menu.selectedOptionNumber] {
// Reinitialize everything.
menu.ShowLoadingScreen(win, imd, canvas)
context = feederContextMappings[feederContexts[menu.selectedOptionNumber]]
context.Initialize()
*currentBirds = nil
initializeSounds()
} else if menu.selectedOptionNumber == menu.NumOptionIndexes() {
if soundDisabled {
enableSounds()
} else {
disableSounds()
}
return
}
menu.open = false
} else if win.JustPressed(pixelgl.KeyEscape) && !menu.justOpenedMenu {
menu.open = false
}
}
func (menu *pauseMenu) PreRender() {
menu.open = false
// Set the selected context number to the index of the current context.
for index, name := range feederContexts {
if context.Name() == name {
menu.selectedOptionNumber = index
break
}
}
// Set the geometry of the menu.
menu.center = pixel.V(0, 0)
menu.width = pauseMenuWidth
menu.height = pauseMenuHeight
menu.upperYBound = menu.center.Y + menu.height/2
menu.lowerYBound = menu.center.Y - menu.height/2
// Set location and intialize text object.
xValue := (menu.center.X - menu.width/2) + 50
yValue := menu.upperYBound - 65
atlas := text.NewAtlas(basicfont.Face7x13, text.ASCII)
menu.text = text.New(pixel.Vec{X: xValue, Y: yValue}, atlas)
// Print text.
menu.PrintMenuText(menu.selectedOptionNumber, context.Name())
}
func (menu *pauseMenu) PrintLoadingText() {
menu.text.Clear()
menu.text.Color = colornames.Pink
fmt.Fprintln(menu.text, "Loading...")
}
func (menu *pauseMenu) PrintMenuText(selectedContextNumber int, currentContextName string) {
menu.text.Clear()
menu.text.Color = colornames.White
fmt.Fprintln(menu.text, menuTitleText)
fmt.Fprintln(menu.text, "------------------------------")
menu.text.Color = colornames.Pink
fmt.Fprintln(menu.text, menuSubtitleText)
for index, name := range feederContexts {
// Show the current feeder in red.
if index == selectedContextNumber {
menu.text.Color = colornames.Red
} else {
menu.text.Color = colornames.Blue
}
if currentContextName == name {
name = name + " (current)"
}
fmt.Fprintln(menu.text, name)
}
fmt.Fprintln(menu.text) // New line.
menu.text.Color = colornames.White
fmt.Fprintln(menu.text, "------------------------------")
// Print the mute/unmute button.
if selectedContextNumber == len(feederContexts) {
menu.text.Color = colornames.Red
} else {
menu.text.Color = colornames.Blue
}
soundOption := "Mute"
if soundDisabled {
soundOption = "Unmute"
}
fmt.Fprintln(menu.text, soundOption)
menu.text.Color = colornames.White
fmt.Fprintln(menu.text, "------------------------------")
menu.text.Color = colornames.Pink
fmt.Fprintln(menu.text, menuAdditionalText)
menu.text.Color = colornames.White
fmt.Fprintln(menu.text, "------------------------------")
}