| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.ECSComponents; |
| | 6 | | using DCL.ECSRuntime; |
| | 7 | | using DCL.Models; |
| | 8 | | using TMPro; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | public class ECSTextShapeComponentHandler : IECSComponentHandler<PBTextShape> |
| | 12 | | { |
| 1 | 13 | | private static readonly int underlayColor = Shader.PropertyToID("_UnderlayColor"); |
| 1 | 14 | | private static readonly int offsetX = Shader.PropertyToID("_UnderlayOffsetX"); |
| 1 | 15 | | private static readonly int offsetY = Shader.PropertyToID("_UnderlayOffsetY"); |
| 1 | 16 | | private static readonly int underlaySoftness = Shader.PropertyToID("_UnderlaySoftness"); |
| | 17 | |
|
| | 18 | | private const string BOTTOM = "bottom"; |
| | 19 | | private const string TOP = "top"; |
| | 20 | | private const string LEFT = "left"; |
| | 21 | | private const string RIGHT = "right"; |
| | 22 | |
|
| | 23 | | private const string COMPONENT_NAME = "TextShape"; |
| | 24 | |
|
| | 25 | | internal GameObject textGameObject; |
| | 26 | | internal TextMeshPro textComponent; |
| | 27 | | internal RectTransform rectTransform; |
| | 28 | | internal AssetPromise_Font promise; |
| | 29 | |
|
| | 30 | | private PBTextShape currentModel; |
| | 31 | | private readonly DataStore_ECS7 dataStore; |
| | 32 | | private readonly AssetPromiseKeeper_Font fontPromiseKeeper; |
| | 33 | |
|
| | 34 | | private string lastFontUsed; |
| | 35 | |
|
| 30 | 36 | | public ECSTextShapeComponentHandler(DataStore_ECS7 dataStoreEcs7, AssetPromiseKeeper_Font fontPromiseKeeper) |
| | 37 | | { |
| 30 | 38 | | dataStore = dataStoreEcs7; |
| 30 | 39 | | this.fontPromiseKeeper = fontPromiseKeeper; |
| 30 | 40 | | } |
| | 41 | |
|
| | 42 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) |
| | 43 | | { |
| 30 | 44 | | textGameObject = new GameObject(COMPONENT_NAME); |
| 30 | 45 | | textGameObject.AddComponent<MeshRenderer>(); |
| 30 | 46 | | rectTransform = textGameObject.AddComponent<RectTransform>(); |
| 30 | 47 | | textComponent = textGameObject.AddComponent<TextMeshPro>(); |
| 30 | 48 | | textGameObject.transform.SetParent(entity.gameObject.transform,false); |
| 30 | 49 | | dataStore.AddShapeReady(entity.entityId,textGameObject); |
| 30 | 50 | | textComponent.text = string.Empty; |
| | 51 | |
|
| 30 | 52 | | if (entity.meshRootGameObject == null) |
| 30 | 53 | | entity.meshesInfo.meshRootGameObject = textGameObject; |
| 30 | 54 | | } |
| | 55 | |
|
| | 56 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 57 | | { |
| 32 | 58 | | RemoveModelFromPending(scene); |
| 32 | 59 | | dataStore.RemoveShapeReady(entity.entityId); |
| 32 | 60 | | if (promise != null) |
| 32 | 61 | | fontPromiseKeeper.Forget(promise); |
| 32 | 62 | | GameObject.Destroy(textGameObject); |
| | 63 | |
|
| 32 | 64 | | textGameObject = null; |
| 32 | 65 | | textComponent = null; |
| 32 | 66 | | rectTransform = null; |
| 32 | 67 | | currentModel = null; |
| 32 | 68 | | } |
| | 69 | |
|
| | 70 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBTextShape model) |
| | 71 | | { |
| 58 | 72 | | if (model.Equals(currentModel)) |
| 0 | 73 | | return; |
| | 74 | |
|
| 58 | 75 | | currentModel = model; |
| | 76 | |
|
| 58 | 77 | | PrepareRectTransform(model); |
| | 78 | |
|
| | 79 | | // If we use the same font than the last time, we just update the model, if not, we download it and apply the ch |
| 58 | 80 | | if (lastFontUsed != null && lastFontUsed == model.Font) |
| | 81 | | { |
| 21 | 82 | | ApplyModelChanges(entity, model); |
| 21 | 83 | | } |
| | 84 | | else |
| | 85 | | { |
| 37 | 86 | | lastFontUsed = model.Font; |
| 37 | 87 | | dataStore.AddPendingResource(scene.sceneData.id, model); |
| 37 | 88 | | promise = new AssetPromise_Font(model.Font); |
| 37 | 89 | | promise.OnSuccessEvent += assetFont => |
| | 90 | | { |
| 30 | 91 | | textComponent.font = assetFont.font; |
| 30 | 92 | | ApplyModelChanges(entity, model); |
| | 93 | |
|
| 30 | 94 | | RemoveModelFromPending(scene); |
| 30 | 95 | | }; |
| 37 | 96 | | promise.OnFailEvent += ( mesh, exception) => |
| | 97 | | { |
| 0 | 98 | | RemoveModelFromPending(scene); |
| 0 | 99 | | }; |
| | 100 | |
|
| 37 | 101 | | fontPromiseKeeper.Keep(promise); |
| | 102 | | } |
| 37 | 103 | | } |
| | 104 | |
|
| | 105 | | private void RemoveModelFromPending(IParcelScene scene) |
| | 106 | | { |
| 62 | 107 | | if (currentModel != null) |
| 58 | 108 | | dataStore.RemovePendingResource(scene.sceneData.id, currentModel); |
| | 109 | |
|
| 62 | 110 | | currentModel = null; |
| 62 | 111 | | } |
| | 112 | |
|
| | 113 | | private void PrepareRectTransform(PBTextShape model) |
| | 114 | | { |
| 58 | 115 | | rectTransform.anchorMin = Vector2.zero; |
| 58 | 116 | | rectTransform.anchorMax = Vector2.one; |
| 58 | 117 | | rectTransform.offsetMin = Vector2.zero; |
| 58 | 118 | | rectTransform.offsetMax = Vector2.zero; |
| 58 | 119 | | rectTransform.sizeDelta = Vector2.zero; |
| | 120 | |
|
| | 121 | | // NOTE: previously width and height weren't working (setting sizeDelta before anchors and offset result in |
| | 122 | | // sizeDelta being reset to 0,0) |
| | 123 | | // to fix textWrapping and avoid backwards compatibility issues as result of the size being properly set (like t |
| | 124 | | // we only set it if textWrapping is enabled. |
| 58 | 125 | | if (model.TextWrapping) |
| | 126 | | { |
| 4 | 127 | | rectTransform.sizeDelta = new Vector2(model.GetWidth(), model.GetHeight()); |
| 4 | 128 | | } |
| | 129 | | else |
| | 130 | | { |
| 54 | 131 | | rectTransform.sizeDelta = Vector2.zero; |
| | 132 | | } |
| 54 | 133 | | } |
| | 134 | |
|
| | 135 | | internal void ApplyModelChanges(IDCLEntity entity, PBTextShape model) |
| | 136 | | { |
| 51 | 137 | | textComponent.text = model.Text; |
| | 138 | |
|
| 51 | 139 | | if (model.TextColor != null) |
| 2 | 140 | | textComponent.color = new UnityEngine.Color(model.TextColor.R, model.TextColor.G, model.TextColor.B, model.O |
| | 141 | |
|
| 51 | 142 | | textComponent.fontSize = model.GetFontSize(); |
| 51 | 143 | | textComponent.richText = true; |
| 51 | 144 | | textComponent.overflowMode = TextOverflowModes.Overflow; |
| 51 | 145 | | textComponent.enableAutoSizing = model.FontAutoSize; |
| | 146 | |
|
| 51 | 147 | | textComponent.margin = |
| | 148 | | new Vector4 |
| | 149 | | ( |
| | 150 | | model.PaddingLeft, |
| | 151 | | model.PaddingTop, |
| | 152 | | model.PaddingRight, |
| | 153 | | model.PaddingBottom |
| | 154 | | ); |
| | 155 | |
|
| 51 | 156 | | textComponent.alignment = GetAlignment(model.GetVTextAlign(), model.GetHTextAlign()); |
| 51 | 157 | | textComponent.lineSpacing = model.LineSpacing; |
| | 158 | |
|
| 51 | 159 | | if (model.LineCount != 0) |
| | 160 | | { |
| 1 | 161 | | textComponent.maxVisibleLines = Mathf.Max(model.LineCount, 1); |
| 1 | 162 | | } |
| | 163 | | else |
| | 164 | | { |
| 50 | 165 | | textComponent.maxVisibleLines = int.MaxValue; |
| | 166 | | } |
| | 167 | |
|
| 51 | 168 | | textComponent.enableWordWrapping = model.TextWrapping && !textComponent.enableAutoSizing; |
| | 169 | |
|
| | 170 | | // Shadows |
| 51 | 171 | | bool underlayKeywordEnabled = false; |
| 51 | 172 | | if (!Mathf.Approximately(model.ShadowBlur,0)) |
| | 173 | | { |
| 0 | 174 | | textComponent.fontSharedMaterial.EnableKeyword("UNDERLAY_ON"); |
| 0 | 175 | | textComponent.fontSharedMaterial.SetFloat(underlaySoftness, model.ShadowBlur); |
| 0 | 176 | | underlayKeywordEnabled = true; |
| | 177 | | } |
| | 178 | |
|
| 51 | 179 | | if (model.ShadowColor != null) |
| | 180 | | { |
| 0 | 181 | | if (!underlayKeywordEnabled) |
| | 182 | | { |
| 0 | 183 | | textComponent.fontSharedMaterial.EnableKeyword("UNDERLAY_ON"); |
| 0 | 184 | | underlayKeywordEnabled = true; |
| | 185 | | } |
| 0 | 186 | | var shadowColor = new UnityEngine.Color(model.ShadowColor.R, model.ShadowColor.G, model.ShadowColor.B, model |
| 0 | 187 | | textComponent.fontSharedMaterial.SetColor(underlayColor, shadowColor); |
| 0 | 188 | | textComponent.fontSharedMaterial.SetFloat(offsetX, model.ShadowOffsetX); |
| 0 | 189 | | textComponent.fontSharedMaterial.SetFloat(offsetY, model.ShadowOffsetY); |
| | 190 | | } |
| | 191 | |
|
| 51 | 192 | | if (!underlayKeywordEnabled && textComponent.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON")) |
| | 193 | | { |
| 0 | 194 | | textComponent.fontSharedMaterial.DisableKeyword("UNDERLAY_ON"); |
| | 195 | | } |
| | 196 | |
|
| | 197 | | // Outline |
| 51 | 198 | | if (model.OutlineWidth > 0f) |
| | 199 | | { |
| 1 | 200 | | textComponent.fontSharedMaterial.EnableKeyword("OUTLINE_ON"); |
| 1 | 201 | | textComponent.outlineWidth = model.OutlineWidth; |
| 1 | 202 | | if (model.OutlineColor != null) |
| | 203 | | { |
| 1 | 204 | | var outlineColor = new UnityEngine.Color(model.OutlineColor.R, model.OutlineColor.G, model.OutlineColor |
| 1 | 205 | | textComponent.outlineColor = outlineColor; |
| | 206 | | } |
| 1 | 207 | | } |
| 50 | 208 | | else if (textComponent.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON")) |
| | 209 | | { |
| 1 | 210 | | textComponent.fontSharedMaterial.DisableKeyword("OUTLINE_ON"); |
| | 211 | | } |
| | 212 | |
|
| 51 | 213 | | textGameObject.SetActive(model.GetVisible()); |
| 51 | 214 | | entity.OnShapeUpdated?.Invoke(entity); |
| 51 | 215 | | } |
| | 216 | |
|
| | 217 | | internal TextAlignmentOptions GetAlignment(string vTextAlign, string hTextAlign) |
| | 218 | | { |
| 55 | 219 | | vTextAlign = vTextAlign.ToLower(); |
| 55 | 220 | | hTextAlign = hTextAlign.ToLower(); |
| | 221 | |
|
| | 222 | | switch (vTextAlign) |
| | 223 | | { |
| | 224 | | case TOP: |
| | 225 | | switch (hTextAlign) |
| | 226 | | { |
| | 227 | | case LEFT: |
| 2 | 228 | | return TextAlignmentOptions.TopLeft; |
| | 229 | | case RIGHT: |
| 2 | 230 | | return TextAlignmentOptions.TopRight; |
| | 231 | | default: |
| 0 | 232 | | return TextAlignmentOptions.Top; |
| | 233 | | } |
| | 234 | |
|
| | 235 | | case BOTTOM: |
| | 236 | | switch (hTextAlign) |
| | 237 | | { |
| | 238 | | case LEFT: |
| 2 | 239 | | return TextAlignmentOptions.BottomLeft; |
| | 240 | | case RIGHT: |
| 2 | 241 | | return TextAlignmentOptions.BottomRight; |
| | 242 | | default: |
| 0 | 243 | | return TextAlignmentOptions.Bottom; |
| | 244 | | } |
| | 245 | |
|
| | 246 | | default: // center |
| | 247 | | switch (hTextAlign) |
| | 248 | | { |
| | 249 | | case LEFT: |
| 0 | 250 | | return TextAlignmentOptions.Left; |
| | 251 | | case RIGHT: |
| 0 | 252 | | return TextAlignmentOptions.Right; |
| | 253 | | default: |
| 47 | 254 | | return TextAlignmentOptions.Center; |
| | 255 | | } |
| | 256 | | } |
| | 257 | | } |
| | 258 | | } |