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

	if self.panEnabled and ((self.startPos and self.lastPos and self.panBeginLook) or self.inFirstPerson or self.inMouseLockedMode) 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
	self:UpdateMouseBehavior()
	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)
	self:UpdateMouseBehavior()
	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 player = Players.LocalPlayer

	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 = Util.Clamp(player.CameraMinZoomDistance, player.CameraMaxZoomDistance, desiredSubjectDistance)
		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
