< Summary

Class:NFTShape_Internal.NFTShapeHQImageHandler
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/NFTShape/NFTShapeHQImageHandler.cs
Covered lines:46
Uncovered lines:14
Coverable lines:60
Total lines:161
Line coverage:76.6% (46 of 60)
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.899077.78%
NFTShapeHQImageHandler(...)0%110100%
OnPlayerPositionChanged(...)0%5.035088.89%
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 UnityEngine;
 3
 4namespace NFTShape_Internal
 5{
 6    public class NFTShapeHQImageConfig
 7    {
 8        public string name;
 9        public string imageUrl;
 10        public NFTShapeConfig nftShapeConfig;
 11        public NFTShapeLoaderController 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        {
 430            if (config.asset == null || config.controller == null)
 31            {
 032                return null;
 33            }
 34
 435            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        {
 646            if (hqImageConfig.controller.collider is null)
 047                return;
 48
 649            if (!isPlayerNear)
 050                return;
 51
 652            isCameraInFront = camera == null ||
 53                              Vector3.Dot(nftControllerT.forward,
 54                                  nftControllerT.position - camera.transform.position)
 55                              > hqImageConfig.nftShapeConfig.hqImgInFrontDotProdMinValue;
 56
 657            if (VERBOSE)
 58            {
 059                Debug.Log($"Camera is in front of {hqImageConfig.name}? {isCameraInFront}");
 60            }
 61
 662            if (!isCameraInFront)
 63            {
 164                RestorePreviewTextureIfInHQ();
 165                return;
 66            }
 67
 568            isPlayerLooking = camera == null ||
 69                              Vector3.Dot(nftControllerT.forward, camera.transform.forward) >=
 70                              hqImageConfig.nftShapeConfig.hqImgFacingDotProdMinValue;
 71
 572            if (VERBOSE)
 73            {
 074                Debug.Log($"Player is looking at {hqImageConfig.name}? {isPlayerLooking}");
 75            }
 76
 577            if (isPlayerLooking)
 78            {
 479                FetchHQTexture();
 480            }
 81            else
 82            {
 183                RestorePreviewTextureIfInHQ();
 84            }
 185        }
 86
 487        private NFTShapeHQImageHandler(NFTShapeHQImageConfig hqImageConfig)
 88        {
 489            this.hqImageConfig = hqImageConfig;
 490            this.asset = hqImageConfig.asset;
 91
 492            camera = Camera.main;
 493            nftControllerT = hqImageConfig.controller.transform;
 94
 495            CommonScriptableObjects.playerUnityPosition.OnChange += OnPlayerPositionChanged;
 496            OnPlayerPositionChanged(CommonScriptableObjects.playerUnityPosition, Vector3.zero);
 497        }
 98
 99        private void OnPlayerPositionChanged(Vector3 current, Vector3 prev)
 100        {
 9693101            isPlayerNear = false;
 102
 9693103            if (hqImageConfig.controller == null || hqImageConfig.controller.collider == null)
 9684104                return;
 105
 9106            isPlayerNear = ((current - hqImageConfig.controller.collider.ClosestPoint(current)).sqrMagnitude
 107                            <= (hqImageConfig.nftShapeConfig.hqImgMinDistance *
 108                                hqImageConfig.nftShapeConfig.hqImgMinDistance));
 109
 9110            if (!isPlayerNear)
 111            {
 5112                RestorePreviewTextureIfInHQ();
 113            }
 114
 9115            if (VERBOSE)
 116            {
 0117                Debug.Log($"Player position relative to {hqImageConfig.name} is near? {isPlayerNear}");
 118            }
 9119        }
 120
 121        private void FetchHQTexture()
 122        {
 4123            if (asset.isHQ)
 0124                return;
 125
 4126            Action debugSuccess = null;
 4127            Action<Exception> debugFail = null;
 128
 4129            if (VERBOSE)
 130            {
 0131                debugSuccess = () => Debug.Log($"Success: Fetch {hqImageConfig.name} HQ image");
 0132                debugFail = error => Debug.Log($"Fail: Fetch {hqImageConfig.name} HQ image, Exception: {error}");
 133            }
 134
 135            // TODO(Brian): Asset is not supposed to fetch. Move this fetching mechanism to this class or elsewhere.
 4136            asset.FetchAndSetHQAsset(hqImageConfig.imageUrl, debugSuccess, debugFail);
 137
 4138            if (VERBOSE)
 139            {
 0140                Debug.Log($"Fetch {hqImageConfig.name} HQ image");
 141            }
 4142        }
 143
 144        private void RestorePreviewTexture()
 145        {
 3146            asset.RestorePreviewAsset();
 3147            if (VERBOSE)
 148            {
 0149                Debug.Log($"Restore {hqImageConfig.name} preview image");
 150            }
 3151        }
 152
 153        private void RestorePreviewTextureIfInHQ()
 154        {
 7155            if (!asset.isHQ)
 4156                return;
 157
 3158            RestorePreviewTexture();
 3159        }
 160    }
 161}