login.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. local Login = class()
  2. local g = love.graphics
  3. function Login:activate()
  4. ctx.ribbon.count = 2
  5. ctx.ribbon.margin = .1
  6. ctx.input:clear()
  7. ctx.input:add('username', username or love.filesystem.read('username') or 'nick')
  8. end
  9. function Login:draw()
  10. local u, v = ctx.u, ctx.v
  11. local anchor = (.3 + (.8 - .3) / 2) * v
  12. local input = ctx.input
  13. g.setFont('BebasNeue', .065 * v)
  14. g.setColor(160, 160, 160)
  15. if input.focused == 'username' then g.setColor(220, 220, 220) else g.setColor(160, 160, 160) end
  16. g.printCenter('Nickname', u * .05, anchor - ctx.ribbon.margin * v / 2, false, true)
  17. g.printCenter(input:val('username'), .4 * u, anchor - ctx.ribbon.margin * v / 2, false, true)
  18. --[[if input.focused == 'password' then g.setColor(220, 220, 220) else g.setColor(160, 160, 160) end
  19. g.printCenter('Password', w(.05), anchor, false, true)
  20. g.printCenter(string.rep('•', #input:val('password')), w(.4), anchor, false, true)]]
  21. love.graphics.setColor(160, 160, 160)
  22. g.printCenter('Enter', .05 * u, anchor + ctx.ribbon.margin * v / 2, false, true)
  23. end
  24. function Login:mousepressed(x, y, button)
  25. local ribbon = ctx.ribbon:test(x, y)
  26. if ribbon == 1 then ctx.input:focus('username')
  27. elseif ribbon == 2 then self:login() end
  28. end
  29. function Login:keypressed(key)
  30. if key == 'return' then self:login()
  31. elseif key == 'backspace' and self.focused then
  32. self[self.focused] = self[self.focused]:sub(1, -2)
  33. end
  34. end
  35. function Login:textinput(char)
  36. if self.focused then
  37. self[self.focused] = self[self.focused] .. char
  38. end
  39. end
  40. function Login:login()
  41. local username = ctx.input:val('username')
  42. if #username == 0 then return end
  43. local success = app.net.goregous:login(username)
  44. if success then
  45. _G['username'] = username
  46. ctx.user.username = username
  47. ctx:push('main')
  48. else
  49. ctx.alert:show('Problem logging in.')
  50. end
  51. end
  52. return Login