< Summary

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

Metrics

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

File(s)

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

#LineLine coverage
 1using System;
 2using DCL.Helpers.NFT;
 3using UnityEngine;
 4
 5namespace NFTShape_Internal
 6{
 7    public class NFTShapeHQImageConfig
 8    {
 9        public NFTInfo nftInfo;
 10        public NFTShapeConfig nftConfig;
 11        public NFTShapeLoaderController controller;
 12        public INFTAsset asset;
 13    }
 14
 15    public class NFTShapeHQImageHandler : IDisposable
 16    {
 17        readonly NFTShapeHQImageConfig config;
 18        readonly INFTAsset asset;
 19        readonly Camera camera;
 20        readonly Transform nftControllerT;
 21
 22        bool isPlayerNear;
 23        bool isCameraInFront;
 24        bool isPlayerLooking;
 25
 26        public static NFTShapeHQImageHandler Create(NFTShapeHQImageConfig config)
 27        {
 1328            if (config.asset == null || config.controller == null)
 29            {
 230                return null;
 31            }
 32
 1133            return new NFTShapeHQImageHandler(config);
 34        }
 35
 36        public void Dispose()
 37        {
 738            CommonScriptableObjects.playerUnityPosition.OnChange -= OnPlayerPositionChanged;
 739            asset.Dispose();
 740        }
 41
 42        public void Update()
 43        {
 2544            if (config.controller.collider is null)
 045                return;
 46
 2547            if (!isPlayerNear)
 1848                return;
 49
 750            isCameraInFront = camera == null ||
 51                              Vector3.Dot(nftControllerT.forward,
 52                                  nftControllerT.position - camera.transform.position)
 53                              > config.nftConfig.hqImgInFrontDotProdMinValue;
 54
 755            if (config.nftConfig.verbose)
 56            {
 057                Debug.Log($"Camera is in front of {config.nftInfo.name}? {isCameraInFront}");
 58            }
 59
 760            if (!isCameraInFront)
 61            {
 162                RestorePreviewTextureIfInHQ();
 163                return;
 64            }
 65
 666            isPlayerLooking = camera == null ||
 67                              Vector3.Dot(nftControllerT.forward, camera.transform.forward) >=
 68                              config.nftConfig.hqImgFacingDotProdMinValue;
 69
 670            if (config.nftConfig.verbose)
 71            {
 072                Debug.Log($"Player is looking at {config.nftInfo.name}? {isPlayerLooking}");
 73            }
 74
 675            if (isPlayerLooking)
 76            {
 577                FetchHQTexture();
 578            }
 79            else
 80            {
 181                RestorePreviewTextureIfInHQ();
 82            }
 183        }
 84
 1185        private NFTShapeHQImageHandler(NFTShapeHQImageConfig config)
 86        {
 1187            this.config = config;
 1188            this.asset = config.asset;
 89
 1190            camera = Camera.main;
 1191            nftControllerT = config.controller.transform;
 92
 1193            CommonScriptableObjects.playerUnityPosition.OnChange += OnPlayerPositionChanged;
 1194            OnPlayerPositionChanged(CommonScriptableObjects.playerUnityPosition, Vector3.zero);
 1195        }
 96
 97        private void OnPlayerPositionChanged(Vector3 current, Vector3 prev)
 98        {
 563299            isPlayerNear = false;
 100
 5632101            if (config.controller == null || config.controller.collider == null)
 5612102                return;
 103
 20104            isPlayerNear = ((current - config.controller.collider.ClosestPoint(current)).sqrMagnitude
 105                            <= (config.nftConfig.hqImgMinDistance * config.nftConfig.hqImgMinDistance));
 106
 20107            if (!isPlayerNear)
 108            {
 15109                RestorePreviewTextureIfInHQ();
 110            }
 111
 20112            if (config.nftConfig.verbose)
 113            {
 0114                Debug.Log($"Player position relative to {config.nftInfo.name} is near? {isPlayerNear}");
 115            }
 20116        }
 117
 118        private void FetchHQTexture()
 119        {
 5120            if (asset.isHQ)
 0121                return;
 122
 5123            string url = $"{config.nftInfo.imageUrl}=s{asset.hqResolution}";
 124
 5125            Action debugSuccess = null;
 5126            Action debugFail = null;
 127
 5128            if (config.nftConfig.verbose)
 129            {
 0130                debugSuccess = () => Debug.Log($"Success: Fetch {config.nftInfo.name} HQ image");
 0131                debugFail = () => Debug.Log($"Fail: Fetch {config.nftInfo.name} HQ image");
 132            }
 133
 5134            asset.FetchAndSetHQAsset(url, debugSuccess, debugFail);
 135
 5136            if (config.nftConfig.verbose)
 137            {
 0138                Debug.Log($"Fetch {config.nftInfo.name} HQ image");
 139            }
 5140        }
 141
 142        private void RestorePreviewTexture()
 143        {
 4144            asset.RestorePreviewAsset();
 4145            if (config.nftConfig.verbose)
 146            {
 0147                Debug.Log($"Restore {config.nftInfo.name} preview image");
 148            }
 4149        }
 150
 151        private void RestorePreviewTextureIfInHQ()
 152        {
 17153            if (!asset.isHQ)
 13154                return;
 155
 4156            RestorePreviewTexture();
 4157        }
 158    }
 159}