vision/src/client/ui/browser.lua

161 lines
4.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' )
2023-09-13 03:22:31 +00:00
local menu = assert( require 'client.ui.menu' )
local strings = assert( require 'client.assets.strings.strings' )
local utf8 = assert( require 'utf8' )
local gs = function( d )
local s = packet.getString( d )
local valid, trip = utf8.len( s )
return valid and s or --Regurgitate name.
( trip > 1 ) and s:sub( 1, trip - 1 ) or --Attempt truncation to initial valid utf8 sequence.
strings.utf8_error --Return fallback string indicating garbled mess.
end
2023-09-04 18:20:02 +00:00
local browser = {}
local test = assert( require 'client.test.browser' )
local font = lg.newFont( "client/assets/fonts/Montserrat-Bold.ttf", 12 )
local headerFont = lg.newFont( "client/assets/fonts/Montserrat-Bold.ttf", 36 )
local midFont = lg.newFont( "client/assets/fonts/Montserrat-Bold.ttf", 24 )
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
2023-09-13 03:22:31 +00:00
end
local serverButtons = test.getTestServers()
do
local cw = font:getWidth( "w" )
local headerText = lg.newText( font )
headerText:add( strings.svinfo_name, 0 )
headerText:add( strings.svinfo_map, cw * 16 )
headerText:add( strings.svinfo_ip, cw * 32 )
headerText:add( strings.svinfo_port, cw * ( 32 + 12 ) )
headerText:add( strings.svinfo_players, cw * ( 32 + 12 + 6 ) )
headerText:add( strings.svinfo_capacity, cw * ( 32 + 12 + 9 ) )
local headerButton = button{
space = 15, y = 135, text = headerText
2023-09-11 22:44:36 +00:00
}
for i, server in ipairs( serverButtons ) do
local text = lg.newText( font )
text:add( gs( server.svname ), 0 )
text:add( gs( server.map ), cw * 16 )
text:add( tostring( server.ip ), cw * 32 )
text:add( server.port, cw * ( 32 + 12 ) )
text:add( server.players, cw * ( 32 + 12 + 6 ) )
text:add( server.capacity, cw * ( 32 + 12 + 9 ) )
serverButtons[i] = button{ space = 0,
ip = tostring( server.ip ),
port = server.port,
text = text,
callback = joinServerCallback,
color = { 0.3 + 0.1 * (i % 2), 0.3 + 0.1 * (i % 2), 0.8, 0.5 }}
end
table.insert( serverButtons, 1, headerButton )
end
local serverList = menu.new{
name = "serverList",
buttons = serverButtons,
fg = nil,
bg = lg.newMesh{
{ 0, 0, 0, 0, 0.4, 0.05, 0.0, 0.1 },
{ 1, 0, 1, 0, 0.8, 0.3, 0.1, 0.8 },
{ 1, 1, 1, 1, 0.7, 0.4, 0.1, 0.8 },
{ 0, 1, 0, 1, 0.4, 0.05, 0.05, 0.1 },
},
font = font,
subScene = true }
serverList.selected = false
serverList.x = 25
serverList.y = 0
serverList.h = 36
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 = 150,
y = 75,
str = "8.8.8.8:1234"
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()
serverList.draw()
2023-09-07 01:03:22 +00:00
lg.setColor( 1, 1, 1, 1 )
lg.setFont( headerFont )
lg.print( strings.server_browser, 15, 15 )
lg.setFont( midFont )
lg.print( strings.ip_button, 15, 85 )
2023-09-07 01:03:22 +00:00
ti: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
function browser.mousemoved( x, y, dx, dy, istouch )
return serverList.mousemoved( x, y, dx, dy, istouch )
end
function browser.resize( x, y )
return serverList.resize( x, y )
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
return serverList.keypressed( key, code, isRepeat )
2023-09-05 00:40:07 +00:00
end
scene.browser = browser
return browser