ComboBox.lua 5.1 KB

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