< Summary

Class:PlayerName
Assembly:PlayerName
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PlayerName/PlayerName.cs
Covered lines:60
Uncovered lines:17
Coverable lines:77
Total lines:169
Line coverage:77.9% (60 of 77)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PlayerName()0%110100%
Awake()0%110100%
OnNamesOpacityChanged(...)0%110100%
OnNamesVisibleChanged(...)0%330100%
SetName(...)0%110100%
Update()0%2100%
Update(...)0%660100%
LookAtCamera(...)0%110100%
Show(...)0%220100%
Hide(...)0%330100%
SetForceShow(...)0%42600%
SetIsTalking(...)0%2100%
SetYOffset(...)0%2100%
ScreenSpaceRect(...)0%2100%
ScreenSpacePos(...)0%2100%
ResolveAlphaByDistance(...)0%3.583060%
UpdateVisuals(...)0%110100%
ScalePivotByDistance(...)0%110100%
GetPivotYOffsetByDistance(...)0%2.152066.67%
OnDestroy()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PlayerName/PlayerName.cs

#LineLine coverage
 1using System;
 2using DCL;
 3using TMPro;
 4using UnityEngine;
 5using UnityEngine.UI;
 6
 7[Serializable]
 8public class PlayerName : MonoBehaviour, IPlayerName
 9{
 10    internal const int DEFAULT_CANVAS_SORTING_ORDER = 0;
 11    internal const int FORCE_CANVAS_SORTING_ORDER = 40;
 112    internal static readonly int TALKING_ANIMATOR_BOOL = Animator.StringToHash("Talking");
 13    internal const float ALPHA_TRANSITION_STEP_PER_SECOND =  1f / 0.25f;
 14    internal const float TARGET_ALPHA_SHOW = 1;
 15    internal const float TARGET_ALPHA_HIDE = 0;
 16    internal const int BACKGROUND_HEIGHT = 30;
 17    internal const int BACKGROUND_EXTRA_WIDTH = 10;
 18
 19    [SerializeField] internal Canvas canvas;
 20    [SerializeField] internal CanvasGroup canvasGroup;
 21    [SerializeField] internal TextMeshProUGUI nameText;
 22    [SerializeField] internal Image background;
 23    [SerializeField] internal Transform pivot;
 24    [SerializeField] internal Animator talkingAnimator;
 25
 210926    internal BaseVariable<float> namesOpacity => DataStore.i.HUDs.avatarNamesOpacity;
 210927    internal BaseVariable<bool> namesVisible => DataStore.i.HUDs.avatarNamesVisible;
 28
 29    internal float alpha;
 30    internal float targetAlpha;
 31    internal bool forceShow = false;
 32    internal Color backgroundOriginalColor;
 33
 34    private void Awake()
 35    {
 70336        backgroundOriginalColor = background.color;
 70337        canvas.sortingOrder = DEFAULT_CANVAS_SORTING_ORDER;
 70338        namesVisible.OnChange += OnNamesVisibleChanged;
 70339        namesOpacity.OnChange += OnNamesOpacityChanged;
 70340        OnNamesVisibleChanged(namesVisible.Get(), true);
 70341        OnNamesOpacityChanged(namesOpacity.Get(), 1);
 70342        Show(true);
 70343    }
 142044    internal void OnNamesOpacityChanged(float current, float previous) { background.color = new Color(backgroundOriginal
 45
 141046    internal void OnNamesVisibleChanged(bool current, bool previous) { canvas.enabled = current || forceShow; }
 47
 48    public void SetName(string name)
 49    {
 450        name = ProfanityFilterSharedInstances.regexFilter.Filter(name);
 451        nameText.text = name;
 452        background.rectTransform.sizeDelta = new Vector2(nameText.GetPreferredValues().x + BACKGROUND_EXTRA_WIDTH, BACKG
 453    }
 54
 055    private void Update() { Update(Time.deltaTime); }
 56
 57    internal void Update(float deltaTime)
 58    {
 359        float finalTargetAlpha = forceShow ? TARGET_ALPHA_SHOW : targetAlpha;
 60
 361        if (!Mathf.Approximately(alpha, finalTargetAlpha))
 262            alpha = Mathf.MoveTowards(alpha, finalTargetAlpha, ALPHA_TRANSITION_STEP_PER_SECOND * deltaTime);
 163        else if (alpha == 0)
 64        {
 165            UpdateVisuals(0);
 66            // We are hidden and we dont have to scale, look at camera or anything, we can disable the gameObject
 167            gameObject.SetActive(false);
 168            return;
 69        }
 270        Vector3 cameraPosition = CommonScriptableObjects.cameraPosition.Get();
 271        Vector3 cameraRight = CommonScriptableObjects.cameraRight.Get();
 272        Quaternion cameraRotation = DataStore.i.camera.rotation.Get();
 73
 74        /*
 75         * TODO: We could obtain distance to player from the AvatarLODController but that coupling it's overkill and ugl
 76         * instead we should have a provider so all the subsystems can use it
 77         */
 278        float distanceToCamera = Vector3.Distance(cameraPosition, gameObject.transform.position);
 279        float resolvedAlpha = forceShow ? TARGET_ALPHA_SHOW : ResolveAlphaByDistance(alpha, distanceToCamera, forceShow)
 280        UpdateVisuals(resolvedAlpha);
 281        ScalePivotByDistance(distanceToCamera);
 282        LookAtCamera(cameraRight, cameraRotation.eulerAngles);
 283        pivot.transform.localPosition = Vector3.up * GetPivotYOffsetByDistance(distanceToCamera);
 284    }
 85
 86    internal void LookAtCamera(Vector3 cameraRight, Vector3 cameraEulerAngles)
 87    {
 388        Transform cachedTransform = transform;
 389        cachedTransform.right = -cameraRight; // This will set the Y rotation
 90
 91        // Now we use the negated X axis rotation to make the rotation steady in horizont
 392        Vector3 finalEulerAngle = cachedTransform.localEulerAngles;
 393        finalEulerAngle.x = -cameraEulerAngles.x;
 394        cachedTransform.localEulerAngles = finalEulerAngle;
 395    }
 96
 97    public void Show(bool instant = false)
 98    {
 70399        targetAlpha = TARGET_ALPHA_SHOW;
 703100        gameObject.SetActive(true);
 703101        if (instant)
 703102            alpha = TARGET_ALPHA_SHOW;
 703103    }
 104
 105    public void Hide(bool instant = false)
 106    {
 3107        targetAlpha = TARGET_ALPHA_HIDE;
 3108        if (instant && !forceShow)
 109        {
 3110            alpha = TARGET_ALPHA_HIDE;
 3111            gameObject.SetActive(false);
 112        }
 3113    }
 114    public void SetForceShow(bool forceShow)
 115    {
 0116        canvas.enabled = forceShow || namesVisible.Get();
 0117        canvas.sortingOrder = forceShow ? FORCE_CANVAS_SORTING_ORDER : DEFAULT_CANVAS_SORTING_ORDER;
 0118        background.color = new Color(backgroundOriginalColor.r, backgroundOriginalColor.g, backgroundOriginalColor.b, fo
 0119        this.forceShow = forceShow;
 0120        if (this.forceShow)
 0121            gameObject.SetActive(true);
 0122    }
 123
 0124    public void SetIsTalking(bool talking) { talkingAnimator.SetBool(TALKING_ANIMATOR_BOOL, talking); }
 125
 0126    public void SetYOffset(float yOffset) { transform.localPosition = Vector3.up * yOffset; }
 127
 128    public Rect ScreenSpaceRect(Camera mainCamera)
 129    {
 0130        Vector3 origin = mainCamera.WorldToScreenPoint(background.transform.position);
 0131        Vector2 size = background.rectTransform.sizeDelta;
 0132        return new Rect(origin.x, Screen.height - origin.y, size.x, size.y);
 133    }
 134
 0135    public Vector3 ScreenSpacePos(Camera mainCamera) { return mainCamera.WorldToScreenPoint(background.transform.positio
 136
 137    internal static float ResolveAlphaByDistance(float alphaValue, float distanceToCamera, bool forceShow)
 138    {
 2139        if (forceShow)
 0140            return alphaValue;
 141
 142        const float MIN_DISTANCE = 5;
 2143        if (distanceToCamera < MIN_DISTANCE)
 2144            return alphaValue;
 145
 0146        return alphaValue * Mathf.Lerp(1, 0, (distanceToCamera - MIN_DISTANCE) / (DataStore.i.avatarsLOD.LODDistance.Get
 147    }
 148
 6149    internal void UpdateVisuals(float resolvedAlpha) { canvasGroup.alpha = resolvedAlpha; }
 150
 4151    internal void ScalePivotByDistance(float distanceToCamera) { pivot.transform.localScale = Vector3.one * 0.15f * dist
 152
 153    internal float GetPivotYOffsetByDistance(float distanceToCamera)
 154    {
 155        const float NEAR_Y_OFFSET = 0f;
 156        const float FAR_Y_OFFSET = 0.1f;
 157        const float MAX_DISTANCE = 5;
 2158        if (distanceToCamera >= MAX_DISTANCE)
 0159            return FAR_Y_OFFSET;
 160
 2161        return Mathf.Lerp(NEAR_Y_OFFSET, FAR_Y_OFFSET, distanceToCamera / MAX_DISTANCE);
 162    }
 163
 164    private void OnDestroy()
 165    {
 703166        namesVisible.OnChange -= OnNamesVisibleChanged;
 703167        namesOpacity.OnChange -= OnNamesOpacityChanged;
 703168    }
 169}