Skip to content

Commit 97c05bd

Browse files
committed
Fix tooltip hover on mobile
Signed-off-by: Ryan Luu <ryan.luu@ryanluu.dev>
1 parent 3e8c5da commit 97c05bd

7 files changed

Lines changed: 140 additions & 100 deletions

File tree

‎src/Components/App.luau‎

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Backpack = require("./Backpack")
66
const HotbarHint = require("./HotbarHint")
77
const InventoryHint = require("./InventoryHint")
88
const Slot = require("./Slot")
9+
const Tooltip = require("./Tooltip")
910

1011
const DOUBLE_CLICK_TIME = 0.5
1112
const DRAG_THRESHOLD = 4
@@ -51,6 +52,69 @@ const function isInBounds(guiObject: GuiObject?, point: Vector2): boolean
5152
and offset.Y <= guiObject.AbsoluteSize.Y
5253
end
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+
54118
const 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
})

‎src/Components/Backpack.luau‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type Props = {
1717
draggingItem: BackpackItem?,
1818
onSlotActivated: ((item: BackpackItem) -> ())?,
1919
onSlotDrag: Slot.OnDrag?,
20+
onSlotHoverChanged: ((slot: GuiObject, hovered: boolean) -> ())?,
2021
}
2122

2223
const function Backpack(props: Props)
@@ -47,6 +48,7 @@ const function Backpack(props: Props)
4748
draggingItem = props.draggingItem,
4849
onSlotActivated = props.onSlotActivated,
4950
onSlotDrag = props.onSlotDrag,
51+
onSlotHoverChanged = props.onSlotHoverChanged,
5052
}),
5153
Inventory = e(Inventory, {
5254
width = props.width,
@@ -57,6 +59,7 @@ const function Backpack(props: Props)
5759
draggingItem = props.draggingItem,
5860
onSlotActivated = props.onSlotActivated,
5961
onSlotDrag = props.onSlotDrag,
62+
onSlotHoverChanged = props.onSlotHoverChanged,
6063
}),
6164
})
6265
end

‎src/Components/Hotbar.luau‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type Props = {
1212
draggingItem: BackpackItem?,
1313
onSlotActivated: ((item: BackpackItem) -> ())?,
1414
onSlotDrag: Slot.OnDrag?,
15+
onSlotHoverChanged: ((slot: GuiObject, hovered: boolean) -> ())?,
1516
}
1617

1718
const function Hotbar(props: Props)
@@ -37,6 +38,7 @@ const function Hotbar(props: Props)
3738
placeholder = item ~= nil and item == props.draggingItem,
3839
onActivated = props.onSlotActivated,
3940
onDrag = if slotUnlocked then props.onSlotDrag else nil,
41+
onHoverChanged = props.onSlotHoverChanged,
4042
})
4143
end
4244
end

‎src/Components/Inventory.luau‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type Props = {
1414
draggingItem: BackpackItem?,
1515
onSlotActivated: ((item: BackpackItem) -> ())?,
1616
onSlotDrag: Slot.OnDrag?,
17+
onSlotHoverChanged: ((slot: GuiObject, hovered: boolean) -> ())?,
1718
}
1819

1920
const function Inventory(props: Props)
@@ -35,6 +36,7 @@ const function Inventory(props: Props)
3536
placeholder = slot.item == props.draggingItem,
3637
onActivated = props.onSlotActivated,
3738
onDrag = props.onSlotDrag,
39+
onHoverChanged = props.onSlotHoverChanged,
3840
})
3941
end
4042
end

‎src/Components/Slot.luau‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export type Props = {
2323
position: UDim2?,
2424
onActivated: ((item: BackpackItem) -> ())?,
2525
onDrag: OnDrag?,
26+
onHoverChanged: ((slot: GuiObject, hovered: boolean) -> ())?,
2627
}
2728

2829
const function Slot(props: Props)
@@ -64,6 +65,12 @@ const function Slot(props: Props)
6465
props.onActivated(props.item)
6566
end
6667
end :: any,
68+
[React.Change.GuiState] = function(rbx: GuiObject)
69+
if props.onHoverChanged then
70+
const state = rbx.GuiState
71+
props.onHoverChanged(rbx, state == Enum.GuiState.Hover or state == Enum.GuiState.Press)
72+
end
73+
end :: any,
6774
}, {
6875
NumberHint = e("TextLabel", {
6976
Text = slotNumber,

‎src/Design/SatchelStyleSheet.model.json‎

Lines changed: 0 additions & 84 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Design/SatchelStyleSheet.rsml‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,6 @@ TextButton.Slot {
7171
}
7272
}
7373

74-
:Hover {
75-
@priority 1;
76-
77-
>.Tooltip {
78-
Visible = true;
79-
}
80-
}
81-
8274
.Unlocked {
8375
@priority 1;
8476
BackgroundColor3 = $SlotUnlockBackgroundColor;
@@ -126,14 +118,6 @@ TextButton.Slot {
126118
CornerRadius = $SlotCornerRadius;
127119
}
128120

129-
:Press {
130-
@priority 1;
131-
132-
>.Tooltip {
133-
Visible = true;
134-
}
135-
}
136-
137121
>TextLabel {
138122
Size = udim2 (0px, 0px);
139123
Visible = false;

0 commit comments

Comments
 (0)