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