< Summary

Class:InteractionHoverCanvasController
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/UUIDComponent/InteractionHoverCanvas/InteractionHoverCanvasController.cs
Covered lines:19
Uncovered lines:26
Coverable lines:45
Total lines:117
Line coverage:42.2% (19 of 45)
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%8.36060%
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{
 010    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    {
 12328        i = this;
 12329        mainCamera = Camera.main;
 12330    }
 31
 32    public void Setup(string button, string feedbackText, IDCLEntity entity)
 33    {
 3534        text.text = feedbackText;
 3535        this.entity = entity;
 36
 3537        ConfigureIcon(button);
 38
 3539        canvas.enabled = enabled && isHovered;
 3540    }
 41
 42    void ConfigureIcon(string button)
 43    {
 3544        hoverIcon?.SetActive(false);
 45
 46        switch (button)
 47        {
 48            case ACTION_BUTTON_POINTER:
 049                hoverIcon = icons[0];
 050                break;
 51            case ACTION_BUTTON_PRIMARY:
 252                hoverIcon = icons[1];
 253                break;
 54            case ACTION_BUTTON_SECONDARY:
 055                hoverIcon = icons[2];
 056                break;
 57            default: // ANY
 3358                hoverIcon = icons[3];
 59                break;
 60        }
 61
 3562        hoverIcon.SetActive(true);
 3563    }
 64
 65    public void SetHoverState(bool hoverState)
 66    {
 1343267        if (!enabled || hoverState == isHovered)
 1340668            return;
 69
 2670        isHovered = hoverState;
 71
 2672        canvas.enabled = isHovered;
 2673    }
 74
 075    public GameObject GetCurrentHoverIcon() { return hoverIcon; }
 76
 77    // This method will be used when we apply a "loose aim" for the 3rd person camera
 78    void CalculateMeshCenteredPos(DCLTransform.Model transformModel = null)
 79    {
 080        if (!canvas.enabled)
 081            return;
 82
 083        if (entity.meshesInfo.renderers == null || entity.meshesInfo.renderers.Length == 0)
 84        {
 085            meshCenteredPos = transform.parent.position;
 086        }
 87        else
 88        {
 089            Vector3 sum = Vector3.zero;
 090            for (int i = 0; i < entity.meshesInfo.renderers.Length; i++)
 91            {
 092                sum += entity.meshesInfo.renderers[i].bounds.center;
 93            }
 94
 095            meshCenteredPos = sum / entity.meshesInfo.renderers.Length;
 96        }
 097    }
 98
 99    // This method will be used when we apply a "loose aim" for the 3rd person camera
 100    public void UpdateSizeAndPos()
 101    {
 0102        if (mainCamera == null)
 0103            mainCamera = Camera.main;
 104
 0105        Vector3 screenPoint = mainCamera.WorldToViewportPoint(meshCenteredPos);
 106
 0107        if (screenPoint.z > 0)
 108        {
 0109            RectTransform canvasRect = (RectTransform) canvas.transform;
 0110            float width = canvasRect.rect.width;
 0111            float height = canvasRect.rect.height;
 0112            screenPoint.Scale(new Vector3(width, height, 0));
 113
 0114            ((RectTransform) backgroundTransform).anchoredPosition = screenPoint;
 115        }
 0116    }
 117}