local lg = assert( love.graphics ) local scene = assert( require 'client.scene' ) 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' ) 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 } serverList.selected = false serverList.x = 25 serverList.y = 220 serverList.h = 36 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 ) end function serverList.select() end function serverList.up() serverList.selected = ( serverList.selected or 0 ) - 1 end function serverList.down() serverList.selected = ( serverList.selected or 0 ) + 1 end local ti = textInput.new{ width = 300, length = 20, x = 15, y = 175 } function browser.draw() lg.setColor( 1, 1, 1, 1 ) lg.print( "Server Browser", 15, 115 ) ti:draw() serverList.draw() end function browser.update( dt ) end function browser.onLoad( ) lg.setColor( 1, 1, 1, 1 ) end 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. print( "browser: Attempting to join server", s ) if not s then return end ti:clear() local valid, ip, port if valid then return browser.joinIP( ip, port ) end end function browser.joinIP( ip, port ) print( "Joining server:", ip, port ) return scene.game{ serverIP = ip, serverPort = port } end function browser.keypressed( key, code, isRepeat ) if code == "escape" then return scene.mainmenu() end if code == "return" then return ti:enterText( browser.joinIPString ) end end scene.browser = browser return browser