< 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:47
Uncovered lines:14
Coverable lines:61
Total lines:157
Line coverage:77% (47 of 61)
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%7.547077.78%
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        {
 428            if (config.asset == null || config.controller == null)
 29            {
 030                return null;
 31            }
 32
 433            return new NFTShapeHQImageHandler(config);
 34        }
 35
 36        public void Dispose()
 37        {
 038            CommonScriptableObjects.playerUnityPosition.OnChange -= OnPlayerPositionChanged;
 039            asset.Dispose();
 040        }
 41
 42        public void Update()
 43        {
 644            if (config.controller.collider is null)
 045                return;
 46
 647            if (!isPlayerNear)
 048                return;
 49
 650            isCameraInFront = Vector3.Dot(nftControllerT.forward,
 51                                  nftControllerT.position - camera.transform.position)
 52                              > config.nftConfig.hqImgInFrontDotProdMinValue;
 53
 654            if (config.nftConfig.verbose)
 55            {
 056                Debug.Log($"Camera is in front of {config.nftInfo.name}? {isCameraInFront}");
 57            }
 58
 659            if (!isCameraInFront)
 60            {
 161                RestorePreviewTextureIfInHQ();
 162                return;
 63            }
 64
 565            isPlayerLooking = Vector3.Dot(nftControllerT.forward, camera.transform.forward) >=
 66                              config.nftConfig.hqImgFacingDotProdMinValue;
 67
 568            if (config.nftConfig.verbose)
 69            {
 070                Debug.Log($"Player is looking at {config.nftInfo.name}? {isPlayerLooking}");
 71            }
 72
 573            if (isPlayerLooking)
 74            {
 475                FetchHQTexture();
 476            }
 77            else
 78            {
 179                RestorePreviewTextureIfInHQ();
 80            }
 181        }
 82
 483        private NFTShapeHQImageHandler(NFTShapeHQImageConfig config)
 84        {
 485            this.config = config;
 486            this.asset = config.asset;
 87
 488            camera = Camera.main;
 489            nftControllerT = config.controller.transform;
 90
 491            CommonScriptableObjects.playerUnityPosition.OnChange += OnPlayerPositionChanged;
 492            OnPlayerPositionChanged(CommonScriptableObjects.playerUnityPosition, Vector3.zero);
 493        }
 94
 95        private void OnPlayerPositionChanged(Vector3 current, Vector3 prev)
 96        {
 572997            isPlayerNear = false;
 98
 572999            if (config.controller == null || config.controller.collider == null)
 5716100                return;
 101
 13102            isPlayerNear = ((current - config.controller.collider.ClosestPoint(current)).sqrMagnitude
 103                            <= (config.nftConfig.hqImgMinDistance * config.nftConfig.hqImgMinDistance));
 104
 13105            if (!isPlayerNear)
 106            {
 9107                RestorePreviewTextureIfInHQ();
 108            }
 109
 13110            if (config.nftConfig.verbose)
 111            {
 0112                Debug.Log($"Player position relative to {config.nftInfo.name} is near? {isPlayerNear}");
 113            }
 13114        }
 115
 116        private void FetchHQTexture()
 117        {
 4118            if (asset.isHQ)
 0119                return;
 120
 4121            string url = $"{config.nftInfo.imageUrl}=s{asset.hqResolution}";
 122
 4123            Action debugSuccess = null;
 4124            Action debugFail = null;
 125
 4126            if (config.nftConfig.verbose)
 127            {
 0128                debugSuccess = () => Debug.Log($"Success: Fetch {config.nftInfo.name} HQ image");
 0129                debugFail = () => Debug.Log($"Fail: Fetch {config.nftInfo.name} HQ image");
 130            }
 131
 4132            asset.FetchAndSetHQAsset(url, debugSuccess, debugFail);
 133
 4134            if (config.nftConfig.verbose)
 135            {
 0136                Debug.Log($"Fetch {config.nftInfo.name} HQ image");
 137            }
 4138        }
 139
 140        private void RestorePreviewTexture()
 141        {
 4142            asset.RestorePreviewAsset();
 4143            if (config.nftConfig.verbose)
 144            {
 0145                Debug.Log($"Restore {config.nftInfo.name} preview image");
 146            }
 4147        }
 148
 149        private void RestorePreviewTextureIfInHQ()
 150        {
 11151            if (!asset.isHQ)
 7152                return;
 153
 4154            RestorePreviewTexture();
 4155        }
 156    }
 157}