function ControlModule.SwitchToController(D,K)if not K then if D.activeController then D.activeController:Enable(false)end D.activeController=nil D.activeControlModule=nil return end if not D.controllers[K]then D.controllers[K]=K.new(CONTROL_ACTION_PRIORITY)end if D.activeController~=D.controllers[K]then if D.activeController then D.activeController:Enable(false)end D.activeController=D.controllers[K]D.activeControlModule=K if D.touchControlFrame and(D.activeControlModule==ClickToMove or D.activeControlModule==TouchThumbstick or D.activeControlModule==DynamicThumbstick)then if not D.controllers[TouchJump]then D.controllers[TouchJump]=TouchJump.new()end D.touchJumpController=D.controllers[TouchJump]D.touchJumpController:Enable(true,D.touchControlFrame)else if D.touchJumpController then D.touchJumpController:Enable(false)end end D:UpdateActiveControlModuleEnabled()end end function ControlModule.OnLastInputTypeChanged(D,K)if lastInputType==K 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=K if lastInputType==Enum.UserInputType.Touch then local K,C=D:SelectTouchModule()if C then while not D.touchControlFrame do wait()end D:SwitchToController(K)end elseif computerInputTypeToModuleMap[lastInputType]~=nil then local K=D:SelectComputerMovementModule()if K then D:SwitchToController(K)end end D:UpdateTouchGuiVisibility()end
-- This function should handle all controller enabling and disabling without relying on
-- ControlModule:Enable() and Disable()
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
