chat.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. local Chat = class()
  2. local g = love.graphics
  3. local w, h = g.width, g.height
  4. function Chat:init()
  5. self.active = false
  6. self.message = ''
  7. self.log = ''
  8. self.timer = 0
  9. self.offset = -love.graphics.getWidth()
  10. self.richText = nil
  11. end
  12. function Chat:update()
  13. local u, v = ctx.hud.u, ctx.hud.v
  14. self.timer = timer.rot(self.timer)
  15. if self.active then self.timer = 2 end
  16. self.offset = math.lerp(self.offset, (self.timer == 0) and -(u * .35) - 4 or 0, math.min(tickRate * 30, 1))
  17. end
  18. function Chat:draw()
  19. local u, v = ctx.hud.u, ctx.hud.v
  20. local width = u * .35
  21. if not self.richText then return end
  22. g.setFont('pixel', 8)
  23. local font = g.getFont()
  24. local height = self.richText.height - 2
  25. if self.active then height = height + (font:getHeight() + 6.5) - 1 end
  26. g.setColor(0, 0, 0, 180)
  27. g.rectangle('fill', 4 + self.offset, v - (height + 4), width, height)
  28. g.setColor(30, 30, 30, 180)
  29. g.rectangle('line', 4 + self.offset, v - (height + 4), width, height)
  30. local yy = v - 4
  31. if self.active then
  32. g.setColor(255, 255, 255, 60)
  33. g.line(4.5 + self.offset, v - 4 - font:getHeight() - 6.5, 3 + width + self.offset, v - 4 - font:getHeight() - 6.5)
  34. g.setColor(255, 255, 255, 180)
  35. g.printf(self.message .. (self.active and '|' or ''), 4 + 4 + self.offset, math.round(yy - font:getHeight() - 5.5 + 2), width, 'left')
  36. yy = yy - font:getHeight() - 6.5
  37. end
  38. if self.richText then
  39. self.richText:draw(4 + 4 + self.offset, math.round(yy - self.richText.height + 4))
  40. end
  41. end
  42. function Chat:textinput(character)
  43. if self.active then
  44. self.message = self.message .. character
  45. ctx.event:emit('sound.play', {sound = 'click', gui = true})
  46. end
  47. end
  48. function Chat:keypressed(key)
  49. if self.active then
  50. if key == 'backspace' then self.message = self.message:sub(1, -2)
  51. elseif key == 'return' or key == 'escape' then
  52. if #self.message > 0 and key ~= 'escape' then
  53. ctx.net:send(app.net.messages.chat, {
  54. message = self.message
  55. })
  56. end
  57. self.active = false
  58. self.message = ''
  59. ctx.event:emit('sound.play', {sound = 'click', gui = true})
  60. end
  61. return true
  62. elseif key == 'return' then
  63. self.active = true
  64. self.message = ''
  65. ctx.event:emit('sound.play', {sound = 'click', gui = true})
  66. end
  67. end
  68. function Chat:add(data)
  69. local message = data.message
  70. local u, v = ctx.hud.u, ctx.hud.v
  71. local width = u * .35
  72. if #message > 0 then
  73. if #self.log > 0 then self.log = self.log .. '\n' end
  74. self.log = self.log .. message
  75. end
  76. g.setFont('pixel', 8)
  77. while g.getFont():getHeight() * select(2, g.getFont():getWrap(self.log, width)) > (v * .25 - 2) do
  78. self.log = self.log:sub(2)
  79. end
  80. self.log = '{white}' .. self.log
  81. self:refresh()
  82. self.timer = math.min(2 + (#message / 50), 5)
  83. end
  84. function Chat:resize()
  85. self:refresh()
  86. end
  87. function Chat:refresh(width)
  88. local u, v = ctx.hud.u, ctx.hud.v
  89. local width = u * .35
  90. self.richText = lib.richtext:new({self.log, width, white = {255, 255, 255}, purple = {190, 160, 220}, orange = {240, 160, 140}, red = {255, 0, 0}, green = {0, 255, 0}})
  91. end
  92. return Chat