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