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

	self:UpdateTouchGuiVisibility()
end
