ui_checkbox.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. local l_gfx = love.graphics
  2. -- the checkbox class definition
  3. -- looks like a rectangle in a rectangle, changes its state on click and does stuff
  4. CheckBox = {}
  5. CheckBox.__index = CheckBox
  6. CheckBox.caption = "CheckBox"
  7. CheckBox.ident = "ui_checkbox"
  8. CheckBox.colorHighlight = {192,192,192,128}
  9. CheckBox.colorWeakFill = {64,64,64,128}
  10. CheckBox.name = "CheckBox"
  11. CheckBox.w = 16
  12. CheckBox.h = 16
  13. CheckBox.buttonStyle = false
  14. function CheckBox:new(name)
  15. local self = {}
  16. setmetatable(self,CheckBox)
  17. if name ~= nil then self.name = name end
  18. self.checked = false
  19. return self
  20. end
  21. setmetatable(CheckBox,{__index = UIElement})
  22. function CheckBox:mousepressed(x,y,b)
  23. if self:isMouseOver(x,y) then
  24. self.checked = not(self.checked) -- toggle checked state on click
  25. self:click(b) -- and do stuff after it
  26. end
  27. end
  28. function CheckBox:draw()
  29. local cr,cg,cb,ca = love.graphics.getColor()
  30. if self.buttonStyle == true then
  31. l_gfx.setColor(self.colorLine)
  32. l_gfx.rectangle("line",self.x,self.y,self.w,self.h)
  33. if self.checked == true then l_gfx.setColor(self.colorFill) else l_gfx.setColor(self.colorWeakFill) end
  34. l_gfx.rectangle("fill",self.x,self.y,self.w,self.h)
  35. l_gfx.setColor(self.colorFont)
  36. l_gfx.printf(self.caption,self.x,self.y+(self.h/2-7),self.w,"center")
  37. else
  38. l_gfx.setColor(self.colorHighlight)
  39. l_gfx.rectangle("line",self.x,self.y,16,16)
  40. if self.checked == true then
  41. l_gfx.setColor(self.colorHighlight)
  42. l_gfx.rectangle("fill",self.x+2,self.y+2,12,12)
  43. end
  44. l_gfx.setColor(self.colorFont)
  45. l_gfx.print(self.caption,self.x+18,self.y)
  46. end
  47. l_gfx.setColor(cr,cg,cb,ca)
  48. end