ComboBox.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 Input = require(SLAB_PATH .. '.Internal.UI.Input')
  23. local LayoutManager = require(SLAB_PATH .. '.Internal.UI.LayoutManager')
  24. local Mouse = require(SLAB_PATH .. '.Internal.Input.Mouse')
  25. local Region = require(SLAB_PATH .. '.Internal.UI.Region')
  26. local Stats = require(SLAB_PATH .. '.Internal.Core.Stats')
  27. local Style = require(SLAB_PATH .. '.Style')
  28. local Tooltip = require(SLAB_PATH .. '.Internal.UI.Tooltip')
  29. local Window = require(SLAB_PATH .. '.Internal.UI.Window')
  30. local ComboBox = {}
  31. local Instances = {}
  32. local Active = nil
  33. local MIN_WIDTH = 150.0
  34. local MIN_HEIGHT = 150.0
  35. local function GetInstance(Id)
  36. if Instances[Id] == nil then
  37. local Instance = {}
  38. Instance.IsOpen = false
  39. Instance.WasOpened = false
  40. Instance.WinW = 0.0
  41. Instance.WinH = 0.0
  42. Instance.StatHandle = nil
  43. Instances[Id] = Instance
  44. end
  45. return Instances[Id]
  46. end
  47. function ComboBox.Begin(Id, Options)
  48. local StatHandle = Stats.Begin('ComboBox', 'Slab')
  49. Options = Options ~= nil and Options or {}
  50. Options.Tooltip = Options.Tooltip == nil and "" or Options.Tooltip
  51. Options.W = Options.W == nil and MIN_WIDTH or Options.W
  52. Options.WinH = Options.WinH == nil and MIN_HEIGHT or Options.WinH
  53. Options.Selected = Options.Selected == nil and "" or Options.Selected
  54. Options.Rounding = Options.Rounding == nil and Style.ComboBoxRounding or Options.Rounding
  55. local Instance = GetInstance(Id)
  56. local WinItemId = Window.GetItemId(Id)
  57. local W = Options.W
  58. local H = Style.Font:getHeight()
  59. W = LayoutManager.ComputeSize(W, H)
  60. LayoutManager.AddControl(W, H)
  61. local X, Y = Cursor.GetPosition()
  62. local Radius = H * 0.35
  63. local InputBgColor = Style.ComboBoxColor
  64. local DropDownW = Radius * 4.0
  65. local DropDownX = X + W - DropDownW
  66. local DropDownColor = Style.ComboBoxDropDownColor
  67. local InputRounding = {Options.Rounding, 0, 0, Options.Rounding}
  68. local DropDownRounding = {0, Options.Rounding, Options.Rounding, 0}
  69. Instance.X = X
  70. Instance.Y = Y
  71. Instance.W = W
  72. Instance.H = H
  73. Instance.WinH = math.min(Instance.WinH, Options.WinH)
  74. Instance.StatHandle = StatHandle
  75. local MouseX, MouseY = Window.GetMousePosition()
  76. local MouseClicked = Mouse.IsClicked(1)
  77. Instance.WasOpened = Instance.IsOpen
  78. local IsObstructed = Window.IsObstructedAtMouse()
  79. local Hovered = not IsObstructed and X <= MouseX and MouseX <= X + W and Y <= MouseY and MouseY <= Y + H
  80. if Hovered then
  81. InputBgColor = Style.ComboBoxHoveredColor
  82. DropDownColor = Style.ComboBoxDropDownHoveredColor
  83. if MouseClicked then
  84. Instance.IsOpen = not Instance.IsOpen
  85. if Instance.IsOpen then
  86. Window.SetStackLock(Id .. '_combobox')
  87. end
  88. end
  89. end
  90. LayoutManager.Begin('Ignore', {Ignore = true})
  91. Input.Begin(Id .. '_Input', {
  92. ReadOnly = true,
  93. Text = Options.Selected,
  94. Align = 'left',
  95. W = math.max(W - DropDownW, DropDownW),
  96. H = H,
  97. BgColor = InputBgColor,
  98. Rounding = InputRounding
  99. })
  100. LayoutManager.End()
  101. Cursor.SameLine()
  102. DrawCommands.Rectangle('fill', DropDownX, Y, DropDownW, H, DropDownColor, DropDownRounding)
  103. DrawCommands.Triangle('fill', DropDownX + Radius * 2.0, Y + H - Radius * 1.35, Radius, 180, Style.ComboBoxArrowColor)
  104. Cursor.SetItemBounds(X, Y, W, H)
  105. Cursor.AdvanceY(H)
  106. if Hovered then
  107. Tooltip.Begin(Options.Tooltip)
  108. Window.SetHotItem(WinItemId)
  109. end
  110. Window.AddItem(X, Y, W, H, WinItemId)
  111. local WinX, WinY = Window.TransformPoint(X, Y)
  112. if Instance.IsOpen then
  113. LayoutManager.Begin('ComboBox', {Ignore = true})
  114. Window.Begin(Id .. '_combobox',
  115. {
  116. X = WinX - 1.0,
  117. Y = WinY + H,
  118. W = math.max(W, Instance.WinW),
  119. H = Instance.WinH,
  120. AllowResize = false,
  121. AutoSizeWindow = false,
  122. AllowFocus = false,
  123. Layer = Window.GetLayer(),
  124. AutoSizeContent = true
  125. })
  126. Active = Instance
  127. else
  128. Stats.End(Instance.StatHandle)
  129. end
  130. return Instance.IsOpen
  131. end
  132. function ComboBox.End()
  133. local Y = 0.0
  134. local H = 0.0
  135. local StatHandle = nil
  136. if Active ~= nil then
  137. Cursor.SetItemBounds(Active.X, Active.Y, Active.W, Active.H)
  138. Y, H = Active.Y, Active.H
  139. local ContentW, ContentH = Window.GetContentSize()
  140. Active.WinH = ContentH
  141. Active.WinW = math.max(ContentW, Active.W)
  142. StatHandle = Active.StatHandle
  143. if Mouse.IsClicked(1) and Active.WasOpened and not Region.IsHoverScrollBar(Window.GetId()) then
  144. Active.IsOpen = false
  145. Active = nil
  146. Window.SetStackLock(nil)
  147. end
  148. end
  149. Window.End()
  150. DrawCommands.SetLayer('Normal')
  151. LayoutManager.End()
  152. if Y ~= 0.0 and H ~= 0.0 then
  153. Cursor.SetY(Y)
  154. Cursor.AdvanceY(H)
  155. end
  156. Stats.End(StatHandle)
  157. end
  158. return ComboBox