| | 1 | | using Cinemachine.Utility; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Camera; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCLFeatures.ScreencaptureCamera.CameraObject |
| | 8 | | { |
| | 9 | | public class ScreencaptureCameraRotation |
| | 10 | | { |
| | 11 | | private const DCLAction_Hold DUMMY_ACTION = new (); |
| | 12 | |
|
| | 13 | | private readonly RotationInputSchema input; |
| | 14 | | private readonly InputSpikeFixer[] inputSpikeFixer; |
| | 15 | |
|
| | 16 | | private bool mouseControlIsEnabled; |
| | 17 | |
|
| | 18 | | private Vector3 axis; |
| | 19 | | private Vector3 axisTarget; |
| | 20 | |
|
| 0 | 21 | | public ScreencaptureCameraRotation(RotationInputSchema inputSchema) |
| | 22 | | { |
| 0 | 23 | | input = inputSchema; |
| | 24 | |
|
| 0 | 25 | | inputSpikeFixer = new[] |
| | 26 | | { |
| 0 | 27 | | new InputSpikeFixer(() => Utils.IsCursorLocked ? CursorLockMode.Locked : CursorLockMode.None), |
| 0 | 28 | | new InputSpikeFixer(() => Utils.IsCursorLocked ? CursorLockMode.Locked : CursorLockMode.None), |
| | 29 | | }; |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | public void Rotate(Transform target, float deltaTime, float rotationSpeed, float dampTime, float maxRotationPerF |
| | 33 | | { |
| 0 | 34 | | if (!mouseControlIsEnabled) return; |
| | 35 | |
|
| 0 | 36 | | Vector3 currentAngles = target.eulerAngles; |
| | 37 | |
|
| 0 | 38 | | axisTarget[0] = input.CameraXAxis.GetValue(); |
| 0 | 39 | | axisTarget[1] = input.CameraYAxis.GetValue(); |
| 0 | 40 | | axis += Damper.Damp(axisTarget - axis, dampTime, deltaTime); |
| | 41 | |
|
| 0 | 42 | | currentAngles.y += Mathf.Clamp(inputSpikeFixer[0].GetValue(axis[0]) * rotationSpeed * deltaTime, -maxRotatio |
| 0 | 43 | | currentAngles.x -= Mathf.Clamp(inputSpikeFixer[1].GetValue(axis[1]) * rotationSpeed * deltaTime, -maxRotatio |
| | 44 | |
|
| 0 | 45 | | target.rotation = Quaternion.Euler(currentAngles); |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | public void Activate() |
| | 49 | | { |
| 0 | 50 | | if (Utils.IsCursorLocked) |
| 0 | 51 | | EnableRotation(DUMMY_ACTION); |
| | 52 | |
|
| 0 | 53 | | input.MouseFirstClick.OnStarted += EnableRotation; |
| 0 | 54 | | input.MouseFirstClick.OnFinished += DisableRotation; |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public void Deactivate() |
| | 58 | | { |
| 0 | 59 | | DisableRotation(DUMMY_ACTION); |
| | 60 | |
|
| 0 | 61 | | input.MouseFirstClick.OnStarted -= EnableRotation; |
| 0 | 62 | | input.MouseFirstClick.OnFinished -= DisableRotation; |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | private void EnableRotation(DCLAction_Hold _) => |
| 0 | 66 | | SwitchRotation(isEnabled: true); |
| | 67 | |
|
| | 68 | | private void DisableRotation(DCLAction_Hold _) |
| | 69 | | { |
| 0 | 70 | | SwitchRotation(isEnabled: false); |
| 0 | 71 | | Utils.UnlockCursor(); |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | private void SwitchRotation(bool isEnabled) |
| | 75 | | { |
| 0 | 76 | | DataStore.i.camera.panning.Set(false); |
| 0 | 77 | | mouseControlIsEnabled = isEnabled; |
| 0 | 78 | | } |
| | 79 | | } |
| | 80 | | } |