< Summary

Class:NFTShape_Internal.NFTShapeHQImageHandler
Assembly:DCL.Components.LoadableShapes
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/NFTShape/NFTShapeHQImageHandler.cs
Covered lines:50
Uncovered lines:11
Coverable lines:61
Total lines:166
Line coverage:81.9% (50 of 61)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Create(...)0%4.594066.67%
Dispose()0%110100%
Update()0%9.899077.78%
NFTShapeHQImageHandler(...)0%110100%
OnPlayerPositionChanged(...)0%5.025090%
FetchHQTexture()0%4.774063.64%
RestorePreviewTexture()0%2.062075%
RestorePreviewTextureIfInHQ()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/NFTShape/NFTShapeHQImageHandler.cs

#LineLine coverage
 1using System;
 2using DCL;
 3using UnityEngine;
 4
 5namespace NFTShape_Internal
 6{
 7    public class NFTShapeHQImageConfig
 8    {
 9        public string name;
 10        public string imageUrl;
 11        public Transform transform;
 12        public Collider collider;
 13        public INFTAsset asset;
 14    }
 15
 16    public class NFTShapeHQImageHandler : IDisposable
 17    {
 18        public static bool VERBOSE = false;
 19
 20        readonly NFTShapeHQImageConfig hqImageConfig;
 21        readonly INFTAsset asset;
 22        readonly Camera camera;
 23        readonly Transform nftControllerT;
 24
 25        bool isPlayerNear;
 26        bool isCameraInFront;
 27        bool isPlayerLooking;
 28
 29        public static NFTShapeHQImageHandler Create(NFTShapeHQImageConfig config)
 30        {
 1331            if (config.asset == null || !config.collider || !config.transform)
 32            {
 033                return null;
 34            }
 35
 1336            return new NFTShapeHQImageHandler(config);
 37        }
 38
 39        public void Dispose()
 40        {
 141            CommonScriptableObjects.playerUnityPosition.OnChange -= OnPlayerPositionChanged;
 142            asset.Dispose();
 143        }
 44
 45        public void Update()
 46        {
 2247            if (!hqImageConfig.collider)
 048                return;
 49
 2250            if (!isPlayerNear)
 051                return;
 52
 2253            var config = DataStore.i.Get<DataStore_NFTShape>();
 54
 2255            isCameraInFront = camera == null ||
 56                              Vector3.Dot(nftControllerT.forward,
 57                                  nftControllerT.position - camera.transform.position)
 58                              > config.hqImgInFrontDotProdMinValue;
 59
 2260            if (VERBOSE)
 61            {
 062                Debug.Log($"Camera is in front of {hqImageConfig.name}? {isCameraInFront}");
 63            }
 64
 2265            if (!isCameraInFront)
 66            {
 167                RestorePreviewTextureIfInHQ();
 168                return;
 69            }
 70
 2171            isPlayerLooking = camera == null ||
 72                              Vector3.Dot(nftControllerT.forward, camera.transform.forward) >=
 73                              config.hqImgFacingDotProdMinValue;
 74
 2175            if (VERBOSE)
 76            {
 077                Debug.Log($"Player is looking at {hqImageConfig.name}? {isPlayerLooking}");
 78            }
 79
 2180            if (isPlayerLooking)
 81            {
 2082                FetchHQTexture();
 83            }
 84            else
 85            {
 186                RestorePreviewTextureIfInHQ();
 87            }
 188        }
 89
 1390        private NFTShapeHQImageHandler(NFTShapeHQImageConfig hqImageConfig)
 91        {
 1392            this.hqImageConfig = hqImageConfig;
 1393            this.asset = hqImageConfig.asset;
 94
 1395            camera = Camera.main;
 1396            nftControllerT = hqImageConfig.transform;
 97
 1398            CommonScriptableObjects.playerUnityPosition.OnChange += OnPlayerPositionChanged;
 1399            OnPlayerPositionChanged(CommonScriptableObjects.playerUnityPosition, Vector3.zero);
 13100        }
 101
 102        private void OnPlayerPositionChanged(Vector3 current, Vector3 prev)
 103        {
 63104            isPlayerNear = false;
 105
 63106            if (!hqImageConfig.transform || !hqImageConfig.collider)
 35107                return;
 108
 28109            var config = DataStore.i.Get<DataStore_NFTShape>();
 110
 28111            isPlayerNear = ((current - hqImageConfig.collider.ClosestPoint(current)).sqrMagnitude
 112                            <= (config.hqImgMinDistance *
 113                                config.hqImgMinDistance));
 114
 28115            if (!isPlayerNear)
 116            {
 10117                RestorePreviewTextureIfInHQ();
 118            }
 119
 28120            if (VERBOSE)
 121            {
 0122                Debug.Log($"Player position relative to {hqImageConfig.name} is near? {isPlayerNear}");
 123            }
 28124        }
 125
 126        private void FetchHQTexture()
 127        {
 20128            if (asset.isHQ)
 0129                return;
 130
 20131            Action debugSuccess = null;
 20132            Action<Exception> debugFail = null;
 133
 20134            if (VERBOSE)
 135            {
 0136                debugSuccess = () => Debug.Log($"Success: Fetch {hqImageConfig.name} HQ image");
 0137                debugFail = error => Debug.Log($"Fail: Fetch {hqImageConfig.name} HQ image, Exception: {error}");
 138            }
 139
 140            // TODO(Brian): Asset is not supposed to fetch. Move this fetching mechanism to this class or elsewhere.
 20141            asset.FetchAndSetHQAsset(hqImageConfig.imageUrl, debugSuccess, debugFail);
 142
 20143            if (VERBOSE)
 144            {
 0145                Debug.Log($"Fetch {hqImageConfig.name} HQ image");
 146            }
 20147        }
 148
 149        private void RestorePreviewTexture()
 150        {
 3151            asset.RestorePreviewAsset();
 3152            if (VERBOSE)
 153            {
 0154                Debug.Log($"Restore {hqImageConfig.name} preview image");
 155            }
 3156        }
 157
 158        private void RestorePreviewTextureIfInHQ()
 159        {
 12160            if (!asset.isHQ)
 9161                return;
 162
 3163            RestorePreviewTexture();
 3164        }
 165    }
 166}