Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. local RunService = game:GetService 'RunService'
  2. local TextService = game:GetService 'TextService'
  3.  
  4. local Root = script.Parent.Parent
  5. local Libraries = Root:WaitForChild 'Libraries'
  6. local Vendor = Root:WaitForChild 'Vendor'
  7. local UI = Root:WaitForChild 'UI'
  8.  
  9. -- Constants
  10. local EMPTY_VECTOR2 = Vector2.new()
  11.  
  12. -- Roact
  13. local Roact = require(Vendor:WaitForChild 'Roact')
  14. local new = Roact.createElement
  15. local Frame = require(UI:WaitForChild 'Frame')
  16. local TextLabel = require(UI:WaitForChild 'TextLabel')
  17.  
  18. -- Create component
  19. local Tooltip = Roact.PureComponent:extend 'Tooltip'
  20.  
  21. -- Set defaults
  22. Tooltip.defaultProps = {
  23. AnchorPoint = Vector2.new(0.5, 0),
  24. BackgroundColor3 = Color3.new(0, 0, 0),
  25. BackgroundTransparency = 0.4,
  26. BorderSizePixel = 0,
  27. Position = UDim2.new(0.5, 0, 1, 3),
  28. ZIndex = 10,
  29. Font = Enum.Font.GothamBold,
  30. TextColor3 = Color3.new(1, 1, 1),
  31. TextSize = 10,
  32. ShowDelay = 0
  33. }
  34.  
  35. function Tooltip:init(props)
  36. local ShowDelay = props.ShowDelay
  37. self:setState {Visible = false}
  38. self.IsHovered = false
  39.  
  40. self.ConnectHover = function()
  41. self.HoverConnection = RunService.Heartbeat:Connect(function()
  42. if self.IsHovered and tick() >= self.TargetTime then
  43. self.DisconnectHover()
  44. self:setState {Visible = true}
  45. end
  46. end)
  47. end
  48.  
  49. self.DisconnectHover = function()
  50. if self.HoverConnection then
  51. self.HoverConnection:Disconnect()
  52. end
  53. end
  54.  
  55. self.InputBegan = function(_, InputObject)
  56. if InputObject.UserInputType == Enum.UserInputType.MouseMovement then
  57. self.IsHovered = true
  58. self.TargetTime = tick() + ShowDelay
  59. self.ConnectHover()
  60. end
  61. end
  62.  
  63. self.InputEnded = function(_, InputObject)
  64. if InputObject.UserInputType == Enum.UserInputType.MouseMovement then
  65. self.IsHovered = false
  66. self.TargetTime = 0
  67. self.DisconnectHover()
  68. self:setState {Visible = false}
  69. end
  70. end
  71. end
  72.  
  73. function Tooltip:willUnmount()
  74. self.DisconnectHover()
  75. end
  76.  
  77. function Tooltip:render()
  78. local Properties = self.props
  79. local Text = Properties.Text
  80. local TextSize = Properties.TextSize
  81. local Font = Properties.Font
  82. local FrameSize = TextService:GetTextSize(Text, TextSize, Font, EMPTY_VECTOR2)
  83.  
  84. return new(Frame, {
  85. [Roact.Event.InputBegan] = self.InputBegan,
  86. [Roact.Event.InputEnded] = self.InputEnded
  87. }, {
  88. Tooltip = new(TextLabel, {
  89. AnchorPoint = Properties.AnchorPoint,
  90. Position = Properties.Position,
  91. Size = UDim2.new(0, FrameSize.X + 10, 0, FrameSize.Y + 10),
  92. Visible = self.state.Visible,
  93. BackgroundColor3 = Properties.BackgroundColor3,
  94. BackgroundTransparency = Properties.BackgroundTransparency,
  95. BorderSizePixel = Properties.BorderSizePixel,
  96. Font = Font,
  97. Text = Text,
  98. TextColor3 = Properties.TextColor3,
  99. TextSize = TextSize,
  100. ZIndex = Properties.ZIndex,
  101. TextXAlignment = Enum.TextXAlignment.Center
  102. })
  103. })
  104. end
  105.  
  106. return Tooltip
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement