function BaseCamera.AdjustTouchSensitivity(O,U,A)local p=game.Workspace.CurrentCamera and game.Workspace.CurrentCamera.CFrame if not p then return A end local C=p:ToEulerAnglesYXZ()local I=TOUCH_SENSITIVTY_ADJUST_MAX_Y if C>TOUCH_ADJUST_AREA_UP and U.Y<0 then local U=(C-TOUCH_ADJUST_AREA_UP)/(MAX_Y-TOUCH_ADJUST_AREA_UP)U=1-(1-U)^3 I=TOUCH_SENSITIVTY_ADJUST_MAX_Y-U*(TOUCH_SENSITIVTY_ADJUST_MAX_Y-TOUCH_SENSITIVTY_ADJUST_MIN_Y)elseif C<TOUCH_ADJUST_AREA_DOWN and U.Y>0 then local U=(C-TOUCH_ADJUST_AREA_DOWN)/(MIN_Y-TOUCH_ADJUST_AREA_DOWN)U=1-(1-U)^3 I=TOUCH_SENSITIVTY_ADJUST_MAX_Y-U*(TOUCH_SENSITIVTY_ADJUST_MAX_Y-TOUCH_SENSITIVTY_ADJUST_MIN_Y)end return Vector2.new(A.X,A.Y*I)end function BaseCamera.OnTouchBegan(O,U,A)local p=O.isDynamicThumbstickEnabled and not A if p then if O.dynamicTouchInput==nil and isInDynamicThumbstickArea(U)then O.dynamicTouchInput=U return end O.fingerTouches[U]=A O.inputStartPositions[U]=U.Position O.inputStartTimes[U]=tick()O.numUnsunkTouches=O.numUnsunkTouches+1 end end function BaseCamera.OnTouchChanged(O,U,A)if O.fingerTouches[U]==nil then if O.isDynamicThumbstickEnabled then return end O.fingerTouches[U]=A if not A then O.numUnsunkTouches=O.numUnsunkTouches+1 end end if O.numUnsunkTouches==1 then if O.fingerTouches[U]==false then O.panBeginLook=O.panBeginLook or O:GetCameraLookVector()O.startPos=O.startPos or U.Position O.lastPos=O.lastPos or O.startPos O.userPanningTheCamera=true local A=U.Position-O.lastPos A=Vector2.new(A.X,A.Y*UserGameSettings:GetCameraYInvertValue())if O.panEnabled then local U=TOUCH_SENSITIVTY O:AdjustTouchSensitivity(A,TOUCH_SENSITIVTY)local p=O:InputTranslationToCameraAngleChange(A,U)O.rotateInput=O.rotateInput+p end O.lastPos=U.Position end else O.panBeginLook=nil O.startPos=nil O.lastPos=nil O.userPanningTheCamera=false end if O.numUnsunkTouches==2 then local U={}for A,O in pairs(O.fingerTouches)do if not O then table.insert(U,A)end end if#U==2 then local A=(U[1].Position-U[2].Position).magnitude if O.startingDiff and O.pinchBeginZoom then local U=A/math.max(.01,O.startingDiff)local p=math.clamp(U,.1,10)if O.distanceChangeEnabled then O:SetCameraToSubjectDistance(O.pinchBeginZoom/p)end else O.startingDiff=A O.pinchBeginZoom=O:GetCameraToSubjectDistance()end end else O.startingDiff=nil O.pinchBeginZoom=nil end end function BaseCamera.OnTouchEnded(O,U,A)if U==O.dynamicTouchInput then O.dynamicTouchInput=nil return end if O.fingerTouches[U]==false then if O.numUnsunkTouches==1 then O.panBeginLook=nil O.startPos=nil O.lastPos=nil O.userPanningTheCamera=false elseif O.numUnsunkTouches==2 then O.startingDiff=nil O.pinchBeginZoom=nil end end if O.fingerTouches[U]~=nil and O.fingerTouches[U]==false then O.numUnsunkTouches=O.numUnsunkTouches-1 end O.fingerTouches[U]=nil O.inputStartPositions[U]=nil O.inputStartTimes[U]=nil end function BaseCamera.OnMouse2Down(O,U,A)if A then return end O.isRightMouseDown=true O:OnMousePanButtonPressed(U,A)end function BaseCamera.OnMouse2Up(O,U,A)O.isRightMouseDown=false O:OnMousePanButtonReleased(U,A)end function BaseCamera.OnMouse3Down(O,U,A)if A then return end O.isMiddleMouseDown=true O:OnMousePanButtonPressed(U,A)end function BaseCamera.OnMouse3Up(O,U,A)O.isMiddleMouseDown=false O:OnMousePanButtonReleased(U,A)end function BaseCamera.OnMouseMoved(O,U,A)if not O.hasGameLoaded and VRService.VREnabled then return end local p=U.Delta p=Vector2.new(p.X,p.Y*UserGameSettings:GetCameraYInvertValue())local C=FFlagUserCameraToggle and CameraInput.getPanning()local I=O.startPos and(O.lastPos and O.panBeginLook)local c=I or O.inFirstPerson or O.inMouseLockedMode or C if O.panEnabled and c then local U=O:InputTranslationToCameraAngleChange(p,MOUSE_SENSITIVITY)O.rotateInput=O.rotateInput+U end if O.startPos and(O.lastPos and O.panBeginLook)then O.lastPos=O.lastPos+U.Delta end end function BaseCamera.OnMousePanButtonPressed(O,U,A)if A then return end if not FFlagUserCameraToggle then O:UpdateMouseBehavior()end O.panBeginLook=O.panBeginLook or O:GetCameraLookVector()O.startPos=O.startPos or U.Position O.lastPos=O.lastPos or O.startPos O.userPanningTheCamera=true end function BaseCamera.OnMousePanButtonReleased(O,U,A)if not FFlagUserCameraToggle then O:UpdateMouseBehavior()end if not(O.isRightMouseDown or O.isMiddleMouseDown)then O.panBeginLook=nil O.startPos=nil O.lastPos=nil O.userPanningTheCamera=false end end function BaseCamera.UpdateMouseBehavior(U)if FFlagUserCameraToggle and U.isCameraToggle then CameraUI.setCameraModeToastEnabled(true)CameraInput.enableCameraToggleInput()CameraToggleStateController(U.inFirstPerson)else if FFlagUserCameraToggle then CameraUI.setCameraModeToastEnabled(false)CameraInput.disableCameraToggleInput()end if U.inFirstPerson or U.inMouseLockedMode then UserInputService.MouseBehavior=Enum.MouseBehavior.LockCenter else UserGameSettings.RotationType=Enum.RotationType.MovementRelative if U.isRightMouseDown or U.isMiddleMouseDown then UserInputService.MouseBehavior=Enum.MouseBehavior.LockCurrentPosition else UserInputService.MouseBehavior=Enum.MouseBehavior.Default end end end end function BaseCamera.UpdateForDistancePropertyChange(U)U:SetCameraToSubjectDistance(U.currentSubjectDistance)end function BaseCamera.SetCameraToSubjectDistance(A,U)local O=A.currentSubjectDistance if player.CameraMode==Enum.CameraMode.LockFirstPerson then A.currentSubjectDistance=.5 if not A.inFirstPerson then A:EnterFirstPerson()end else local O=math.clamp(U,player.CameraMinZoomDistance,player.CameraMaxZoomDistance)if O<FIRST_PERSON_DISTANCE_THRESHOLD then A.currentSubjectDistance=.5 if not A.inFirstPerson then A:EnterFirstPerson()end else A.currentSubjectDistance=O if A.inFirstPerson then A:LeaveFirstPerson()end end end ZoomController.SetZoomParameters(A.currentSubjectDistance,math.sign(U-O))return A.currentSubjectDistance end function BaseCamera.SetCameraType(A,U)A.cameraType=U end function BaseCamera.GetCameraType(U)return U.cameraType end

function BaseCamera:AdjustTouchSensitivity(delta, sensitivity)
	local cameraCFrame = game.Workspace.CurrentCamera and game.Workspace.CurrentCamera.CFrame
	if not cameraCFrame then
		return sensitivity
	end
	local currPitchAngle = cameraCFrame:ToEulerAnglesYXZ()

	local multiplierY = TOUCH_SENSITIVTY_ADJUST_MAX_Y
	if currPitchAngle > TOUCH_ADJUST_AREA_UP and delta.Y < 0 then
		local fractionAdjust = (currPitchAngle - TOUCH_ADJUST_AREA_UP)/(MAX_Y - TOUCH_ADJUST_AREA_UP)
		fractionAdjust = 1 - (1 - fractionAdjust)^3
		multiplierY = TOUCH_SENSITIVTY_ADJUST_MAX_Y - fractionAdjust * (
			TOUCH_SENSITIVTY_ADJUST_MAX_Y - TOUCH_SENSITIVTY_ADJUST_MIN_Y)
	elseif currPitchAngle < TOUCH_ADJUST_AREA_DOWN and delta.Y > 0 then
		local fractionAdjust = (currPitchAngle - TOUCH_ADJUST_AREA_DOWN)/(MIN_Y - TOUCH_ADJUST_AREA_DOWN)
		fractionAdjust = 1 - (1 - fractionAdjust)^3
		multiplierY = TOUCH_SENSITIVTY_ADJUST_MAX_Y - fractionAdjust * (
			TOUCH_SENSITIVTY_ADJUST_MAX_Y - TOUCH_SENSITIVTY_ADJUST_MIN_Y)
	end

	return Vector2.new(
		sensitivity.X,
		sensitivity.Y * multiplierY
	)
end

function BaseCamera:OnTouchBegan(input, processed)
	local canUseDynamicTouch = self.isDynamicThumbstickEnabled and not processed
	if canUseDynamicTouch then
		if self.dynamicTouchInput == nil and isInDynamicThumbstickArea(input) then
			-- First input in the dynamic thumbstick area should always be ignored for camera purposes
			-- Even if the dynamic thumbstick does not process it immediately
			self.dynamicTouchInput = input
			return
		end
		self.fingerTouches[input] = processed
		self.inputStartPositions[input] = input.Position
		self.inputStartTimes[input] = tick()
		self.numUnsunkTouches = self.numUnsunkTouches + 1
	end
end

function BaseCamera:OnTouchChanged(input, processed)
	if self.fingerTouches[input] == nil then
		if self.isDynamicThumbstickEnabled then
			return
		end
		self.fingerTouches[input] = processed
		if not processed then
			self.numUnsunkTouches = self.numUnsunkTouches + 1
		end
	end

	if self.numUnsunkTouches == 1 then
		if self.fingerTouches[input] == false then
			self.panBeginLook = self.panBeginLook or self:GetCameraLookVector()
			self.startPos = self.startPos or input.Position
			self.lastPos = self.lastPos or self.startPos
			self.userPanningTheCamera = true

			local delta = input.Position - self.lastPos
			delta = Vector2.new(delta.X, delta.Y * UserGameSettings:GetCameraYInvertValue())
			if self.panEnabled then
				local adjustedTouchSensitivity = TOUCH_SENSITIVTY
				self:AdjustTouchSensitivity(delta, TOUCH_SENSITIVTY)

				local desiredXYVector = self:InputTranslationToCameraAngleChange(delta, adjustedTouchSensitivity)
				self.rotateInput = self.rotateInput + desiredXYVector
			end
			self.lastPos = input.Position
		end
	else
		self.panBeginLook = nil
		self.startPos = nil
		self.lastPos = nil
		self.userPanningTheCamera = false
	end
	if self.numUnsunkTouches == 2 then
		local unsunkTouches = {}
		for touch, wasSunk in pairs(self.fingerTouches) do
			if not wasSunk then
				table.insert(unsunkTouches, touch)
			end
		end
		if #unsunkTouches == 2 then
			local difference = (unsunkTouches[1].Position - unsunkTouches[2].Position).magnitude
			if self.startingDiff and self.pinchBeginZoom then
				local scale = difference / math.max(0.01, self.startingDiff)
				local clampedScale = math.clamp(scale, 0.1, 10)
				if self.distanceChangeEnabled then
					self:SetCameraToSubjectDistance(self.pinchBeginZoom / clampedScale)
				end
			else
				self.startingDiff = difference
				self.pinchBeginZoom = self:GetCameraToSubjectDistance()
			end
		end
	else
		self.startingDiff = nil
		self.pinchBeginZoom = nil
	end
end

function BaseCamera:OnTouchEnded(input, processed)
	if input == self.dynamicTouchInput then
		self.dynamicTouchInput = nil
		return
	end

	if self.fingerTouches[input] == false then
		if self.numUnsunkTouches == 1 then
			self.panBeginLook = nil
			self.startPos = nil
			self.lastPos = nil
			self.userPanningTheCamera = false
		elseif self.numUnsunkTouches == 2 then
			self.startingDiff = nil
			self.pinchBeginZoom = nil
		end
	end

	if self.fingerTouches[input] ~= nil and self.fingerTouches[input] == false then
		self.numUnsunkTouches = self.numUnsunkTouches - 1
	end
	self.fingerTouches[input] = nil
	self.inputStartPositions[input] = nil
	self.inputStartTimes[input] = nil
end

function BaseCamera:OnMouse2Down(input, processed)
	if processed then return end

	self.isRightMouseDown = true
	self:OnMousePanButtonPressed(input, processed)
end

function BaseCamera:OnMouse2Up(input, processed)
	self.isRightMouseDown = false
	self:OnMousePanButtonReleased(input, processed)
end

function BaseCamera:OnMouse3Down(input, processed)
	if processed then return end

	self.isMiddleMouseDown = true
	self:OnMousePanButtonPressed(input, processed)
end

function BaseCamera:OnMouse3Up(input, processed)
	self.isMiddleMouseDown = false
	self:OnMousePanButtonReleased(input, processed)
end

function BaseCamera:OnMouseMoved(input, processed)
	if not self.hasGameLoaded and VRService.VREnabled then
		return
	end

	local inputDelta = input.Delta
	inputDelta = Vector2.new(inputDelta.X, inputDelta.Y * UserGameSettings:GetCameraYInvertValue())

	local isInputPanning = FFlagUserCameraToggle and CameraInput.getPanning()
	local isBeginLook = self.startPos and self.lastPos and self.panBeginLook
	local isPanning = isBeginLook or self.inFirstPerson or self.inMouseLockedMode or isInputPanning

	if self.panEnabled and isPanning then
		local desiredXYVector = self:InputTranslationToCameraAngleChange(inputDelta, MOUSE_SENSITIVITY)
		self.rotateInput = self.rotateInput + desiredXYVector
	end

	if self.startPos and self.lastPos and self.panBeginLook then
		self.lastPos = self.lastPos + input.Delta
	end
end

function BaseCamera:OnMousePanButtonPressed(input, processed)
	if processed then return end
	if not FFlagUserCameraToggle then
		self:UpdateMouseBehavior()
	end
	self.panBeginLook = self.panBeginLook or self:GetCameraLookVector()
	self.startPos = self.startPos or input.Position
	self.lastPos = self.lastPos or self.startPos
	self.userPanningTheCamera = true
end

function BaseCamera:OnMousePanButtonReleased(input, processed)
	if not FFlagUserCameraToggle then
		self:UpdateMouseBehavior()
	end
	if not (self.isRightMouseDown or self.isMiddleMouseDown) then
		self.panBeginLook = nil
		self.startPos = nil
		self.lastPos = nil
		self.userPanningTheCamera = false
	end
end

function BaseCamera:UpdateMouseBehavior()
	if FFlagUserCameraToggle and self.isCameraToggle then
		CameraUI.setCameraModeToastEnabled(true)
		CameraInput.enableCameraToggleInput()
		CameraToggleStateController(self.inFirstPerson)
	else
		if FFlagUserCameraToggle then
			CameraUI.setCameraModeToastEnabled(false)
			CameraInput.disableCameraToggleInput()
		end
		-- first time transition to first person mode or mouse-locked third person
		if self.inFirstPerson or self.inMouseLockedMode then
			--UserGameSettings.RotationType = Enum.RotationType.CameraRelative
			UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		else
			UserGameSettings.RotationType = Enum.RotationType.MovementRelative
			if self.isRightMouseDown or self.isMiddleMouseDown then
				UserInputService.MouseBehavior = Enum.MouseBehavior.LockCurrentPosition
			else
				UserInputService.MouseBehavior = Enum.MouseBehavior.Default
			end
		end
	end
end

function BaseCamera:UpdateForDistancePropertyChange()
	-- Calling this setter with the current value will force checking that it is still
	-- in range after a change to the min/max distance limits
	self:SetCameraToSubjectDistance(self.currentSubjectDistance)
end

function BaseCamera:SetCameraToSubjectDistance(desiredSubjectDistance)
	local lastSubjectDistance = self.currentSubjectDistance

	-- By default, camera modules will respect LockFirstPerson and override the currentSubjectDistance with 0
	-- regardless of what Player.CameraMinZoomDistance is set to, so that first person can be made
	-- available by the developer without needing to allow players to mousewheel dolly into first person.
	-- Some modules will override this function to remove or change first-person capability.
	if player.CameraMode == Enum.CameraMode.LockFirstPerson then
		self.currentSubjectDistance = 0.5
		if not self.inFirstPerson then
			self:EnterFirstPerson()
		end
	else
		local newSubjectDistance = math.clamp(desiredSubjectDistance, player.CameraMinZoomDistance, player.CameraMaxZoomDistance)
		if newSubjectDistance < FIRST_PERSON_DISTANCE_THRESHOLD then
			self.currentSubjectDistance = 0.5
			if not self.inFirstPerson then
				self:EnterFirstPerson()
			end
		else
			self.currentSubjectDistance = newSubjectDistance
			if self.inFirstPerson then
				self:LeaveFirstPerson()
			end
		end
	end

	-- Pass target distance and zoom direction to the zoom controller
	ZoomController.SetZoomParameters(self.currentSubjectDistance, math.sign(desiredSubjectDistance - lastSubjectDistance))

	-- Returned only for convenience to the caller to know the outcome
	return self.currentSubjectDistance
end

function BaseCamera:SetCameraType( cameraType )
	--Used by derived classes
	self.cameraType = cameraType
end

function BaseCamera:GetCameraType()
	return self.cameraType
end
