function Thumbpad.Enable(o)ThumbpadFrame.Visible=true end function Thumbpad.Disable(o)ThumbpadFrame.Visible=false OnInputEnded()end function Thumbpad.Create(T,o)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 u=o.AbsoluteSize.y<=500 local z=u and 70 or 120 local W=u and UDim2.new(0,z*1.25,1,-z-20)or UDim2.new(0,z/2-10,1,-z*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,z+20,0,z+20)ThumbpadFrame.Position=W ThumbpadFrame.BackgroundTransparency=1 local S=Instance.new("\073\109\097\103\101\076\097\098\101\108")S.Name="\079\117\116\101\114\073\109\097\103\101"S.Image=TOUCH_CONTROL_SHEET S.ImageRectOffset=Vector2.new(0,0)S.ImageRectSize=Vector2.new(220,220)S.BackgroundTransparency=1 S.Size=UDim2.new(0,z,0,z)S.Position=UDim2.new(0,10,0,10)S.Parent=ThumbpadFrame local C=u and UDim2.new(0,32,0,32)or UDim2.new(0,64,0,64)local s=UDim2.new(0,C.X.Offset*2,0,C.Y.Offset*2)local a=Vector2.new(110,110)local n=u and-4 or-9 local m=u and-28 or-55 local e=createArrowLabel("\068\111\119\110\065\114\114\111\119",S,UDim2.new(.5,-C.X.Offset/2,1,m),C,Vector2.new(8,8),a)local h=createArrowLabel("\085\112\065\114\114\111\119",S,UDim2.new(.5,-C.X.Offset/2,0,n),C,Vector2.new(8,266),a)local d=createArrowLabel("\076\101\102\116\065\114\114\111\119",S,UDim2.new(0,n,.5,-C.Y.Offset/2),C,Vector2.new(137,137),a)local K=createArrowLabel("\082\105\103\104\116\065\114\114\111\119",S,UDim2.new(1,m,.5,-C.Y.Offset/2),C,Vector2.new(8,137),a)local function O(o,T,u)o:TweenSizeAndPosition(T,u,Enum.EasingDirection.InOut,Enum.EasingStyle.Linear,.15,true)end local Z=nil local D=.1 local t,M,x,E=false,false,false,false local p=Vector3.new(0,0,-1)local P=Vector3.new(1,0,0)local function U(o)MasterControl:AddToPlayerMovement(-currentMoveVector)local T=Vector2.new(o.x,o.y)-Z currentMoveVector=T/(z/2)local u=currentMoveVector.magnitude if u<D then currentMoveVector=Vector3.new(0,0,0)else currentMoveVector=currentMoveVector.unit*((u-D)/(1-D))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 W=currentMoveVector:Dot(p)local S=currentMoveVector:Dot(P)if W>.5 then if not x then x,E=true,false O(h,s,UDim2.new(.5,-C.X.Offset,0,n-C.Y.Offset*1.5))O(e,C,UDim2.new(.5,-C.X.Offset/2,1,m))end elseif W<-0.5 then if not E then E,x=true,false O(e,s,UDim2.new(.5,-C.X.Offset,1,m+C.Y.Offset/2))O(h,C,UDim2.new(.5,-C.X.Offset/2,0,n))end else x,E=false,false O(e,C,UDim2.new(.5,-C.X.Offset/2,1,m))O(h,C,UDim2.new(.5,-C.X.Offset/2,0,n))end if S>.5 then if not t then t,M=true,false O(K,s,UDim2.new(1,m+C.X.Offset/2,.5,-C.Y.Offset))O(d,C,UDim2.new(0,n,.5,-C.Y.Offset/2))end elseif S<-0.5 then if not M then M,t=true,false O(d,s,UDim2.new(0,n-C.X.Offset*1.5,.5,-C.Y.Offset))O(K,C,UDim2.new(1,m,.5,-C.Y.Offset/2))end else t,M=false,false O(d,C,UDim2.new(0,n,.5,-C.Y.Offset/2))O(K,C,UDim2.new(1,m,.5,-C.Y.Offset/2))end end ThumbpadFrame.InputBegan:connect(function(o)if TouchObject or o.UserInputType~=Enum.UserInputType.Touch then return end ThumbpadFrame.Position=UDim2.new(0,o.Position.x-ThumbpadFrame.AbsoluteSize.x/2,0,o.Position.y-ThumbpadFrame.Size.Y.Offset/2)Z=Vector2.new(ThumbpadFrame.AbsolutePosition.x+ThumbpadFrame.AbsoluteSize.x/2,ThumbpadFrame.AbsolutePosition.y+ThumbpadFrame.AbsoluteSize.y/2)U(o.Position)TouchObject=o end)OnTouchChangedCn=UserInputService.TouchMoved:connect(function(o,T)if o==TouchObject then U(TouchObject.Position)end end)OnInputEnded=function()MasterControl:AddToPlayerMovement(-currentMoveVector)currentMoveVector=Vector3.new(0,0,0)ThumbpadFrame.Position=W TouchObject=nil x,E,M,t=false,false,false,false O(e,C,UDim2.new(.5,-C.X.Offset/2,1,m))O(h,C,UDim2.new(.5,-C.X.Offset/2,0,n))O(d,C,UDim2.new(0,n,.5,-C.Y.Offset/2))O(K,C,UDim2.new(1,m,.5,-C.Y.Offset/2))end OnTouchEndedCn=UserInputService.TouchEnded:connect(function(o)if o==TouchObject then OnInputEnded()end end)ThumbpadFrame.Parent=o end return Thumbpad
--[[ Public API ]]
--
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
