ui_image.lua 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. local l_gfx = love.graphics
  2. -- this elements just draws an image which you can specify with SetImage method
  3. -- you can also set offset,scale,angle,skew and make it display its border
  4. Image = {}
  5. Image.__index = Image
  6. Image.ident = "ui_image"
  7. Image.name = "Image"
  8. Image.caption = ""
  9. Image.ox = 0
  10. Image.oy = 0
  11. Image.sx = 1
  12. Image.sy = 1
  13. Image.r = 0
  14. Image.kx = 0
  15. Image.ky = 0
  16. Image.showBorder = false
  17. Image.colorTint = {255,255,255,255}
  18. Image.mode = "alpha"
  19. function Image:new(name)
  20. local self = {}
  21. setmetatable(self,Image)
  22. if name ~= nil then self.name = name end
  23. return self
  24. end
  25. setmetatable(Image,{__index = UIElement})
  26. function Image:draw()
  27. if self.img ~= nil then
  28. local r,g,b,a = l_gfx.getColor()
  29. l_gfx.setColor(self.colorTint)
  30. if self.stencil ~= nil then
  31. l_gfx.stencil(self.stencil)
  32. end
  33. local mode = l_gfx.getBlendMode()
  34. l_gfx.setBlendMode(self.mode)
  35. l_gfx.draw(self.img, self.x,self.y,self.r,self.sx,self.sy,self.ox,self.oy,self.kx,self.ky)
  36. l_gfx.setBlendMode(mode)
  37. l_gfx.setStencilTest()
  38. if self.showBorder == true then
  39. l_gfx.setColor(self.colorLine)
  40. local w,h = self.img:getWidth(),self.img:getHeight()
  41. l_gfx.rectangle("line",self.x-1,self.y-1,w+2,h+2)
  42. end
  43. l_gfx.setColor(r,g,b,a)
  44. end
  45. end
  46. function Image:setImage(img)
  47. if type(img) == "string" then
  48. self.img = l_gfx.newImage(img)
  49. else
  50. self.img = img
  51. end
  52. end
  53. QuadSlider = {}
  54. QuadSlider.__index = QuadSlider
  55. QuadSlider.name = "QuadSlider"
  56. QuadSlider.ident = "ui_quadslider"
  57. function QuadSlider:new(name)
  58. local self = setmetatable({},QuadSlider)
  59. self.name = name or self.name
  60. self.index = 1
  61. return self
  62. end
  63. setmetatable(QuadSlider,{__index = Image})
  64. function QuadSlider:setQuads(q)
  65. self.quads = q
  66. end
  67. function QuadSlider:draw()
  68. if self.img ~= nil and self.quads ~= nil and self.index<=#self.quads then
  69. l_gfx.setColor(self.colorTint)
  70. l_gfx.draw(self.img, self.quads[self.index], self.x,self.y,self.r,self.sx,self.sy,self.ox,self.oy,self.kx,self.ky)
  71. if self.showBorder == true then
  72. l_gfx.setColor(self.colorLine)
  73. local w,h = self.img:getWidth(),self.img:getHeight()
  74. l_gfx.rectangle("line",self.x-1,self.y-1,w+2,h+2)
  75. end
  76. end
  77. end