function Thumbpad.Enable(l)ThumbpadFrame.Visible=true end function Thumbpad.Disable(l)ThumbpadFrame.Visible=false OnInputEnded()end function Thumbpad.Create(Q,l)if ThumbpadFrame then ThumbpadFrame:Destroy()ThumbpadFrame=nil if OnTouchChangedCn then OnTouchChangedCn:disconnect()OnTouchChangedCn=nil end if OnTouchEndedCn then OnTouchEndedCn:disconnect()OnTouchEndedCn=nil end end local x=l.AbsoluteSize.y<=500 local V=x and 70 or 120 local p=x and UDim2.new(0,V*1.25,1,-V-20)or UDim2.new(0,V/2-10,1,-V*1.75-10)ThumbpadFrame=Instance.new("\070\114\097\109\101")ThumbpadFrame.Name="\084\104\117\109\098\112\097\100\070\114\097\109\101"ThumbpadFrame.Visible=false ThumbpadFrame.Active=true ThumbpadFrame.Size=UDim2.new(0,V+20,0,V+20)ThumbpadFrame.Position=p ThumbpadFrame.BackgroundTransparency=1 local f=Instance.new("\073\109\097\103\101\076\097\098\101\108")f.Name="\079\117\116\101\114\073\109\097\103\101"f.Image=TOUCH_CONTROL_SHEET f.ImageRectOffset=Vector2.new(0,0)f.ImageRectSize=Vector2.new(220,220)f.BackgroundTransparency=1 f.Size=UDim2.new(0,V,0,V)f.Position=UDim2.new(0,10,0,10)f.Parent=ThumbpadFrame local H=x and UDim2.new(0,32,0,32)or UDim2.new(0,64,0,64)local s=UDim2.new(0,H.X.Offset*2,0,H.Y.Offset*2)local B=Vector2.new(110,110)local z=x and-4 or-9 local W=x and-28 or-55 local F=createArrowLabel("\068\111\119\110\065\114\114\111\119",f,UDim2.new(.5,-H.X.Offset/2,1,W),H,Vector2.new(8,8),B)local S=createArrowLabel("\085\112\065\114\114\111\119",f,UDim2.new(.5,-H.X.Offset/2,0,z),H,Vector2.new(8,266),B)local t=createArrowLabel("\076\101\102\116\065\114\114\111\119",f,UDim2.new(0,z,.5,-H.Y.Offset/2),H,Vector2.new(137,137),B)local R=createArrowLabel("\082\105\103\104\116\065\114\114\111\119",f,UDim2.new(1,W,.5,-H.Y.Offset/2),H,Vector2.new(8,137),B)local function K(l,Q,x)l:TweenSizeAndPosition(Q,x,Enum.EasingDirection.InOut,Enum.EasingStyle.Linear,.15,true)end local n=nil local v=.1 local e,i,r,j=false,false,false,false local D=Vector3.new(0,0,-1)local E=Vector3.new(1,0,0)local function w(l)MasterControl:AddToPlayerMovement(-currentMoveVector)local Q=Vector2.new(l.x,l.y)-n currentMoveVector=Q/(V/2)local x=currentMoveVector.magnitude if x<v then currentMoveVector=Vector3.new(0,0,0)else currentMoveVector=currentMoveVector.unit*((x-v)/(1-v))if currentMoveVector.magnitude==0 then currentMoveVector=Vector3.new(0,0,0)else currentMoveVector=(Vector3.new(currentMoveVector.x,0,currentMoveVector.y)).unit end end MasterControl:AddToPlayerMovement(currentMoveVector)local p=currentMoveVector:Dot(D)local f=currentMoveVector:Dot(E)if p>.5 then if not r then r,j=true,false K(S,s,UDim2.new(.5,-H.X.Offset,0,z-H.Y.Offset*1.5))K(F,H,UDim2.new(.5,-H.X.Offset/2,1,W))end elseif p<-0.5 then if not j then j,r=true,false K(F,s,UDim2.new(.5,-H.X.Offset,1,W+H.Y.Offset/2))K(S,H,UDim2.new(.5,-H.X.Offset/2,0,z))end else r,j=false,false K(F,H,UDim2.new(.5,-H.X.Offset/2,1,W))K(S,H,UDim2.new(.5,-H.X.Offset/2,0,z))end if f>.5 then if not e then e,i=true,false K(R,s,UDim2.new(1,W+H.X.Offset/2,.5,-H.Y.Offset))K(t,H,UDim2.new(0,z,.5,-H.Y.Offset/2))end elseif f<-0.5 then if not i then i,e=true,false K(t,s,UDim2.new(0,z-H.X.Offset*1.5,.5,-H.Y.Offset))K(R,H,UDim2.new(1,W,.5,-H.Y.Offset/2))end else e,i=false,false K(t,H,UDim2.new(0,z,.5,-H.Y.Offset/2))K(R,H,UDim2.new(1,W,.5,-H.Y.Offset/2))end end ThumbpadFrame.InputBegan:connect(function(l)if TouchObject or l.UserInputType~=Enum.UserInputType.Touch then return end ThumbpadFrame.Position=UDim2.new(0,l.Position.x-ThumbpadFrame.AbsoluteSize.x/2,0,l.Position.y-ThumbpadFrame.Size.Y.Offset/2)n=Vector2.new(ThumbpadFrame.AbsolutePosition.x+ThumbpadFrame.AbsoluteSize.x/2,ThumbpadFrame.AbsolutePosition.y+ThumbpadFrame.AbsoluteSize.y/2)w(l.Position)TouchObject=l end)OnTouchChangedCn=UserInputService.TouchMoved:connect(function(l,Q)if l==TouchObject then w(TouchObject.Position)end end)OnInputEnded=function()MasterControl:AddToPlayerMovement(-currentMoveVector)currentMoveVector=Vector3.new(0,0,0)ThumbpadFrame.Position=p TouchObject=nil r,j,i,e=false,false,false,false K(F,H,UDim2.new(.5,-H.X.Offset/2,1,W))K(S,H,UDim2.new(.5,-H.X.Offset/2,0,z))K(t,H,UDim2.new(0,z,.5,-H.Y.Offset/2))K(R,H,UDim2.new(1,W,.5,-H.Y.Offset/2))end OnTouchEndedCn=UserInputService.TouchEnded:connect(function(l)if l==TouchObject then OnInputEnded()end end)ThumbpadFrame.Parent=l end return Thumbpad
--
function Thumbpad:Enable()
	ThumbpadFrame.Visible = true
end

function Thumbpad:Disable()
	ThumbpadFrame.Visible = false
	OnInputEnded()
end

function Thumbpad:Create(parentFrame)
	if ThumbpadFrame then
		ThumbpadFrame:Destroy()
		ThumbpadFrame = nil
		if OnTouchChangedCn then
			OnTouchChangedCn:disconnect()
			OnTouchChangedCn = nil
		end
		if OnTouchEndedCn then
			OnTouchEndedCn:disconnect()
			OnTouchEndedCn = nil
		end
	end
	
	local isSmallScreen = parentFrame.AbsoluteSize.y <= 500
	local thumbpadSize = isSmallScreen and 70 or 120
	local position = isSmallScreen and UDim2.new(0, thumbpadSize * 1.25, 1, -thumbpadSize - 20) or
		UDim2.new(0, thumbpadSize/2 - 10, 1, -thumbpadSize * 1.75 - 10)
	
	ThumbpadFrame = Instance.new('Frame')
	ThumbpadFrame.Name = "ThumbpadFrame"
	ThumbpadFrame.Visible = false
	ThumbpadFrame.Active = true
	ThumbpadFrame.Size = UDim2.new(0, thumbpadSize + 20, 0, thumbpadSize + 20)
	ThumbpadFrame.Position = position
	ThumbpadFrame.BackgroundTransparency = 1
	
	local outerImage = Instance.new('ImageLabel')
	outerImage.Name = "OuterImage"
	outerImage.Image = TOUCH_CONTROL_SHEET
	outerImage.ImageRectOffset = Vector2.new(0, 0)
	outerImage.ImageRectSize = Vector2.new(220, 220)
	outerImage.BackgroundTransparency = 1
	outerImage.Size = UDim2.new(0, thumbpadSize, 0, thumbpadSize)
	outerImage.Position = UDim2.new(0, 10, 0, 10)
	outerImage.Parent = ThumbpadFrame
	
	local smArrowSize = isSmallScreen and UDim2.new(0, 32, 0, 32) or UDim2.new(0, 64, 0, 64)
	local lgArrowSize = UDim2.new(0, smArrowSize.X.Offset * 2, 0, smArrowSize.Y.Offset * 2)
	local imgRectSize = Vector2.new(110, 110)
	local smImgOffset = isSmallScreen and -4 or -9
	local lgImgOffset = isSmallScreen and -28 or -55
	
	local dArrow = createArrowLabel("DownArrow", outerImage, UDim2.new(0.5, -smArrowSize.X.Offset/2, 1, lgImgOffset), smArrowSize, Vector2.new(8, 8), imgRectSize)
	local uArrow = createArrowLabel("UpArrow", outerImage, UDim2.new(0.5, -smArrowSize.X.Offset/2, 0, smImgOffset), smArrowSize, Vector2.new(8, 266), imgRectSize)
	local lArrow = createArrowLabel("LeftArrow", outerImage, UDim2.new(0, smImgOffset, 0.5, -smArrowSize.Y.Offset/2), smArrowSize, Vector2.new(137, 137), imgRectSize)
	local rArrow = createArrowLabel("RightArrow", outerImage, UDim2.new(1, lgImgOffset, 0.5, -smArrowSize.Y.Offset/2), smArrowSize, Vector2.new(8, 137), imgRectSize)
	
	local function doTween(guiObject, endSize, endPosition)
		guiObject:TweenSizeAndPosition(endSize, endPosition, Enum.EasingDirection.InOut, Enum.EasingStyle.Linear, 0.15, true)
	end
	
	local padOrigin = nil
	local deadZone = 0.1
	local isRight, isLeft, isUp, isDown = false, false, false, false
	local vForward = Vector3.new(0, 0, -1)
	local vRight = Vector3.new(1, 0, 0)
	local function doMove(pos)
		MasterControl:AddToPlayerMovement(-currentMoveVector)
		
		local delta = Vector2.new(pos.x, pos.y) - padOrigin
		currentMoveVector = delta / (thumbpadSize/2)
		
		-- Scaled Radial Dead Zone
		local inputAxisMagnitude = currentMoveVector.magnitude
		if inputAxisMagnitude < deadZone then
			currentMoveVector = Vector3.new(0, 0, 0)
		else
			currentMoveVector = currentMoveVector.unit * ((inputAxisMagnitude - deadZone) / (1 - deadZone))
			-- catch possible NAN Vector
			if currentMoveVector.magnitude == 0 then
				currentMoveVector = Vector3.new(0, 0, 0)
			else
				currentMoveVector = Vector3.new(currentMoveVector.x, 0, currentMoveVector.y).unit
			end
		end
		
		MasterControl:AddToPlayerMovement(currentMoveVector)
		
		local forwardDot = currentMoveVector:Dot(vForward)
		local rightDot = currentMoveVector:Dot(vRight)
		if forwardDot > 0.5 then		-- UP
			if not isUp then
				isUp, isDown = true, false
				doTween(uArrow, lgArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset, 0, smImgOffset - smArrowSize.Y.Offset * 1.5))
				doTween(dArrow, smArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset/2, 1, lgImgOffset))
			end
		elseif forwardDot < -0.5 then	-- DOWN
			if not isDown then
				isDown, isUp = true, false
				doTween(dArrow, lgArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset, 1, lgImgOffset + smArrowSize.Y.Offset/2))
				doTween(uArrow, smArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset/2, 0, smImgOffset))
			end
		else
			isUp, isDown = false, false
			doTween(dArrow, smArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset/2, 1, lgImgOffset))
			doTween(uArrow, smArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset/2, 0, smImgOffset))
		end
		
		if rightDot > 0.5 then
			if not isRight then
				isRight, isLeft = true, false
				doTween(rArrow, lgArrowSize, UDim2.new(1, lgImgOffset + smArrowSize.X.Offset/2, 0.5, -smArrowSize.Y.Offset))
				doTween(lArrow, smArrowSize, UDim2.new(0, smImgOffset, 0.5, -smArrowSize.Y.Offset/2))
			end
		elseif rightDot < -0.5 then
			if not isLeft then
				isLeft, isRight = true, false
				doTween(lArrow, lgArrowSize, UDim2.new(0, smImgOffset - smArrowSize.X.Offset * 1.5, 0.5, -smArrowSize.Y.Offset))
				doTween(rArrow, smArrowSize, UDim2.new(1, lgImgOffset, 0.5, -smArrowSize.Y.Offset/2))
			end
		else
			isRight, isLeft = false, false
			doTween(lArrow, smArrowSize, UDim2.new(0, smImgOffset, 0.5, -smArrowSize.Y.Offset/2))
			doTween(rArrow, smArrowSize, UDim2.new(1, lgImgOffset, 0.5, -smArrowSize.Y.Offset/2))
		end
	end
	
	--input connections
	ThumbpadFrame.InputBegan:connect(function(inputObject)
		if TouchObject or inputObject.UserInputType ~= Enum.UserInputType.Touch then
			return
		end
		
		ThumbpadFrame.Position = UDim2.new(0, inputObject.Position.x - ThumbpadFrame.AbsoluteSize.x/2, 0, inputObject.Position.y - ThumbpadFrame.Size.Y.Offset/2)
		padOrigin = Vector2.new(ThumbpadFrame.AbsolutePosition.x + ThumbpadFrame.AbsoluteSize.x/2,
			ThumbpadFrame.AbsolutePosition.y + ThumbpadFrame.AbsoluteSize.y/2)
		doMove(inputObject.Position)
		TouchObject = inputObject
	end)
	
	OnTouchChangedCn = UserInputService.TouchMoved:connect(function(inputObject, isProcessed)
		if inputObject == TouchObject then
			doMove(TouchObject.Position)
		end
	end)
	
	OnInputEnded = function()
		MasterControl:AddToPlayerMovement(-currentMoveVector)
		currentMoveVector = Vector3.new(0,0,0)

		ThumbpadFrame.Position = position
		TouchObject = nil
		isUp, isDown, isLeft, isRight = false, false, false, false
		doTween(dArrow, smArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset/2, 1, lgImgOffset))
		doTween(uArrow, smArrowSize, UDim2.new(0.5, -smArrowSize.X.Offset/2, 0, smImgOffset))
		doTween(lArrow, smArrowSize, UDim2.new(0, smImgOffset, 0.5, -smArrowSize.Y.Offset/2))
		doTween(rArrow, smArrowSize, UDim2.new(1, lgImgOffset, 0.5, -smArrowSize.Y.Offset/2))
	end
	
	OnTouchEndedCn = UserInputService.TouchEnded:connect(function(inputObject)
		if inputObject == TouchObject then
			OnInputEnded()
		end
	end)
	
	ThumbpadFrame.Parent = parentFrame
end

return Thumbpad
