< Summary

Class:DCL.NavmapToastView
Assembly:Navmap
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NavMap/NavmapToastView.cs
Covered lines:47
Uncovered lines:39
Coverable lines:86
Total lines:192
Line coverage:54.6% (47 of 86)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
NavmapToastView()0%110100%
Open(...)0%12300%
Awake()0%110100%
Start()0%2100%
OnDestroy()0%2.092071.43%
Populate(...)0%24.817070%
OnMapMetadataInfoUpdated(...)0%30500%
PositionToast(...)0%10.0810090.91%
Close()0%220100%
OnGotoClick()0%2100%
DisplayThumbnail(...)0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/NavMap/NavmapToastView.cs

#LineLine coverage
 1using DCL.Interface;
 2using TMPro;
 3using UnityEngine;
 4using UnityEngine.UI;
 5
 6namespace DCL
 7{
 8    public class NavmapToastView : MonoBehaviour
 9    {
 110        private static readonly int triggerLoadingComplete = Animator.StringToHash("LoadingComplete");
 11
 12        [SerializeField] internal TextMeshProUGUI sceneTitleText;
 13        [SerializeField] internal TextMeshProUGUI sceneOwnerText;
 14        [SerializeField] internal TextMeshProUGUI sceneLocationText;
 15        [SerializeField] internal RectTransform toastContainer;
 16        [SerializeField] internal GameObject scenePreviewContainer;
 17        [SerializeField] internal RawImageFillParent scenePreviewImage;
 18        [SerializeField] internal Sprite scenePreviewFailImage;
 19        [SerializeField] internal Animator toastAnimator;
 20
 21        [SerializeField] internal Button goToButton;
 22
 23        Vector2Int location;
 24        RectTransform rectTransform;
 25        MinimapMetadata minimapMetadata;
 26
 27        AssetPromise_Texture texturePromise;
 28        string currentImageUrl;
 29
 30        private void Open(int cursorTileX, int cursorTileY)
 31        {
 032            if (gameObject.activeInHierarchy)
 033                Close();
 34
 035            var sceneInfo = minimapMetadata.GetSceneInfo(cursorTileX, cursorTileY);
 036            if (sceneInfo == null)
 037                WebInterface.RequestScenesInfoAroundParcel(new Vector2(cursorTileX, cursorTileY), 15);
 38
 039            Populate(new Vector2Int(cursorTileX, cursorTileY), sceneInfo);
 040        }
 41
 42        private void Awake()
 43        {
 244            minimapMetadata = MinimapMetadata.GetMetadata();
 245            rectTransform = transform as RectTransform;
 246        }
 47
 48        private void Start()
 49        {
 050            gameObject.SetActive(false);
 51
 052            goToButton.onClick.AddListener(OnGotoClick);
 53
 054            minimapMetadata.OnSceneInfoUpdated += OnMapMetadataInfoUpdated;
 055            MapRenderer.OnParcelClicked += Open;
 056            MapRenderer.OnCursorFarFromParcel += Close;
 057        }
 58
 59        private void OnDestroy()
 60        {
 261            minimapMetadata.OnSceneInfoUpdated -= OnMapMetadataInfoUpdated;
 262            MapRenderer.OnParcelClicked -= Open;
 263            MapRenderer.OnCursorFarFromParcel -= Close;
 64
 265            if (texturePromise != null)
 66            {
 067                AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 068                texturePromise = null;
 69            }
 270        }
 71
 72        public void Populate(Vector2Int coordinates, MinimapMetadata.MinimapSceneInfo sceneInfo)
 73        {
 174            if (!gameObject.activeSelf)
 075                AudioScriptableObjects.dialogOpen.Play(true);
 76
 177            bool sceneInfoExists = sceneInfo != null;
 78
 179            gameObject.SetActive(true);
 180            scenePreviewImage.gameObject.SetActive(false);
 181            location = coordinates;
 82
 183            PositionToast(coordinates);
 84
 185            sceneLocationText.text = $"{coordinates.x}, {coordinates.y}";
 86
 187            sceneOwnerText.transform.parent.gameObject.SetActive(sceneInfoExists && !string.IsNullOrEmpty(sceneInfo.owne
 188            sceneTitleText.text = "Untitled Scene";
 89
 190            bool useDefaultThumbnail =
 91                !sceneInfoExists || (sceneInfoExists && string.IsNullOrEmpty(sceneInfo.previewImageUrl));
 92
 193            if (useDefaultThumbnail)
 94            {
 195                DisplayThumbnail(scenePreviewFailImage.texture);
 196                currentImageUrl = "";
 97            }
 98
 199            if (sceneInfoExists)
 100            {
 1101                sceneTitleText.text = sceneInfo.name;
 1102                sceneOwnerText.text = $"Created by: {sceneInfo.owner}";
 103
 1104                if (currentImageUrl == sceneInfo.previewImageUrl)
 105                {
 0106                    DisplayThumbnail(texturePromise?.asset?.texture);
 0107                    return;
 108                }
 109
 1110                if (texturePromise != null)
 111                {
 0112                    AssetPromiseKeeper_Texture.i.Forget(texturePromise);
 0113                    texturePromise = null;
 114                }
 115
 116
 1117                if (!string.IsNullOrEmpty(sceneInfo.previewImageUrl))
 118                {
 0119                    texturePromise = new AssetPromise_Texture(sceneInfo.previewImageUrl, storeTexAsNonReadable: false);
 0120                    texturePromise.OnSuccessEvent += (textureAsset) => { DisplayThumbnail(textureAsset.texture); };
 0121                    texturePromise.OnFailEvent += (textureAsset, error) => { DisplayThumbnail(scenePreviewFailImage.text
 0122                    AssetPromiseKeeper_Texture.i.Keep(texturePromise);
 123                }
 124
 1125                currentImageUrl = sceneInfoExists ? sceneInfo.previewImageUrl : "";
 126            }
 1127        }
 128
 129        public void OnMapMetadataInfoUpdated(MinimapMetadata.MinimapSceneInfo sceneInfo)
 130        {
 0131            if (!gameObject.activeInHierarchy)
 0132                return;
 133
 0134            bool updatedCurrentLocationInfo = false;
 0135            foreach (Vector2Int parcel in sceneInfo.parcels)
 136            {
 0137                if (parcel == location)
 138                {
 0139                    updatedCurrentLocationInfo = true;
 0140                    break;
 141                }
 142            }
 143
 0144            if (updatedCurrentLocationInfo)
 0145                Populate(location, sceneInfo);
 0146        }
 147
 148        void PositionToast(Vector2Int coordinates)
 149        {
 1150            if (toastContainer == null || rectTransform == null)
 0151                return;
 152
 153            // position the toast over the parcel parcelHighlightImage so that we can easily check with LOCAL pos info w
 1154            toastContainer.position = MapRenderer.i.GetParcelHighlightTransform();
 155
 1156            bool useBottom = toastContainer.localPosition.y > 0;
 157
 1158            bool shouldOffsetHorizontally = Mathf.Abs(toastContainer.localPosition.x) > rectTransform.rect.width / 4;
 1159            bool useLeft = false;
 160
 1161            if (shouldOffsetHorizontally)
 1162                useLeft = toastContainer.localPosition.x > 0;
 163
 164            // By setting the pivot accordingly BEFORE we position the toast, we can have it always visible in an easier
 1165            toastContainer.pivot = new Vector2(shouldOffsetHorizontally ? (useLeft ? 1 : 0) : 0.5f, useBottom ? 1 : 0);
 1166            toastContainer.position = MapRenderer.i.GetParcelHighlightTransform();
 167
 1168        }
 169
 170        public void Close()
 171        {
 1172            if (gameObject.activeSelf)
 1173                AudioScriptableObjects.dialogClose.Play(true);
 174
 1175            gameObject.SetActive(false);
 1176        }
 177
 178        private void OnGotoClick()
 179        {
 0180            DataStore.i.HUDs.navmapVisible.Set(false);
 0181            Environment.i.world.teleportController.Teleport(location.x, location.y);
 182
 0183            Close();
 0184        }
 185
 186        private void DisplayThumbnail(Texture2D texture)
 187        {
 1188            scenePreviewImage.texture = texture;
 1189            toastAnimator.SetTrigger(triggerLoadingComplete);
 1190        }
 191    }
 192}