function DPad.Enable(d)DPadFrame.Visible=true end function DPad.Disable(d)DPadFrame.Visible=false OnInputEnded()end function DPad.Create(F,d)if DPadFrame then DPadFrame:Destroy()DPadFrame=nil end local a=UDim2.new(0,10,1,-230)DPadFrame=Instance.new("\070\114\097\109\101")DPadFrame.Name="\068\080\097\100\070\114\097\109\101"DPadFrame.Active=true DPadFrame.Visible=false DPadFrame.Size=UDim2.new(0,192,0,192)DPadFrame.Position=a DPadFrame.BackgroundTransparency=1 local D=UDim2.new(0,23,0,23)local b=UDim2.new(0,64,0,64)local p=Vector2.new(46,46)local f=Vector2.new(128,128)local l=createArrowLabel("\066\097\099\107\066\117\116\116\111\110",UDim2.new(.5,-32,1,-64),b,Vector2.new(0,0),f)local B=createArrowLabel("\070\111\114\119\097\114\100\066\117\116\116\111\110",UDim2.new(.5,-32,0,0),b,Vector2.new(0,258),f)local r=createArrowLabel("\076\101\102\116\066\117\116\116\111\110",UDim2.new(0,0,.5,-32),b,Vector2.new(129,129),f)local k=createArrowLabel("\082\105\103\104\116\066\117\116\116\111\110",UDim2.new(1,-64,.5,-32),b,Vector2.new(0,129),f)local h=createArrowLabel("\074\117\109\112\066\117\116\116\111\110",UDim2.new(.5,-32,.5,-32),b,Vector2.new(129,0),f)local E=createArrowLabel("\070\111\114\119\097\114\100\076\101\102\116\066\117\116\116\111\110",UDim2.new(0,35,0,35),D,Vector2.new(129,258),p)local W=createArrowLabel("\070\111\114\119\097\114\100\082\105\103\104\116\066\117\116\116\111\110",UDim2.new(1,-55,0,35),D,Vector2.new(176,258),p)E.Visible=false W.Visible=false h.InputBegan:connect(function(d)MasterControl:DoJump()end)local Q=Vector3.new(0,0,0)local function s(d)local F=h.AbsoluteSize.x/2 local a=getCenterPosition()local D=Vector2.new(d.x-a.x,d.y-a.y)if D.magnitude>F then local d=ATAN2(D.y,D.x)local F=FLOOR((8*d)/(2*PI)+8.5)%8+1 Q=COMPASS_DIR[F]end if not E.Visible and Q==COMPASS_DIR[7]then E.Visible=true W.Visible=true end end DPadFrame.InputBegan:connect(function(d)if TouchObject or d.UserInputType~=Enum.UserInputType.Touch then return end MasterControl:AddToPlayerMovement(-Q)TouchObject=d s(TouchObject.Position)MasterControl:AddToPlayerMovement(Q)end)DPadFrame.InputChanged:connect(function(d)if d==TouchObject then MasterControl:AddToPlayerMovement(-Q)s(TouchObject.Position)MasterControl:AddToPlayerMovement(Q)end end)OnInputEnded=function()TouchObject=nil E.Visible=false W.Visible=false MasterControl:AddToPlayerMovement(-Q)Q=Vector3.new(0,0,0)end DPadFrame.InputEnded:connect(function(d)if d==TouchObject then OnInputEnded()end end)DPadFrame.Parent=d end return DPad
--
function DPad:Enable()
	DPadFrame.Visible = true
end

function DPad:Disable()
	DPadFrame.Visible = false
	OnInputEnded()
end

function DPad:Create(parentFrame)
	if DPadFrame then
		DPadFrame:Destroy()
		DPadFrame = nil
	end
	
	local position = UDim2.new(0, 10, 1, -230)
	DPadFrame = Instance.new('Frame')
	DPadFrame.Name = "DPadFrame"
	DPadFrame.Active = true
	DPadFrame.Visible = false
	DPadFrame.Size = UDim2.new(0, 192, 0, 192)
	DPadFrame.Position = position
	DPadFrame.BackgroundTransparency = 1
	
	local smArrowSize = UDim2.new(0, 23, 0, 23)
	local lgArrowSize = UDim2.new(0, 64, 0, 64)
	local smImgOffset = Vector2.new(46, 46)
	local lgImgOffset = Vector2.new(128, 128)
	
	local bBtn = createArrowLabel("BackButton", UDim2.new(0.5, -32, 1, -64), lgArrowSize, Vector2.new(0, 0), lgImgOffset)
	local fBtn = createArrowLabel("ForwardButton", UDim2.new(0.5, -32, 0, 0), lgArrowSize, Vector2.new(0, 258), lgImgOffset)
	local lBtn = createArrowLabel("LeftButton", UDim2.new(0, 0, 0.5, -32), lgArrowSize, Vector2.new(129, 129), lgImgOffset)
	local rBtn = createArrowLabel("RightButton", UDim2.new(1, -64, 0.5, -32), lgArrowSize, Vector2.new(0, 129), lgImgOffset)
	local jumpBtn = createArrowLabel("JumpButton", UDim2.new(0.5, -32, 0.5, -32), lgArrowSize, Vector2.new(129, 0), lgImgOffset)
	local flBtn = createArrowLabel("ForwardLeftButton", UDim2.new(0, 35, 0, 35), smArrowSize, Vector2.new(129, 258), smImgOffset)
	local frBtn = createArrowLabel("ForwardRightButton", UDim2.new(1, -55, 0, 35), smArrowSize, Vector2.new(176, 258), smImgOffset)
	flBtn.Visible = false
	frBtn.Visible = false
	
	-- input connections
	jumpBtn.InputBegan:connect(function(inputObject)
		MasterControl:DoJump()
	end)
	
	local movementVector = Vector3.new(0,0,0)
	local function normalizeDirection(inputPosition)
		local jumpRadius = jumpBtn.AbsoluteSize.x/2
		local centerPosition = getCenterPosition()
		local direction = Vector2.new(inputPosition.x - centerPosition.x, inputPosition.y - centerPosition.y)
		
		if direction.magnitude > jumpRadius then
			local angle = ATAN2(direction.y, direction.x)
			local octant = (FLOOR(8 * angle / (2 * PI) + 8.5)%8) + 1
			movementVector = COMPASS_DIR[octant]
		end
		
		if not flBtn.Visible and movementVector == COMPASS_DIR[7] then
			flBtn.Visible = true
			frBtn.Visible = true
		end
	end
	
	DPadFrame.InputBegan:connect(function(inputObject)
		if TouchObject or inputObject.UserInputType ~= Enum.UserInputType.Touch then
			return
		end
		
		MasterControl:AddToPlayerMovement(-movementVector)
		
		TouchObject = inputObject
		normalizeDirection(TouchObject.Position)
		
		MasterControl:AddToPlayerMovement(movementVector)
	end)
	
	DPadFrame.InputChanged:connect(function(inputObject)
		if inputObject == TouchObject then
			MasterControl:AddToPlayerMovement(-movementVector)
			normalizeDirection(TouchObject.Position)
			MasterControl:AddToPlayerMovement(movementVector)
		end
	end)
	
	OnInputEnded = function()
		TouchObject = nil
		flBtn.Visible = false
		frBtn.Visible = false
		
		MasterControl:AddToPlayerMovement(-movementVector)
		movementVector = Vector3.new(0, 0, 0)
	end
	
	DPadFrame.InputEnded:connect(function(inputObject)
		if inputObject == TouchObject then
			OnInputEnded()
		end
	end)
	
	DPadFrame.Parent = parentFrame
end

return DPad
