Tooltip.lua 3.2 KB

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