ListBox.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 Mouse = require(SLAB_PATH .. '.Internal.Input.Mouse')
  24. local Region = require(SLAB_PATH .. '.Internal.UI.Region')
  25. local Stats = require(SLAB_PATH .. '.Internal.Core.Stats')
  26. local Style = require(SLAB_PATH .. '.Style')
  27. local Text = require(SLAB_PATH .. '.Internal.UI.Text')
  28. local Window = require(SLAB_PATH .. '.Internal.UI.Window')
  29. local ListBox = {}
  30. local Instances = {}
  31. local ActiveInstance = nil
  32. local function GetItemInstance(Instance, Id)
  33. if Instance ~= nil then
  34. if Instance.Items[Id] == nil then
  35. local Item = {}
  36. Item.Id = Id
  37. Item.X = 0.0
  38. Item.Y = 0.0
  39. Item.W = 0.0
  40. Item.H = 0.0
  41. Instance.Items[Id] = Item
  42. end
  43. return Instance.Items[Id]
  44. end
  45. return nil
  46. end
  47. local function GetInstance(Id)
  48. if Instances[Id] == nil then
  49. local Instance = {}
  50. Instance.Id = Id
  51. Instance.X = 0.0
  52. Instance.Y = 0.0
  53. Instance.W = 0.0
  54. Instance.H = 0.0
  55. Instance.Items = {}
  56. Instance.ActiveItem = nil
  57. Instance.HotItem = nil
  58. Instance.Selected = false
  59. Instance.StatHandle = nil
  60. Instances[Id] = Instance
  61. end
  62. return Instances[Id]
  63. end
  64. function ListBox.Begin(Id, Options)
  65. local StatHandle = Stats.Begin('ListBox', 'Slab')
  66. Options = Options == nil and {} or Options
  67. Options.W = Options.W == nil and 150.0 or Options.W
  68. Options.H = Options.H == nil and 150.0 or Options.H
  69. Options.Clear = Options.Clear == nil and false or Options.Clear
  70. Options.Rounding = Options.Rounding == nil and Style.WindowRounding or Options.Rounding
  71. local Instance = GetInstance(Window.GetItemId(Id))
  72. local W = Options.W
  73. local H = Options.H
  74. if Options.Clear then
  75. Instance.Items = {}
  76. end
  77. W, H = LayoutManager.ComputeSize(W, H)
  78. LayoutManager.AddControl(W, H)
  79. local X, Y = Cursor.GetPosition()
  80. Instance.X = X
  81. Instance.Y = Y
  82. Instance.W = W
  83. Instance.H = H
  84. Instance.StatHandle = StatHandle
  85. ActiveInstance = Instance
  86. Cursor.SetItemBounds(X, Y, W, H)
  87. Cursor.AdvanceY(0.0)
  88. Window.AddItem(X, Y, W, H, Instance.Id)
  89. local IsObstructed = Window.IsObstructedAtMouse()
  90. local TX, TY = Window.TransformPoint(X, Y)
  91. local MouseX, MouseY = Window.GetMousePosition()
  92. Region.Begin(Instance.Id, {
  93. X = X,
  94. Y = Y,
  95. W = W,
  96. H = H,
  97. SX = TX,
  98. SY = TY,
  99. AutoSizeContent = true,
  100. NoBackground = true,
  101. Intersect = true,
  102. MouseX = MouseX,
  103. MouseY = MouseY,
  104. ResetContent = Window.HasResized(),
  105. IsObstructed = IsObstructed,
  106. Rounding = Options.Rounding
  107. })
  108. Instance.HotItem = nil
  109. MouseX, MouseY = Region.InverseTransform(Instance.Id, MouseX, MouseY)
  110. for K, V in pairs(Instance.Items) do
  111. if not IsObstructed
  112. and not Region.IsHoverScrollBar(Instance.Id)
  113. and V.X <= MouseX and MouseX <= V.X + Instance.W and V.Y <= MouseY and MouseY <= V.Y + V.H
  114. then
  115. Instance.HotItem = V
  116. end
  117. if Instance.HotItem == V or V.Selected then
  118. DrawCommands.Rectangle('fill', V.X, V.Y, Instance.W, V.H, Style.TextHoverBgColor)
  119. end
  120. end
  121. LayoutManager.Begin('Ignore', {Ignore = true})
  122. end
  123. function ListBox.BeginItem(Id, Options)
  124. Options = Options == nil and {} or Options
  125. Options.Selected = Options.Selected == nil and false or Options.Selected
  126. assert(ActiveInstance ~= nil, "Trying to call BeginListBoxItem outside of BeginListBox.")
  127. assert(ActiveInstance.ActiveItem == nil,
  128. "BeginListBoxItem was called for item '" .. (ActiveInstance.ActiveItem ~= nil and ActiveInstance.ActiveItem.Id or "nil") ..
  129. "' without a call to EndListBoxItem.")
  130. local Item = GetItemInstance(ActiveInstance, Id)
  131. Item.X = ActiveInstance.X
  132. Item.Y = Cursor.GetY()
  133. Cursor.SetX(Item.X)
  134. Cursor.AdvanceX(0.0)
  135. ActiveInstance.ActiveItem = Item
  136. ActiveInstance.ActiveItem.Selected = Options.Selected
  137. end
  138. function ListBox.IsItemClicked(Button, IsDoubleClick)
  139. assert(ActiveInstance ~= nil, "Trying to call IsItemClicked outside of BeginListBox.")
  140. assert(ActiveInstance.ActiveItem ~= nil, "IsItemClicked was called outside of BeginListBoxItem.")
  141. if ActiveInstance.HotItem == ActiveInstance.ActiveItem then
  142. Button = Button == nil and 1 or Button
  143. if IsDoubleClick then
  144. return Mouse.IsDoubleClicked(Button)
  145. else
  146. return Mouse.IsClicked(Button)
  147. end
  148. end
  149. return false
  150. end
  151. function ListBox.EndItem()
  152. assert(ActiveInstance ~= nil, "Trying to call BeginListBoxItem outside of BeginListBox.")
  153. assert(ActiveInstance.ActiveItem ~= nil, "Trying to call EndListBoxItem without calling BeginListBoxItem.")
  154. local ItemX, ItemY, ItemW, ItemH = Cursor.GetItemBounds()
  155. ActiveInstance.ActiveItem.W = ItemW
  156. ActiveInstance.ActiveItem.H = Cursor.GetLineHeight()
  157. Cursor.SetY(ActiveInstance.ActiveItem.Y + ActiveInstance.ActiveItem.H)
  158. Cursor.AdvanceY(0.0)
  159. ActiveInstance.ActiveItem = nil
  160. end
  161. function ListBox.End()
  162. assert(ActiveInstance ~= nil, "EndListBox was called without calling BeginListBox.")
  163. Region.End()
  164. Region.ApplyScissor()
  165. Cursor.SetItemBounds(ActiveInstance.X, ActiveInstance.Y, ActiveInstance.W, ActiveInstance.H)
  166. Cursor.SetPosition(ActiveInstance.X, ActiveInstance.Y)
  167. Cursor.AdvanceY(ActiveInstance.H)
  168. LayoutManager.End()
  169. Stats.End(ActiveInstance.StatHandle)
  170. ActiveInstance = nil
  171. end
  172. return ListBox