-
Notifications
You must be signed in to change notification settings - Fork 6
/
menu.lua
370 lines (311 loc) · 10.1 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
local menu = {
ip = ""
}
local scr, listScr
Images = require "images"
local playernameInput, addressInput
local ERROR_MSG = nil
local ERROR_TIMER = 0
function menu:init()
scr = ui:newScreen( "menu" )
scr:addPanel( "centerPanel",
50,
love.graphics.getHeight()/2-200,
300, 350 )
local y = 0
scr:addHeader( "centerPanel", "welcome", 0, y, "Welcome!" )
y = y + 30
scr:addText( "centerPanel", "welcometxt", 10, y, nil, 7, "Start a server or join someone else's server. Press 'H' for help.")
y = y + 40
playernameInput = scr:addInput( "centerPanel", "name", 10, y, nil, 20, "p", menu.playername )
playernameInput:setContent( PLAYERNAME )
y = y + 40
--scr:addText( "centerPanel", "h1", 10, y, nil, 7, "{h}Server:")
scr:addHeader( "centerPanel", "h1", 0, y, "Server:" )
y = y + 20
scr:addFunction( "centerPanel", "serverOnline", 10, y, "Start Server (Online)", "s", function() menu.startServer("online") end )
y = y + 20
scr:addFunction( "centerPanel", "serverLAN", 10, y, "Start Server (LAN)", "a", function() menu.startServer("lan") end )
y = y + 40
--scr:addText( "centerPanel", "h2", 10, y, nil, 7, "{h}Client:")
scr:addHeader( "centerPanel", "h2", 0, y, "Client:" )
y = y + 20
--addressInput = scr:addInput( "centerPanel", "ip", 10, y, nil, 20, "i", menu.ipEntered )
--addressInput:setContent( ADDRESS )
--y = y + 20
scr:addFunction( "centerPanel", "connect", 10, y, "Connect", "c", menu.showServerList )
y = y + 40
scr:addFunction( "centerPanel", "options", 10, y, "Options", "o", menu.toggleOptions )
y = y + 20
scr:addFunction( "centerPanel", "help", 10, y, "Help", "h", menu.toggleHelp )
y = y + 20
scr:addFunction( "centerPanel", "close", 10, y, "Quit", "q", love.event.quit )
listScr = ui:newScreen( "Serverlist" )
listScr:addPanel( "headerPanel",
60, 60,
love.graphics.getWidth()-120, 50 )
listScr:addHeader( "headerPanel", "h", 20, 10, "Servers:" )
listScr:addFunction( "headerPanel", "return", love.graphics.getWidth() - 260, 10, "Return", "q", menu.show )
listScr:addFunction( "headerPanel", "refresh", love.graphics.getWidth() - 360, 10, "Refresh", "r", menu.showServerList )
end
function menu.playername( name )
name = name:gsub( "|", "" )
name = name:gsub( " ", "" )
if #name > 0 then
PLAYERNAME = name
config.setValue( "PLAYERNAME", PLAYERNAME )
end
if playernameInput then
playernameInput:setContent( PLAYERNAME )
end
end
function menu:show()
STATE = "Menu"
ui:setActiveScreen( scr )
menu.ip = ADDRESS
if addressInput then
addressInput:setContent( ADDRESS )
end
chat:reset()
stats:clear()
map:reset()
menu:closeConnectPanel()
network.advertise:stop()
ERROR_TIMER = 0
end
function menu.showServerList()
if serverList ~= nil then
listScr:removeList( serverList.name )
end
network.advertise.callbacks.newEntryOnline = newServerListEntryRemote
network.advertise.callbacks.newEntryLAN = newServerListEntryLocal
network.advertise.callbacks.requestWarnings = function( msg )
menu:newWarning( "Could not load online list:\n" .. msg ) end
local list = {}
serverList = listScr:newList( 60, 120, love.graphics.getWidth() - 132, list, 15 )
--network:requestServerList( GAME_ID, MAIN_SERVER_URL )
--network:requestServerListLAN( GAME_ID )
network.advertise:setURL( MAIN_SERVER_URL )
network.advertise:setID( GAME_ID )
network.advertise:request( "both" )
ui:setActiveScreen( listScr )
end
function newServerListEntryRemote( entry )
if serverList then
-- Event to be called when clicking the button:
local event = function()
menu.connect( entry.address, entry.port)
end
local servername, otherinfo = entry.info:match("Name:(.-),(.*)")
otherinfo = otherinfo:gsub(",","\t")
otherinfo = otherinfo:gsub(":",": ")
local item = {
--txt = "(Online) " .. entry.address .. "\t" .. entry.info:gsub(",","\t"):gsub(":", ": "),
txt = "(Online) " .. servername .. "\t" .. otherinfo,
event = event
}
serverList:addListItem( item )
end
end
function newServerListEntryLocal( entry )
if serverList then
-- Event to be called when clicking the button:
local event = function()
menu.connect( entry.address, entry.port)
end
local servername, otherinfo = entry.info:match("Name:(.-),(.*)")
otherinfo = otherinfo:gsub(",","\t")
local item = {
txt = "(LAN) " .. entry.address .. "\t" .. entry.info:gsub(",","\t"):gsub(":", ": "),
event = event
}
serverList:addListItem( item )
end
end
function menu:closeConnectPanel()
scr:removePanel( "connectPanel" )
end
function menu:update( dt )
if ERROR_TIMER > 0 then
ERROR_TIMER = ERROR_TIMER - dt
end
end
function menu:draw()
x, y, tbl = love.window.getMode()
love.graphics.setColor( 255,255,255,255 )
love.graphics.draw(images["Logo.png"], x/2, 50, 0, 1, 1, 0, 0, 0, 0)
--love.graphics.draw(images["Logo.png"], 0, 0, 0, 1, 1, 0, 0, 0, 0)
if ERROR_TIMER > 0 then
love.graphics.setColor( 0, 0, 0, 200 )
love.graphics.rectangle( "fill", 60, love.graphics.getHeight() - 70,
love.graphics.getWidth() - 120, 60 )
love.graphics.setColor( 255,128,0, 200 )
love.graphics.printf( ERROR_MSG, 60, love.graphics.getHeight() - 60,
love.graphics.getWidth() - 120, "center" )
end
end
function menu:keypressed( key )
end
function menu:mousepressed( button, x, y )
end
function menu.startServer( lan )
if lan == "lan" then
LAN_ONLY = true
else
LAN_ONLY = false
end
local err
server, err = network:startServer( MAX_PLAYERS, PORT )
if server then
-- set client callbacks:
setServerCallbacks( server )
updateAdvertisementInfo()
network.advertise:setURL( MAIN_SERVER_URL )
network.advertise:setID( GAME_ID )
if LAN_ONLY then
network.advertise:start( server, "lan" )
else
network.advertise:start( server, "both" )
end
else
menu:errorMsg( "Error:", err )
end
-- Also start a client!
if server then
menu.ipEntered( 'localhost' )
menu.connect()
end
end
function menu.ipEntered( ip )
menu.ip = ip
end
function menu.connect( ip, port )
local y = 0
scr:addPanel( "connectPanel",
love.graphics.getWidth()/2 - 175,
love.graphics.getHeight()-100,
350, 50 )
scr:addHeader( "connectPanel", "hConnect", 0, 0, "Connecting" )
scr:addText( "connectPanel", "connectTxt", 10, 15, nil, 7, "Connecting to: '" ..
(ip or menu.ip) .. "'...")
local err
client, err = network:startClient( ip or menu.ip, PLAYERNAME, port or PORT, VERSION )
if client then
-- set client callbacks:
setClientCallbacks( client )
if not server then -- only save address if this is not a server (in this case ADDRESS would be localhost)
config.setValue( "ADDRESS", menu.ip )
end
--ui:setActiveScreen( nil )
else
print("Could not conect:", client )
local commands = {}
commands[1] = { txt = "Ok", key = "y" }
listScr:newMsgBox( "Error:", "Could not connect:" .. (err or ""), nil, nil, nil, commands)
menu:closeConnectPanel()
end
end
function menu:authorized( auth, reason )
if not auth then
self:errorMsg( "Could not connect: ", reason )
menu:closeConnectPanel()
end
end
function menu:errorMsg( header, msg )
local commands = {}
commands[1] = { txt = "Ok", key = "y" }
scr:newMsgBox( header, msg, nil, nil, nil, commands)
end
function menu:toggleHelp()
if scr:panelByName( "optionsPanel" ) ~= nil then
scr:removePanel( "optionsPanel" )
end
if scr:panelByName( "helpPanel" ) ~= nil then
scr:removePanel( "helpPanel" )
else
local width = 600
local x = math.min( 450, love.graphics.getWidth() - width - 50 )
local y = 0
scr:addPanel( "helpPanel",
x,
love.graphics.getHeight()/2-150,
width, 320 )
scr:addHeader( "helpPanel", "h1", 0, y, "Help:" )
y = y + 30
--scr:addText( "helpPanel", "helpText1", 10, y, nil, 7, "Press {f}'p'{p}")
scr:addText( "helpPanel", "helpText", 10, y, nil, 7, "To change a server's setting go to:{g}\n " .. love.filesystem.getSaveDirectory() .. "/config.txt{p}\n\nPress {f}'p'{p} to change your playername.\n{f}'s'{p} starts a server.\nUse {f}'c'{p} to see a server list.\nIf the server is not in your LAN, but on the web, then the server must probably port-forward port " .. PORT .. " on his/her router.\n\nTo play your own maps, put them into the following folder:\n{g} " ..love.filesystem.getSaveDirectory() .. "/maps/" )
end
end
function menu:toggleOptions()
if scr:panelByName( "helpPanel" ) ~= nil then
scr:removePanel( "helpPanel" )
end
if scr:panelByName( "optionsPanel" ) ~= nil then
scr:removePanel( "optionsPanel" )
else
local width = 300
local x = math.min( 450, love.graphics.getWidth() - width - 50 )
local y = 0
scr:addPanel( "optionsPanel",
x,
love.graphics.getHeight()/2-150,
width, 320 )
scr:addHeader( "optionsPanel", "h1", 0, y, "Options:" )
y = y + 30
scr:addHeader( "optionsPanel", "h2", 0, y, "Width and Height:" )
y = y + 20
local input
local w, h, flags = love.window.getMode()
input = scr:addInput( "optionsPanel", "width", 10, y, nil, 20, "1", menu.widthEntered )
input:setContent( tostring(w) )
y = y + 20
input = scr:addInput( "optionsPanel", "height", 10, y, nil, 20, "2", menu.heightEntered )
input:setContent( tostring(h) )
y = y + 40
if flags.fullscreen then
scr:addFunction( "optionsPanel", "fullscreen", 10, y, "Fullscreen (on)", "f", menu.fullscreen )
else
scr:addFunction( "optionsPanel", "fullscreen", 10, y, "Fullscreen (off)", "f", menu.fullscreen )
end
end
end
function menu.widthEntered( txt )
local num = tonumber(txt)
if num then
ok = pcall( love.window.setMode, num, HEIGHT, {fullscreen = FULLSCREEN} )
if ok then
WIDTH = num
config.setValue( "WIDTH", num )
love.load()
menu:toggleOptions()
end
end
end
function menu.heightEntered( txt )
local num = tonumber(txt)
if num then
ok = pcall( love.window.setMode, WIDTH, num, {fullscreen = FULLSCREEN} )
if ok then
HEIGHT = num
config.setValue( "HEIGHT", num )
love.load()
menu:toggleOptions()
end
end
end
function menu.fullscreen()
local w, h, flags = love.window.getMode()
local fscr = not flags.fullscreen
ok = pcall( love.window.setMode, WIDTH, HEIGHT, {fullscreen = fscr} )
if ok then
config.setValue( "FULLSCREEN", fscr )
FULLSCREEN = fscr
love.load()
menu:toggleOptions()
end
end
function menu:newWarning( msg )
ERROR_MSG = msg
ERROR_TIMER = 10
end
return menu