71 lines
1.7 KiB
Lua
71 lines
1.7 KiB
Lua
local MainMenu = {}
|
|
local UI = require("ui")
|
|
local rgb = require("helpers.rgb")
|
|
local darken = require("helpers.darken")
|
|
local lighten = require("helpers.lighten")
|
|
local Gamestate = require("lib.hump.gamestate")
|
|
local http = require("socket.http")
|
|
local enet = require("enet")
|
|
|
|
local function getPublicIP()
|
|
local ip = http.request("https://ipinfo.io/ip")
|
|
return ip
|
|
end
|
|
|
|
local function splitat(str, char)
|
|
local index = str:find(char)
|
|
return str:sub(1, index-1), str:sub(index+1)
|
|
end
|
|
|
|
function MainMenu:init()
|
|
self.ui = UI.new{
|
|
font = love.graphics.newFont(32),
|
|
text_color = rgb(255, 255, 255),
|
|
bg_color = rgb(60, 60, 60),
|
|
bg_hover_color = {lighten(rgb(60, 60, 60))},
|
|
bg_pressed_color = {darken(rgb(60, 60, 60))},
|
|
}
|
|
|
|
self.addr_textbox = { text = "" }
|
|
|
|
self.host_socket = enet.host_create("*:0")
|
|
|
|
self.peer_socket = nil
|
|
self.hosting = false
|
|
self.connecting = false
|
|
end
|
|
|
|
function MainMenu:update()
|
|
local event = self.host_socket:service()
|
|
if event and event.type == "connect" then
|
|
Gamestate.switch(require("states.main"), self.host_socket)
|
|
end
|
|
end
|
|
|
|
function MainMenu:keypressed(...)
|
|
self.ui:keypressed(...)
|
|
end
|
|
|
|
function MainMenu:textinput(...)
|
|
self.ui:textinput(...)
|
|
end
|
|
|
|
function MainMenu:draw()
|
|
local w, h = love.graphics.getDimensions()
|
|
|
|
self.ui:textbox(self.addr_textbox, w/2-250, h/2-30, 280, 60)
|
|
if self.ui:button("Copy my address", w/2-50, h/2-100, 300, 60) then
|
|
local _, port = splitat(self.host_socket:get_socket_address(), ":")
|
|
local address = getPublicIP() .. ":" .. port
|
|
love.system.setClipboardText(address)
|
|
end
|
|
if self.ui:button("Connect", w/2+50, h/2-30, 200, 60) then
|
|
self.connecting = true
|
|
self.host_socket:connect(self.addr_textbox.text)
|
|
end
|
|
|
|
self.ui:postDraw()
|
|
end
|
|
|
|
return MainMenu
|