Shape.lua 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. --[[
  2. MIT License
  3. Copyright (c) 2019 Mitchell Davis <coding.jackalope@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. --]]
  20. local Cursor = require(SLAB_PATH .. '.Internal.Core.Cursor')
  21. local DrawCommands = require(SLAB_PATH .. '.Internal.Core.DrawCommands')
  22. local LayoutManager = require(SLAB_PATH .. '.Internal.UI.LayoutManager')
  23. local Stats = require(SLAB_PATH .. '.Internal.Core.Stats')
  24. local Window = require(SLAB_PATH .. '.Internal.UI.Window')
  25. local Shape = {}
  26. local Curve = nil
  27. local CurveX, CurveY = 0, 0
  28. function Shape.Rectangle(Options)
  29. local StatHandle = Stats.Begin('Rectangle', 'Slab')
  30. Options = Options == nil and {} or Options
  31. Options.Mode = Options.Mode == nil and 'fill' or Options.Mode
  32. Options.W = Options.W == nil and 32 or Options.W
  33. Options.H = Options.H == nil and 32 or Options.H
  34. Options.Color = Options.Color == nil and nil or Options.Color
  35. Options.Rounding = Options.Rounding == nil and 2.0 or Options.Rounding
  36. Options.Outline = Options.Outline == nil and false or Options.Outline
  37. Options.OutlineColor = Options.OutlineColor == nil and {0.0, 0.0, 0.0, 1.0} or Options.OutlineColor
  38. Options.Segments = Options.Segments == nil and 10 or Options.Segments
  39. local W = Options.W
  40. local H = Options.H
  41. LayoutManager.AddControl(W, H)
  42. local X, Y = Cursor.GetPosition()
  43. if Options.Outline then
  44. DrawCommands.Rectangle('line', X, Y, W, H, Options.OutlineColor, Options.Rounding, Options.Segments)
  45. end
  46. DrawCommands.Rectangle(Options.Mode, X, Y, W, H, Options.Color, Options.Rounding, Options.Segments)
  47. Window.AddItem(X, Y, W, H)
  48. Cursor.SetItemBounds(X, Y, W, H)
  49. Cursor.AdvanceY(H)
  50. Stats.End(StatHandle)
  51. end
  52. function Shape.Circle(Options)
  53. local StatHandle = Stats.Begin('Circle', 'Slab')
  54. Options = Options == nil and {} or Options
  55. Options.Mode = Options.Mode == nil and 'fill' or Options.Mode
  56. Options.Radius = Options.Radius == nil and 12.0 or Options.Radius
  57. Options.Color = Options.Color == nil and nil or Options.Color
  58. Options.Segments = Options.Segments == nil and nil or Options.Segments
  59. local Diameter = Options.Radius * 2.0
  60. LayoutManager.AddControl(Diameter, Diameter)
  61. local X, Y = Cursor.GetPosition()
  62. local CenterX = X + Options.Radius
  63. local CenterY = Y + Options.Radius
  64. DrawCommands.Circle(Options.Mode, CenterX, CenterY, Options.Radius, Options.Color, Options.Segments)
  65. Window.AddItem(X, Y, Diameter, Diameter)
  66. Cursor.SetItemBounds(X, Y, Diameter, Diameter)
  67. Cursor.AdvanceY(Diameter)
  68. Stats.End(StatHandle)
  69. end
  70. function Shape.Triangle(Options)
  71. local StatHandle = Stats.Begin('Triangle', 'Slab')
  72. Options = Options == nil and {} or Options
  73. Options.Mode = Options.Mode == nil and 'fill' or Options.Mode
  74. Options.Radius = Options.Radius == nil and 12 or Options.Radius
  75. Options.Rotation = Options.Rotation == nil and 0 or Options.Rotation
  76. Options.Color = Options.Color == nil and nil or Options.Color
  77. local Diameter = Options.Radius * 2.0
  78. LayoutManager.AddControl(Diameter, Diameter)
  79. local X, Y = Cursor.GetPosition()
  80. local CenterX = X + Options.Radius
  81. local CenterY = Y + Options.Radius
  82. DrawCommands.Triangle(Options.Mode, CenterX, CenterY, Options.Radius, Options.Rotation, Options.Color)
  83. Window.AddItem(X, Y, Diameter, Diameter)
  84. Cursor.SetItemBounds(X, Y, Diameter, Diameter)
  85. Cursor.AdvanceY(Diameter)
  86. Stats.End(StatHandle)
  87. end
  88. function Shape.Line(X2, Y2, Options)
  89. local StatHandle = Stats.Begin('Line', 'Slab')
  90. Options = Options == nil and {} or Options
  91. Options.Width = Options.Width == nil and 1.0 or Options.Width
  92. Options.Color = Options.Color == nil and nil or Options.Color
  93. local X, Y = Cursor.GetPosition()
  94. local W, H = math.abs(X2 - X), math.abs(Y2 - Y)
  95. H = math.max(H, Options.Width)
  96. DrawCommands.Line(X, Y, X2, Y2, Options.Width, Options.Color)
  97. Window.AddItem(X, Y, W, H)
  98. Cursor.SetItemBounds(X, Y, W, H)
  99. Cursor.AdvanceY(H)
  100. Stats.End(StatHandle)
  101. end
  102. function Shape.Curve(Points, Options)
  103. local StatHandle = Stats.Begin('Curve', 'Slab')
  104. Options = Options == nil and {} or Options
  105. Options.Color = Options.Color == nil and nil or Options.Color
  106. Options.Depth = Options.Depth == nil and nil or Options.Depth
  107. Curve = love.math.newBezierCurve(Points)
  108. local MinX, MinY = math.huge, math.huge
  109. local MaxX, MaxY = 0, 0
  110. for I = 1, Curve:getControlPointCount(), 1 do
  111. local PX, PY = Curve:getControlPoint(I)
  112. MinX = math.min(MinX, PX)
  113. MinY = math.min(MinY, PY)
  114. MaxX = math.max(MaxX, PX)
  115. MaxY = math.max(MaxY, PY)
  116. end
  117. local W = math.abs(MaxX - MinX)
  118. local H = math.abs(MaxY - MinY)
  119. LayoutManager.AddControl(W, H)
  120. CurveX, CurveY = Cursor.GetPosition()
  121. Curve:translate(CurveX, CurveY)
  122. DrawCommands.Curve(Curve:render(Options.Depth), Options.Color)
  123. Window.AddItem(MinX, MinY, W, H)
  124. Cursor.SetItemBounds(MinX, MinY, W, H)
  125. Cursor.AdvanceY(H)
  126. Stats.End(StatHandle)
  127. end
  128. function Shape.GetCurveControlPointCount()
  129. if Curve ~= nil then
  130. return Curve:getControlPointCount()
  131. end
  132. return 0
  133. end
  134. function Shape.GetCurveControlPoint(Index, Options)
  135. Options = Options == nil and {} or Options
  136. Options.LocalSpace = Options.LocalSpace == nil and true or Options.LocalSpace
  137. local X, Y = 0, 0
  138. if Curve ~= nil then
  139. if Options.LocalSpace then
  140. Curve:translate(-CurveX, -CurveY)
  141. end
  142. X, Y = Curve:getControlPoint(Index)
  143. if Options.LocalSpace then
  144. Curve:translate(CurveX, CurveY)
  145. end
  146. end
  147. return X, Y
  148. end
  149. function Shape.EvaluateCurve(Time, Options)
  150. Options = Options == nil and {} or Options
  151. Options.LocalSpace = Options.LocalSpace == nil and true or Options.LocalSpace
  152. local X, Y = 0, 0
  153. if Curve ~= nil then
  154. if Options.LocalSpace then
  155. Curve:translate(-CurveX, -CurveY)
  156. end
  157. X, Y = Curve:evaluate(Time)
  158. if Options.LocalSpace then
  159. Curve:translate(CurveX, CurveY)
  160. end
  161. end
  162. return X, Y
  163. end
  164. function Shape.Polygon(Points, Options)
  165. local StatHandle = Stats.Begin('Polygon', 'Slab')
  166. Options = Options == nil and {} or Options
  167. Options.Color = Options.Color == nil and nil or Options.Color
  168. Options.Mode = Options.Mode == nil and 'fill' or Options.Mode
  169. local MinX, MinY = math.huge, math.huge
  170. local MaxX, MaxY = 0, 0
  171. local Verts = {}
  172. for I = 1, #Points, 2 do
  173. MinX = math.min(MinX, Points[I])
  174. MinY = math.min(MinY, Points[I+1])
  175. MaxX = math.max(MaxX, Points[I])
  176. MaxY = math.max(MaxY, Points[I+1])
  177. end
  178. local W = math.abs(MaxX - MinX)
  179. local H = math.abs(MaxY - MinY)
  180. LayoutManager.AddControl(W, H)
  181. MinX, MinY = math.huge, math.huge
  182. MaxX, MaxY = 0, 0
  183. local X, Y = Cursor.GetPosition()
  184. for I = 1, #Points, 2 do
  185. table.insert(Verts, Points[I] + X)
  186. table.insert(Verts, Points[I+1] + Y)
  187. MinX = math.min(MinX, Verts[I])
  188. MinY = math.min(MinY, Verts[I+1])
  189. MaxX = math.max(MaxX, Verts[I])
  190. MaxY = math.max(MaxY, Verts[I+1])
  191. end
  192. DrawCommands.Polygon(Options.Mode, Verts, Options.Color)
  193. Window.AddItem(MinX, MinY, W, H)
  194. Cursor.SetItemBounds(MinX, MinY, W, H)
  195. Cursor.AdvanceY(H)
  196. Stats.End(StatHandle)
  197. end
  198. return Shape