< 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:2
Uncovered lines:58
Coverable lines:60
Total lines:171
Line coverage:3.3% (2 of 60)
Covered branches:0
Total branches:0
Covered methods:1
Total methods:8
Method coverage:12.5% (1 of 8)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Create(...)0%330100%
Dispose()0%2100%
Update()0%90900%
NFTShapeHQImageHandler(...)0%2100%
OnPlayerPositionChanged(...)0%30500%
FetchHQTexture()0%20400%
RestorePreviewTexture()0%6200%
RestorePreviewTextureIfInHQ()0%6200%

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        {
 931            if (config.asset == null || !config.collider || !config.transform)
 32            {
 933                return null;
 34            }
 35            /**
 36            * We dont have HQ images now.
 37            * OpenSEA returns always the best image, there are no thumbnails images.
 38            * BUT we keep this function, so when we add a transform image worker so we can resize them,
 39            * we can use this mechanism again.
 40            */
 41            return null; // new NFTShapeHQImageHandler(config);
 42        }
 43
 44        public void Dispose()
 45        {
 046            CommonScriptableObjects.playerUnityPosition.OnChange -= OnPlayerPositionChanged;
 047            asset.Dispose();
 048        }
 49
 50        public void Update()
 51        {
 052            if (!hqImageConfig.collider)
 053                return;
 54
 055            if (!isPlayerNear)
 056                return;
 57
 058            var config = DataStore.i.Get<DataStore_NFTShape>();
 59
 060            isCameraInFront = camera == null ||
 61                              Vector3.Dot(nftControllerT.forward,
 62                                  nftControllerT.position - camera.transform.position)
 63                              > config.hqImgInFrontDotProdMinValue;
 64
 065            if (VERBOSE)
 66            {
 067                Debug.Log($"Camera is in front of {hqImageConfig.name}? {isCameraInFront}");
 68            }
 69
 070            if (!isCameraInFront)
 71            {
 072                RestorePreviewTextureIfInHQ();
 073                return;
 74            }
 75
 076            isPlayerLooking = camera == null ||
 77                              Vector3.Dot(nftControllerT.forward, camera.transform.forward) >=
 78                              config.hqImgFacingDotProdMinValue;
 79
 080            if (VERBOSE)
 81            {
 082                Debug.Log($"Player is looking at {hqImageConfig.name}? {isPlayerLooking}");
 83            }
 84
 085            if (isPlayerLooking)
 86            {
 087                FetchHQTexture();
 88            }
 89            else
 90            {
 091                RestorePreviewTextureIfInHQ();
 92            }
 093        }
 94
 095        private NFTShapeHQImageHandler(NFTShapeHQImageConfig hqImageConfig)
 96        {
 097            this.hqImageConfig = hqImageConfig;
 098            this.asset = hqImageConfig.asset;
 99
 0100            camera = Camera.main;
 0101            nftControllerT = hqImageConfig.transform;
 102
 0103            CommonScriptableObjects.playerUnityPosition.OnChange += OnPlayerPositionChanged;
 0104            OnPlayerPositionChanged(CommonScriptableObjects.playerUnityPosition, Vector3.zero);
 0105        }
 106
 107        private void OnPlayerPositionChanged(Vector3 current, Vector3 prev)
 108        {
 0109            isPlayerNear = false;
 110
 0111            if (!hqImageConfig.transform || !hqImageConfig.collider)
 0112                return;
 113
 0114            var config = DataStore.i.Get<DataStore_NFTShape>();
 115
 0116            isPlayerNear = ((current - hqImageConfig.collider.ClosestPoint(current)).sqrMagnitude
 117                            <= (config.hqImgMinDistance *
 118                                config.hqImgMinDistance));
 119
 0120            if (!isPlayerNear)
 121            {
 0122                RestorePreviewTextureIfInHQ();
 123            }
 124
 0125            if (VERBOSE)
 126            {
 0127                Debug.Log($"Player position relative to {hqImageConfig.name} is near? {isPlayerNear}");
 128            }
 0129        }
 130
 131        private void FetchHQTexture()
 132        {
 0133            if (asset.isHQ)
 0134                return;
 135
 0136            Action debugSuccess = null;
 0137            Action<Exception> debugFail = null;
 138
 0139            if (VERBOSE)
 140            {
 0141                debugSuccess = () => Debug.Log($"Success: Fetch {hqImageConfig.name} HQ image");
 0142                debugFail = error => Debug.Log($"Fail: Fetch {hqImageConfig.name} HQ image, Exception: {error}");
 143            }
 144
 145            // TODO(Brian): Asset is not supposed to fetch. Move this fetching mechanism to this class or elsewhere.
 0146            asset.FetchAndSetHQAsset(hqImageConfig.imageUrl, debugSuccess, debugFail);
 147
 0148            if (VERBOSE)
 149            {
 0150                Debug.Log($"Fetch {hqImageConfig.name} HQ image");
 151            }
 0152        }
 153
 154        private void RestorePreviewTexture()
 155        {
 0156            asset.RestorePreviewAsset();
 0157            if (VERBOSE)
 158            {
 0159                Debug.Log($"Restore {hqImageConfig.name} preview image");
 160            }
 0161        }
 162
 163        private void RestorePreviewTextureIfInHQ()
 164        {
 0165            if (!asset.isHQ)
 0166                return;
 167
 0168            RestorePreviewTexture();
 0169        }
 170    }
 171}