function DPad.Enable(w)DPadFrame.Visible=true end function DPad.Disable(w)DPadFrame.Visible=false OnInputEnded()end function DPad.Create(k,w)if DPadFrame then DPadFrame:Destroy()DPadFrame=nil end local u=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=u DPadFrame.BackgroundTransparency=1 local X=UDim2.new(0,23,0,23)local a=UDim2.new(0,64,0,64)local y=Vector2.new(46,46)local l=Vector2.new(128,128)local U=createArrowLabel("\066\097\099\107\066\117\116\116\111\110",UDim2.new(.5,-32,1,-64),a,Vector2.new(0,0),l)local z=createArrowLabel("\070\111\114\119\097\114\100\066\117\116\116\111\110",UDim2.new(.5,-32,0,0),a,Vector2.new(0,258),l)local N=createArrowLabel("\076\101\102\116\066\117\116\116\111\110",UDim2.new(0,0,.5,-32),a,Vector2.new(129,129),l)local I=createArrowLabel("\082\105\103\104\116\066\117\116\116\111\110",UDim2.new(1,-64,.5,-32),a,Vector2.new(0,129),l)local j=createArrowLabel("\074\117\109\112\066\117\116\116\111\110",UDim2.new(.5,-32,.5,-32),a,Vector2.new(129,0),l)local K=createArrowLabel("\070\111\114\119\097\114\100\076\101\102\116\066\117\116\116\111\110",UDim2.new(0,35,0,35),X,Vector2.new(129,258),y)local B=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),X,Vector2.new(176,258),y)K.Visible=false B.Visible=false j.InputBegan:connect(function(w)MasterControl:DoJump()end)local F=Vector3.new(0,0,0)local function n(w)local k=j.AbsoluteSize.x/2 local u=getCenterPosition()local X=Vector2.new(w.x-u.x,w.y-u.y)if X.magnitude>k then local w=ATAN2(X.y,X.x)local k=FLOOR((8*w)/(2*PI)+8.5)%8+1 F=COMPASS_DIR[k]end if not K.Visible and F==COMPASS_DIR[7]then K.Visible=true B.Visible=true end end DPadFrame.InputBegan:connect(function(w)if TouchObject or w.UserInputType~=Enum.UserInputType.Touch then return end MasterControl:AddToPlayerMovement(-F)TouchObject=w n(TouchObject.Position)MasterControl:AddToPlayerMovement(F)end)DPadFrame.InputChanged:connect(function(w)if w==TouchObject then MasterControl:AddToPlayerMovement(-F)n(TouchObject.Position)MasterControl:AddToPlayerMovement(F)MasterControl:SetIsJumping(false)end end)OnInputEnded=function()TouchObject=nil K.Visible=false B.Visible=false MasterControl:AddToPlayerMovement(-F)F=Vector3.new(0,0,0)end DPadFrame.InputEnded:connect(function(w)if w==TouchObject then OnInputEnded()end end)GuiService.MenuOpened:connect(function()if TouchObject then OnInputEnded()end end)DPadFrame.Parent=w 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)
			MasterControl:SetIsJumping(false)
		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)
	
	GuiService.MenuOpened:connect(function()
		if TouchObject then
			OnInputEnded()
		end
	end)
	
	DPadFrame.Parent = parentFrame
end

return DPad
