< Summary

Class:DCL.Camera.CameraController
Assembly:Camera
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Camera/CameraController.cs
Covered lines:85
Uncovered lines:26
Coverable lines:111
Total lines:282
Line coverage:76.5% (85 of 111)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
CameraController()0%110100%
Awake()0%55095.65%
OnFullscreenUIVisibilityChange(...)0%12300%
OnOutputTextureChange(...)0%110100%
TryGetCameraStateByType[T](...)0%3.143075%
OnRenderingStateChanged(...)0%220100%
CameraBlocked_OnChange(...)0%6200%
OnMouseWheelChangeValue(...)0%72800%
OnCameraChangeAction(...)0%2.262060%
SetCameraMode(...)0%110100%
OnCameraModeChange(...)0%220100%
GetCameraMode(...)0%110100%
OnWorldReposition(...)0%110100%
Update()0%7.127086.67%
SetRotation(...)0%220100%
SetRotation(...)0%6200%
GetRotation()0%2.152066.67%
GetPosition()0%2100%
GetCamera()0%2100%
SetInvertYAxis(...)0%110100%
OnDestroy()0%330100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/Camera/CameraController.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using Cinemachine;
 5using DCL.Helpers;
 6using DCL.Interface;
 7using DCL;
 8using UnityEngine;
 9
 10namespace DCL.Camera
 11{
 12    public class CameraController : MonoBehaviour
 13    {
 14        [SerializeField]
 15        internal new UnityEngine.Camera camera;
 16
 17        private Transform cameraTransform;
 18
 19        [SerializeField]
 20        internal CinemachineFreeLook thirdPersonCamera;
 21
 22        [Header("Virtual Cameras")]
 23        [SerializeField]
 24        internal CinemachineBrain cameraBrain;
 25
 26        [SerializeField]
 27        internal CameraStateBase[] cameraModes;
 28
 29        [Header("InputActions")]
 30        [SerializeField]
 31        internal InputAction_Trigger cameraChangeAction;
 32        [SerializeField]
 33        internal InputAction_Measurable mouseWheelAction;
 34
 35        internal Dictionary<CameraMode.ModeId, CameraStateBase> cachedModeToVirtualCamera;
 36
 37        public delegate void CameraBlendStarted();
 38
 39        public event CameraBlendStarted onCameraBlendStarted;
 40
 41        public delegate void CameraBlendFinished();
 42
 43        public event CameraBlendFinished onCameraBlendFinished;
 44
 45        private bool wasBlendingLastFrame;
 46
 59847        private float mouseWheelThreshold = 0.04f;
 48
 554849        private Vector3Variable cameraForward => CommonScriptableObjects.cameraForward;
 554850        private Vector3Variable cameraRight => CommonScriptableObjects.cameraRight;
 554851        private Vector3Variable cameraPosition => CommonScriptableObjects.cameraPosition;
 119452        private Vector3Variable worldOffset => CommonScriptableObjects.worldOffset;
 554853        private BooleanVariable cameraIsBlending => CommonScriptableObjects.cameraIsBlending;
 54
 558055        public CameraStateBase currentCameraState => cachedModeToVirtualCamera[CommonScriptableObjects.cameraMode];
 56
 57        [HideInInspector]
 58        public Action<CameraMode.ModeId> onSetCameraMode;
 59
 60        private void Awake()
 61        {
 59762            cameraTransform = this.camera.transform;
 63
 59764            CommonScriptableObjects.rendererState.OnChange += OnRenderingStateChanged;
 59765            OnRenderingStateChanged(CommonScriptableObjects.rendererState.Get(), false);
 66
 59767            CommonScriptableObjects.cameraBlocked.OnChange += CameraBlocked_OnChange;
 68
 417969            cachedModeToVirtualCamera = cameraModes.ToDictionary(x => x.cameraModeId, x => x);
 70
 59771            using (var iterator = cachedModeToVirtualCamera.GetEnumerator())
 72            {
 238873                while (iterator.MoveNext())
 74                {
 179175                    iterator.Current.Value.Initialize(camera);
 76                }
 59777            }
 78
 59779            cameraChangeAction.OnTriggered += OnCameraChangeAction;
 59780            mouseWheelAction.OnValueChanged += OnMouseWheelChangeValue;
 59781            worldOffset.OnChange += OnWorldReposition;
 59782            CommonScriptableObjects.cameraMode.OnChange += OnCameraModeChange;
 83
 59784            OnCameraModeChange(CommonScriptableObjects.cameraMode, CameraMode.ModeId.FirstPerson);
 85
 59786            if (CommonScriptableObjects.isFullscreenHUDOpen)
 087                OnFullscreenUIVisibilityChange(CommonScriptableObjects.isFullscreenHUDOpen.Get(), !CommonScriptableObjec
 88
 59789            CommonScriptableObjects.isFullscreenHUDOpen.OnChange += OnFullscreenUIVisibilityChange;
 90
 59791            DataStore.i.camera.outputTexture.OnChange += OnOutputTextureChange;
 59792            OnOutputTextureChange(DataStore.i.camera.outputTexture.Get(), null);
 93
 59794            DataStore.i.camera.invertYAxis.OnChange += SetInvertYAxis;
 59795            SetInvertYAxis(DataStore.i.camera.invertYAxis.Get(), false);
 96
 59797            wasBlendingLastFrame = false;
 59798        }
 99
 100        void OnFullscreenUIVisibilityChange(bool visibleState, bool prevVisibleState)
 101        {
 0102            if (visibleState == prevVisibleState)
 0103                return;
 104
 0105            camera.enabled = !visibleState && CommonScriptableObjects.rendererState.Get();
 0106        }
 107
 108        void OnOutputTextureChange(RenderTexture current, RenderTexture previous)
 109        {
 597110            camera.targetTexture = current;
 597111        }
 112
 113        public bool TryGetCameraStateByType<T>(out CameraStateBase searchedCameraState)
 114        {
 539115            foreach (CameraStateBase cameraMode in cameraModes)
 116            {
 231117                if (cameraMode.GetType() == typeof(T))
 118                {
 77119                    searchedCameraState = cameraMode;
 77120                    return true;
 121                }
 122            }
 123
 0124            searchedCameraState = null;
 0125            return false;
 126        }
 127
 128        private void OnRenderingStateChanged(bool enabled, bool prevState)
 129        {
 599130            camera.enabled = enabled && !CommonScriptableObjects.isFullscreenHUDOpen;
 599131        }
 132
 133        private void CameraBlocked_OnChange(bool current, bool previous)
 134        {
 0135            foreach (CameraStateBase cam in cameraModes)
 136            {
 0137                cam.OnBlock(current);
 138            }
 0139        }
 140
 141        private void OnMouseWheelChangeValue(DCLAction_Measurable action, float value)
 142        {
 0143            if (value > -mouseWheelThreshold && value < mouseWheelThreshold) return;
 0144            if (Utils.IsPointerOverUIElement()) return;
 145
 0146            if (CommonScriptableObjects.cameraMode == CameraMode.ModeId.FirstPerson && value < -mouseWheelThreshold)
 0147                SetCameraMode(CameraMode.ModeId.ThirdPerson);
 148
 0149            if (CommonScriptableObjects.cameraMode == CameraMode.ModeId.ThirdPerson && value > mouseWheelThreshold)
 0150                SetCameraMode(CameraMode.ModeId.FirstPerson);
 0151        }
 152
 153        private void OnCameraChangeAction(DCLAction_Trigger action)
 154        {
 1155            if (CommonScriptableObjects.cameraMode == CameraMode.ModeId.FirstPerson)
 156            {
 0157                SetCameraMode(CameraMode.ModeId.ThirdPerson);
 0158            }
 159            else
 160            {
 1161                SetCameraMode(CameraMode.ModeId.FirstPerson);
 162            }
 1163        }
 164
 165        public void SetCameraMode(CameraMode.ModeId newMode)
 166        {
 3167            CommonScriptableObjects.cameraMode.Set(newMode);
 3168        }
 169
 170        private void OnCameraModeChange(CameraMode.ModeId current, CameraMode.ModeId previous)
 171        {
 599172            cachedModeToVirtualCamera[previous].OnUnselect();
 599173            cachedModeToVirtualCamera[current].OnSelect();
 174
 599175            WebInterface.ReportCameraChanged(current);
 176
 599177            onSetCameraMode?.Invoke(current);
 2178        }
 179
 180        public CameraStateBase GetCameraMode(CameraMode.ModeId mode)
 181        {
 1716182            return cameraModes.FirstOrDefault(x => x.cameraModeId == mode);
 183        }
 184
 185        private void OnWorldReposition(Vector3 newValue, Vector3 oldValue)
 186        {
 2187            transform.position += newValue - oldValue;
 2188        }
 189
 190        private void Update()
 191        {
 5548192            cameraForward.Set(cameraTransform.forward);
 5548193            cameraRight.Set(cameraTransform.right);
 5548194            DataStore.i.camera.rotation.Set(cameraTransform.rotation);
 5548195            cameraPosition.Set(cameraTransform.position);
 5548196            cameraIsBlending.Set(cameraBrain.IsBlending);
 197
 5548198            if (cameraBrain.IsBlending)
 199            {
 101200                if (!wasBlendingLastFrame)
 38201                    onCameraBlendStarted?.Invoke();
 202
 101203                wasBlendingLastFrame = true;
 101204            }
 5447205            else if (wasBlendingLastFrame)
 206            {
 0207                onCameraBlendFinished?.Invoke();
 208
 0209                wasBlendingLastFrame = false;
 210            }
 211
 5548212            currentCameraState?.OnUpdate();
 5548213        }
 214
 215        public void SetRotation(string setRotationPayload)
 216        {
 6217            var payload = Utils.FromJsonWithNulls<SetRotationPayload>(setRotationPayload);
 6218            currentCameraState?.OnSetRotation(payload);
 6219        }
 220
 221        public void SetRotation(float x, float y, float z, Vector3? cameraTarget = null)
 222        {
 0223            currentCameraState?.OnSetRotation(new SetRotationPayload() { x = x, y = y, z = z, cameraTarget = cameraTarge
 0224        }
 225
 226        public Vector3 GetRotation()
 227        {
 12228            if (currentCameraState != null)
 12229                return currentCameraState.OnGetRotation();
 230
 0231            return Vector3.zero;
 232        }
 233
 234        public Vector3 GetPosition()
 235        {
 0236            return CinemachineCore.Instance.GetActiveBrain(0).ActiveVirtualCamera.State.FinalPosition;
 237        }
 238
 239        public UnityEngine.Camera GetCamera()
 240        {
 0241            return camera;
 242        }
 243
 244        private void SetInvertYAxis(bool current, bool previous)
 245        {
 610246            thirdPersonCamera.m_YAxis.m_InvertInput = !current;
 610247        }
 248
 249        private void OnDestroy()
 250        {
 597251            if (cachedModeToVirtualCamera != null)
 252            {
 597253                using (var iterator = cachedModeToVirtualCamera.GetEnumerator())
 254                {
 2388255                    while (iterator.MoveNext())
 256                    {
 1791257                        iterator.Current.Value.Cleanup();
 258                    }
 597259                }
 260            }
 261
 597262            worldOffset.OnChange -= OnWorldReposition;
 597263            cameraChangeAction.OnTriggered -= OnCameraChangeAction;
 597264            mouseWheelAction.OnValueChanged -= OnMouseWheelChangeValue;
 597265            CommonScriptableObjects.rendererState.OnChange -= OnRenderingStateChanged;
 597266            CommonScriptableObjects.cameraBlocked.OnChange -= CameraBlocked_OnChange;
 597267            CommonScriptableObjects.isFullscreenHUDOpen.OnChange -= OnFullscreenUIVisibilityChange;
 597268            CommonScriptableObjects.cameraMode.OnChange -= OnCameraModeChange;
 597269            DataStore.i.camera.outputTexture.OnChange -= OnOutputTextureChange;
 597270            DataStore.i.camera.invertYAxis.OnChange -= SetInvertYAxis;
 597271        }
 272
 273        [Serializable]
 274        public class SetRotationPayload
 275        {
 276            public float x;
 277            public float y;
 278            public float z;
 279            public Vector3? cameraTarget;
 280        }
 281    }
 282}