CheckBox.lua 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 Stats = require(SLAB_PATH .. '.Internal.Core.Stats')
  25. local Style = require(SLAB_PATH .. '.Style')
  26. local Text = require(SLAB_PATH .. '.Internal.UI.Text')
  27. local Tooltip = require(SLAB_PATH .. '.Internal.UI.Tooltip')
  28. local Window = require(SLAB_PATH .. '.Internal.UI.Window')
  29. local CheckBox = {}
  30. function CheckBox.Begin(Enabled, Label, Options)
  31. local StatHandle = Stats.Begin('CheckBox', 'Slab')
  32. Label = Label == nil and "" or Label
  33. Options = Options == nil and {} or Options
  34. Options.Tooltip = Options.Tooltip == nil and "" or Options.Tooltip
  35. Options.Id = Options.Id == nil and Label or Options.Id
  36. Options.Rounding = Options.Rounding == nil and Style.CheckBoxRounding or Options.Rounding
  37. Options.Size = Options.Size == nil and 16 or Options.Size
  38. local Id = Window.GetItemId(Options.Id and Options.Id or ('_' .. Label .. '_CheckBox'))
  39. local BoxW, BoxH = Options.Size, Options.Size
  40. local TextW, TextH = Text.GetSize(Label)
  41. local W = BoxW + Cursor.PadX() + 2.0 + TextW
  42. local H = math.max(BoxH, TextH)
  43. local Radius = Options.Size * 0.5
  44. LayoutManager.AddControl(W, H)
  45. local Result = false
  46. local Color = Style.ButtonColor
  47. local X, Y = Cursor.GetPosition()
  48. local MouseX, MouseY = Window.GetMousePosition()
  49. local IsObstructed = Window.IsObstructedAtMouse()
  50. if not IsObstructed and X <= MouseX and MouseX <= X + BoxW and Y <= MouseY and MouseY <= Y + BoxH then
  51. Color = Style.ButtonHoveredColor
  52. if Mouse.IsPressed(1) then
  53. Color = Style.ButtonPressedColor
  54. elseif Mouse.IsReleased(1) then
  55. Result = true
  56. end
  57. end
  58. DrawCommands.Rectangle('fill', X, Y, BoxW, BoxH, Color, Options.Rounding)
  59. if Enabled then
  60. DrawCommands.Cross(X + Radius, Y + Radius, Radius - 1.0, Style.CheckBoxSelectedColor)
  61. end
  62. if Label ~= "" then
  63. local CursorY = Cursor.GetY()
  64. Cursor.AdvanceX(BoxW + 2.0)
  65. LayoutManager.Begin('Ignore', {Ignore = true})
  66. Text.Begin(Label)
  67. LayoutManager.End()
  68. Cursor.SetY(CursorY)
  69. end
  70. if not IsObstructed and X <= MouseX and MouseX <= X + W and Y <= MouseY and MouseY <= Y + H then
  71. Tooltip.Begin(Options.Tooltip)
  72. Window.SetHotItem(Id)
  73. end
  74. Cursor.SetItemBounds(X, Y, W, H)
  75. Cursor.AdvanceY(H)
  76. Window.AddItem(X, Y, W, H, Id)
  77. Stats.End(StatHandle)
  78. return Result
  79. end
  80. return CheckBox