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