-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.lua
566 lines (538 loc) · 20.7 KB
/
menu.lua
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
menu = {}
function menu.addButton(t)
local defaults = {
state = 'main',
text = 'Button',
font = fonts.c17,
type = 'default',
x = 480/2,
y = 270/2
-- active, action nil
}
for k, v in pairs(defaults) do
if t[k] == nil then t[k] = v end
end
if t.type == 'cycle' then
t.bw, t.bh = 0, 0
t.items = t.items or {'<item>'}
for _, v in pairs(t.items) do
t.bw = math.max(lume.round(t.font:getWidth(v) + 8), t.bw)
t.bh = math.max(lume.round(t.font:getHeight() + 4), t.bh)
end
else
t.bw = lume.round(t.font:getWidth(t.text) + 8)
t.bh = lume.round(t.font:getHeight() + 4)
end
t.bx = lume.round(t.x - t.bw/2)
t.by = lume.round(t.y - t.bh/2)
if not menu.buttons[t.state] then menu.buttons[t.state] = {} end
table.insert(menu.buttons[t.state], t)
return t
end
function menu.addSlider(t)
local defaults = {
state = 'main',
text = 'Slider',
font = fonts.c17,
value = 0.5,
x = 480/2,
y = 270/2,
w = 80
}
for k, v in pairs(defaults) do
if t[k] == nil then t[k] = v end
end
t.w = lume.round(t.w)
t.bw = t.w
t.bh = lume.round(t.font:getHeight() + 4)
t.bx = lume.round(t.x - t.bw/2)
t.by = lume.round(t.y - t.bh/2)
if not menu.sliders[t.state] then menu.sliders[t.state] = {} end
table.insert(menu.sliders[t.state], t)
return t
end
function menu.addInput(t)
local defaults = {
state = 'main',
text = 'Input',
font = fonts.c17,
value = '',
x = 480/2,
y = 270/2,
w = 80
}
for k, v in pairs(defaults) do
if t[k] == nil then t[k] = v end
end
t.w = lume.round(t.w)
t.bw = t.w
t.bh = lume.round(t.font:getHeight() + 4)
t.bx = lume.round(t.x - t.bw/2)
t.by = lume.round(t.y - t.bh/2)
if not menu.inputs[t.state] then menu.inputs[t.state] = {} end
table.insert(menu.inputs[t.state], t)
return t
end
function menu.addInfo(t)
local defaults = {
state = 'main',
text = 'Info',
font = fonts.c17,
x = 480/2,
y = 270/2
}
for k, v in pairs(defaults) do
if t[k] == nil then t[k] = v end
end
if not menu.infos[t.state] then menu.infos[t.state] = {} end
table.insert(menu.infos[t.state], t)
return t
end
menu.factoryDefaults = {
name = 'Player',
ip = '127.0.0.1',
port = '1357',
resolution = 1,
fullscreen = 1,
vsync = false,
cursorLock = false,
masterVolume = 0.4
}
-- mutable defaults
function menu.readDefaults()
local path = love.filesystem.getRealDirectory('menuDefaults.json') .. '/menuDefaults.json'
local file = io.open(path, 'rb')
local content = file:read('*a')
file:close()
menu.defaults = json.decode(content)
for k, v in pairs(menu.factoryDefaults) do
if menu.defaults[k] == nil then
menu.defaults[k] = v
end
end
end
function menu.writeDefaults()
local content = json.encode{
name = menu.nameInput.value,
ip = menu.ipInput.value,
port = menu.portInput.value,
resolution = menu.resolutionBtn.active,
fullscreen = menu.fullscreenBtn.active,
vsync = menu.vsyncBtn.active,
cursorLock = menu.cursorLockBtn.active,
masterVolume = menu.masterVolumeSlider.value
}
love.filesystem.write('menuDefaults.json', content)
end
function menu.applyOptions()
local w, h, flags = love.window.getMode()
local vsyncOn = flags.vsync ~= 0
flags.vsync = menu.vsyncBtn.active and 1 or 0
local fullscreen, fstype = love.window.getFullscreen()
local newResolution = menu.resolutionBtn.items[menu.resolutionBtn.active]
if not (fullscreen and fstype == 'desktop')
and newResolution ~= string.format('%sx%s', w, h) then
w, h = newResolution:match('(%d+)x(%d+)')
local wd, hd = love.window.getDesktopDimensions()
flags.x = wd/2 - w/2
flags.y = hd/2 - h/2
love.window.setMode(w, h, flags)
love.resize(w, h)
elseif vsyncOn ~= menu.defaults.vsync then
love.window.setMode(w, h, flags)
end
local currentWindowType = love.window.getFullscreen()
local newWindowType = menu.fullscreenBtn.items[menu.fullscreenBtn.active]
if newWindowType == 'Windowed'
and currentWindowType ~= 'Windowed' then
love.window.setFullscreen(false)
w, h = love.graphics.getDimensions()
love.resize(w, h)
elseif newWindowType == 'Borderless Fullscreen Windowed'
and currentWindowType ~= 'Borderless Fullscreen Windowed' then
love.window.setFullscreen(true, 'desktop')
w, h = love.graphics.getDimensions()
love.resize(w, h)
elseif newWindowType == 'Fullscreen'
and currentWindowType ~= 'Fullscreen' then
love.window.setFullscreen(true, 'exclusive')
w, h = love.graphics.getDimensions()
love.resize(w, h)
end
love.audio.setVolume(menu.masterVolumeSlider.value)
end
function menu.load()
menu.state = 'main'
menu.buttons = {}
menu.sliders = {}
menu.inputs = {}
menu.infos = {}
menu.activeInput = nil
menu.logoAnimTimer = 0
if not pcall(menu.readDefaults) then
-- write default if not exist or malformed
local content = json.encode(menu.factoryDefaults)
love.filesystem.write('menuDefaults.json', content)
menu.readDefaults()
end
-- main menu
local exitY = 220
local h = 40
menu.addButton{text='Play', y=exitY - h*2.5, action = function()
sound.play('select')
menu.state = 'play'
end}
menu.addButton{text='Options', y=exitY - h*1.5, action=function()
sound.play('select')
menu.state = 'options_video'
end}
menu.addButton{text='Exit', y=exitY, action = function()
love.event.quit()
end}
-- play menu
menu.nameInput = menu.addInput{state='play', text='Player Name', value=menu.defaults.name, x=gsx/2 - 70, y=exitY - h*2}
menu.addButton{state='play', text='Singleplayer', x=gsx/2 - 70, y=exitY - h*1, action=function()
sound.play('select')
chat.log = {}
server.start(nil, true)
client.connect('127.0.0.1', server.nutServer.port)
menu.state = 'connect'
menu.connectInfo.text = 'Starting Game'
end}
menu.ipInput = menu.addInput{state='play', text='IP', value=menu.defaults.ip, x=gsx/2 + 70, y=exitY - h*3}
menu.portInput = menu.addInput{state='play', text='Port', value=menu.defaults.port, x=gsx/2 + 70, y=exitY - h*2}
menu.addButton{state='play', text='Host', x=gsx/2 + 70 - 25, y=exitY - h*1, action=function()
sound.play('select')
chat.log = {}
-- todo: notify if not open or other err
-- remove whitespace
local port = menu.portInput.value:gsub("%s+", "")
server.start(port)
client.connect('127.0.0.1', port)
menu.state = 'connect'
menu.connectInfo.text = 'Starting Game'
end}
menu.addButton{state='play', text='Join', x=gsx/2 + 70 + 25, y=exitY - h*1, action=function()
sound.play('select')
chat.log = {}
local ip = menu.ipInput.value:gsub("%s+", "")
local port = menu.portInput.value:gsub("%s+", "")
client.connect(ip, port)
menu.state = 'connect'
menu.connectInfo.text = 'Starting Game'
end}
menu.addButton{state='play', text='Back', y=exitY, action=function()
sound.play('select2')
menu.state = 'main'
end}
-- connecting screen
menu.connectInfo = menu.addInfo{state='connect', text='[connection info]', y=gsy/2}
menu.addButton{state='connect', text='Cancel', y=exitY, action=function()
sound.play('select2')
menu.state = 'play'
if server.running then
server.close()
end
if client.connected then
client.close()
end
end}
-- options menu - video
menu.addButton{state='options_video', text='Video', x=40, y=20, action=function()
sound.play('select')
menu.state = 'options_video'
end}
menu.addButton{state='options_video', text='Audio', x=100, y=20, action=function()
sound.play('select')
menu.state = 'options_audio'
end}
menu.resolutionBtn = menu.addButton{state='options_video', text='Resolution', y=exitY - h*4,
type='cycle', items={'960x540', '1440x810', '1920x1080'},
active=menu.defaults.resolution, action=function(v)
sound.play('select')
local fullscreen, fstype = love.window.getFullscreen()
if not fullscreen then
local w, h, flags = love.window.getMode()
w, h = v:match('(%d+)x(%d+)')
local wd, hd = love.window.getDesktopDimensions()
flags.x = wd/2 - w/2
flags.y = hd/2 - h/2
love.window.setMode(w, h, flags)
love.resize(w, h)
end
end, draw=function(v, mx, my)
if mx > v.bx and mx < v.bx + v.bw and my > v.by and my < v.by + v.bh then
cursor.cursor = cursor.hand
love.graphics.setColor(0.3, 0.3, 0.3)
else
love.graphics.setColor(0.4, 0.4, 0.4)
end
local fullscreen, fstype = love.window.getFullscreen()
if fullscreen then
love.graphics.setColor(0.7, 0.7, 0.7)
end
love.graphics.rectangle('fill', v.bx, v.by, v.bw, v.bh)
love.graphics.setColor(0.8, 0.8, 0.8)
love.graphics.setFont(v.font)
text.print(v.text, lume.round(v.x - v.font:getWidth(v.text)/2), lume.round(v.by - v.font:getHeight()))
love.graphics.setColor(1, 1, 1)
local txt = v.items[v.active]
if fullscreen then
local w, h = love.graphics.getDimensions()
txt = w .. 'x' .. h
end
text.print(txt, lume.round(v.x - v.font:getWidth(txt)/2), lume.round(v.y - v.font:getHeight()/2))
end}
menu.fullscreenBtn = menu.addButton{state='options_video', text='Fullscreen', y=exitY - h*3,
type='cycle', items={'Windowed', 'Borderless Fullscreen Windowed', 'Fullscreen'},
active=menu.defaults.fullscreen, action=function(v)
sound.play('select')
if v == 'Windowed' then
local w, h, flags = love.window.getMode()
local rb = menu.resolutionBtn
w, h = rb.items[rb.active]:match('(%d+)x(%d+)')
flags.fullscreen = false
flags.fullscreentype = 'desktop'
love.window.setMode(w, h, flags)
love.resize(w, h)
local dw, dh = love.window.getDesktopDimensions(flags.display)
love.window.setPosition(lume.round(dw/2 - w/2), lume.round(dh/2 - h/2))
elseif v == 'Borderless Fullscreen Windowed' then
local w, h, flags = love.window.getMode()
w, h = love.window.getDesktopDimensions(flags.display)
flags.fullscreen = true
flags.fullscreentype = 'desktop'
love.window.setMode(w, h, flags)
love.resize(w, h)
elseif v == 'Fullscreen' then
local w, h, flags = love.window.getMode()
w, h = love.window.getDesktopDimensions(flags.display)
flags.fullscreen = true
flags.fullscreentype = 'exclusive'
love.window.setMode(w, h, flags)
love.resize(w, h)
end
end}
menu.vsyncBtn = menu.addButton{state='options_video', text='Vsync', y=exitY - h*2.2, type='toggle',
active=menu.defaults.vsync, action=function(v)
sound.play('select')
local w, h, flags = love.window.getMode()
flags.vsync = v and 1 or 0
love.window.setMode(w, h, flags)
end}
menu.cursorLockBtn = menu.addButton{state='options_video', text='Cursor Lock', y=exitY - h*1.4, type='toggle',
active=menu.defaults.cursorLock, action=function(v)
sound.play('select')
end}
menu.addButton{state='options_video', text='Back', y=exitY, action=function()
sound.play('select2')
menu.state = 'main'
end}
menu.addButton{state='options_video', text='Load Defaults', x=400, y=exitY, action=function()
sound.play('select')
local content = json.encode(menu.factoryDefaults)
love.filesystem.write('menuDefaults.json', content)
menu.load()
menu.state = 'options_video'
end}
-- options menu - audio
menu.addButton{state='options_audio', text='Video', x=40, y=20, action=function()
sound.play('select')
menu.state = 'options_video'
end}
menu.addButton{state='options_audio', text='Audio', x=100, y=20, action=function()
sound.play('select')
menu.state = 'options_audio'
end}
menu.masterVolumeSlider = menu.addSlider{state='options_audio', text='Master Volume',
w=120, value=menu.defaults.masterVolume, action=function(v)
--sound.play('select')
love.audio.setVolume(v)
end}
menu.addButton{state='options_audio', text='Back', y=exitY, action=function()
sound.play('select2')
menu.state = 'main'
end}
menu.addButton{state='options_audio', text='Load Defaults', x=400, y=exitY, action=function()
sound.play('select')
local content = json.encode(menu.factoryDefaults)
love.filesystem.write('menuDefaults.json', content)
menu.load()
menu.state = 'options_audio'
end}
menu.applyOptions()
end
function menu.update(dt)
menu.logoAnimTimer = menu.logoAnimTimer + dt
local mx, my = window2game(love.mouse.getPosition())
mx, my = lume.round(mx), lume.round(my)
if love.mouse.isDown(1) then
for _, v in pairs(menu.sliders[menu.state] or {}) do
if mx > v.bx and mx < v.bx + v.bw and my > v.by and my < v.by + v.bh then
v.value = (mx - v.bx)/v.bw
if v.action then v.action(v.value) end
uiMouseDown = true
return
end
end
end
end
function menu.mousepressed(mx, my, btn)
mx, my = window2game(mx, my)
mx, my = lume.round(mx), lume.round(my)
if gameState == 'menu' then
menu.activeInput = nil
for _, v in pairs(menu.buttons[menu.state] or {}) do
if mx > v.bx and mx < v.bx + v.bw and my > v.by and my < v.by + v.bh then
if v.type == 'toggle' then
v.active = not v.active
if v.action then v.action(v.active) end
elseif v.type == 'cycle' then
v.active = ((v.active - 1 + (btn == 2 and -1 or 1)) % #v.items) + 1
if v.action then v.action(v.items[v.active]) end
else
if v.action then v.action() end
end
uiMouseDown = true
return
end
end
for _, v in pairs(menu.inputs[menu.state] or {}) do
if mx > v.bx and mx < v.bx + v.bw and my > v.by and my < v.by + v.bh then
menu.activeInput = v
return
end
end
end
end
function menu.mousereleased(mx, my, btn)
end
function menu.textinput(t)
if gameState == 'menu' then
if menu.activeInput then
menu.activeInput.value = menu.activeInput.value .. t
end
end
end
function menu.keypressed(k, scancode, isrepeat)
if menu.state == 'play' then
if k == 'escape' and not isrepeat then
menu.state = 'main'
menu.activeInput = nil
end
elseif menu.state == 'connect' then
if k == 'escape' and not isrepeat then
menu.state = 'play'
menu.activeInput = nil
end
elseif menu.state == 'options_video' or menu.state == 'options_audio' then
if k == 'escape' and not isrepeat then
menu.state = 'main'
menu.activeInput = nil
end
end
if menu.activeInput then
if k == 'return' and not isrepeat then
menu.activeInput = nil
elseif k == 'backspace' then
menu.activeInput.value = menu.activeInput.value:sub(0, math.max(menu.activeInput.value:len()-1, 0))
elseif k == 'v' and (love.keyboard.isScancodeDown('lctrl') or love.keyboard.isScancodeDown('rctrl')) then
local paste = love.system.getClipboardText()
for v in paste:gmatch('.') do
-- todo: filter for input type (numerical ports etc)
menu.activeInput.value = menu.activeInput.value .. v
end
end
end
end
function menu.draw()
if gameState == 'menu' then
local mx, my = window2game(love.mouse.getPosition())
mx, my = lume.round(mx), lume.round(my)
if menu.state == 'main' then
love.graphics.setColor(1, 1, 1)
local logoFrameIdx = math.floor(menu.logoAnimTimer*12) % #anims.logo.quads + 1
local quad = anims.logo.quads[logoFrameIdx]
love.graphics.draw(gfx.logoAnim, quad, lume.round(gsx/2 - gfx.logo:getWidth()/2), 12)
end
for _, v in pairs(menu.buttons[menu.state] or {}) do
if v.draw then
v.draw(v, mx, my)
else
if mx > v.bx and mx < v.bx + v.bw and my > v.by and my < v.by + v.bh then
cursor.cursor = cursor.hand
love.graphics.setColor(0.3, 0.3, 0.3)
else
if v.type == 'toggle' and v.active then
love.graphics.setColor(0.25, 0.25, 0.25)
else
love.graphics.setColor(0.4, 0.4, 0.4)
end
end
love.graphics.rectangle('fill', v.bx, v.by, v.bw, v.bh)
local txt = v.text
love.graphics.setFont(v.font)
if v.type == 'cycle' then
love.graphics.setColor(0.8, 0.8, 0.8)
text.print(v.text, lume.round(v.x - v.font:getWidth(v.text)/2), lume.round(v.by - v.font:getHeight()))
txt = v.items[v.active]
end
love.graphics.setColor(1, 1, 1)
text.print(txt, lume.round(v.x - v.font:getWidth(txt)/2), lume.round(v.y - v.font:getHeight()/2))
end
end
for _, v in pairs(menu.sliders[menu.state] or {}) do
if v.draw then
v.draw(v, mx, my)
else
if mx > v.bx and mx < v.bx + v.bw and my > v.by and my < v.by + v.bh then
love.graphics.setColor(0.3, 0.3, 0.3)
love.graphics.rectangle('fill', v.bx, v.by, v.bw, v.bh)
love.graphics.setColor(0.4, 0.4, 0.4)
love.graphics.rectangle('fill', v.bx, v.by, v.bw*v.value, v.bh)
else
love.graphics.setColor(0.4, 0.4, 0.4)
love.graphics.rectangle('fill', v.bx, v.by, v.bw, v.bh)
love.graphics.setColor(0.5, 0.5, 0.5)
love.graphics.rectangle('fill', v.bx, v.by, v.bw*v.value, v.bh)
end
local txt = v.text
love.graphics.setFont(v.font)
love.graphics.setColor(0.8, 0.8, 0.8)
text.print(v.text, lume.round(v.x - v.font:getWidth(v.text)/2), lume.round(v.by - v.font:getHeight()))
end
end
for _, v in pairs(menu.inputs[menu.state] or {}) do
if v.draw then
v.draw(v, mx, my)
else
if mx > v.bx and mx < v.bx + v.bw and my > v.by and my < v.by + v.bh then
cursor.cursor = cursor.hand
if (menu.activeInput == v or menu.activeInput == nil) or menu.activeInput == v then
love.graphics.setColor(0.3, 0.3, 0.3)
end
else
love.graphics.setColor(0.6, 0.6, 0.6)
end
love.graphics.rectangle('fill', v.bx, v.by, v.bw, v.bh)
love.graphics.setColor(0.8, 0.8, 0.8)
love.graphics.setFont(v.font)
text.print(v.text, lume.round(v.x - v.font:getWidth(v.text)/2), lume.round(v.by - v.font:getHeight()))
local txt = v.value
if menu.activeInput == v then txt = txt .. (time % 1 < 0.5 and '' or '|') end
love.graphics.setColor(1, 1, 1)
love.graphics.setFont(v.font)
text.print(txt, lume.round(v.x - v.font:getWidth(txt)/2), lume.round(v.y - v.font:getHeight()/2))
end
end
for _, v in pairs(menu.infos[menu.state] or {}) do
if v.draw then
v.draw(v, mx, my)
else
love.graphics.setColor(1, 1, 1)
love.graphics.setFont(v.font)
text.print(v.text, lume.round(v.x - v.font:getWidth(v.text)/2), lume.round(v.y - v.font:getHeight()/2))
end
end
end
end