< Summary

Class:InteractionHoverCanvasController
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UUIDComponent/InteractionHoverCanvas/InteractionHoverCanvasController.cs
Covered lines:22
Uncovered lines:25
Coverable lines:47
Total lines:119
Line coverage:46.8% (22 of 47)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
Setup(...)0%330100%
ConfigureIcon(...)0%7.736063.64%
SetHoverState(...)0%330100%
GetCurrentHoverIcon()0%2100%
CalculateMeshCenteredPos(...)0%30500%
UpdateSizeAndPos()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UUIDComponent/InteractionHoverCanvas/InteractionHoverCanvasController.cs

#LineLine coverage
 1using DCL.Models;
 2using DCL.Components;
 3using UnityEngine;
 4using TMPro;
 5using System.Collections.Generic;
 6using DCL;
 7
 8public class InteractionHoverCanvasController : MonoBehaviour
 9{
 66710    public static InteractionHoverCanvasController i { get; private set; }
 11    public Canvas canvas;
 12    public RectTransform backgroundTransform;
 13    public TextMeshProUGUI text;
 14    public GameObject[] icons;
 15
 16    bool isHovered = false;
 17    Camera mainCamera;
 18    GameObject hoverIcon;
 19    Vector3 meshCenteredPos;
 20    IDCLEntity entity;
 21
 22    const string ACTION_BUTTON_POINTER = "POINTER";
 23    const string ACTION_BUTTON_PRIMARY = "PRIMARY";
 24    const string ACTION_BUTTON_SECONDARY = "SECONDARY";
 25
 26    void Awake()
 27    {
 8928        i = this;
 8929        mainCamera = Camera.main;
 8930        backgroundTransform.gameObject.SetActive(false);
 8931    }
 32
 33    public void Setup(string button, string feedbackText, IDCLEntity entity)
 34    {
 27835        text.text = feedbackText;
 27836        this.entity = entity;
 37
 27838        ConfigureIcon(button);
 39
 27840        canvas.enabled = enabled && isHovered;
 27841    }
 42
 43    void ConfigureIcon(string button)
 44    {
 27845        hoverIcon?.SetActive(false);
 46
 47        switch (button)
 48        {
 49            case ACTION_BUTTON_POINTER:
 050                hoverIcon = icons[0];
 051                break;
 52            case ACTION_BUTTON_PRIMARY:
 153                hoverIcon = icons[1];
 154                break;
 55            case ACTION_BUTTON_SECONDARY:
 056                hoverIcon = icons[2];
 057                break;
 58            default: // ANY
 27759                hoverIcon = icons[3];
 60                break;
 61        }
 62
 27863        hoverIcon.SetActive(true);
 27864        backgroundTransform.gameObject.SetActive(true);
 27865    }
 66
 67    public void SetHoverState(bool hoverState)
 68    {
 60369        if (!enabled || hoverState == isHovered)
 57970            return;
 71
 2472        isHovered = hoverState;
 73
 2474        canvas.enabled = isHovered;
 2475    }
 76
 077    public GameObject GetCurrentHoverIcon() { return hoverIcon; }
 78
 79    // This method will be used when we apply a "loose aim" for the 3rd person camera
 80    void CalculateMeshCenteredPos(DCLTransform.Model transformModel = null)
 81    {
 082        if (!canvas.enabled)
 083            return;
 84
 085        if (entity.meshesInfo.renderers == null || entity.meshesInfo.renderers.Length == 0)
 86        {
 087            meshCenteredPos = transform.parent.position;
 088        }
 89        else
 90        {
 091            Vector3 sum = Vector3.zero;
 092            for (int i = 0; i < entity.meshesInfo.renderers.Length; i++)
 93            {
 094                sum += entity.meshesInfo.renderers[i].bounds.center;
 95            }
 96
 097            meshCenteredPos = sum / entity.meshesInfo.renderers.Length;
 98        }
 099    }
 100
 101    // This method will be used when we apply a "loose aim" for the 3rd person camera
 102    public void UpdateSizeAndPos()
 103    {
 0104        if (mainCamera == null)
 0105            mainCamera = Camera.main;
 106
 0107        Vector3 screenPoint = mainCamera.WorldToViewportPoint(meshCenteredPos);
 108
 0109        if (screenPoint.z > 0)
 110        {
 0111            RectTransform canvasRect = (RectTransform) canvas.transform;
 0112            float width = canvasRect.rect.width;
 0113            float height = canvasRect.rect.height;
 0114            screenPoint.Scale(new Vector3(width, height, 0));
 115
 0116            ((RectTransform) backgroundTransform).anchoredPosition = screenPoint;
 117        }
 0118    }
 119}