-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
249 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
local React = require("@pkg/React") | ||
local usePrevious = require("./usePrevious") | ||
|
||
local useCallback = React.useCallback | ||
local useEffect = React.useEffect | ||
local useMemo = React.useMemo | ||
local useState = React.useState | ||
local useRef = React.useRef | ||
|
||
local function useScreen(ancestor: Instance?): LayerCollector? | ||
local screen: LayerCollector?, setScreen = useState(nil :: LayerCollector?) | ||
|
||
useEffect(function() | ||
if ancestor then | ||
setScreen(ancestor:FindFirstAncestorWhichIsA("LayerCollector")) | ||
end | ||
end, { ancestor }) | ||
|
||
return screen | ||
end | ||
|
||
export type Props = { | ||
onTrigger: () -> (), | ||
LayoutOrder: number?, | ||
} | ||
|
||
local function ViewportTrigger(props: Props) | ||
local ref: { current: Frame? } = useRef() | ||
local screen = useScreen(ref.current) | ||
local position: Vector2, setPosition = useState(Vector2.zero) | ||
|
||
local onPositionChange = useCallback(function(rbx: Frame) | ||
setPosition(rbx.AbsolutePosition) | ||
end, {}) | ||
|
||
local isInBounds = useMemo(function() | ||
if screen then | ||
if | ||
position.X > screen.AbsolutePosition.X | ||
and position.X < screen.AbsolutePosition.X + screen.AbsoluteSize.X | ||
and position.Y > screen.AbsolutePosition.Y | ||
and position.Y < screen.AbsolutePosition.Y + screen.AbsoluteSize.Y | ||
then | ||
return true | ||
end | ||
end | ||
return false | ||
end, { position, screen }) | ||
|
||
local wasInBounds = usePrevious(isInBounds) | ||
|
||
useEffect(function() | ||
if isInBounds and not wasInBounds then | ||
props.onTrigger() | ||
end | ||
end, { isInBounds }) | ||
|
||
return React.createElement("Frame", { | ||
LayoutOrder = props.LayoutOrder, | ||
Size = UDim2.fromOffset(1, 1), | ||
BackgroundTransparency = 1, | ||
ref = ref, | ||
[React.Change.AbsolutePosition] = onPositionChange, | ||
}) | ||
end | ||
|
||
return ViewportTrigger |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
local HttpService = game:GetService("HttpService") | ||
local CollectionService = game:GetService("CollectionService") | ||
|
||
local React = require("@pkg/React") | ||
local Sift = require("@pkg/Sift") | ||
local useEvent = require("./useEvent") | ||
|
||
local useCallback = React.useCallback | ||
local useEffect = React.useEffect | ||
local useMemo = React.useMemo | ||
local useState = React.useState | ||
|
||
type SizeMap = { | ||
[string]: number, | ||
} | ||
|
||
local function useCombinedSize() | ||
local sizeMap: SizeMap, setSizeMap = useState({} :: SizeMap) | ||
local tag = useMemo(function() | ||
local guid = HttpService:GenerateGUID() | ||
return `combined-size-{guid}` | ||
end, {}) | ||
|
||
local onAdded = useCallback(function(instance: Instance) | ||
if instance:IsA("GuiObject") then | ||
setSizeMap(function(prev) | ||
return Sift.Dictionary.merge(prev, { | ||
[instance.Name] = instance.AbsoluteSize, | ||
}) | ||
end) | ||
end | ||
end, {}) | ||
|
||
local onRemoved = useCallback(function(instance: Instance) | ||
if instance:IsA("GuiObject") then | ||
setSizeMap(function(prev) | ||
return Sift.Dictionary.merge(prev, { | ||
[instance.Name] = Sift.None, | ||
}) | ||
end) | ||
end | ||
end, {}) | ||
|
||
useEffect(function() | ||
for _, instance in CollectionService:GetTagged(tag) do | ||
onAdded(instance) | ||
end | ||
|
||
local added = CollectionService:GetInstanceAddedSignal(tag):Connect(onAdded) | ||
local removed = CollectionService:GetInstanceRemovedSignal(tag):Connect(onRemoved) | ||
|
||
return function() | ||
added:Disconnect() | ||
removed:Disconnect() | ||
end | ||
end, {}) | ||
|
||
local size = useMemo(function() | ||
local vec = Vector2.zero | ||
for _, value in sizeMap do | ||
vec += value | ||
end | ||
return UDim2.fromOffset(vec.X, vec.Y) | ||
end, { sizeMap }) | ||
|
||
return { | ||
size = size, | ||
width = UDim2.new(size.X, UDim.new()), | ||
height = UDim2.new(UDim.new(), size.Y), | ||
tag = tag, | ||
} | ||
end | ||
|
||
return useCombinedSize |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
local React = require("@pkg/React") | ||
|
||
local function useEvent(event: RBXScriptSignal, callback: (...any) -> ()) | ||
React.useEffect(function() | ||
local conn = event:Connect(callback) | ||
|
||
return function() | ||
conn:Disconnect() | ||
end | ||
end) | ||
end | ||
|
||
return useEvent |
Oops, something went wrong.