| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Models; |
| | 6 | | using TMPro; |
| | 7 | | using UnityEngine; |
| | 8 | | using Object = UnityEngine.Object; |
| | 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")] |
| 37 | 20 | | public string value = ""; |
| | 21 | |
|
| 37 | 22 | | public bool visible = true; |
| | 23 | |
|
| 37 | 24 | | public Color color = Color.white; |
| 37 | 25 | | public float opacity = 1f; |
| 37 | 26 | | public float fontSize = 100f; |
| | 27 | | public bool fontAutoSize = false; |
| | 28 | | public string font; |
| | 29 | |
|
| | 30 | | [Header("Text box properties")] |
| 37 | 31 | | public string hTextAlign = "bottom"; |
| | 32 | |
|
| 37 | 33 | | public string vTextAlign = "left"; |
| 37 | 34 | | public float width = 1f; |
| 37 | 35 | | public float height = 0.2f; |
| | 36 | | public float paddingTop = 0f; |
| | 37 | | public float paddingRight = 0f; |
| | 38 | | public float paddingBottom = 0f; |
| | 39 | | public float paddingLeft = 0f; |
| | 40 | | public float lineSpacing = 0f; |
| | 41 | | public int lineCount = 0; |
| | 42 | | public bool textWrapping = false; |
| | 43 | |
|
| | 44 | | [Header("Text shadow properties")] |
| | 45 | | public float shadowBlur = 0f; |
| | 46 | |
|
| | 47 | | public float shadowOffsetX = 0f; |
| | 48 | | public float shadowOffsetY = 0f; |
| 37 | 49 | | public Color shadowColor = new Color(1, 1, 1); |
| | 50 | |
|
| | 51 | | [Header("Text outline properties")] |
| | 52 | | public float outlineWidth = 0f; |
| | 53 | |
|
| 37 | 54 | | public Color outlineColor = Color.white; |
| | 55 | |
|
| 14 | 56 | | public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); } |
| | 57 | | } |
| | 58 | |
|
| | 59 | | public TextMeshPro text; |
| | 60 | | public RectTransform rectTransform; |
| | 61 | | private Model cachedModel; |
| | 62 | | private Material cachedFontMaterial; |
| | 63 | |
|
| | 64 | | private void Awake() |
| | 65 | | { |
| 10 | 66 | | model = new Model(); |
| 10 | 67 | | cachedFontMaterial = new Material(text.fontSharedMaterial); |
| 10 | 68 | | text.fontSharedMaterial = cachedFontMaterial; |
| 10 | 69 | | text.text = string.Empty; |
| 10 | 70 | | } |
| | 71 | |
|
| | 72 | | public void Update() |
| | 73 | | { |
| 32 | 74 | | if (cachedModel.billboard && Camera.main != null) |
| | 75 | | { |
| 0 | 76 | | transform.forward = Camera.main.transform.forward; |
| | 77 | | } |
| 32 | 78 | | } |
| | 79 | |
|
| 0 | 80 | | new public Model GetModel() { return cachedModel; } |
| | 81 | |
|
| | 82 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 83 | | { |
| 14 | 84 | | if (rectTransform == null) |
| 0 | 85 | | yield break; |
| | 86 | |
|
| 14 | 87 | | Model model = (Model) newModel; |
| 14 | 88 | | cachedModel = model; |
| 14 | 89 | | PrepareRectTransform(); |
| | 90 | |
|
| | 91 | | // We avoid using even yield break; as this instruction skips a frame and we don't want that. |
| 14 | 92 | | if ( !DCLFont.IsFontLoaded(scene, model.font) ) |
| | 93 | | { |
| 0 | 94 | | yield return DCLFont.WaitUntilFontIsReady(scene, model.font); |
| | 95 | | } |
| | 96 | |
|
| 14 | 97 | | DCLFont.SetFontFromComponent(scene, model.font, text); |
| 14 | 98 | | ApplyModelChanges(text, model); |
| | 99 | |
|
| 14 | 100 | | if (entity.meshRootGameObject == null) |
| 10 | 101 | | entity.meshesInfo.meshRootGameObject = gameObject; |
| | 102 | |
|
| 14 | 103 | | entity.OnShapeUpdated?.Invoke(entity); |
| 14 | 104 | | } |
| | 105 | |
|
| | 106 | | public static void ApplyModelChanges(TMP_Text text, Model model) |
| | 107 | | { |
| 14 | 108 | | text.text = model.value; |
| | 109 | |
|
| 14 | 110 | | text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0); |
| 14 | 111 | | text.fontSize = (int) model.fontSize; |
| 14 | 112 | | text.richText = true; |
| 14 | 113 | | text.overflowMode = TextOverflowModes.Overflow; |
| 14 | 114 | | text.enableAutoSizing = model.fontAutoSize; |
| | 115 | |
|
| 14 | 116 | | text.margin = |
| | 117 | | new Vector4 |
| | 118 | | ( |
| | 119 | | (int) model.paddingLeft, |
| | 120 | | (int) model.paddingTop, |
| | 121 | | (int) model.paddingRight, |
| | 122 | | (int) model.paddingBottom |
| | 123 | | ); |
| | 124 | |
|
| 14 | 125 | | text.alignment = GetAlignment(model.vTextAlign, model.hTextAlign); |
| 14 | 126 | | text.lineSpacing = model.lineSpacing; |
| | 127 | |
|
| 14 | 128 | | if (model.lineCount != 0) |
| | 129 | | { |
| 1 | 130 | | text.maxVisibleLines = Mathf.Max(model.lineCount, 1); |
| 1 | 131 | | } |
| | 132 | | else |
| | 133 | | { |
| 13 | 134 | | text.maxVisibleLines = int.MaxValue; |
| | 135 | | } |
| | 136 | |
|
| 14 | 137 | | text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing; |
| | 138 | |
|
| 14 | 139 | | if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0) |
| | 140 | | { |
| 0 | 141 | | text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON"); |
| 0 | 142 | | text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor); |
| 0 | 143 | | text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur); |
| 0 | 144 | | } |
| 14 | 145 | | else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON")) |
| | 146 | | { |
| 0 | 147 | | text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON"); |
| | 148 | | } |
| | 149 | |
|
| 14 | 150 | | if (model.outlineWidth > 0f) |
| | 151 | | { |
| 0 | 152 | | text.fontSharedMaterial.EnableKeyword("OUTLINE_ON"); |
| 0 | 153 | | text.outlineWidth = model.outlineWidth; |
| 0 | 154 | | text.outlineColor = model.outlineColor; |
| 0 | 155 | | } |
| 14 | 156 | | else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON")) |
| | 157 | | { |
| 0 | 158 | | text.fontSharedMaterial.DisableKeyword("OUTLINE_ON"); |
| | 159 | | } |
| 14 | 160 | | } |
| | 161 | |
|
| | 162 | | public static TextAlignmentOptions GetAlignment(string vTextAlign, string hTextAlign) |
| | 163 | | { |
| 43 | 164 | | vTextAlign = vTextAlign.ToLower(); |
| 43 | 165 | | hTextAlign = hTextAlign.ToLower(); |
| | 166 | |
|
| | 167 | | switch (vTextAlign) |
| | 168 | | { |
| | 169 | | case "top": |
| | 170 | | switch (hTextAlign) |
| | 171 | | { |
| | 172 | | case "left": |
| 1 | 173 | | return TextAlignmentOptions.TopLeft; |
| | 174 | | case "right": |
| 1 | 175 | | return TextAlignmentOptions.TopRight; |
| | 176 | | default: |
| 1 | 177 | | return TextAlignmentOptions.Top; |
| | 178 | | } |
| | 179 | |
|
| | 180 | | case "bottom": |
| | 181 | | switch (hTextAlign) |
| | 182 | | { |
| | 183 | | case "left": |
| 1 | 184 | | return TextAlignmentOptions.BottomLeft; |
| | 185 | | case "right": |
| 1 | 186 | | return TextAlignmentOptions.BottomRight; |
| | 187 | | default: |
| 1 | 188 | | return TextAlignmentOptions.Bottom; |
| | 189 | | } |
| | 190 | |
|
| | 191 | | default: // center |
| | 192 | | switch (hTextAlign) |
| | 193 | | { |
| | 194 | | case "left": |
| 1 | 195 | | return TextAlignmentOptions.Left; |
| | 196 | | case "right": |
| 1 | 197 | | return TextAlignmentOptions.Right; |
| | 198 | | default: |
| 35 | 199 | | return TextAlignmentOptions.Center; |
| | 200 | | } |
| | 201 | | } |
| | 202 | | } |
| | 203 | |
|
| | 204 | | private void PrepareRectTransform() |
| | 205 | | { |
| 14 | 206 | | rectTransform.anchorMin = Vector2.zero; |
| 14 | 207 | | rectTransform.anchorMax = Vector2.one; |
| 14 | 208 | | rectTransform.offsetMin = Vector2.zero; |
| 14 | 209 | | rectTransform.offsetMax = Vector2.zero; |
| | 210 | |
|
| | 211 | | // NOTE: previously width and height weren't working (setting sizeDelta before anchors and offset result in |
| | 212 | | // sizeDelta being reset to 0,0) |
| | 213 | | // to fix textWrapping and avoid backwards compatibility issues as result of the size being properly set (li |
| | 214 | | // we only set it if textWrapping is enabled. |
| 14 | 215 | | if (cachedModel.textWrapping) |
| | 216 | | { |
| 0 | 217 | | rectTransform.sizeDelta = new Vector2(cachedModel.width, cachedModel.height); |
| 0 | 218 | | } |
| | 219 | | else |
| | 220 | | { |
| 14 | 221 | | rectTransform.sizeDelta = Vector2.zero; |
| | 222 | | } |
| 14 | 223 | | } |
| | 224 | |
|
| 0 | 225 | | public override int GetClassId() { return (int) CLASS_ID.UI_TEXT_SHAPE; } |
| | 226 | |
|
| | 227 | | public override void Cleanup() |
| | 228 | | { |
| 0 | 229 | | text.text = string.Empty; |
| 0 | 230 | | base.Cleanup(); |
| 0 | 231 | | } |
| | 232 | |
|
| | 233 | | private void OnDestroy() |
| | 234 | | { |
| 10 | 235 | | base.Cleanup(); |
| 10 | 236 | | Destroy(cachedFontMaterial); |
| 10 | 237 | | } |
| | 238 | | } |
| | 239 | | } |