< 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:49
Uncovered lines:13
Coverable lines:62
Total lines:165
Line coverage:79% (49 of 62)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Create(...)0%3.333066.67%
Dispose()0%2100%
Update()0%9.329084.21%
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 INFTShapeLoaderController controller;
 12        public INFTAsset asset;
 13    }
 14
 15    public class NFTShapeHQImageHandler : IDisposable
 16    {
 17        public static bool VERBOSE = false;
 18
 19        readonly NFTShapeHQImageConfig hqImageConfig;
 20        readonly INFTAsset asset;
 21        readonly Camera camera;
 22        readonly Transform nftControllerT;
 23
 24        bool isPlayerNear;
 25        bool isCameraInFront;
 26        bool isPlayerLooking;
 27
 28        public static NFTShapeHQImageHandler Create(NFTShapeHQImageConfig config)
 29        {
 1130            if (config.asset == null || config.controller == null)
 31            {
 032                return null;
 33            }
 34
 1135            return new NFTShapeHQImageHandler(config);
 36        }
 37
 38        public void Dispose()
 39        {
 040            CommonScriptableObjects.playerUnityPosition.OnChange -= OnPlayerPositionChanged;
 041            asset.Dispose();
 042        }
 43
 44        public void Update()
 45        {
 2046            if (hqImageConfig.controller.nftCollider is null)
 047                return;
 48
 2049            if (!isPlayerNear)
 150                return;
 51
 1952            var config = DataStore.i.Get<DataStore_NFTShape>();
 53
 1954            isCameraInFront = camera == null ||
 55                              Vector3.Dot(nftControllerT.forward,
 56                                  nftControllerT.position - camera.transform.position)
 57                              > config.hqImgInFrontDotProdMinValue;
 58
 1959            if (VERBOSE)
 60            {
 061                Debug.Log($"Camera is in front of {hqImageConfig.name}? {isCameraInFront}");
 62            }
 63
 1964            if (!isCameraInFront)
 65            {
 166                RestorePreviewTextureIfInHQ();
 167                return;
 68            }
 69
 1870            isPlayerLooking = camera == null ||
 71                              Vector3.Dot(nftControllerT.forward, camera.transform.forward) >=
 72                              config.hqImgFacingDotProdMinValue;
 73
 1874            if (VERBOSE)
 75            {
 076                Debug.Log($"Player is looking at {hqImageConfig.name}? {isPlayerLooking}");
 77            }
 78
 1879            if (isPlayerLooking)
 80            {
 1781                FetchHQTexture();
 1782            }
 83            else
 84            {
 185                RestorePreviewTextureIfInHQ();
 86            }
 187        }
 88
 1189        private NFTShapeHQImageHandler(NFTShapeHQImageConfig hqImageConfig)
 90        {
 1191            this.hqImageConfig = hqImageConfig;
 1192            this.asset = hqImageConfig.asset;
 93
 1194            camera = Camera.main;
 1195            nftControllerT = hqImageConfig.controller.transform;
 96
 1197            CommonScriptableObjects.playerUnityPosition.OnChange += OnPlayerPositionChanged;
 1198            OnPlayerPositionChanged(CommonScriptableObjects.playerUnityPosition, Vector3.zero);
 1199        }
 100
 101        private void OnPlayerPositionChanged(Vector3 current, Vector3 prev)
 102        {
 57103            isPlayerNear = false;
 104
 57105            if (hqImageConfig.controller == null || hqImageConfig.controller.nftCollider == null)
 33106                return;
 107
 24108            var config = DataStore.i.Get<DataStore_NFTShape>();
 109
 24110            isPlayerNear = ((current - hqImageConfig.controller.nftCollider.ClosestPoint(current)).sqrMagnitude
 111                            <= (config.hqImgMinDistance *
 112                                config.hqImgMinDistance));
 113
 24114            if (!isPlayerNear)
 115            {
 10116                RestorePreviewTextureIfInHQ();
 117            }
 118
 24119            if (VERBOSE)
 120            {
 0121                Debug.Log($"Player position relative to {hqImageConfig.name} is near? {isPlayerNear}");
 122            }
 24123        }
 124
 125        private void FetchHQTexture()
 126        {
 17127            if (asset.isHQ)
 0128                return;
 129
 17130            Action debugSuccess = null;
 17131            Action<Exception> debugFail = null;
 132
 17133            if (VERBOSE)
 134            {
 0135                debugSuccess = () => Debug.Log($"Success: Fetch {hqImageConfig.name} HQ image");
 0136                debugFail = error => Debug.Log($"Fail: Fetch {hqImageConfig.name} HQ image, Exception: {error}");
 137            }
 138
 139            // TODO(Brian): Asset is not supposed to fetch. Move this fetching mechanism to this class or elsewhere.
 17140            asset.FetchAndSetHQAsset(hqImageConfig.imageUrl, debugSuccess, debugFail);
 141
 17142            if (VERBOSE)
 143            {
 0144                Debug.Log($"Fetch {hqImageConfig.name} HQ image");
 145            }
 17146        }
 147
 148        private void RestorePreviewTexture()
 149        {
 3150            asset.RestorePreviewAsset();
 3151            if (VERBOSE)
 152            {
 0153                Debug.Log($"Restore {hqImageConfig.name} preview image");
 154            }
 3155        }
 156
 157        private void RestorePreviewTextureIfInHQ()
 158        {
 12159            if (!asset.isHQ)
 9160                return;
 161
 3162            RestorePreviewTexture();
 3163        }
 164    }
 165}