vision/src/client/ui/browser.lua

176 lines
4.2 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-13 03:22:31 +00:00
local menu = assert( require 'client.ui.menu' )
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{
2023-09-13 03:22:31 +00:00
players = 8,
capacity = 255,
ip = ipString.new{ 123, 456, 789, 101 },
port = 51312,
version = 25,
svname = "test server",
map = "testMap_02",
}
packet.serverInfo{
2023-09-13 03:22:31 +00:00
players = 14,
capacity = 64,
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
2023-09-13 03:22:31 +00:00
local function joinServerCallback( button )
if button.ip and button.port then return browser.joinIP( button.ip, button.port ) end
end
local serverButtons = populateTestServers()
for i, server in ipairs( serverButtons ) do
serverButtons[i] = button{
ip = tostring( server.ip ),
port = server.port,
text = tostring( server.ip ),
callback = joinServerCallback}
end
local serverList = menu.new( "serverList",
serverButtons,
nil,
nil,
nil,
true )
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-13 03:22:31 +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-13 03:22:31 +00:00
end]]
2023-09-11 22:44:36 +00:00
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-13 03:22:31 +00:00
browser.selected = false
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( )
2023-09-13 03:22:31 +00:00
serverList:onLoad()
2023-09-04 18:20:02 +00:00
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
2023-09-13 03:22:31 +00:00
return serverList.mousepressed( x, y, button, istouch, pressed )
2023-09-07 01:03:22 +00:00
end
function browser.joinIPString( s )
--Parse IP address and port from string. If it's valid, join the server.
2023-09-13 03:22:31 +00:00
print( "browser: entered IP and port", s )
2023-09-07 01:03:22 +00:00
if not s then return end
ti:clear()
2023-09-13 03:22:31 +00:00
local ip, port = s:match '(%d+%.%d+%.%d+%.%d+)', s:match ':(%d+)'
print( "browser:", "ip:", ip, port )
2023-09-13 03:22:31 +00:00
if ip and port then return browser.joinIP( ip, port ) end
2023-09-07 01:03:22 +00:00
end
function browser.joinIP( ip, port )
2023-09-11 22:44:36 +00:00
print( "Joining server:", ip, port )
2023-09-13 03:22:31 +00:00
return scene.loadScene( scene.connecting, { ip = ip, port = 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