| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | [Serializable] |
| | 10 | | public class PlayerName : MonoBehaviour, IPlayerName |
| | 11 | | { |
| | 12 | | internal const int DEFAULT_CANVAS_SORTING_ORDER = 0; |
| | 13 | | internal const int FORCE_CANVAS_SORTING_ORDER = 40; |
| 1 | 14 | | internal static readonly int TALKING_ANIMATOR_BOOL = Animator.StringToHash("Talking"); |
| | 15 | | internal const float ALPHA_TRANSITION_STEP_PER_SECOND = 1f / 0.25f; |
| | 16 | | internal const float TARGET_ALPHA_SHOW = 1; |
| | 17 | | internal const float TARGET_ALPHA_HIDE = 0; |
| | 18 | | internal const int BACKGROUND_HEIGHT = 30; |
| | 19 | | internal const int BACKGROUND_EXTRA_WIDTH = 10; |
| | 20 | |
|
| | 21 | | [SerializeField] internal Canvas canvas; |
| | 22 | | [SerializeField] internal CanvasGroup canvasGroup; |
| | 23 | | [SerializeField] internal TextMeshProUGUI nameText; |
| | 24 | | [SerializeField] internal Image background; |
| | 25 | | [SerializeField] internal Transform pivot; |
| | 26 | | [SerializeField] internal Animator talkingAnimator; |
| | 27 | |
|
| | 28 | | [SerializeField] internal List<CanvasRenderer> canvasRenderers; |
| | 29 | |
|
| 1338 | 30 | | internal BaseVariable<float> namesOpacity => DataStore.i.HUDs.avatarNamesOpacity; |
| 1338 | 31 | | internal BaseVariable<bool> namesVisible => DataStore.i.HUDs.avatarNamesVisible; |
| | 32 | |
|
| | 33 | | internal float alpha; |
| | 34 | | internal float targetAlpha; |
| | 35 | | internal bool forceShow = false; |
| | 36 | | internal Color backgroundOriginalColor; |
| 452 | 37 | | internal List<string> hideConstraints = new List<string>(); |
| | 38 | |
|
| | 39 | | private void Awake() |
| | 40 | | { |
| 446 | 41 | | backgroundOriginalColor = background.color; |
| 446 | 42 | | canvas.sortingOrder = DEFAULT_CANVAS_SORTING_ORDER; |
| 446 | 43 | | namesVisible.OnChange += OnNamesVisibleChanged; |
| 446 | 44 | | namesOpacity.OnChange += OnNamesOpacityChanged; |
| 446 | 45 | | OnNamesVisibleChanged(namesVisible.Get(), true); |
| 446 | 46 | | OnNamesOpacityChanged(namesOpacity.Get(), 1); |
| 446 | 47 | | Show(true); |
| 446 | 48 | | } |
| 904 | 49 | | internal void OnNamesOpacityChanged(float current, float previous) { background.color = new Color(backgroundOriginal |
| | 50 | |
|
| 896 | 51 | | internal void OnNamesVisibleChanged(bool current, bool previous) { canvas.enabled = current || forceShow; } |
| | 52 | |
|
| 26 | 53 | | public void SetName(string name) { AsyncSetName(name).Forget(); } |
| | 54 | | private async UniTaskVoid AsyncSetName(string name) |
| | 55 | | { |
| 13 | 56 | | name = await ProfanityFilterSharedInstances.regexFilter.Filter(name); |
| 13 | 57 | | nameText.text = name; |
| 13 | 58 | | background.rectTransform.sizeDelta = new Vector2(nameText.GetPreferredValues().x + BACKGROUND_EXTRA_WIDTH, BACKG |
| 13 | 59 | | } |
| | 60 | |
|
| 94 | 61 | | private void Update() { Update(Time.deltaTime); } |
| | 62 | |
|
| | 63 | | private void SetRenderersVisible(bool value) |
| | 64 | | { |
| 3381 | 65 | | canvasRenderers.ForEach(c => c.SetAlpha(value ? 1f : 0f)); |
| 483 | 66 | | } |
| | 67 | |
|
| | 68 | | internal void Update(float deltaTime) |
| | 69 | | { |
| 50 | 70 | | if (hideConstraints.Count > 0) |
| | 71 | | { |
| 0 | 72 | | UpdateVisuals(0); |
| 0 | 73 | | return; |
| | 74 | | } |
| | 75 | |
|
| 50 | 76 | | float finalTargetAlpha = forceShow ? TARGET_ALPHA_SHOW : targetAlpha; |
| | 77 | |
|
| 50 | 78 | | if (!Mathf.Approximately(alpha, finalTargetAlpha)) |
| 20 | 79 | | alpha = Mathf.MoveTowards(alpha, finalTargetAlpha, ALPHA_TRANSITION_STEP_PER_SECOND * deltaTime); |
| 30 | 80 | | else if (alpha == 0) |
| | 81 | | { |
| 2 | 82 | | UpdateVisuals(0); |
| | 83 | | // We are hidden and we dont have to scale, look at camera or anything, we can disable the gameObject |
| 2 | 84 | | SetRenderersVisible(false); |
| 2 | 85 | | return; |
| | 86 | | } |
| 48 | 87 | | Vector3 cameraPosition = CommonScriptableObjects.cameraPosition.Get(); |
| 48 | 88 | | Vector3 cameraRight = CommonScriptableObjects.cameraRight.Get(); |
| 48 | 89 | | Quaternion cameraRotation = DataStore.i.camera.rotation.Get(); |
| | 90 | |
|
| | 91 | | /* |
| | 92 | | * TODO: We could obtain distance to player from the AvatarLODController but that coupling it's overkill and ugl |
| | 93 | | * instead we should have a provider so all the subsystems can use it |
| | 94 | | */ |
| 48 | 95 | | float distanceToCamera = Vector3.Distance(cameraPosition, gameObject.transform.position); |
| 48 | 96 | | float resolvedAlpha = forceShow ? TARGET_ALPHA_SHOW : ResolveAlphaByDistance(alpha, distanceToCamera, forceShow) |
| 48 | 97 | | UpdateVisuals(resolvedAlpha); |
| 48 | 98 | | ScalePivotByDistance(distanceToCamera); |
| 48 | 99 | | LookAtCamera(cameraRight, cameraRotation.eulerAngles); |
| 48 | 100 | | pivot.transform.localPosition = Vector3.up * GetPivotYOffsetByDistance(distanceToCamera); |
| 48 | 101 | | } |
| | 102 | |
|
| | 103 | | internal void LookAtCamera(Vector3 cameraRight, Vector3 cameraEulerAngles) |
| | 104 | | { |
| 49 | 105 | | Transform cachedTransform = transform; |
| 49 | 106 | | cachedTransform.right = -cameraRight; // This will set the Y rotation |
| | 107 | |
|
| | 108 | | // Now we use the negated X axis rotation to make the rotation steady in horizont |
| 49 | 109 | | Vector3 finalEulerAngle = cachedTransform.localEulerAngles; |
| 49 | 110 | | finalEulerAngle.x = -cameraEulerAngles.x; |
| 49 | 111 | | cachedTransform.localEulerAngles = finalEulerAngle; |
| 49 | 112 | | } |
| | 113 | |
|
| | 114 | | public void Show(bool instant = false) |
| | 115 | | { |
| 466 | 116 | | targetAlpha = TARGET_ALPHA_SHOW; |
| 466 | 117 | | SetRenderersVisible(true); |
| 466 | 118 | | if (instant) |
| 450 | 119 | | alpha = TARGET_ALPHA_SHOW; |
| 466 | 120 | | } |
| | 121 | |
|
| | 122 | | public void Hide(bool instant = false) |
| | 123 | | { |
| 30 | 124 | | targetAlpha = TARGET_ALPHA_HIDE; |
| 30 | 125 | | if (instant && !forceShow) |
| | 126 | | { |
| 15 | 127 | | alpha = TARGET_ALPHA_HIDE; |
| 15 | 128 | | SetRenderersVisible(false); |
| | 129 | | } |
| 30 | 130 | | } |
| | 131 | |
|
| | 132 | | // Note: Ideally we should separate the LODController logic from this one so we don't have to add constraints |
| 0 | 133 | | public void AddVisibilityConstaint(string constraint) { hideConstraints.Add(constraint); } |
| | 134 | |
|
| 0 | 135 | | public void RemoveVisibilityConstaint(string constraint) { hideConstraints.Remove(constraint); } |
| | 136 | |
|
| | 137 | | public void SetForceShow(bool forceShow) |
| | 138 | | { |
| 0 | 139 | | canvas.enabled = forceShow || namesVisible.Get(); |
| 0 | 140 | | canvas.sortingOrder = forceShow ? FORCE_CANVAS_SORTING_ORDER : DEFAULT_CANVAS_SORTING_ORDER; |
| 0 | 141 | | background.color = new Color(backgroundOriginalColor.r, backgroundOriginalColor.g, backgroundOriginalColor.b, fo |
| 0 | 142 | | this.forceShow = forceShow; |
| 0 | 143 | | if (this.forceShow) |
| 0 | 144 | | SetRenderersVisible(true); |
| 0 | 145 | | } |
| | 146 | |
|
| 10 | 147 | | public void SetIsTalking(bool talking) { talkingAnimator.SetBool(TALKING_ANIMATOR_BOOL, talking); } |
| | 148 | |
|
| 10 | 149 | | public void SetYOffset(float yOffset) { transform.localPosition = Vector3.up * yOffset; } |
| | 150 | |
|
| | 151 | | public Rect ScreenSpaceRect(Camera mainCamera) |
| | 152 | | { |
| 0 | 153 | | Vector3 origin = mainCamera.WorldToScreenPoint(background.transform.position); |
| 0 | 154 | | Vector2 size = background.rectTransform.sizeDelta; |
| 0 | 155 | | return new Rect(origin.x, Screen.height - origin.y, size.x, size.y); |
| | 156 | | } |
| | 157 | |
|
| 24 | 158 | | public Vector3 ScreenSpacePos(Camera mainCamera) { return mainCamera.WorldToScreenPoint(background.transform.positio |
| | 159 | |
|
| | 160 | | internal static float ResolveAlphaByDistance(float alphaValue, float distanceToCamera, bool forceShow) |
| | 161 | | { |
| 48 | 162 | | if (forceShow) |
| 0 | 163 | | return alphaValue; |
| | 164 | |
|
| | 165 | | const float MIN_DISTANCE = 5; |
| 48 | 166 | | if (distanceToCamera < MIN_DISTANCE) |
| 48 | 167 | | return alphaValue; |
| | 168 | |
|
| 0 | 169 | | return alphaValue * Mathf.Lerp(1, 0, (distanceToCamera - MIN_DISTANCE) / (DataStore.i.avatarsLOD.LODDistance.Get |
| | 170 | | } |
| | 171 | |
|
| | 172 | | internal void UpdateVisuals(float resolvedAlpha) |
| | 173 | | { |
| 50 | 174 | | canvasGroup.alpha = resolvedAlpha; |
| 50 | 175 | | } |
| | 176 | |
|
| 96 | 177 | | internal void ScalePivotByDistance(float distanceToCamera) { pivot.transform.localScale = Vector3.one * 0.15f * dist |
| | 178 | |
|
| | 179 | | internal float GetPivotYOffsetByDistance(float distanceToCamera) |
| | 180 | | { |
| | 181 | | const float NEAR_Y_OFFSET = 0f; |
| | 182 | | const float FAR_Y_OFFSET = 0.1f; |
| | 183 | | const float MAX_DISTANCE = 5; |
| 48 | 184 | | if (distanceToCamera >= MAX_DISTANCE) |
| 0 | 185 | | return FAR_Y_OFFSET; |
| | 186 | |
|
| 48 | 187 | | return Mathf.Lerp(NEAR_Y_OFFSET, FAR_Y_OFFSET, distanceToCamera / MAX_DISTANCE); |
| | 188 | | } |
| | 189 | |
|
| | 190 | | private void OnDestroy() |
| | 191 | | { |
| 446 | 192 | | namesVisible.OnChange -= OnNamesVisibleChanged; |
| 446 | 193 | | namesOpacity.OnChange -= OnNamesOpacityChanged; |
| 446 | 194 | | } |
| | 195 | | } |