function ControlModule.SwitchToController(A,V)if not V then if A.activeController then A.activeController:Enable(false)end A.activeController=nil A.activeControlModule=nil else if not A.controllers[V]then A.controllers[V]=V.new(CONTROL_ACTION_PRIORITY)end if A.activeController~=A.controllers[V]then if A.activeController then A.activeController:Enable(false)end A.activeController=A.controllers[V]A.activeControlModule=V if A.touchControlFrame then A.activeController:Enable(true,A.touchControlFrame)else if A.activeControlModule==ClickToMove then A.activeController:Enable(true,Players.LocalPlayer.DevComputerMovementMode==Enum.DevComputerMovementMode.UserChoice)else A.activeController:Enable(true)end end if A.touchControlFrame and(A.activeControlModule==TouchThumbpad or A.activeControlModule==TouchThumbstick or A.activeControlModule==ClickToMove or A.activeControlModule==DynamicThumbstick)then if not A.controllers[TouchJump]then A.controllers[TouchJump]=TouchJump.new()end A.touchJumpController=A.controllers[TouchJump]A.touchJumpController:Enable(true,A.touchControlFrame)else if A.touchJumpController then A.touchJumpController:Enable(false)end end end end end function ControlModule.OnLastInputTypeChanged(A,V)if lastInputType==V then warn("\076\097\115\116\073\110\112\117\116\084\121\112\101 \067\104\097\110\103\101 \108\105\115\116\101\110\101\114 \099\097\108\108\101\100 \119\105\116\104 \099\117\114\114\101\110\116 \116\121\112\101\046")end lastInputType=V if lastInputType==Enum.UserInputType.Touch then local V,T=A:SelectTouchModule()if T then while not A.touchControlFrame do wait()end A:SwitchToController(V)end elseif computerInputTypeToModuleMap[lastInputType]~=nil then local V=A:SelectComputerMovementModule()if V then A:SwitchToController(V)end end end

function ControlModule:SwitchToController(controlModule)
	if not controlModule then
		if self.activeController then
			self.activeController:Enable(false)
		end
		self.activeController = nil
		self.activeControlModule = nil
	else
		if not self.controllers[controlModule] then
			self.controllers[controlModule] = controlModule.new(CONTROL_ACTION_PRIORITY)
		end

		if self.activeController ~= self.controllers[controlModule] then
			if self.activeController then
				self.activeController:Enable(false)
			end
			self.activeController = self.controllers[controlModule]
			self.activeControlModule = controlModule -- Only used to check if controller switch is necessary
			if self.touchControlFrame then
				self.activeController:Enable(true, self.touchControlFrame)
			else
				if self.activeControlModule == ClickToMove then
					-- For ClickToMove, when it is the player's choice, we also enable the full keyboard controls.
					-- When the developer is forcing click to move, the most keyboard controls (WASD) are not available, only spacebar to jump.
					self.activeController:Enable(true, Players.LocalPlayer.DevComputerMovementMode == Enum.DevComputerMovementMode.UserChoice)
				else				
					self.activeController:Enable(true)
				end
			end
			if self.touchControlFrame and (self.activeControlModule == TouchThumbpad
									or self.activeControlModule == TouchThumbstick
									or self.activeControlModule == ClickToMove
									or self.activeControlModule == DynamicThumbstick) then
				if not self.controllers[TouchJump] then
					self.controllers[TouchJump] = TouchJump.new()
				end
				self.touchJumpController = self.controllers[TouchJump]
				self.touchJumpController:Enable(true, self.touchControlFrame)
			else
				if self.touchJumpController then
					self.touchJumpController:Enable(false)
				end
			end
		end
	end
end

function ControlModule:OnLastInputTypeChanged(newLastInputType)
	if lastInputType == newLastInputType then
		warn("LastInputType Change listener called with current type.")
	end
	lastInputType = newLastInputType

	if lastInputType == Enum.UserInputType.Touch then
		-- TODO: Check if touch module already active
		local touchModule, success = self:SelectTouchModule()
		if success then
			while not self.touchControlFrame do
				wait()
			end
			self:SwitchToController(touchModule)
		end
	elseif computerInputTypeToModuleMap[lastInputType] ~= nil then
		local computerModule = self:SelectComputerMovementModule()
		if computerModule then
			self:SwitchToController(computerModule)
		end
	end
end
