| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using Decentraland.Sdk.Ecs6; |
| | 8 | | using MainScripts.DCL.Components; |
| | 9 | |
|
| | 10 | | namespace DCL.Components |
| | 11 | | { |
| | 12 | | public class TextShape : BaseComponent |
| | 13 | | { |
| | 14 | | [Serializable] |
| | 15 | | public class Model : BaseModel |
| | 16 | | { |
| | 17 | | public bool billboard; |
| | 18 | |
|
| | 19 | | [Header("Font Properties")] |
| 346 | 20 | | public string value = ""; |
| | 21 | |
|
| 346 | 22 | | public bool visible = true; |
| | 23 | |
|
| 346 | 24 | | public Color color = Color.white; |
| 346 | 25 | | public float opacity = 1f; |
| 346 | 26 | | public float fontSize = 100f; |
| | 27 | | public bool fontAutoSize = false; |
| | 28 | | public string font; |
| | 29 | |
|
| | 30 | | [Header("Text box properties")] |
| 346 | 31 | | public string hTextAlign = "bottom"; |
| 346 | 32 | | public string vTextAlign = "left"; |
| 346 | 33 | | public float width = 1f; |
| 346 | 34 | | public float height = 0.2f; |
| | 35 | | public float paddingTop = 0f; |
| | 36 | | public float paddingRight = 0f; |
| | 37 | | public float paddingBottom = 0f; |
| | 38 | | public float paddingLeft = 0f; |
| | 39 | | public float lineSpacing = 0f; |
| | 40 | | public int lineCount = 0; |
| | 41 | | public bool textWrapping = false; |
| | 42 | |
|
| | 43 | | [Header("Text shadow properties")] |
| | 44 | | public float shadowBlur = 0f; |
| | 45 | | public float shadowOffsetX = 0f; |
| | 46 | | public float shadowOffsetY = 0f; |
| 346 | 47 | | public Color shadowColor = new Color(1, 1, 1); |
| | 48 | |
|
| | 49 | | [Header("Text outline properties")] |
| | 50 | | public float outlineWidth = 0f; |
| | 51 | |
|
| 346 | 52 | | public Color outlineColor = Color.white; |
| | 53 | |
|
| 15 | 54 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 55 | |
|
| | 56 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 57 | | { |
| 0 | 58 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.TextShape) |
| 0 | 59 | | return Utils.SafeUnimplemented<TextShape, Model>(expected: ComponentBodyPayload.PayloadOneofCase.Tex |
| | 60 | |
|
| 0 | 61 | | var model = new Model(); |
| | 62 | |
|
| | 63 | | try { |
| 0 | 64 | | if (pbModel.TextShape.HasBillboard) model.billboard = pbModel.TextShape.Billboard; |
| 0 | 65 | | if (pbModel.TextShape.Color != null) model.color = pbModel.TextShape.Color.AsUnityColor(); |
| 0 | 66 | | if (pbModel.TextShape.HasFont) model.font = pbModel.TextShape.Font; |
| 0 | 67 | | if (pbModel.TextShape.HasHeight) model.height = pbModel.TextShape.Height; |
| 0 | 68 | | if (pbModel.TextShape.HasOpacity) model.opacity = pbModel.TextShape.Opacity; |
| 0 | 69 | | if (pbModel.TextShape.HasValue) model.value = pbModel.TextShape.Value; |
| 0 | 70 | | if (pbModel.TextShape.HasVisible) model.visible = pbModel.TextShape.Visible; |
| 0 | 71 | | if (pbModel.TextShape.HasWidth) model.width = pbModel.TextShape.Width; |
| 0 | 72 | | if (pbModel.TextShape.HasLineCount) model.lineCount = pbModel.TextShape.LineCount; |
| 0 | 73 | | if (pbModel.TextShape.OutlineColor != null) model.outlineColor = pbModel.TextShape.OutlineColor.AsUn |
| 0 | 74 | | if (pbModel.TextShape.HasOutlineWidth) model.outlineWidth = pbModel.TextShape.OutlineWidth; |
| 0 | 75 | | if (pbModel.TextShape.HasPaddingBottom ) model.paddingBottom = pbModel.TextShape.PaddingBottom; |
| 0 | 76 | | if (pbModel.TextShape.HasPaddingLeft) model.paddingLeft = pbModel.TextShape.PaddingLeft; |
| 0 | 77 | | if (pbModel.TextShape.HasPaddingRight) model.paddingRight = pbModel.TextShape.PaddingRight; |
| 0 | 78 | | if (pbModel.TextShape.HasPaddingTop) model.paddingTop = pbModel.TextShape.PaddingTop; |
| 0 | 79 | | if (pbModel.TextShape.HasShadowBlur) model.shadowBlur = pbModel.TextShape.ShadowBlur; |
| 0 | 80 | | if (pbModel.TextShape.ShadowColor != null) model.shadowColor = pbModel.TextShape.ShadowColor.AsUnity |
| 0 | 81 | | if (pbModel.TextShape.HasShadowOffsetX) model.shadowOffsetX = pbModel.TextShape.ShadowOffsetX; |
| 0 | 82 | | if (pbModel.TextShape.HasShadowOffsetY) model.shadowOffsetY = pbModel.TextShape.ShadowOffsetY; |
| 0 | 83 | | if (pbModel.TextShape.HasTextWrapping) model.textWrapping = pbModel.TextShape.TextWrapping; |
| 0 | 84 | | if (pbModel.TextShape.HasVTextAlign) model.vTextAlign = pbModel.TextShape.VTextAlign; |
| 0 | 85 | | if (pbModel.TextShape.HasHTextAlign) model.hTextAlign = pbModel.TextShape.HTextAlign; |
| 0 | 86 | | if (pbModel.TextShape.HasFontSize) model.fontAutoSize = pbModel.TextShape.FontSize == 0; |
| 0 | 87 | | if (pbModel.TextShape.HasFontSize) model.fontSize = pbModel.TextShape.FontSize; |
| | 88 | | // if (pbModel.TextShape.HasLineSpacing) model.lineSpacing = float.Parse(pbModel.TextShape.LineSpaci |
| 0 | 89 | | } |
| | 90 | | catch (Exception e) |
| | 91 | | { |
| 0 | 92 | | Debug.LogException(e); |
| 0 | 93 | | } |
| | 94 | |
|
| 0 | 95 | | return model; |
| | 96 | | } |
| | 97 | |
|
| | 98 | | } |
| | 99 | |
|
| | 100 | | public MeshRenderer meshRenderer; |
| | 101 | | public TextMeshPro text; |
| | 102 | | public RectTransform rectTransform; |
| | 103 | | private Model cachedModel; |
| | 104 | | private Material cachedFontMaterial; |
| | 105 | | private Camera mainCamera; |
| | 106 | | private bool cameraFound; |
| | 107 | |
|
| 4 | 108 | | public override string componentName => "text"; |
| | 109 | |
|
| | 110 | | private bool CameraFound |
| | 111 | | { |
| | 112 | | get |
| | 113 | | { |
| 0 | 114 | | if (!cameraFound) |
| | 115 | | { |
| 0 | 116 | | mainCamera = Camera.main; |
| 0 | 117 | | if (mainCamera != null) |
| 0 | 118 | | cameraFound = true; |
| | 119 | | } |
| | 120 | |
|
| 0 | 121 | | return cameraFound; |
| | 122 | | } |
| | 123 | | } |
| | 124 | |
|
| | 125 | | private void Awake() |
| | 126 | | { |
| 318 | 127 | | model = new Model(); |
| | 128 | |
|
| 318 | 129 | | cachedFontMaterial = new Material(text.fontSharedMaterial); |
| | 130 | |
|
| 318 | 131 | | text.fontSharedMaterial = cachedFontMaterial; |
| 318 | 132 | | text.text = string.Empty; |
| | 133 | |
|
| 318 | 134 | | Environment.i.platform.updateEventHandler.AddListener(IUpdateEventHandler.EventType.Update, LookToCamera); |
| 318 | 135 | | } |
| | 136 | |
|
| | 137 | | private void LookToCamera() |
| | 138 | | { |
| | 139 | | // Cameras are not detected while loading, so we can not load the camera on Awake or Start |
| 12088 | 140 | | if (cachedModel is { billboard: true } && CameraFound) |
| 0 | 141 | | transform.forward = mainCamera.transform.forward; |
| 12088 | 142 | | } |
| | 143 | |
|
| | 144 | | public new Model GetModel() => |
| 17 | 145 | | cachedModel; |
| | 146 | |
|
| | 147 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 148 | | { |
| 15 | 149 | | if (rectTransform == null) |
| 0 | 150 | | yield break; |
| | 151 | |
|
| 15 | 152 | | Model model = (Model)newModel; |
| 15 | 153 | | cachedModel = model; |
| 15 | 154 | | PrepareRectTransform(); |
| | 155 | |
|
| | 156 | | // We avoid using even yield break; as this instruction skips a frame and we don't want that. |
| 15 | 157 | | if (!DCLFont.IsFontLoaded(scene, model.font)) |
| | 158 | | { |
| 0 | 159 | | yield return DCLFont.WaitUntilFontIsReady(scene, model.font); |
| | 160 | | } |
| | 161 | |
|
| 15 | 162 | | DCLFont.SetFontFromComponent(scene, model.font, text); |
| | 163 | |
|
| 15 | 164 | | ApplyModelChanges(text, model); |
| | 165 | |
|
| 15 | 166 | | if (entity.meshRootGameObject == null) |
| | 167 | | { |
| 11 | 168 | | entity.meshesInfo.meshRootGameObject = gameObject; |
| 11 | 169 | | entity.meshesInfo.RootIsPoolableObject = true; |
| | 170 | | } |
| | 171 | |
|
| 15 | 172 | | entity.OnShapeUpdated?.Invoke(entity); |
| 15 | 173 | | } |
| | 174 | |
|
| | 175 | | private static void ApplyModelChanges(TMP_Text text, Model model) |
| | 176 | | { |
| 15 | 177 | | text.text = model.value; |
| | 178 | |
|
| 15 | 179 | | text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0); |
| 15 | 180 | | text.fontSize = (int)model.fontSize; |
| 15 | 181 | | text.richText = true; |
| 15 | 182 | | text.overflowMode = TextOverflowModes.Overflow; |
| 15 | 183 | | text.enableAutoSizing = model.fontAutoSize; |
| | 184 | |
|
| 15 | 185 | | text.margin = |
| | 186 | | new Vector4 |
| | 187 | | ( |
| | 188 | | (int)model.paddingLeft, |
| | 189 | | (int)model.paddingTop, |
| | 190 | | (int)model.paddingRight, |
| | 191 | | (int)model.paddingBottom |
| | 192 | | ); |
| | 193 | |
|
| 15 | 194 | | text.alignment = GetAlignment(model.vTextAlign, model.hTextAlign); |
| 15 | 195 | | text.lineSpacing = model.lineSpacing; |
| | 196 | |
|
| 15 | 197 | | if (model.lineCount != 0) |
| | 198 | | { |
| 1 | 199 | | text.maxVisibleLines = Mathf.Max(model.lineCount, 1); |
| | 200 | | } |
| | 201 | | else |
| | 202 | | { |
| 14 | 203 | | text.maxVisibleLines = int.MaxValue; |
| | 204 | | } |
| | 205 | |
|
| 15 | 206 | | text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing; |
| | 207 | |
|
| 15 | 208 | | if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0) |
| | 209 | | { |
| 0 | 210 | | text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON"); |
| 0 | 211 | | text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor); |
| 0 | 212 | | text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur); |
| | 213 | | } |
| 15 | 214 | | else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON")) |
| | 215 | | { |
| 0 | 216 | | text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON"); |
| | 217 | | } |
| | 218 | |
|
| 15 | 219 | | if (model.outlineWidth > 0f) |
| | 220 | | { |
| 0 | 221 | | text.fontSharedMaterial.EnableKeyword("OUTLINE_ON"); |
| 0 | 222 | | text.outlineWidth = model.outlineWidth; |
| 0 | 223 | | text.outlineColor = model.outlineColor; |
| | 224 | | } |
| 15 | 225 | | else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON")) |
| | 226 | | { |
| 0 | 227 | | text.fontSharedMaterial.DisableKeyword("OUTLINE_ON"); |
| | 228 | | } |
| 15 | 229 | | } |
| | 230 | |
|
| | 231 | | public static TextAlignmentOptions GetAlignment(string vTextAlign, string hTextAlign) |
| | 232 | | { |
| 44 | 233 | | vTextAlign = vTextAlign.ToLower(); |
| 44 | 234 | | hTextAlign = hTextAlign.ToLower(); |
| | 235 | |
|
| 44 | 236 | | return vTextAlign switch |
| | 237 | | { |
| 3 | 238 | | "top" => hTextAlign switch |
| | 239 | | { |
| 1 | 240 | | "left" => TextAlignmentOptions.TopLeft, |
| 1 | 241 | | "right" => TextAlignmentOptions.TopRight, |
| 1 | 242 | | _ => TextAlignmentOptions.Top |
| | 243 | | }, |
| 3 | 244 | | "bottom" => hTextAlign switch |
| | 245 | | { |
| 1 | 246 | | "left" => TextAlignmentOptions.BottomLeft, |
| 1 | 247 | | "right" => TextAlignmentOptions.BottomRight, |
| 1 | 248 | | _ => TextAlignmentOptions.Bottom |
| | 249 | | }, |
| 38 | 250 | | _ => hTextAlign switch |
| | 251 | | { |
| 1 | 252 | | "left" => TextAlignmentOptions.Left, |
| 1 | 253 | | "right" => TextAlignmentOptions.Right, |
| 36 | 254 | | _ => TextAlignmentOptions.Center |
| | 255 | | } |
| | 256 | | }; |
| | 257 | | } |
| | 258 | |
|
| | 259 | | private void PrepareRectTransform() |
| | 260 | | { |
| 15 | 261 | | rectTransform.anchorMin = Vector2.zero; |
| 15 | 262 | | rectTransform.anchorMax = Vector2.one; |
| 15 | 263 | | rectTransform.offsetMin = Vector2.zero; |
| 15 | 264 | | rectTransform.offsetMax = Vector2.zero; |
| | 265 | |
|
| | 266 | | // NOTE: previously width and height weren't working (setting sizeDelta before anchors and offset result in |
| | 267 | | // sizeDelta being reset to 0,0) |
| | 268 | | // to fix textWrapping and avoid backwards compatibility issues as result of the size being properly set (li |
| | 269 | | // we only set it if textWrapping is enabled. |
| 15 | 270 | | if (cachedModel.textWrapping) |
| | 271 | | { |
| 0 | 272 | | rectTransform.sizeDelta = new Vector2(cachedModel.width, cachedModel.height); |
| | 273 | | } |
| | 274 | | else |
| | 275 | | { |
| 15 | 276 | | rectTransform.sizeDelta = Vector2.zero; |
| | 277 | | } |
| 15 | 278 | | } |
| | 279 | |
|
| | 280 | | public override int GetClassId() => |
| 1 | 281 | | (int)CLASS_ID_COMPONENT.TEXT_SHAPE; |
| | 282 | |
|
| | 283 | | public override void Cleanup() |
| | 284 | | { |
| 26 | 285 | | text.text = string.Empty; |
| 26 | 286 | | base.Cleanup(); |
| 26 | 287 | | } |
| | 288 | |
|
| | 289 | | private void OnDestroy() |
| | 290 | | { |
| 318 | 291 | | Environment.i.platform.updateEventHandler?.RemoveListener(IUpdateEventHandler.EventType.Update, LookToCamera |
| | 292 | |
|
| 318 | 293 | | base.Cleanup(); |
| 318 | 294 | | Destroy(cachedFontMaterial); |
| 318 | 295 | | } |
| | 296 | | } |
| | 297 | | } |