vision/src/client/ui/browser.lua

153 lines
3.5 KiB
Lua
Raw Normal View History

2023-09-04 18:20:02 +00:00
local lg = assert( love.graphics )
2023-09-04 18:31:47 +00:00
local scene = assert( require 'client.scene' )
2023-09-07 01:03:22 +00:00
local textInput = assert( require 'client.ui.textinput' )
local button = assert( require 'client.ui.button' )
local packet = assert( require 'shared.packet' )
local ipString = assert( require 'shared.ipstring' )
2023-09-04 18:20:02 +00:00
local browser = {}
local font = lg.newFont( "client/assets/fonts/Montserrat-Bold.ttf", 20 )
local function populateTestServers()
packet.get()
packet.serverInfo{
players = 24,
capacity = 255,
ip = ipString.new{ 123, 456, 789, 101 },
port = 51312,
version = 25,
svname = "test server",
map = "testMap_01",
}
packet.serverInfo{
players = 24,
capacity = 255,
ip = ipString.new{ 123, 456, 789, 101 },
port = 51312,
version = 25,
svname = "test server",
map = "testMap_02",
}
packet.serverInfo{
players = 24,
capacity = 255,
ip = ipString.new{ 150, 645, 151, 67 },
port = 51312,
version = 25,
svname = "test server",
map = "testMap_03",
}
packet.serverInfo{
players = 24,
capacity = 255,
ip = ipString.new{ 123, 456, 789, 101 },
port = 51312,
version = 25,
svname = "test server",
map = "testMap_01",
}
return packet.deserialise( packet.get() )
end
local serverList = {}
serverList.servers = populateTestServers()
serverList.offsets = {
15,
50,
150,
100,
100,
50,
50
2023-09-11 22:44:36 +00:00
}
serverList.selected = false
serverList.x = 25
serverList.y = 220
serverList.h = 36
2023-09-11 22:44:36 +00:00
function serverList.draw()
local gs = packet.getString
local x, y, h = serverList.x, serverList.y, serverList.h
local oldFont = lg.getFont()
lg.setFont( font )
for i, svInfo in ipairs( serverList.servers ) do
lg.setColor( 0.7, 0.7, 0.7, 0.7 )
lg.rectangle( "fill", x, y, 800, h, 5, 5 )
x = x + 15
y = y + 9
lg.setColor( 0, 0, 0, 0.7 )
lg.print( svInfo.version, x, y )
lg.print( gs( svInfo.svname ), 50 + x, y )
lg.print( tostring( svInfo.ip ), 200 + x, y )
lg.print( svInfo.port, 300 + x, y )
lg.print( svInfo.players, 400 + x, y )
lg.print( svInfo.capacity, 450 + x, y )
lg.print( gs( svInfo.map ), 500 + x, y )
y = y - 12
x = x - 15
y = y + h + 5
end
lg.setFont( oldFont )
2023-09-11 22:44:36 +00:00
end
function serverList.select()
2023-09-11 22:44:36 +00:00
end
function serverList.up()
serverList.selected = ( serverList.selected or 0 ) - 1
end
function serverList.down()
serverList.selected = ( serverList.selected or 0 ) + 1
end
2023-09-07 01:03:22 +00:00
local ti = textInput.new{
width = 300,
length = 20,
x = 15,
2023-09-11 22:44:36 +00:00
y = 175
2023-09-07 01:03:22 +00:00
}
2023-09-04 18:20:02 +00:00
function browser.draw()
2023-09-07 01:03:22 +00:00
lg.setColor( 1, 1, 1, 1 )
lg.print( "Server Browser", 15, 115 )
ti:draw()
serverList.draw()
2023-09-04 18:20:02 +00:00
end
function browser.update( dt )
2023-09-04 18:20:02 +00:00
end
function browser.onLoad( )
lg.setColor( 1, 1, 1, 1 )
end
2023-09-07 01:03:22 +00:00
function browser.mousepressed(x, y, button, istouch, pressed)
if ti:contains( x, y ) then return ti:enterText( browser.joinIPString ) end
end
function browser.joinIPString( s )
--Parse IP address and port from string. If it's valid, join the server.
2023-09-11 22:44:36 +00:00
print( "browser: Attempting to join server", s )
2023-09-07 01:03:22 +00:00
if not s then return end
ti:clear()
local valid, ip, port
2023-09-07 01:03:22 +00:00
if valid then return browser.joinIP( ip, port ) end
end
function browser.joinIP( ip, port )
2023-09-11 22:44:36 +00:00
print( "Joining server:", ip, port )
2023-09-07 01:03:22 +00:00
return scene.game{ serverIP = ip, serverPort = port }
2023-09-05 00:40:07 +00:00
end
function browser.keypressed( key, code, isRepeat )
if code == "escape" then return scene.mainmenu() end
2023-09-07 01:03:22 +00:00
if code == "return" then return ti:enterText( browser.joinIPString ) end
2023-09-05 00:40:07 +00:00
end
scene.browser = browser
return browser