| | 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 MINIMUM_ALPHA_TO_SHOW = 1f / 32f; |
| | 16 | | internal const float ALPHA_TRANSITION_STEP_PER_SECOND = 1f / 0.25f; |
| | 17 | | internal const float ALPHA_STEPS = 32f; |
| | 18 | | internal const float TARGET_ALPHA_SHOW = 1; |
| | 19 | | internal const float TARGET_ALPHA_HIDE = 0; |
| | 20 | | internal const int BACKGROUND_HEIGHT = 30; |
| | 21 | | internal const int BACKGROUND_EXTRA_WIDTH = 10; |
| | 22 | |
|
| | 23 | | [SerializeField] internal Canvas canvas; |
| | 24 | | [SerializeField] internal CanvasGroup canvasGroup; |
| | 25 | | [SerializeField] internal TextMeshProUGUI nameText; |
| | 26 | | [SerializeField] internal Image background; |
| | 27 | | [SerializeField] internal Transform pivot; |
| | 28 | | [SerializeField] internal Animator talkingAnimator; |
| | 29 | |
|
| | 30 | | [SerializeField] internal List<CanvasRenderer> canvasRenderers; |
| | 31 | |
|
| 1302 | 32 | | internal BaseVariable<float> namesOpacity => DataStore.i.HUDs.avatarNamesOpacity; |
| 1302 | 33 | | internal BaseVariable<bool> namesVisible => DataStore.i.HUDs.avatarNamesVisible; |
| | 34 | | internal BaseVariable<bool> profanityFilterEnabled; |
| | 35 | |
|
| | 36 | | internal bool forceShow = false; |
| | 37 | | internal Color backgroundOriginalColor; |
| 440 | 38 | | internal List<string> hideConstraints = new List<string>(); |
| 440 | 39 | | internal string currentName = ""; |
| | 40 | |
|
| | 41 | | private float alpha; |
| | 42 | | private float targetAlpha; |
| | 43 | | private bool renderersVisible; |
| | 44 | |
|
| | 45 | |
|
| | 46 | | internal float Alpha |
| | 47 | | { |
| 0 | 48 | | get => alpha; |
| | 49 | | set |
| | 50 | | { |
| 3 | 51 | | alpha = Mathf.Clamp01(value); |
| 3 | 52 | | if (alpha < 0.01f) |
| | 53 | | { |
| 2 | 54 | | UpdateVisuals(0); |
| 2 | 55 | | SetRenderersVisible(false); |
| 2 | 56 | | return; |
| | 57 | | } |
| | 58 | |
|
| 1 | 59 | | UpdateVisuals(alpha); |
| 1 | 60 | | SetRenderersVisible(true); |
| 1 | 61 | | } |
| | 62 | | } |
| | 63 | |
|
| | 64 | | internal float TargetAlpha |
| | 65 | | { |
| 0 | 66 | | get => targetAlpha; |
| 3 | 67 | | set => targetAlpha = Mathf.Clamp01(value); |
| | 68 | | } |
| | 69 | |
|
| | 70 | |
|
| | 71 | | private void Awake() |
| | 72 | | { |
| 434 | 73 | | backgroundOriginalColor = background.color; |
| 434 | 74 | | canvas.sortingOrder = DEFAULT_CANVAS_SORTING_ORDER; |
| 434 | 75 | | profanityFilterEnabled = DataStore.i.settings.profanityChatFilteringEnabled; |
| 434 | 76 | | namesVisible.OnChange += OnNamesVisibleChanged; |
| 434 | 77 | | namesOpacity.OnChange += OnNamesOpacityChanged; |
| 434 | 78 | | profanityFilterEnabled.OnChange += OnProfanityFilterChanged; |
| | 79 | |
|
| 434 | 80 | | OnNamesVisibleChanged(namesVisible.Get(), true); |
| 434 | 81 | | OnNamesOpacityChanged(namesOpacity.Get(), 1); |
| 434 | 82 | | Show(true); |
| 434 | 83 | | } |
| 880 | 84 | | internal void OnNamesOpacityChanged(float current, float previous) { background.color = new Color(backgroundOriginal |
| | 85 | |
|
| 872 | 86 | | internal void OnNamesVisibleChanged(bool current, bool previous) { canvas.enabled = current || forceShow; } |
| 4 | 87 | | private void OnProfanityFilterChanged(bool current, bool previous) { SetName(currentName); } |
| 30 | 88 | | public void SetName(string name) { AsyncSetName(name).Forget(); } |
| | 89 | | private async UniTaskVoid AsyncSetName(string name) |
| | 90 | | { |
| 15 | 91 | | currentName = name; |
| 15 | 92 | | name = await FilterName(currentName); |
| 15 | 93 | | nameText.text = name; |
| 15 | 94 | | background.rectTransform.sizeDelta = new Vector2(nameText.GetPreferredValues().x + BACKGROUND_EXTRA_WIDTH, BACKG |
| 15 | 95 | | } |
| | 96 | |
|
| | 97 | | private async UniTask<string> FilterName(string name) |
| | 98 | | { |
| 15 | 99 | | return IsProfanityFilteringEnabled() |
| | 100 | | ? await ProfanityFilterSharedInstances.regexFilter.Filter(name) |
| | 101 | | : name; |
| 15 | 102 | | } |
| | 103 | |
|
| | 104 | | private bool IsProfanityFilteringEnabled() |
| | 105 | | { |
| 15 | 106 | | return DataStore.i.settings.profanityChatFilteringEnabled.Get(); |
| | 107 | | } |
| | 108 | |
|
| 94 | 109 | | private void Update() { Update(Time.deltaTime); } |
| | 110 | |
|
| | 111 | | private void SetRenderersVisible(bool value) |
| | 112 | | { |
| 518 | 113 | | if (renderersVisible == value) |
| 65 | 114 | | return; |
| | 115 | |
|
| 3171 | 116 | | canvasRenderers.ForEach(c => c.SetAlpha(value ? 1f : 0f)); |
| 453 | 117 | | renderersVisible = value; |
| 453 | 118 | | } |
| | 119 | |
|
| | 120 | | internal void Update(float deltaTime) |
| | 121 | | { |
| 50 | 122 | | if (hideConstraints.Count > 0) |
| | 123 | | { |
| 0 | 124 | | UpdateVisuals(0); |
| 0 | 125 | | return; |
| | 126 | | } |
| | 127 | |
|
| 50 | 128 | | float finalTargetAlpha = forceShow ? TARGET_ALPHA_SHOW : targetAlpha; |
| 50 | 129 | | alpha = Mathf.MoveTowards(alpha, finalTargetAlpha, ALPHA_TRANSITION_STEP_PER_SECOND * deltaTime); |
| 50 | 130 | | if (CheckAndUpdateVisibility(alpha)) |
| 2 | 131 | | return; |
| | 132 | |
|
| 48 | 133 | | Vector3 cameraPosition = CommonScriptableObjects.cameraPosition.Get(); |
| 48 | 134 | | Vector3 cameraRight = CommonScriptableObjects.cameraRight.Get(); |
| 48 | 135 | | Quaternion cameraRotation = DataStore.i.camera.rotation.Get(); |
| | 136 | |
|
| | 137 | | /* |
| | 138 | | * TODO: We could obtain distance to player from the AvatarLODController but that coupling it's overkill and ugl |
| | 139 | | * instead we should have a provider so all the subsystems can use it |
| | 140 | | */ |
| 48 | 141 | | float distanceToCamera = Vector3.Distance(cameraPosition, gameObject.transform.position); |
| 48 | 142 | | ScalePivotByDistance(distanceToCamera); |
| 48 | 143 | | LookAtCamera(cameraRight, cameraRotation.eulerAngles); |
| 48 | 144 | | pivot.transform.localPosition = Vector3.up * GetPivotYOffsetByDistance(distanceToCamera); |
| | 145 | |
|
| 48 | 146 | | float resolvedAlpha = forceShow ? TARGET_ALPHA_SHOW : ResolveAlphaByDistance(alpha, distanceToCamera, forceShow) |
| 48 | 147 | | if (CheckAndUpdateVisibility(resolvedAlpha)) |
| 0 | 148 | | return; |
| | 149 | |
|
| 48 | 150 | | SetRenderersVisible(true); |
| 48 | 151 | | float resolvedAlphaStep = GetNearestAlphaStep(resolvedAlpha); |
| 48 | 152 | | float canvasAlphaStep = GetNearestAlphaStep(canvasGroup.alpha); |
| | 153 | |
|
| 48 | 154 | | if (resolvedAlphaStep != canvasAlphaStep) |
| 21 | 155 | | UpdateVisuals(resolvedAlpha); |
| | 156 | |
|
| | 157 | |
|
| | 158 | | // Local Methods |
| | 159 | | bool CheckAndUpdateVisibility(float checkAlpha) |
| | 160 | | { |
| 98 | 161 | | if (checkAlpha < MINIMUM_ALPHA_TO_SHOW) |
| | 162 | | { |
| 2 | 163 | | if (renderersVisible) |
| | 164 | | { |
| 0 | 165 | | UpdateVisuals(0); |
| 0 | 166 | | SetRenderersVisible(false); |
| | 167 | | } |
| 2 | 168 | | return true; |
| | 169 | | } |
| | 170 | |
|
| 96 | 171 | | return false; |
| | 172 | | } |
| 48 | 173 | | } |
| | 174 | |
|
| | 175 | | internal void LookAtCamera(Vector3 cameraRight, Vector3 cameraEulerAngles) |
| | 176 | | { |
| 49 | 177 | | Transform cachedTransform = transform; |
| 49 | 178 | | cachedTransform.right = -cameraRight; // This will set the Y rotation |
| | 179 | |
|
| | 180 | | // Now we use the negated X axis rotation to make the rotation steady in horizont |
| 49 | 181 | | Vector3 finalEulerAngle = cachedTransform.localEulerAngles; |
| 49 | 182 | | finalEulerAngle.x = -cameraEulerAngles.x; |
| 49 | 183 | | cachedTransform.localEulerAngles = finalEulerAngle; |
| 49 | 184 | | } |
| | 185 | |
|
| | 186 | | public void Show(bool instant = false) |
| | 187 | | { |
| 452 | 188 | | targetAlpha = TARGET_ALPHA_SHOW; |
| 452 | 189 | | SetRenderersVisible(true); |
| 452 | 190 | | if (instant) |
| 438 | 191 | | alpha = TARGET_ALPHA_SHOW; |
| 452 | 192 | | } |
| | 193 | |
|
| | 194 | | public void Hide(bool instant = false) |
| | 195 | | { |
| 32 | 196 | | targetAlpha = TARGET_ALPHA_HIDE; |
| 32 | 197 | | if (instant && !forceShow) |
| | 198 | | { |
| 15 | 199 | | alpha = TARGET_ALPHA_HIDE; |
| 15 | 200 | | SetRenderersVisible(false); |
| | 201 | | } |
| 32 | 202 | | } |
| | 203 | |
|
| | 204 | | // Note: Ideally we should separate the LODController logic from this one so we don't have to add constraints |
| 0 | 205 | | public void AddVisibilityConstaint(string constraint) { hideConstraints.Add(constraint); } |
| | 206 | |
|
| 0 | 207 | | public void RemoveVisibilityConstaint(string constraint) { hideConstraints.Remove(constraint); } |
| | 208 | |
|
| | 209 | | public void SetForceShow(bool forceShow) |
| | 210 | | { |
| 0 | 211 | | canvas.enabled = forceShow || namesVisible.Get(); |
| 0 | 212 | | canvas.sortingOrder = forceShow ? FORCE_CANVAS_SORTING_ORDER : DEFAULT_CANVAS_SORTING_ORDER; |
| 0 | 213 | | background.color = new Color(backgroundOriginalColor.r, backgroundOriginalColor.g, backgroundOriginalColor.b, fo |
| 0 | 214 | | this.forceShow = forceShow; |
| 0 | 215 | | if (this.forceShow) |
| 0 | 216 | | SetRenderersVisible(true); |
| 0 | 217 | | } |
| | 218 | |
|
| 10 | 219 | | public void SetIsTalking(bool talking) { talkingAnimator.SetBool(TALKING_ANIMATOR_BOOL, talking); } |
| | 220 | |
|
| 10 | 221 | | public void SetYOffset(float yOffset) { transform.localPosition = Vector3.up * yOffset; } |
| | 222 | |
|
| | 223 | | public Rect ScreenSpaceRect(Camera mainCamera) |
| | 224 | | { |
| 0 | 225 | | Vector3 origin = mainCamera.WorldToScreenPoint(background.transform.position); |
| 0 | 226 | | Vector2 size = background.rectTransform.sizeDelta; |
| 0 | 227 | | return new Rect(origin.x, Screen.height - origin.y, size.x, size.y); |
| | 228 | | } |
| | 229 | |
|
| 20 | 230 | | public Vector3 ScreenSpacePos(Camera mainCamera) { return mainCamera.WorldToScreenPoint(background.transform.positio |
| | 231 | |
|
| | 232 | | internal static float ResolveAlphaByDistance(float alphaValue, float distanceToCamera, bool forceShow) |
| | 233 | | { |
| 48 | 234 | | if (forceShow) |
| 0 | 235 | | return alphaValue; |
| | 236 | |
|
| | 237 | | const float MIN_DISTANCE = 5; |
| 48 | 238 | | if (distanceToCamera < MIN_DISTANCE) |
| 48 | 239 | | return alphaValue; |
| | 240 | |
|
| 0 | 241 | | return alphaValue * Mathf.Lerp(1, 0, (distanceToCamera - MIN_DISTANCE) / (DataStore.i.avatarsLOD.LODDistance.Get |
| | 242 | | } |
| | 243 | |
|
| | 244 | | internal void UpdateVisuals(float resolvedAlpha) |
| | 245 | | { |
| 24 | 246 | | canvasGroup.alpha = resolvedAlpha; |
| 24 | 247 | | } |
| | 248 | |
|
| | 249 | | internal float GetNearestAlphaStep(float alpha) |
| | 250 | | { |
| 96 | 251 | | return Mathf.Floor(alpha * ALPHA_STEPS); |
| | 252 | | } |
| | 253 | |
|
| 96 | 254 | | internal void ScalePivotByDistance(float distanceToCamera) { pivot.transform.localScale = Vector3.one * 0.15f * dist |
| | 255 | |
|
| | 256 | | internal float GetPivotYOffsetByDistance(float distanceToCamera) |
| | 257 | | { |
| | 258 | | const float NEAR_Y_OFFSET = 0f; |
| | 259 | | const float FAR_Y_OFFSET = 0.1f; |
| | 260 | | const float MAX_DISTANCE = 5; |
| 48 | 261 | | if (distanceToCamera >= MAX_DISTANCE) |
| 0 | 262 | | return FAR_Y_OFFSET; |
| | 263 | |
|
| 48 | 264 | | return Mathf.Lerp(NEAR_Y_OFFSET, FAR_Y_OFFSET, distanceToCamera / MAX_DISTANCE); |
| | 265 | | } |
| | 266 | |
|
| | 267 | | private void OnDestroy() |
| | 268 | | { |
| 434 | 269 | | namesVisible.OnChange -= OnNamesVisibleChanged; |
| 434 | 270 | | namesOpacity.OnChange -= OnNamesOpacityChanged; |
| 434 | 271 | | profanityFilterEnabled.OnChange -= OnProfanityFilterChanged; |
| 434 | 272 | | } |
| | 273 | | } |