function ControlModule.SwitchToController(Z,j)if not j then if Z.activeController then Z.activeController:Enable(false)end Z.activeController=nil Z.activeControlModule=nil else if not Z.controllers[j]then Z.controllers[j]=j.new(CONTROL_ACTION_PRIORITY)end if Z.activeController~=Z.controllers[j]then if Z.activeController then Z.activeController:Enable(false)end Z.activeController=Z.controllers[j]Z.activeControlModule=j if Z.touchControlFrame then Z.activeController:Enable(true,Z.touchControlFrame)else if Z.activeControlModule==ClickToMove then Z.activeController:Enable(true,Players.LocalPlayer.DevComputerMovementMode==Enum.DevComputerMovementMode.UserChoice)else Z.activeController:Enable(true)end end if Z.touchControlFrame and(Z.activeControlModule==TouchThumbpad or Z.activeControlModule==TouchThumbstick or Z.activeControlModule==ClickToMove or Z.activeControlModule==DynamicThumbstick)then if not Z.controllers[TouchJump]then Z.controllers[TouchJump]=TouchJump.new()end Z.touchJumpController=Z.controllers[TouchJump]Z.touchJumpController:Enable(true,Z.touchControlFrame)else if Z.touchJumpController then Z.touchJumpController:Enable(false)end end end end end function ControlModule.OnLastInputTypeChanged(Z,j)if lastInputType==j 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=j if lastInputType==Enum.UserInputType.Touch then local j,h=Z:SelectTouchModule()if h then while not Z.touchControlFrame do wait()end Z:SwitchToController(j)end elseif computerInputTypeToModuleMap[lastInputType]~=nil then local j=Z:SelectComputerMovementModule()if j then Z:SwitchToController(j)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 then
				self.activeController:Enable(true, self.touchControlFrame)
			else
				if self.activeControlModule == ClickToMove then
					-- For ClickToMove, when it is the player's choice, we also enable the full keyboard controls.
					-- When the developer is forcing click to move, the most keyboard controls (WASD) are not available, only spacebar to jump.
					self.activeController:Enable(
						true,
						Players.LocalPlayer.DevComputerMovementMode == Enum.DevComputerMovementMode.UserChoice
					)
				else
					self.activeController:Enable(true)
				end
			end
			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
		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
