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