Last active
July 9, 2021 02:04
-
-
Save lextoumbourou/3c6e50638a6490df3fac3c8b6deaee6a to your computer and use it in GitHub Desktop.
A Roblox module that allows Gamepad scrolling using the DPad
This file contains hidden or 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
local ContextActionService = game:GetService('ContextActionService') | |
local module = {hold=false} | |
local ActionNames={ | |
['ScrollDown']='ScrollDown', | |
['ScrollUp']='ScrollUp', | |
['ScrollRight']='ScrollRight', | |
['ScrollLeft']='ScrollLeft' | |
} | |
local UpDownDirection = { | |
[Enum.ScrollingDirection.XY]=true, | |
[Enum.ScrollingDirection.Y]=true, | |
} | |
local LeftRightDirection = { | |
[Enum.ScrollingDirection.XY]=true, | |
[Enum.ScrollingDirection.X]=true, | |
} | |
function module:_doScroll(scrollingFrame, actionName, inputState) | |
if not ActionNames[actionName] then | |
return | |
end | |
if inputState == Enum.UserInputState.Begin then | |
local hold = tick() | |
self.hold = hold | |
local increaseAmount = 25 | |
while self.hold == hold do | |
local xIncrease = 0 | |
local yIncrease = 0 | |
if actionName == 'ScrollDown' and UpDownDirection[scrollingFrame.ScrollingDirection] then | |
yIncrease = increaseAmount | |
elseif actionName == 'ScrollUp' and UpDownDirection[scrollingFrame.ScrollingDirection] then | |
yIncrease = -(increaseAmount) | |
elseif actionName == 'ScrollRight' and LeftRightDirection[scrollingFrame.ScrollingDirection] then | |
xIncrease = -(increaseAmount) | |
elseif actionName == 'ScrollLeft' and LeftRightDirection[scrollingFrame.ScrollingDirection] then | |
xIncrease = increaseAmount | |
end | |
scrollingFrame.CanvasPosition = Vector2.new( | |
scrollingFrame.CanvasPosition.x + xIncrease, | |
scrollingFrame.CanvasPosition.y + yIncrease | |
) | |
increaseAmount = math.max(increaseAmount + 1, 100) | |
wait(0.05) | |
end | |
elseif inputState == Enum.UserInputState.End then | |
self.hold = false | |
end | |
end | |
function module:addGamepadScroll(scrollingFrame) | |
local outputCallbacks = {} | |
local cb = scrollingFrame.MouseEnter:Connect(function(x, y) | |
ContextActionService:UnbindAction("ScrollDown") | |
ContextActionService:UnbindAction("ScrollUp") | |
ContextActionService:UnbindAction("ScrollRight") | |
ContextActionService:UnbindAction("ScrollLeft") | |
ContextActionService:BindAction("ScrollDown", function(...) self:_doScroll(scrollingFrame, ...) end, false, Enum.KeyCode.DPadDown) | |
ContextActionService:BindAction("ScrollUp", function(...) self:_doScroll(scrollingFrame, ...) end, false, Enum.KeyCode.DPadUp) | |
ContextActionService:BindAction("ScrollRight", function(...) self:_doScroll(scrollingFrame, ...) end, false, Enum.KeyCode.DPadLeft) | |
ContextActionService:BindAction("ScrollLeft", function(...) self:_doScroll(scrollingFrame, ...) end, false, Enum.KeyCode.DPadRight) | |
end) | |
table.insert(outputCallbacks, cb) | |
local cb = scrollingFrame.MouseLeave:Connect(function(x, y) | |
ContextActionService:UnbindAction("ScrollDown") | |
ContextActionService:UnbindAction("ScrollUp") | |
ContextActionService:UnbindAction("ScrollRight") | |
ContextActionService:UnbindAction("ScrollLeft") | |
end) | |
table.insert(outputCallbacks, cb) | |
return outputCallbacks | |
end | |
return module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment