| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL; |
| | 5 | | using DCL.ProfanityFiltering; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | | using Environment = DCL.Environment; |
| | 10 | |
|
| | 11 | | [Serializable] |
| | 12 | | public class PlayerName : MonoBehaviour, IPlayerName |
| | 13 | | { |
| 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 | | [SerializeField] internal List<CanvasRenderer> canvasRenderers; |
| | 30 | | [SerializeField] internal Material nameMaterial; |
| | 31 | | [SerializeField] internal Material nameOnTopMaterial; |
| | 32 | | [SerializeField] internal Material bgOnTopMaterial; |
| | 33 | |
|
| 1002 | 34 | | internal BaseVariable<float> namesOpacity => DataStore.i.HUDs.avatarNamesOpacity; |
| 1002 | 35 | | internal BaseVariable<bool> namesVisible => DataStore.i.HUDs.avatarNamesVisible; |
| | 36 | | internal BaseVariable<bool> profanityFilterEnabled; |
| | 37 | |
|
| | 38 | | internal bool forceShow; |
| | 39 | | internal Color backgroundOriginalColor; |
| 340 | 40 | | internal List<string> hideConstraints = new (); |
| 340 | 41 | | internal string currentName = ""; |
| | 42 | |
|
| | 43 | | private float alpha; |
| | 44 | | private float targetAlpha; |
| | 45 | | private bool renderersVisible; |
| | 46 | | private bool isNameClaimed; |
| | 47 | | private bool isUserGuest; |
| | 48 | | private IProfanityFilter profanityFilter; |
| | 49 | |
|
| | 50 | | // TODO: remove this property, is only used on tests |
| | 51 | | internal float Alpha |
| | 52 | | { |
| 3 | 53 | | get => alpha; |
| | 54 | |
|
| | 55 | | set |
| | 56 | | { |
| 3 | 57 | | alpha = Mathf.Clamp01(value); |
| | 58 | |
|
| 3 | 59 | | if (alpha < 0.01f) |
| | 60 | | { |
| 2 | 61 | | UpdateVisuals(0); |
| 2 | 62 | | SetRenderersVisible(false); |
| 2 | 63 | | return; |
| | 64 | | } |
| | 65 | |
|
| 1 | 66 | | UpdateVisuals(alpha); |
| 1 | 67 | | SetRenderersVisible(true); |
| 1 | 68 | | } |
| | 69 | | } |
| | 70 | |
|
| | 71 | | // TODO: remove this property, is only used on tests |
| | 72 | | internal float TargetAlpha |
| | 73 | | { |
| 0 | 74 | | get => targetAlpha; |
| 3 | 75 | | set => targetAlpha = Mathf.Clamp01(value); |
| | 76 | | } |
| | 77 | |
|
| | 78 | | private void Awake() |
| | 79 | | { |
| 334 | 80 | | backgroundOriginalColor = background.color; |
| 334 | 81 | | profanityFilterEnabled = DataStore.i.settings.profanityChatFilteringEnabled; |
| 334 | 82 | | namesVisible.OnChange += OnNamesVisibleChanged; |
| 334 | 83 | | namesOpacity.OnChange += OnNamesOpacityChanged; |
| 334 | 84 | | profanityFilterEnabled.OnChange += OnProfanityFilterChanged; |
| | 85 | |
|
| 334 | 86 | | OnNamesVisibleChanged(namesVisible.Get(), true); |
| 334 | 87 | | OnNamesOpacityChanged(namesOpacity.Get(), 1); |
| 334 | 88 | | Show(true); |
| 334 | 89 | | } |
| | 90 | |
|
| | 91 | | private void OnDestroy() |
| | 92 | | { |
| 334 | 93 | | namesVisible.OnChange -= OnNamesVisibleChanged; |
| 334 | 94 | | namesOpacity.OnChange -= OnNamesOpacityChanged; |
| 334 | 95 | | profanityFilterEnabled.OnChange -= OnProfanityFilterChanged; |
| 334 | 96 | | } |
| | 97 | |
|
| | 98 | | public void SetName(string name, bool isClaimed, bool isGuest) => |
| 9 | 99 | | AsyncSetName(name, isClaimed, isGuest).Forget(); |
| | 100 | |
|
| | 101 | | public void Show(bool instant = false) |
| | 102 | | { |
| 344 | 103 | | targetAlpha = TARGET_ALPHA_SHOW; |
| 344 | 104 | | SetRenderersVisible(true); |
| | 105 | |
|
| 344 | 106 | | if (instant) |
| 334 | 107 | | alpha = TARGET_ALPHA_SHOW; |
| 344 | 108 | | } |
| | 109 | |
|
| | 110 | | public void Hide(bool instant = false) |
| | 111 | | { |
| 18 | 112 | | targetAlpha = TARGET_ALPHA_HIDE; |
| | 113 | |
|
| 18 | 114 | | if (instant && !forceShow) |
| | 115 | | { |
| 4 | 116 | | alpha = TARGET_ALPHA_HIDE; |
| 4 | 117 | | SetRenderersVisible(false); |
| | 118 | | } |
| 18 | 119 | | } |
| | 120 | |
|
| | 121 | | // Note: Ideally we should separate the LODController logic from this one so we don't have to add constraints |
| | 122 | | public void AddVisibilityConstaint(string constraint) |
| | 123 | | { |
| 0 | 124 | | hideConstraints.Add(constraint); |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | public void RemoveVisibilityConstaint(string constraint) |
| | 128 | | { |
| 0 | 129 | | hideConstraints.Remove(constraint); |
| 0 | 130 | | } |
| | 131 | |
|
| | 132 | | public void SetForceShow(bool forceShow) |
| | 133 | | { |
| 0 | 134 | | canvas.enabled = forceShow || namesVisible.Get(); |
| | 135 | |
|
| 0 | 136 | | background.color = new Color(backgroundOriginalColor.r, backgroundOriginalColor.g, backgroundOriginalColor.b, fo |
| 0 | 137 | | background.material = forceShow ? bgOnTopMaterial : null; |
| 0 | 138 | | nameText.fontSharedMaterial = forceShow ? nameOnTopMaterial : nameMaterial; |
| 0 | 139 | | this.forceShow = forceShow; |
| | 140 | |
|
| 0 | 141 | | if (this.forceShow) |
| 0 | 142 | | SetRenderersVisible(true); |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | public void SetIsTalking(bool talking) |
| | 146 | | { |
| 0 | 147 | | talkingAnimator.SetBool(TALKING_ANIMATOR_BOOL, talking); |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | public void SetYOffset(float yOffset) |
| | 151 | | { |
| 0 | 152 | | transform.localPosition = Vector3.up * yOffset; |
| 0 | 153 | | } |
| | 154 | |
|
| | 155 | | public Rect ScreenSpaceRect(Camera mainCamera) |
| | 156 | | { |
| 0 | 157 | | Vector3 origin = mainCamera.WorldToScreenPoint(background.transform.position); |
| 0 | 158 | | Vector2 size = background.rectTransform.sizeDelta; |
| 0 | 159 | | return new Rect(origin.x, Screen.height - origin.y, size.x, size.y); |
| | 160 | | } |
| | 161 | |
|
| | 162 | | public Vector3 ScreenSpacePos(Camera mainCamera) |
| | 163 | | { |
| 24 | 164 | | return mainCamera.WorldToScreenPoint(background.transform.position); |
| | 165 | | } |
| | 166 | |
|
| | 167 | | internal void OnNamesOpacityChanged(float current, float previous) |
| | 168 | | { |
| 340 | 169 | | background.color = new Color(backgroundOriginalColor.r, backgroundOriginalColor.g, backgroundOriginalColor.b, cu |
| 340 | 170 | | } |
| | 171 | |
|
| | 172 | | internal void OnNamesVisibleChanged(bool current, bool previous) |
| | 173 | | { |
| 336 | 174 | | canvas.enabled = current || forceShow; |
| 336 | 175 | | } |
| | 176 | |
|
| | 177 | | private void OnProfanityFilterChanged(bool current, bool previous) |
| | 178 | | { |
| 2 | 179 | | SetName(currentName, isNameClaimed, isUserGuest); |
| 2 | 180 | | } |
| | 181 | |
|
| | 182 | | private async UniTaskVoid AsyncSetName(string name, bool isClaimed, bool isGuest) |
| | 183 | | { |
| 9 | 184 | | currentName = name; |
| 9 | 185 | | isNameClaimed = isClaimed; |
| 9 | 186 | | isUserGuest = isGuest; |
| | 187 | |
|
| 9 | 188 | | if (string.IsNullOrEmpty(name)) |
| | 189 | | { |
| 2 | 190 | | background.rectTransform.sizeDelta = Vector3.zero; |
| 2 | 191 | | nameText.text = name; |
| 2 | 192 | | return; |
| | 193 | | } |
| | 194 | |
|
| 7 | 195 | | name = await FilterName(currentName); |
| 7 | 196 | | nameText.text = GetNameWithColorCodes(isClaimed, isGuest, name); |
| 7 | 197 | | background.rectTransform.sizeDelta = new Vector2(nameText.GetPreferredValues().x + BACKGROUND_EXTRA_WIDTH, BACKG |
| 9 | 198 | | } |
| | 199 | |
|
| | 200 | | private async UniTask<string> FilterName(string name) |
| | 201 | | { |
| 7 | 202 | | profanityFilter ??= Environment.i.serviceLocator.Get<IProfanityFilter>(); |
| | 203 | |
|
| 7 | 204 | | return IsProfanityFilteringEnabled() |
| | 205 | | ? await profanityFilter.Filter(name) |
| | 206 | | : name; |
| 7 | 207 | | } |
| | 208 | |
|
| | 209 | | private bool IsProfanityFilteringEnabled() => |
| 7 | 210 | | DataStore.i.settings.profanityChatFilteringEnabled.Get(); |
| | 211 | |
|
| | 212 | | private void Update() |
| | 213 | | { |
| 35 | 214 | | Update(Time.deltaTime); |
| 35 | 215 | | } |
| | 216 | |
|
| | 217 | | private void SetRenderersVisible(bool value) |
| | 218 | | { |
| 387 | 219 | | if (renderersVisible == value) |
| 49 | 220 | | return; |
| | 221 | |
|
| 2366 | 222 | | canvasRenderers.ForEach(c => c.SetAlpha(value ? 1f : 0f)); |
| 338 | 223 | | renderersVisible = value; |
| 338 | 224 | | } |
| | 225 | |
|
| | 226 | | internal void Update(float deltaTime) |
| | 227 | | { |
| 38 | 228 | | if (hideConstraints.Count > 0 || currentName == null) |
| | 229 | | { |
| 0 | 230 | | UpdateVisuals(0); |
| 0 | 231 | | return; |
| | 232 | | } |
| | 233 | |
|
| 38 | 234 | | float finalTargetAlpha = forceShow ? TARGET_ALPHA_SHOW : targetAlpha; |
| 38 | 235 | | alpha = Mathf.MoveTowards(alpha, finalTargetAlpha, ALPHA_TRANSITION_STEP_PER_SECOND * deltaTime); |
| | 236 | |
|
| 38 | 237 | | if (CheckAndUpdateVisibility(alpha)) |
| 2 | 238 | | return; |
| | 239 | |
|
| 36 | 240 | | Vector3 cameraPosition = CommonScriptableObjects.cameraPosition.Get(); |
| 36 | 241 | | Vector3 cameraRight = CommonScriptableObjects.cameraRight.Get(); |
| 36 | 242 | | Quaternion cameraRotation = DataStore.i.camera.rotation.Get(); |
| | 243 | |
|
| | 244 | | /* |
| | 245 | | * TODO: We could obtain distance to player from the AvatarLODController but that coupling it's overkill and ugl |
| | 246 | | * instead we should have a provider so all the subsystems can use it |
| | 247 | | */ |
| 36 | 248 | | float distanceToCamera = Vector3.Distance(cameraPosition, gameObject.transform.position); |
| 36 | 249 | | ScalePivotByDistance(distanceToCamera); |
| 36 | 250 | | LookAtCamera(cameraRight, cameraRotation.eulerAngles); |
| 36 | 251 | | pivot.transform.localPosition = Vector3.up * GetPivotYOffsetByDistance(distanceToCamera); |
| | 252 | |
|
| 36 | 253 | | float resolvedAlpha = forceShow ? TARGET_ALPHA_SHOW : ResolveAlphaByDistance(alpha, distanceToCamera, forceShow) |
| | 254 | |
|
| 36 | 255 | | if (CheckAndUpdateVisibility(resolvedAlpha)) |
| 0 | 256 | | return; |
| | 257 | |
|
| 36 | 258 | | SetRenderersVisible(true); |
| 36 | 259 | | float resolvedAlphaStep = GetNearestAlphaStep(resolvedAlpha); |
| 36 | 260 | | float canvasAlphaStep = GetNearestAlphaStep(canvasGroup.alpha); |
| | 261 | |
|
| 36 | 262 | | if (Math.Abs(resolvedAlphaStep - canvasAlphaStep) > Mathf.Epsilon) |
| 7 | 263 | | UpdateVisuals(resolvedAlpha); |
| | 264 | |
|
| | 265 | | bool CheckAndUpdateVisibility(float checkAlpha) |
| | 266 | | { |
| 146 | 267 | | if (checkAlpha >= MINIMUM_ALPHA_TO_SHOW) return false; |
| 4 | 268 | | if (!renderersVisible) return true; |
| | 269 | |
|
| 0 | 270 | | UpdateVisuals(0); |
| 0 | 271 | | SetRenderersVisible(false); |
| | 272 | |
|
| 0 | 273 | | return true; |
| | 274 | | } |
| 36 | 275 | | } |
| | 276 | |
|
| | 277 | | internal void LookAtCamera(Vector3 cameraRight, Vector3 cameraEulerAngles) |
| | 278 | | { |
| 37 | 279 | | Transform cachedTransform = transform; |
| 37 | 280 | | cachedTransform.right = -cameraRight; // This will set the Y rotation |
| | 281 | |
|
| | 282 | | // Now we use the negated X axis rotation to make the rotation steady in horizont |
| 37 | 283 | | Vector3 finalEulerAngle = cachedTransform.localEulerAngles; |
| 37 | 284 | | finalEulerAngle.x = -cameraEulerAngles.x; |
| 37 | 285 | | cachedTransform.localEulerAngles = finalEulerAngle; |
| 37 | 286 | | } |
| | 287 | |
|
| | 288 | | internal static float ResolveAlphaByDistance(float alphaValue, float distanceToCamera, bool forceShow) |
| | 289 | | { |
| 36 | 290 | | if (forceShow) |
| 0 | 291 | | return alphaValue; |
| | 292 | |
|
| | 293 | | const float MIN_DISTANCE = 5; |
| | 294 | |
|
| 36 | 295 | | if (distanceToCamera < MIN_DISTANCE) |
| 36 | 296 | | return alphaValue; |
| | 297 | |
|
| 0 | 298 | | return alphaValue * Mathf.Lerp(1, 0, (distanceToCamera - MIN_DISTANCE) / (DataStore.i.avatarsLOD.LODDistance.Get |
| | 299 | | } |
| | 300 | |
|
| | 301 | | internal void UpdateVisuals(float resolvedAlpha) |
| | 302 | | { |
| 10 | 303 | | canvasGroup.alpha = resolvedAlpha; |
| 10 | 304 | | } |
| | 305 | |
|
| | 306 | | internal float GetNearestAlphaStep(float alpha) => |
| 72 | 307 | | Mathf.Floor(alpha * ALPHA_STEPS); |
| | 308 | |
|
| | 309 | | internal void ScalePivotByDistance(float distanceToCamera) |
| | 310 | | { |
| 36 | 311 | | pivot.transform.localScale = Vector3.one * 0.15f * distanceToCamera; |
| 36 | 312 | | } |
| | 313 | |
|
| | 314 | | internal float GetPivotYOffsetByDistance(float distanceToCamera) |
| | 315 | | { |
| | 316 | | const float NEAR_Y_OFFSET = 0f; |
| | 317 | | const float FAR_Y_OFFSET = 0.1f; |
| | 318 | | const float MAX_DISTANCE = 5; |
| | 319 | |
|
| 36 | 320 | | if (distanceToCamera >= MAX_DISTANCE) |
| 0 | 321 | | return FAR_Y_OFFSET; |
| | 322 | |
|
| 36 | 323 | | return Mathf.Lerp(NEAR_Y_OFFSET, FAR_Y_OFFSET, distanceToCamera / MAX_DISTANCE); |
| | 324 | | } |
| | 325 | |
|
| | 326 | | private string GetNameWithColorCodes(bool isClaimed, bool isGuest, string name) |
| | 327 | | { |
| 7 | 328 | | string text = name; |
| | 329 | |
|
| 7 | 330 | | if (isClaimed) |
| 1 | 331 | | text = $"<color=#FFFFFF>{name}</color>"; |
| | 332 | | else |
| | 333 | | { |
| 6 | 334 | | if (isGuest) |
| | 335 | | { |
| 1 | 336 | | string[] split = name.Split('#', StringSplitOptions.RemoveEmptyEntries); |
| | 337 | |
|
| 1 | 338 | | text = split.Length > 1 |
| | 339 | | ? $"<color=#A09BA8>{split[0]}</color><color=#716B7C>#{split[1]}</color>" |
| | 340 | | : $"<color=#A09BA8>{name}</color>"; |
| | 341 | | } |
| | 342 | | else |
| | 343 | | { |
| 5 | 344 | | string[] split = name.Split('#'); |
| | 345 | |
|
| 5 | 346 | | text = split.Length > 1 |
| | 347 | | ? $"<color=#CFCDD4>{split[0]}</color><color=#A09BA8>#{split[1]}</color>" |
| | 348 | | : $"<color=#CFCDD4>{name}</color>"; |
| | 349 | | } |
| | 350 | | } |
| | 351 | |
|
| 7 | 352 | | return text; |
| | 353 | | } |
| | 354 | | } |