ui_textfield.lua 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. local l_gfx = love.graphics
  2. local utf8 = require("utf8")
  3. TextField = {}
  4. TextField.__index = TextField
  5. TextField.name = "TextField"
  6. TextField.ident = "ui_textfield"
  7. TextField.inContext = false
  8. TextField.limit = 26
  9. TextField.align = "left"
  10. function TextField:new(name)
  11. local self = setmetatable({},TextField)
  12. self.name = name or self.name
  13. self.text = ""
  14. return self
  15. end
  16. setmetatable(TextField,{__index = UIElement})
  17. function TextField:draw()
  18. local cr,cg,cb,ca = l_gfx.getColor()
  19. l_gfx.setColor(self.colorFill)
  20. l_gfx.rectangle("fill",self.x,self.y,self.w,self.h)
  21. l_gfx.setColor(self.colorFont)
  22. l_gfx.printf(self.text,self.x,self.y,self.w,self.align)
  23. if self.inContext == true then
  24. l_gfx.setColor(self.colorLine)
  25. l_gfx.rectangle("line",self.x,self.y,self.w,self.h)
  26. end
  27. l_gfx.setColor(cr,cg,cb,ca)
  28. end
  29. function TextField:mousepressed(x,y,b)
  30. if self:isMouseOver(x,y) then
  31. if b == "l" then
  32. self.inContext = true
  33. end
  34. self:click(b)
  35. else
  36. self.inContext = false
  37. end
  38. end
  39. function TextField:keypressed(key,isrepeat)
  40. if self.inContext == true and #self.text>0 and key == "backspace" then
  41. local l = string.len(self.text)
  42. if l>0 then
  43. self.text = string.sub(self.text,1,l-1)
  44. end
  45. end
  46. end
  47. function TextField:textinput(t)
  48. if self.inContext == true and #self.text<=self.limit then
  49. self.text = self.text .. t
  50. end
  51. end
  52. function TextField:clear()
  53. self.text = ""
  54. end