-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenu.wren
113 lines (96 loc) · 2.88 KB
/
menu.wren
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
import "dome" for Process
import "graphics" for Canvas, ImageData
import "input" for Mouse
import "./api" for Button, Fading, Scene, Sprite
class Menu is Scene {
construct init (parent) {
_parent = parent
_prefs = parent.loadPrefs()
_mouseIsDown = true
// Set up the window and load in the files we need.
setupDrawLoop()
_background = Sprite.new("res/gridbg.png")
_title = Sprite.new("res/title.png", true)
Canvas.resize(_background.width, _background.height)
_cursor = Sprite.new("res/arrow.png", false)
// Load and play the background music
if (!_prefs["mute"]) { _channelID = Fading.play("bluebeat") }
// Needs to be done manually once, to ensure that everything is set up.
drawMainMenu()
draw(0)
}
drawMainMenu() {
// Create the functions used for the menu
var startPressed = Fn.new {
Fading.stop(_channelID)
_parent.loadScene("game")
}
var settingsPressed = Fn.new { drawSettingsMenu() }
var quitPressed = Fn.new { Process.exit() }
// Fill the button list
_buttons = [
Button.new("res/start_button.png", startPressed, true),
Button.new("res/settings_button.png", settingsPressed, true),
Button.new("res/quit_button.png", quitPressed, true)
]
// This function does all work needed to draw a menu
drawMenuCommon()
}
drawSettingsMenu() {
// Create the functions used for the menu
var mutePressed = Fn.new {
if (Fading.isPlaying(_channelID)) {
Fading.stopAllChannels()
_prefs["mute"] = true
} else {
_channelID = Fading.play("bluebeat")
_prefs["mute"] = false
}
_parent.savePrefs(_prefs)
drawSettingsMenu()
}
var menuPressed = Fn.new { drawMainMenu() }
// Fill the button list
var mute_img
if (_prefs["mute"]) {
mute_img = "res/mute_active_button.png"
} else {
mute_img = "res/mute_button.png"
}
_buttons = [
Button.new(mute_img, mutePressed, true),
Button.new("res/back_button.png", menuPressed, true)
]
drawMenuCommon()
}
drawMenuCommon() {
// Clear everything, and then draw the background
clearCanvasItems()
addCanvasItem(_background, 0, 0)
addCanvasItem(_title, 400, 120)
// Draw the buttons that we currently want
var y = 300
for (button in _buttons) {
addCanvasItem(button, 400, y)
button.hover = Fn.new { addTempCanvasItem(_cursor, 265, button.getSize()[0].y + 15) }
y = y + 75
}
}
mouseHandler() {
// We use this pair of bools to ensure that a mouse click is only interpreted once
// Kinda like using the SDL MouseEvent stuff, but not as good
_mouseWasDown = _mouseIsDown
_mouseIsDown = Mouse.isButtonPressed("left")
for (item in _buttons) {
var size = item.getSize()
if ( (size[0].x <= Mouse.x) && (size[0].y <= Mouse.y) && (size[1].x >= Mouse.x) && (size[1].y >= Mouse.y) ) {
item.onHover()
if (_mouseIsDown && (!_mouseWasDown)) { item.onClick() }
}
}
}
// Boilerplate + functions
static run(parent) {
return init(parent)
}
}