| | 1 | | using DCL; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.ECS7.InternalComponents; |
| | 4 | | using DCL.ECSComponents; |
| | 5 | | using DCL.ECSRuntime; |
| | 6 | | using DCL.Models; |
| | 7 | | using Decentraland.Common; |
| | 8 | | using TMPro; |
| | 9 | | using UnityEngine; |
| | 10 | | using Vector2 = UnityEngine.Vector2; |
| | 11 | |
|
| | 12 | | public class ECSTextShapeComponentHandler : IECSComponentHandler<PBTextShape> |
| | 13 | | { |
| 1 | 14 | | private static readonly int UNDERLAY_COLOR_SHADER_PROP = Shader.PropertyToID("_UnderlayColor"); |
| 1 | 15 | | private static readonly int UNDERLAY_SOFTNESS_SHADER_PROP = Shader.PropertyToID("_UnderlaySoftness"); |
| | 16 | |
|
| | 17 | | private readonly AssetPromiseKeeper_Font fontPromiseKeeper; |
| | 18 | | private readonly IInternalECSComponent<InternalRenderers> renderersInternalComponent; |
| | 19 | | private readonly IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent; |
| | 20 | |
|
| | 21 | | internal TextMeshPro textComponent; |
| | 22 | | internal AssetPromise_Font fontPromise; |
| | 23 | |
|
| | 24 | | private IParcelScene scene; |
| | 25 | | private IDCLEntity entity; |
| | 26 | | private GameObject textGameObject; |
| | 27 | | private RectTransform rectTransform; |
| | 28 | | private Renderer textRenderer; |
| | 29 | | private PBTextShape currentModel; |
| | 30 | |
|
| 22 | 31 | | public ECSTextShapeComponentHandler(AssetPromiseKeeper_Font fontPromiseKeeper, IInternalECSComponent<InternalRendere |
| | 32 | | { |
| 22 | 33 | | this.fontPromiseKeeper = fontPromiseKeeper; |
| 22 | 34 | | this.renderersInternalComponent = renderersInternalComponent; |
| 22 | 35 | | this.sbcInternalComponent = sbcInternalComponent; |
| 22 | 36 | | } |
| | 37 | |
|
| | 38 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) |
| | 39 | | { |
| 22 | 40 | | this.scene = scene; |
| 22 | 41 | | this.entity = entity; |
| | 42 | |
|
| 22 | 43 | | textGameObject = new GameObject("TextShape"); |
| | 44 | |
|
| 22 | 45 | | rectTransform = textGameObject.AddComponent<RectTransform>(); |
| 22 | 46 | | textComponent = textGameObject.AddComponent<TextMeshPro>(); |
| 22 | 47 | | textRenderer = textComponent.renderer; |
| 22 | 48 | | rectTransform.SetParent(entity.gameObject.transform, false); |
| | 49 | |
|
| 22 | 50 | | textComponent.text = string.Empty; |
| 22 | 51 | | textComponent.richText = true; |
| 22 | 52 | | textComponent.overflowMode = TextOverflowModes.Overflow; |
| 22 | 53 | | textComponent.OnPreRenderText += OnTextRendererUpdated; |
| 22 | 54 | | renderersInternalComponent.AddRenderer(scene, entity, textRenderer); |
| 22 | 55 | | } |
| | 56 | |
|
| | 57 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 58 | | { |
| 24 | 59 | | textComponent.OnPreRenderText -= OnTextRendererUpdated; |
| 24 | 60 | | renderersInternalComponent.RemoveRenderer(scene, entity, textRenderer); |
| | 61 | |
|
| 24 | 62 | | fontPromiseKeeper.Forget(fontPromise); |
| | 63 | |
|
| 24 | 64 | | Object.Destroy(textGameObject); |
| 24 | 65 | | } |
| | 66 | |
|
| | 67 | | private void OnTextRendererUpdated(TMP_TextInfo tmproInfo) |
| | 68 | | { |
| 1 | 69 | | var model = sbcInternalComponent.GetFor(scene, entity)?.model; |
| 2 | 70 | | if (model == null) return; |
| | 71 | |
|
| 0 | 72 | | InternalSceneBoundsCheck finalModel = model.Value; |
| 0 | 73 | | finalModel.meshesDirty = true; |
| | 74 | |
|
| 0 | 75 | | sbcInternalComponent.PutFor(scene, entity, finalModel); |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBTextShape model) |
| | 79 | | { |
| 21 | 80 | | if (model.Equals(currentModel)) |
| 0 | 81 | | return; |
| | 82 | |
|
| 21 | 83 | | currentModel = model; |
| | 84 | |
|
| 21 | 85 | | SetRectTransform(rectTransform, model.GetTextWrapping() ? new Vector2(model.GetWidth(), model.GetHeight()) : Vec |
| | 86 | |
|
| 21 | 87 | | textComponent.text = model.Text; |
| | 88 | |
|
| 21 | 89 | | var color = model.GetTextColor().ToUnityColor(); |
| 21 | 90 | | textComponent.color = color; |
| | 91 | |
|
| 21 | 92 | | textComponent.fontSize = model.GetFontSize(); |
| 21 | 93 | | textComponent.enableAutoSizing = model.GetFontAutoSize(); |
| 21 | 94 | | textComponent.margin = model.GetPadding(); |
| 21 | 95 | | textComponent.alignment = GetAlignment(model.GetTextAlign()); |
| 21 | 96 | | textComponent.lineSpacing = model.GetLineSpacing(); |
| 21 | 97 | | textComponent.maxVisibleLines = model.GetLineCount() != 0 ? Mathf.Max(model.GetLineCount(), 1) : int.MaxValue; |
| 21 | 98 | | textComponent.textWrappingMode = model.GetTextWrapping() && !model.GetFontAutoSize() ? TextWrappingModes.Normal |
| | 99 | |
|
| 21 | 100 | | var prevPromise = fontPromise; |
| 21 | 101 | | fontPromise = new AssetPromise_Font(model.GetFont().ToFontName()); |
| | 102 | |
|
| 21 | 103 | | fontPromise.OnSuccessEvent += assetFont => |
| | 104 | | { |
| 20 | 105 | | textComponent.font = assetFont.font; |
| 20 | 106 | | SetShadow(textComponent.fontSharedMaterial, model.GetShadowOffsetX(), model.GetShadowOffsetY(), model.GetSha |
| 20 | 107 | | SetOutline(textComponent, model.GetOutlineWidth(), model.GetOutlineColor()); |
| 20 | 108 | | }; |
| | 109 | |
|
| 21 | 110 | | fontPromiseKeeper.Keep(fontPromise); |
| 21 | 111 | | fontPromiseKeeper.Forget(prevPromise); |
| 21 | 112 | | } |
| | 113 | |
|
| | 114 | | private static void SetRectTransform(RectTransform rectTransform, Vector2 sizeDelta) |
| | 115 | | { |
| 21 | 116 | | rectTransform.anchorMin = Vector2.zero; |
| 21 | 117 | | rectTransform.anchorMax = Vector2.one; |
| 21 | 118 | | rectTransform.offsetMin = Vector2.zero; |
| 21 | 119 | | rectTransform.offsetMax = Vector2.zero; |
| 21 | 120 | | rectTransform.sizeDelta = sizeDelta; |
| 21 | 121 | | } |
| | 122 | |
|
| | 123 | | private static void SetShadow(Material fontMaterial, float shadowOffsetX, float shadowOffsetY, |
| | 124 | | float shadowBlur, Color3 shadowColor) |
| | 125 | | { |
| | 126 | | const string UNDERLAY_ON_KERYWORD = "UNDERLAY_ON"; |
| | 127 | |
|
| 20 | 128 | | if (shadowOffsetX != 0 || shadowOffsetY != 0) |
| | 129 | | { |
| 0 | 130 | | fontMaterial.EnableKeyword(UNDERLAY_ON_KERYWORD); |
| 0 | 131 | | fontMaterial.SetColor(UNDERLAY_COLOR_SHADER_PROP, shadowColor.ToUnityColor()); |
| 0 | 132 | | fontMaterial.SetFloat(UNDERLAY_SOFTNESS_SHADER_PROP, shadowBlur); |
| | 133 | | } |
| 20 | 134 | | else if (fontMaterial.IsKeywordEnabled(UNDERLAY_ON_KERYWORD)) |
| | 135 | | { |
| 0 | 136 | | fontMaterial.DisableKeyword(UNDERLAY_ON_KERYWORD); |
| | 137 | | } |
| 20 | 138 | | } |
| | 139 | |
|
| | 140 | | private static void SetOutline(TextMeshPro textComponent, float outlineWidth, Color3 outlineColor) |
| | 141 | | { |
| | 142 | | const string OUTLINE_ON_KERYWORD = "OUTLINE_ON"; |
| | 143 | |
|
| 20 | 144 | | Material fontSharedMaterial = textComponent.fontSharedMaterial; |
| | 145 | |
|
| 20 | 146 | | if (outlineWidth > 0f) |
| | 147 | | { |
| 1 | 148 | | fontSharedMaterial.EnableKeyword(OUTLINE_ON_KERYWORD); |
| 1 | 149 | | textComponent.outlineWidth = outlineWidth; |
| 1 | 150 | | textComponent.outlineColor = outlineColor.ToUnityColor(); |
| | 151 | | } |
| 19 | 152 | | else if (fontSharedMaterial.IsKeywordEnabled(OUTLINE_ON_KERYWORD)) |
| | 153 | | { |
| 1 | 154 | | fontSharedMaterial.DisableKeyword(OUTLINE_ON_KERYWORD); |
| | 155 | | } |
| 19 | 156 | | } |
| | 157 | |
|
| | 158 | | private static TextAlignmentOptions GetAlignment(TextAlignMode value) |
| | 159 | | { |
| | 160 | | switch (value) |
| | 161 | | { |
| | 162 | | case TextAlignMode.TamTopLeft: |
| 1 | 163 | | return TextAlignmentOptions.TopLeft; |
| | 164 | | case TextAlignMode.TamTopRight: |
| 1 | 165 | | return TextAlignmentOptions.TopRight; |
| | 166 | | case TextAlignMode.TamTopCenter: |
| 0 | 167 | | return TextAlignmentOptions.Top; |
| | 168 | | case TextAlignMode.TamBottomLeft: |
| 1 | 169 | | return TextAlignmentOptions.BottomLeft; |
| | 170 | | case TextAlignMode.TamBottomRight: |
| 1 | 171 | | return TextAlignmentOptions.BottomRight; |
| | 172 | | case TextAlignMode.TamBottomCenter: |
| 0 | 173 | | return TextAlignmentOptions.Bottom; |
| | 174 | | case TextAlignMode.TamMiddleLeft: |
| 0 | 175 | | return TextAlignmentOptions.Left; |
| | 176 | | case TextAlignMode.TamMiddleCenter: |
| 17 | 177 | | return TextAlignmentOptions.Center; |
| | 178 | | case TextAlignMode.TamMiddleRight: |
| 0 | 179 | | return TextAlignmentOptions.Right; |
| | 180 | | default: |
| 0 | 181 | | return TextAlignmentOptions.Center; |
| | 182 | | } |
| | 183 | | } |
| | 184 | | } |