Advertisement
alex290

Tooltip.lua

Feb 24th, 2025
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.36 KB | None | 0 0
  1. --!optimize 2
  2.  
  3. local TextService = game:GetService("TextService")
  4.  
  5. local root = script.Parent.Parent
  6.  
  7. local functions = root.Functions
  8. local shadow = require(functions.Shadow)
  9.  
  10. local services = root.Services
  11. local tweens = require(services.Tweens)
  12.  
  13. local gui = root.Gui
  14. local colors = require(gui.Colors)
  15.  
  16. local core = require(gui.Core)
  17. local mouseInBounds = core.MouseInBounds
  18. local instanceEvent = core.InstanceEvent
  19. local event = core.Event
  20.  
  21. local maxWidth = 115
  22.  
  23. local container = Instance.new("Frame")
  24. container.BackgroundTransparency = 1
  25. container.BorderSizePixel = 0
  26. container.ClipsDescendants = true
  27. container.Visible = false
  28. container.Name = "Tooltip"
  29. container.Parent = core.Window
  30. local frame = Instance.new("Frame")
  31. frame.BackgroundColor3 = colors.Tooltip
  32. frame.BorderColor3 = colors.Border.Primary
  33. frame.ZIndex = 2147483647 -- int-32 limit.
  34. frame.Parent = container
  35. local label = Instance.new("TextLabel")
  36. label.Position = UDim2.fromOffset(6, 6)
  37. label.Size = UDim2.new(0, maxWidth, 1, 0)
  38. label.BackgroundTransparency = 1
  39. label.Font = Enum.Font.SourceSans
  40. label.RichText = true
  41. label.TextSize = 14
  42. label.TextColor3 = colors.Text.Primary
  43. label.TextXAlignment = Enum.TextXAlignment.Left
  44. label.TextYAlignment = Enum.TextYAlignment.Top
  45. label.TextWrapped = true
  46. label.Text = ""
  47. label.ZIndex = 2147483647 -- int-32 limit.
  48. label.Parent = frame
  49.  
  50. shadow(frame, true)
  51.  
  52. local function updatePosition()
  53.     if container.Visible == false then return end
  54.    
  55.     local mousePosition = core.MousePosition
  56.     container.Position = UDim2.fromOffset(
  57.         mousePosition.X + 3,
  58.         mousePosition.Y + 16
  59.     )
  60. end
  61. event.MouseMove:Connect(updatePosition)
  62.  
  63. local toggled = true
  64.  
  65. local wasVisible = false
  66. local becameVisible = false
  67. local moveUpdate = false
  68.  
  69. type Module = {
  70.     Setup: (area: GuiBase2d, tooltip: string) -> (),
  71.     Toggle: (state: boolean) -> ()
  72. }
  73. local module = {}
  74. module.Toggle = function(state)
  75.     if state == true then
  76.         toggled = true
  77.     else
  78.         toggled = false
  79.         container.Visible = false
  80.     end
  81. end
  82. module.Setup = function(area, tooltip)
  83.     local size = TextService:GetTextSize(tooltip, 14, Enum.Font.SourceSans, Vector2.new(maxWidth, math.huge))
  84.     local containerSize = UDim2.fromOffset(size.X + 18, size.Y + 22)
  85.     size = UDim2.fromOffset(size.X + 12, size.Y + 16)
  86.    
  87.     local thread = nil
  88.     local moveConnection = nil
  89.     local clickConnection = nil
  90.    
  91.     local function hover()
  92.         thread = coroutine.create(function()
  93.             task.wait(0.7)
  94.            
  95.             moveConnection:Disconnect()
  96.             moveConnection = nil
  97.            
  98.             local mousePosition = core.MousePosition
  99.             container.Position = UDim2.fromOffset(
  100.                 mousePosition.X + 3,
  101.                 mousePosition.Y + 16
  102.             )
  103.             frame.Position = UDim2.fromOffset(1, -(containerSize.Y.Offset + 1))
  104.             frame.Size = size
  105.             container.Size = containerSize
  106.             label.Text = tooltip
  107.             container.Visible = true
  108.             tweens:Create(frame, TweenInfo.new(0.0667, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {Position = UDim2.fromOffset(1, 1)}):Play()
  109.            
  110.             task.wait(10)
  111.            
  112.             container.Visible = false
  113.             if moveConnection == nil then
  114.                 moveConnection = event.MouseMove:Connect(function()
  115.                     if toggled == false then return end
  116.                     if mouseInBounds(area) == true then
  117.                         if thread ~= nil then coroutine.close(thread) end
  118.                         hover()
  119.                     end
  120.                 end)
  121.             end
  122.             thread = nil
  123.         end)
  124.         coroutine.resume(thread)
  125.     end
  126.    
  127.     instanceEvent.MouseEnter(area, function()
  128.         if toggled == false then return end
  129.         if thread ~= nil then return end
  130.        
  131.         if container.Visible == false and wasVisible == false then
  132.             moveConnection = event.MouseMove:Connect(function()
  133.                 if toggled == false then return end
  134.                 if mouseInBounds(area) == true then
  135.                     if thread ~= nil then coroutine.close(thread) end
  136.                     hover()
  137.                 end
  138.             end)
  139.             clickConnection = event.MouseButton1Down:Connect(function()
  140.                 if thread ~= nil then
  141.                     coroutine.close(thread)
  142.                     thread = nil
  143.                 end
  144.                 if moveConnection == nil then
  145.                     moveConnection = event.MouseMove:Connect(function()
  146.                         if toggled == false then return end
  147.                         if mouseInBounds(area) == true then
  148.                             if thread ~= nil then coroutine.close(thread) end
  149.                             hover()
  150.                         end
  151.                     end)
  152.                 end
  153.                 container.Visible = false
  154.             end)
  155.             hover()
  156.         else
  157.             local mousePosition = core.MousePosition
  158.             container.Position = UDim2.fromOffset(
  159.                 mousePosition.X + 3,
  160.                 mousePosition.Y + 16
  161.             )
  162.             frame.Position = UDim2.fromOffset(1, 1)
  163.             frame.Size = size
  164.             container.Size = containerSize
  165.             label.Text = tooltip
  166.             container.Visible = true
  167.             becameVisible = true
  168.             task.wait()
  169.             becameVisible = false
  170.         end
  171.     end)
  172.     instanceEvent.MouseLeave(area, function()
  173.         if thread ~= nil then
  174.             coroutine.close(thread)
  175.             thread = nil
  176.         end
  177.         if moveConnection ~= nil then
  178.             moveConnection:Disconnect()
  179.             moveConnection = nil
  180.         end
  181.         if clickConnection ~= nil then
  182.             clickConnection:Disconnect()
  183.             clickConnection = nil
  184.         end
  185.         if becameVisible == false and container.Visible == true then
  186.             wasVisible = true
  187.             container.Visible = false
  188.             task.wait()
  189.             wasVisible = false
  190.         end
  191.     end)
  192.     area:GetPropertyChangedSignal("AbsolutePosition"):Connect(function()
  193.         if toggled == false then return end
  194.         if moveUpdate == true then return end
  195.        
  196.         moveUpdate = true
  197.         updatePosition()
  198.         task.wait()
  199.         moveUpdate = false
  200.     end)
  201. end
  202. return table.freeze(module) :: Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement