-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.chai
187 lines (166 loc) · 4.28 KB
/
main.chai
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
def conf(t) {
global width = 960
global height = 720
t.window.title = "Launcher"
t.version = "0.28.0"
t.identity = "launcher"
t.window.width = 960
t.window.height = 720
}
def loadFile(file) {
var data = love.filesystem.read(file)
if (data != "") {
for (item : from_json(data)) {
games.push_back(Game(to_string(item.first), to_string(item.second)))
}
}
}
def load() {
// Menu
global games = []
loadFile("chailove-launcher.json")
loadFile("libretro/saves/chailove-launcher.json")
loadFile("libretro/system/chailove-launcher.json")
if (games.size() == 0) {
games.push_back(Game("Missing chailove-launcher.json", "quit"))
love.window.showMessageBox("Create a chailove-launcher.json file. See documentation for more information.")
}
else {
games.push_back(Game("Quit", "quit"))
}
// Menu Functionality
global bgMoveLeft = true
global bgPosition = 0.0f
global currentSelection = 0
global lineHeight = 50
// Font
global fontSize = 40
global font = love.graphics.newFont("Resources/Font/Font.ttf", fontSize)
love.graphics.setFont(font)
// Background
love.graphics.setBackgroundColor(255, 255, 255)
global bg = love.graphics.newImage("Resources/Background/Background.png")
// Sounds
global soundBackgroundVolume = 0.3f
global soundBackground = love.audio.newSource("Resources/Music/Music.ogg")
soundBackground.setVolume(soundBackgroundVolume)
soundBackground.setLooping(true)
global mute = false
global soundFocus = love.audio.newSource("Resources/Sound/Focus.wav")
global soundSelect = love.audio.newSource("Resources/Sound/Select.wav")
love.audio.play(soundBackground)
// Logo
global logo = love.graphics.newImage("Resources/Logo/Logo.png")
// Mute
global muteImage = love.graphics.newImage("Resources/Mute.png")
}
def draw() {
// Draw the background elements.
love.graphics.clear()
// Background
if (love.config.options["highquality"]) {
var quad := love.graphics.newQuad(-bgPosition, 0, love.graphics.getWidth(), love.graphics.getHeight(), 0, 0)
love.graphics.draw(bg, quad)
}
// Logo
love.graphics.draw(logo, lineHeight, lineHeight)
// Mute button
if (mute) {
love.graphics.draw(muteImage, love.graphics.getWidth() - muteImage.getWidth() - lineHeight / 2.0f, lineHeight / 2.0f);
}
// Display each game.
var y = lineHeight * 2
var i = 0
for (game : games) {
// Black text and selector.
love.graphics.setColor(0, 0, 0)
// Display the menu cursor.
if (currentSelection == i) {
love.graphics.rectangle("fill", logo.getWidth() + lineHeight * 2, y + lineHeight / 4, lineHeight / 3, lineHeight / 3)
}
// Display the menu item
love.graphics.print(game.title, logo.getWidth() + lineHeight * 2.8f, y)
y = y + lineHeight
i = i + 1
}
}
def joystickpressed(joystick, button) {
switch (button) {
case ("down") {
currentSelection = currentSelection + 1
if (currentSelection >= games.size()) {
currentSelection = games.size() - 1
}
love.audio.play(soundFocus)
if (!mute) {
soundBackground.setVolume(soundBackgroundVolume)
}
break
}
case ("up") {
currentSelection = currentSelection - 1
if (currentSelection < 0) {
currentSelection = 0
}
love.audio.play(soundFocus)
if (!mute) {
soundBackground.setVolume(soundBackgroundVolume)
}
break
}
case ("start") {
// Fallthrough
}
case ("a") {
love.audio.play(soundSelect)
soundBackground.setVolume(0.0f)
launchGame(games[currentSelection])
break
}
case ("b") {
mute = !mute
if (mute) {
soundBackground.setVolume(0.0f)
}
else {
soundBackground.setVolume(soundBackgroundVolume)
}
break
}
}
}
def update(dt) {
// Animate the background.
if (bgMoveLeft) {
bgPosition = bgPosition - dt * 40.0f
}
else {
bgPosition = bgPosition + dt * 40.0f
}
// Make sure the background doesn't leave the background area.
if (bgPosition + bg.getWidth() <= love.graphics.getWidth()) {
bgMoveLeft = false
} else if (bgPosition >= 0.0f) {
bgMoveLeft = true
}
}
/**
* A menu list item.
*/
class Game {
def Game(title, executable) {
this.title = title
this.executable = executable
}
}
/**
* Launches the given game object.
*/
def launchGame(game) {
if (game.executable == "quit") {
love.event.quit()
} else {
love.window.showMessageBox("Launching " + game.title, 300)
love.system.execute(game.executable)
}
}