function ControlModule.SwitchToController(W,A)if not A then if W.activeController then W.activeController:Enable(false)end W.activeController=nil W.activeControlModule=nil return end if not W.controllers[A]then W.controllers[A]=A.new(CONTROL_ACTION_PRIORITY)end if W.activeController~=W.controllers[A]then if W.activeController then W.activeController:Enable(false)end W.activeController=W.controllers[A]W.activeControlModule=A if W.touchControlFrame and(W.activeControlModule==ClickToMove or W.activeControlModule==TouchThumbstick or W.activeControlModule==DynamicThumbstick)then if not W.controllers[TouchJump]then W.controllers[TouchJump]=TouchJump.new()end W.touchJumpController=W.controllers[TouchJump]W.touchJumpController:Enable(true,W.touchControlFrame)else if W.touchJumpController then W.touchJumpController:Enable(false)end end W:UpdateActiveControlModuleEnabled()end end function ControlModule.OnLastInputTypeChanged(W,A)if lastInputType==A 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=A if lastInputType==Enum.UserInputType.Touch then local A,X=W:SelectTouchModule()if X then while not W.touchControlFrame do wait()end W:SwitchToController(A)end elseif computerInputTypeToModuleMap[lastInputType]~=nil then local A=W:SelectComputerMovementModule()if A then W:SwitchToController(A)end end W:UpdateTouchGuiVisibility()end

function ControlModule:SwitchToController(controlModule)
	-- controlModule is invalid, just disable current controller
	if not controlModule then
		if self.activeController then
			self.activeController:Enable(false)
		end
		self.activeController = nil
		self.activeControlModule = nil
		return
	end

	-- first time switching to this control module, should instantiate it
	if not self.controllers[controlModule] then
		self.controllers[controlModule] = controlModule.new(CONTROL_ACTION_PRIORITY)
	end

	-- switch to the new controlModule
	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 and (self.activeControlModule == ClickToMove
					or self.activeControlModule == TouchThumbstick
					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

		self:UpdateActiveControlModuleEnabled()
	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

	self:UpdateTouchGuiVisibility()
end
