function ControlModule.SwitchToController(T,F)if not F then if T.activeController then T.activeController:Enable(false)end T.activeController=nil T.activeControlModule=nil else if not T.controllers[F]then T.controllers[F]=F.new(CONTROL_ACTION_PRIORITY)end if T.activeController~=T.controllers[F]then if T.activeController then T.activeController:Enable(false)end T.activeController=T.controllers[F]T.activeControlModule=F if T.touchControlFrame and(T.activeControlModule==TouchThumbpad or T.activeControlModule==TouchThumbstick or T.activeControlModule==ClickToMove or T.activeControlModule==DynamicThumbstick)then if not T.controllers[TouchJump]then T.controllers[TouchJump]=TouchJump.new()end T.touchJumpController=T.controllers[TouchJump]T.touchJumpController:Enable(true,T.touchControlFrame)else if T.touchJumpController then T.touchJumpController:Enable(false)end end T:EnableActiveControlModule()end end end function ControlModule.OnLastInputTypeChanged(T,F)if lastInputType==F 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=F if lastInputType==Enum.UserInputType.Touch then local F,r=T:SelectTouchModule()if r then while not T.touchControlFrame do wait()end T:SwitchToController(F)end elseif computerInputTypeToModuleMap[lastInputType]~=nil then local F=T:SelectComputerMovementModule()if F then T:SwitchToController(F)end end end
-- Helper function to lazily instantiate a controller if it does not yet exist,
-- disable the active controller if it is different from the on being switched to,
-- and then enable the requested controller. The argument to this function must be
-- a reference to one of the control modules, i.e. Keyboard, Gamepad, etc.
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 == 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

			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
