@@ -6,6 +6,7 @@ const Backpack = require("./Backpack")
66const HotbarHint = require ("./HotbarHint" )
77const InventoryHint = require ("./InventoryHint" )
88const Slot = require ("./Slot" )
9+ const Tooltip = require ("./Tooltip" )
910
1011const DOUBLE_CLICK_TIME = 0.5
1112const DRAG_THRESHOLD = 4
@@ -51,6 +52,69 @@ const function isInBounds(guiObject: GuiObject?, point: Vector2): boolean
5152 and offset .Y <= guiObject .AbsoluteSize .Y
5253end
5354
55+ -- Whether the point is over the GUI object without being hidden or clipped by an ancestor
56+ const function isVisibleAt (guiObject : GuiObject , point : Vector2 ): boolean
57+ local current : Instance ? = guiObject
58+ while current and current :IsA ("GuiObject" ) do
59+ if not current .Visible then
60+ return false
61+ elseif (current == guiObject or current .ClipsDescendants ) and not isInBounds (current , point ) then
62+ return false
63+ end
64+ current = current .Parent
65+ end
66+ return current ~= nil
67+ end
68+
69+ const function getCenter (guiObject : GuiObject ): Vector2
70+ return guiObject .AbsolutePosition + guiObject .AbsoluteSize / 2
71+ end
72+
73+ type SlotTooltipProps = {
74+ slot : GuiObject ,
75+ layer : GuiObject ,
76+ text : string ,
77+ }
78+
79+ -- Rendered outside of the slot so the inventory's ScrollingFrame doesn't clip it
80+ const function SlotTooltip (props : SlotTooltipProps )
81+ const slot = props .slot
82+ const layer = props .layer
83+ const _ , forceUpdate = React .useReducer (function (count : number )
84+ return count + 1
85+ end , 0 )
86+
87+ -- Follow the slot as it moves, such as when the inventory scrolls
88+ React .useEffect (function ()
89+ const connections = {
90+ slot :GetPropertyChangedSignal ("AbsolutePosition" ):Connect (forceUpdate ),
91+ slot :GetPropertyChangedSignal ("AbsoluteSize" ):Connect (forceUpdate ),
92+ layer :GetPropertyChangedSignal ("AbsolutePosition" ):Connect (forceUpdate ),
93+ }
94+
95+ return function ()
96+ for _ , connection in connections do
97+ connection :Disconnect ()
98+ end
99+ end
100+ end , { slot , layer } :: { any })
101+
102+ if not isVisibleAt (slot , getCenter (slot )) then
103+ return nil
104+ end
105+
106+ const position = slot .AbsolutePosition - layer .AbsolutePosition
107+ return e ("Frame" , {
108+ BackgroundTransparency = 1 ,
109+ Position = UDim2 .fromOffset (position .X , position .Y ),
110+ Size = UDim2 .fromOffset (slot .AbsoluteSize .X , slot .AbsoluteSize .Y ),
111+ }, {
112+ ToolTip = e (Tooltip , {
113+ text = props .text ,
114+ }),
115+ })
116+ end
117+
54118const function findItemIndex (items : Items , item : BackpackItem ): number ?
55119 for index , slot in items do
56120 if slot .item == item then
@@ -73,6 +137,7 @@ const function App(props: Props)
73137 const lastClickTimes = React .useRef ({} :: { [BackpackItem ]: number })
74138 const hintsRef = React .useRef (nil :: Frame ? )
75139 const dragLayerRef = React .useRef (nil :: Frame ? )
140+ const hoveredSlot , setHoveredSlot = React .useState (nil :: GuiObject ? )
76141
77142 -- Gamepad uses select/swap instead of dragging, like the CoreGui backpack
78143 const gamepadPreferred , setGamepadPreferred = React .useState (isGamepadPreferred )
@@ -87,6 +152,49 @@ const function App(props: Props)
87152 end
88153 end , {})
89154
155+ const function onSlotHoverChanged (slot : GuiObject , hovered : boolean )
156+ setHoveredSlot (function (current : GuiObject ? )
157+ return if hovered then slot elseif current == slot then nil else current
158+ end )
159+ end
160+
161+ -- Touches don't hover, so show the tooltip of the slot under the touch as it moves
162+ React .useEffect (function ()
163+ const function onTouch (input : InputObject )
164+ const root = hintsRef .current
165+ if not root then
166+ return
167+ end
168+ const point = Vector2 .new (input .Position .X , input .Position .Y )
169+ for _ , descendant in root :GetDescendants () do
170+ if descendant :IsA ("GuiButton" ) and descendant :HasTag ("Slot" ) and isVisibleAt (descendant , point ) then
171+ setHoveredSlot (descendant )
172+ return
173+ end
174+ end
175+ setHoveredSlot (nil )
176+ end
177+
178+ const touchStartedSignal = UserInputService .TouchStarted :Connect (onTouch )
179+ const touchMovedSignal = UserInputService .TouchMoved :Connect (onTouch )
180+ const touchEndedSignal = UserInputService .TouchEnded :Connect (function ()
181+ setHoveredSlot (nil )
182+ end )
183+
184+ return function ()
185+ touchStartedSignal :Disconnect ()
186+ touchMovedSignal :Disconnect ()
187+ touchEndedSignal :Disconnect ()
188+ end
189+ end , {})
190+
191+ -- Hidden or removed slots don't always leave the hover state
192+ React .useEffect (function ()
193+ if hoveredSlot and not isVisibleAt (hoveredSlot , getCenter (hoveredSlot )) then
194+ setHoveredSlot (nil )
195+ end
196+ end )
197+
90198 const function setDragState (state : DragState ? )
91199 dragRef .current = state
92200 setDrag (state )
@@ -211,6 +319,22 @@ const function App(props: Props)
211319 })
212320 end
213321
322+ local slotTooltip = nil
323+ if hoveredSlot and dragLayer and not (drag and drag .moved ) then
324+ const parent = hoveredSlot .Parent
325+ const index = if parent and parent .Name == "Hotbar"
326+ then hoveredSlot .LayoutOrder
327+ else hoveredSlot .LayoutOrder + slotCount
328+ const item = items [index ] and items [index ].item
329+ if item and item :IsA ("Tool" ) and item .ToolTip ~= "" then
330+ slotTooltip = e (SlotTooltip , {
331+ slot = hoveredSlot ,
332+ layer = dragLayer ,
333+ text = item .ToolTip ,
334+ })
335+ end
336+ end
337+
214338 local visibleHotbarSlots = 0
215339 for order = 1 , slotCount do
216340 if items [order ] then
@@ -277,6 +401,7 @@ const function App(props: Props)
277401 draggingItem = if drag and drag .moved then drag .item else nil ,
278402 onSlotActivated = props .onSlotActivated ,
279403 onSlotDrag = if gamepadPreferred then nil else onSlotDrag ,
404+ onSlotHoverChanged = onSlotHoverChanged ,
280405 }),
281406 }),
282407 }),
@@ -288,6 +413,7 @@ const function App(props: Props)
288413 ref = dragLayerRef ,
289414 [React .Tag ] = if props .tenFootInterface then "TenFootInterface" else nil ,
290415 }, {
416+ SlotTooltip = slotTooltip ,
291417 DraggedSlot = draggedSlot ,
292418 }),
293419 })
0 commit comments