ui_shape.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. local l_gfx = love.graphics
  2. -- these are various UI shapes
  3. Rectangle = {}
  4. Rectangle.__index = Rectangle
  5. Rectangle.ident = "ui_rectangle"
  6. Rectangle.name = "Rectangle"
  7. Rectangle.centerOnPos = false
  8. function Rectangle:new(name)
  9. local self = {}
  10. setmetatable(self,Rectangle)
  11. self.mode = "fill"
  12. if name ~= nil then self.name = name end
  13. return self
  14. end
  15. setmetatable(Rectangle,{__index = UIElement})
  16. function Rectangle:draw()
  17. local cr,cg,cb,ca = love.graphics.getColor()
  18. l_gfx.setColor(self.colorFill)
  19. if self.centerOnPos == true then
  20. l_gfx.rectangle(self.mode,self.x-self.w/2,self.y-self.h/2,self.w,self.h)
  21. else
  22. l_gfx.rectangle(self.mode,self.x,self.y,self.w,self.h)
  23. end
  24. l_gfx.setColor(cr,cg,cb,ca)
  25. end
  26. Circle = {}
  27. Circle.__index = Circle
  28. Circle.ident = "ui_circle"
  29. Circle.name = "Circle"
  30. Circle.centerOnPos = false
  31. function Circle:new()
  32. local self = {}
  33. setmetatable(self,Circle)
  34. self.mode = "fill"
  35. self.radius = 32
  36. self.segments = 20
  37. if name ~= nil then self.name = name end
  38. return self
  39. end
  40. setmetatable(Circle,{__index = UIElement})
  41. function Circle:draw()
  42. local cr,cg,cb,ca = love.graphics.getColor()
  43. l_gfx.setColor(self.colorFill)
  44. if self.centerOnPos == true then
  45. l_gfx.circle(self.mode,self.x,self.y,self.radius,self.segments)
  46. else
  47. l_gfx.circle(self.mode,self.x+self.radius,self.y+self.radius,self.radius,self.segments)
  48. end
  49. l_gfx.setColor(cr,cg,cb,ca)
  50. end
  51. Line = {}
  52. Line.__index = Line
  53. Line.ident = "ui_line"
  54. Line.name = "Line"
  55. Line.horizontal = false
  56. function Line:new(name)
  57. local self = {}
  58. setmetatable(self,Line)
  59. if name ~= nil then self.name = name end
  60. return self
  61. end
  62. setmetatable(Line,{__index = UIElement})
  63. function Line:draw()
  64. local cr,cg,cb,ca = love.graphics.getColor()
  65. l_gfx.setColor(self.colorFill)
  66. if self.horizontal == true then
  67. l_gfx.line(self.x.self.y,self.x+self.w,self.y)
  68. else
  69. l_gfx.line(self.x,self.y,self.x,self.y+self.h)
  70. end
  71. l_gfx.setColor(cr,cg,cb,ca)
  72. end
  73. Cross = {}
  74. Cross.__index = Cross
  75. Cross.name = "Cross"
  76. Cross.ident = "ui_cross"
  77. Cross.centerOnPos = false
  78. function Cross:new(name)
  79. local self = {}
  80. setmetatable(self,Cross)
  81. if name ~= nil then self.name = name end
  82. return self
  83. end
  84. setmetatable(Cross,{__index = UIElement})
  85. function Cross:draw()
  86. local cr,cg,cb,ca = love.graphics.getColor()
  87. local bm = l_gfx.getBlendMode()
  88. l_gfx.setBlendMode(self.blendMode)
  89. l_gfx.setColor(self.colorLine)
  90. if self.centerOnPos == true then
  91. l_gfx.line(self.x-self.w/2,self.y,self.x+self.w/2,self.y)
  92. l_gfx.line(self.x,self.y-self.h/2,self.x,self.y+self.h/2)
  93. else
  94. l_gfx.line(self.x,self.y+self.h/2,self.x+self.w,self.y+self.h/2)
  95. l_gfx.line(self.x+self.w/2,self.y,self.x+self.w/2,self.y+self.h)
  96. end
  97. l_gfx.setBlendMode(bm)
  98. l_gfx.setColor(cr,cg,cb,ca)
  99. end
  100. Arc = {}
  101. Arc.__index = Arc
  102. Arc.name = "Arc"
  103. Arc.ident = "ui_arc"
  104. Arc.centerOnPos = true
  105. Arc.mode = "fill"
  106. Arc.segments = 20
  107. function Arc:new(name)
  108. local self = {}
  109. setmetatable(self,Arc)
  110. self.a1 = 0
  111. self.a2 = 0
  112. self.radius = 16
  113. if name ~= nil then self.name = name end
  114. return self
  115. end
  116. setmetatable(Arc,{__index = UIElement})
  117. function Arc:draw(st)
  118. local self = st or self
  119. local cr,cg,cb,ca = love.graphics.getColor()
  120. l_gfx.setColor(self.colorFill)
  121. if self.centerOnPos == true then
  122. l_gfx.arc(self.mode,self.x,self.y,self.radius,self.a1,self.a2,self.segments)
  123. else
  124. l_gfx.arc(self.mode,self.x+self.radius,self.y+self.radius,self.radius,self.a1,self.a2,self.segments)
  125. end
  126. l_gfx.setColor(cr,cg,cb,ca)
  127. end
  128. function Arc:setAngle(a1,a2)
  129. self.a1,self.a2 = a1 or self.a1,a2 or self.a2
  130. end
  131. Polygon = {}
  132. Polygon.__index = Polygon
  133. Polygon.ident = "ui_polygon"
  134. Polygon.mode = "fill"
  135. Polygon.name = "Polygon"
  136. Polygon.colorFill = {32,64,64,48}
  137. function Polygon:new(name)
  138. local self = {}
  139. setmetatable(self,Polygon)
  140. if name ~= nil then self.name = name end
  141. return self
  142. end
  143. setmetatable(Polygon,{__index = UIElement})
  144. function Polygon:setVertices(v)
  145. if #v%2 == 0 then
  146. self.v = v
  147. end
  148. end
  149. function Polygon:setPosition(x,y)
  150. local px,py = self.x,self.y
  151. local dx,dy = x-px,y-py
  152. if self.v ~= nil then
  153. for i=1,#self.v,2 do
  154. self.v[i] = self.v[i]+dx
  155. self.v[i+1] = self.v[i+1]+dy
  156. end
  157. end
  158. end
  159. function Polygon:draw()
  160. if self.v ~= nil then
  161. local r,g,b,a = l_gfx.getColor()
  162. l_gfx.setColor(self.colorFill)
  163. l_gfx.polygon(self.mode,self.v)
  164. l_gfx.setColor(r,g,b,a)
  165. end
  166. end