Tooltip.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 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 Utility = require(SLAB_PATH .. '.Internal.Core.Utility')
  28. local Tooltip = {}
  29. local LastDisplayTime = 0.0
  30. local AccumDisplayTime = 0.0
  31. local TooltipTime = 0.75
  32. local TooltipExpireTime = 0.025
  33. local Alpha = 0.0
  34. local OffsetY = 0.0
  35. local ResetSize = false
  36. function Tooltip.Begin(Tip)
  37. if Tip == nil or Tip == "" then
  38. return
  39. end
  40. local Elapsed = love.timer.getTime() - LastDisplayTime
  41. if Elapsed > TooltipExpireTime then
  42. AccumDisplayTime = 0.0
  43. Alpha = 0.0
  44. ResetSize = true
  45. end
  46. local DeltaTime = love.timer.getDelta()
  47. AccumDisplayTime = AccumDisplayTime + DeltaTime
  48. LastDisplayTime = love.timer.getTime()
  49. if AccumDisplayTime > TooltipTime then
  50. local X, Y = Mouse.Position()
  51. Alpha = math.min(Alpha + DeltaTime * 4.0, 1.0)
  52. local BgColor = Utility.MakeColor(Style.WindowBackgroundColor)
  53. local TextColor = Utility.MakeColor(Style.TextColor)
  54. BgColor[4] = Alpha
  55. TextColor[4] = Alpha
  56. local CursorX, CursorY = Cursor.GetPosition()
  57. LayoutManager.Begin('Ignore', {Ignore = true})
  58. Window.Begin('tooltip',
  59. {
  60. X = X,
  61. Y = Y - OffsetY,
  62. W = 0,
  63. H = 0,
  64. AutoSizeWindow = true,
  65. AutoSizeContent = false,
  66. AllowResize = false,
  67. AllowFocus = false,
  68. Layer = 'ContextMenu',
  69. ResetWindowSize = ResetSize,
  70. CanObstruct = false
  71. })
  72. Text.Begin(Tip, {Color = TextColor})
  73. OffsetY = Window.GetHeight()
  74. Window.End()
  75. LayoutManager.End()
  76. Cursor.SetPosition(CursorX, CursorY)
  77. ResetSize = false
  78. end
  79. end
  80. function Tooltip.GetDebugInfo()
  81. local Info = {}
  82. local Elapsed = love.timer.getTime() - LastDisplayTime
  83. table.insert(Info, string.format("Time: %.2f", AccumDisplayTime))
  84. table.insert(Info, string.format("Is Visible: %s", tostring(AccumDisplayTime > TooltipTime and Elapsed <= TooltipExpireTime)))
  85. table.insert(Info, string.format("Time to Display: %.2f", TooltipTime))
  86. table.insert(Info, string.format("Expire Time: %f", TooltipExpireTime))
  87. return Info
  88. end
  89. return Tooltip