Menu.lua 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 MenuState = require(SLAB_PATH .. '.Internal.UI.MenuState')
  23. local Mouse = require(SLAB_PATH .. '.Internal.Input.Mouse')
  24. local Style = require(SLAB_PATH .. '.Style')
  25. local Text = require(SLAB_PATH .. '.Internal.UI.Text')
  26. local Window = require(SLAB_PATH .. '.Internal.UI.Window')
  27. local Menu = {}
  28. local Instances = {}
  29. local Pad = 8.0
  30. local LeftPad = 25.0
  31. local RightPad = 70.0
  32. local CheckSize = 5.0
  33. local OpenedContextMenu = nil
  34. local CursorStack = {}
  35. local function ConstrainPosition(X, Y, W, H)
  36. local ResultX, ResultY = X, Y
  37. local Right = X + W
  38. local Bottom = Y + H
  39. local OffsetX = Right >= love.graphics.getWidth()
  40. local OffsetY = Bottom >= love.graphics.getHeight()
  41. if OffsetX then
  42. ResultX = X - (Right - love.graphics.getWidth())
  43. end
  44. if OffsetY then
  45. ResultY = Y - H
  46. end
  47. local WinX, WinY, WinW, WinH = Window.GetBounds()
  48. if OffsetX then
  49. ResultX = WinX - W
  50. end
  51. ResultX = math.max(ResultX, 0.0)
  52. ResultY = math.max(ResultY, 0.0)
  53. return ResultX, ResultY
  54. end
  55. local function BeginWindow(Id, X, Y)
  56. local Instance = Instances[Id]
  57. if Instance ~= nil then
  58. X, Y = ConstrainPosition(X, Y, Instance.W, Instance.H)
  59. end
  60. Window.Begin(Id,
  61. {
  62. X = X,
  63. Y = Y,
  64. W = 0.0,
  65. H = 0.0,
  66. AllowResize = false,
  67. AllowFocus = false,
  68. Border = 0.0,
  69. AutoSizeWindow = true,
  70. Layer = 'ContextMenu',
  71. BgColor = Style.MenuColor,
  72. Rounding = {0, 0, 2, 2}
  73. })
  74. end
  75. function Menu.BeginMenu(Label)
  76. local Result = false
  77. local X, Y = Cursor.GetPosition()
  78. local IsMenuBar = Window.IsMenuBar()
  79. local Id = Window.GetId() .. "." .. Label
  80. local Win = Window.Top()
  81. local Options = {IsSelectable = true, SelectOnHover = true, IsSelected = Win.Selected == Id}
  82. if IsMenuBar then
  83. Options.IsSelectableTextOnly = true
  84. Options.Pad = Pad * 2
  85. else
  86. Cursor.SetX(X + LeftPad)
  87. end
  88. local MenuX = 0.0
  89. local MenuY = 0.0
  90. Result = Text.Begin(Label, Options)
  91. local ItemX, ItemY, ItemW, ItemH = Cursor.GetItemBounds()
  92. if IsMenuBar then
  93. Cursor.SameLine()
  94. if Result then
  95. if Mouse.IsClicked(1) then
  96. MenuState.WasOpened = MenuState.IsOpened
  97. MenuState.IsOpened = not MenuState.IsOpened
  98. end
  99. end
  100. if MenuState.IsOpened then
  101. MenuState.RequestClose = Mouse.IsClicked(1) and MenuState.WasOpened
  102. if Result then
  103. Win.Selected = Id
  104. end
  105. else
  106. Win.Selected = nil
  107. end
  108. MenuX = X
  109. MenuY = Y + Window.GetHeight()
  110. else
  111. local WinX, WinY, WinW, WinH = Window.GetBounds()
  112. local H = Style.Font:getHeight()
  113. local TriX = WinX + WinW - H * 0.75
  114. local TriY = Y + H * 0.5
  115. local Radius = H * 0.35
  116. DrawCommands.Triangle('fill', TriX, TriY, Radius, 90, Style.TextColor)
  117. MenuX = X + WinW
  118. MenuY = Y
  119. if Result then
  120. Win.Selected = Id
  121. end
  122. Window.AddItem(ItemX, ItemY, ItemW + RightPad, ItemH)
  123. end
  124. Result = Win.Selected == Id
  125. if Result then
  126. local CursorX, CursorY = Cursor.GetPosition()
  127. table.insert(CursorStack, 1, {X = CursorX, Y = CursorY})
  128. BeginWindow(Id, MenuX, MenuY)
  129. end
  130. return Result
  131. end
  132. function Menu.MenuItem(Label)
  133. Cursor.SetX(Cursor.GetX() + LeftPad)
  134. local Result = Text.Begin(Label, {IsSelectable = true, SelectOnHover = true})
  135. local ItemX, ItemY, ItemW, ItemH = Cursor.GetItemBounds()
  136. Window.AddItem(ItemX, ItemY, ItemW + RightPad, ItemH)
  137. if Result then
  138. local Win = Window.Top()
  139. Win.Selected = nil
  140. Result = Mouse.IsClicked(1)
  141. if Result then
  142. MenuState.IsOpened = false
  143. end
  144. end
  145. return Result
  146. end
  147. function Menu.MenuItemChecked(Label, IsChecked)
  148. local X, Y = Cursor.GetPosition()
  149. local Result = Menu.MenuItem(Label)
  150. if IsChecked then
  151. local H = Style.Font:getHeight()
  152. DrawCommands.Check(X + LeftPad * 0.5, Y + H * 0.5, CheckSize, Style.TextColor)
  153. end
  154. return Result
  155. end
  156. function Menu.Separator()
  157. local Ctx = Context.Top()
  158. if Ctx.Type == 'Menu' then
  159. local Item = GetItem("Sep_" .. Ctx.Data.SeparatorId)
  160. Item.IsSeparator = true
  161. Ctx.Data.SeparatorId = Ctx.Data.SeparatorId + 1
  162. end
  163. end
  164. function Menu.EndMenu()
  165. local Id = Window.GetId()
  166. if Instances[Id] == nil then
  167. Instances[Id] = {}
  168. end
  169. Instances[Id].W = Window.GetWidth()
  170. Instances[Id].H = Window.GetHeight()
  171. Window.End()
  172. if #CursorStack > 0 then
  173. local Top = CursorStack[1]
  174. Cursor.SetPosition(Top.X, Top.Y)
  175. table.remove(CursorStack, 1)
  176. end
  177. end
  178. function Menu.Pad()
  179. return Pad
  180. end
  181. function Menu.BeginContextMenu(Options)
  182. Options = Options == nil and {} or Options
  183. local BaseId = nil
  184. local Id = nil
  185. if Options.IsWindow then
  186. BaseId = Window.GetId()
  187. elseif Options.IsItem then
  188. BaseId = Window.GetContextHotItem()
  189. if BaseId == nil then
  190. BaseId = Window.GetHotItem()
  191. end
  192. end
  193. if Options.IsItem and Window.GetLastItem() ~= BaseId then
  194. return false
  195. end
  196. if BaseId ~= nil then
  197. Id = BaseId .. '.ContextMenu'
  198. end
  199. if Id == nil then
  200. return false
  201. end
  202. if MenuState.IsOpened and OpenedContextMenu ~= nil then
  203. if OpenedContextMenu.Id == Id then
  204. local CursorX, CursorY = Cursor.GetPosition()
  205. table.insert(CursorStack, 1, {X = CursorX, Y = CursorY})
  206. BeginWindow(OpenedContextMenu.Id, OpenedContextMenu.X, OpenedContextMenu.Y)
  207. return true
  208. end
  209. return false
  210. end
  211. local IsOpening = false
  212. if not Window.IsObstructedAtMouse() and Window.IsMouseHovered() and Mouse.IsClicked(2) then
  213. local IsValidWindow = Options.IsWindow and Window.GetHotItem() == nil
  214. local IsValidItem = Options.IsItem
  215. if IsValidWindow or IsValidItem then
  216. MenuState.IsOpened = true
  217. IsOpening = true
  218. end
  219. end
  220. if IsOpening then
  221. local X, Y = Mouse.Position()
  222. X, Y = ConstrainPosition(X, Y, 0.0, 0.0)
  223. OpenedContextMenu = {Id = Id, X = X, Y = Y, Win = Window.Top()}
  224. Window.SetContextHotItem(Options.IsItem and BaseId or nil)
  225. end
  226. return false
  227. end
  228. function Menu.EndContextMenu()
  229. Menu.EndMenu()
  230. end
  231. function Menu.Close()
  232. MenuState.WasOpened = MenuState.IsOpened
  233. MenuState.IsOpened = false
  234. MenuState.RequestClose = false
  235. if OpenedContextMenu ~= nil then
  236. OpenedContextMenu.Win.ContextHotItem = nil
  237. OpenedContextMenu = nil
  238. end
  239. end
  240. return Menu