| | 1 | | using System; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using Newtonsoft.Json; |
| | 8 | | using TMPro; |
| | 9 | | using UnityEngine; |
| | 10 | |
|
| | 11 | | namespace DCL.Components |
| | 12 | | { |
| | 13 | | public class UIText : UIShape<UITextReferencesContainer, UIText.Model> |
| | 14 | | { |
| | 15 | | [System.Serializable] |
| | 16 | | new public class Model : UIShape.Model |
| | 17 | | { |
| | 18 | | public float outlineWidth = 0f; |
| 16 | 19 | | public Color outlineColor = Color.white; |
| | 20 | |
|
| 16 | 21 | | public Color color = Color.white; |
| | 22 | |
|
| | 23 | | public bool adaptWidth = false; |
| | 24 | | public bool adaptHeight = false; |
| 16 | 25 | | public float fontSize = 100f; |
| | 26 | | public bool fontAutoSize = false; |
| | 27 | |
|
| | 28 | | public string font; |
| 16 | 29 | | public string value = ""; |
| | 30 | | public float lineSpacing = 0f; |
| | 31 | | public int lineCount = 0; |
| 16 | 32 | | public string hTextAlign = "bottom"; |
| 16 | 33 | | public string vTextAlign = "left"; |
| | 34 | | public bool textWrapping = false; |
| | 35 | |
|
| | 36 | | public float shadowBlur = 0f; |
| | 37 | | public float shadowOffsetX = 0f; |
| | 38 | | public float shadowOffsetY = 0f; |
| 16 | 39 | | public Color shadowColor = new Color(1, 1, 1); |
| | 40 | |
|
| | 41 | | public float paddingTop = 0f; |
| | 42 | | public float paddingRight = 0f; |
| | 43 | | public float paddingBottom = 0f; |
| | 44 | | public float paddingLeft = 0f; |
| | 45 | |
|
| | 46 | | public override BaseModel GetDataFromJSON(string json) |
| | 47 | | { |
| 4 | 48 | | Model model = Utils.SafeFromJson<Model>(json); |
| | 49 | |
|
| 4 | 50 | | return model; |
| | 51 | | } |
| | 52 | | } |
| | 53 | |
|
| 4 | 54 | | public override string referencesContainerPrefabName => "UIText"; |
| | 55 | |
|
| 12 | 56 | | public UIText() { model = new Model(); } |
| | 57 | |
|
| 0 | 58 | | public override int GetClassId() { return (int) CLASS_ID.UI_TEXT_SHAPE; } |
| | 59 | |
|
| 0 | 60 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) { Debug.LogError("Abo |
| | 61 | |
|
| 0 | 62 | | public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { } |
| | 63 | |
|
| | 64 | | public override IEnumerator ApplyChanges(BaseModel baseModel) |
| | 65 | | { |
| 8 | 66 | | model = (Model) baseModel; |
| | 67 | |
|
| | 68 | | // We avoid using even yield break; as this instruction skips a frame and we don't want that. |
| 8 | 69 | | if ( !DCLFont.IsFontLoaded(scene, model.font) ) |
| | 70 | | { |
| 0 | 71 | | yield return DCLFont.WaitUntilFontIsReady(scene, model.font); |
| | 72 | | } |
| | 73 | |
|
| 8 | 74 | | DCLFont.SetFontFromComponent(scene, model.font, referencesContainer.text); |
| 8 | 75 | | bool shouldMarkDirty = ShouldMarkDirty(); |
| | 76 | |
|
| 8 | 77 | | ApplyModelChanges(referencesContainer.text, model); |
| 16 | 78 | | if (shouldMarkDirty) MarkLayoutDirty(); |
| 8 | 79 | | } |
| | 80 | | private bool ShouldMarkDirty() |
| | 81 | | { |
| 8 | 82 | | TextMeshProUGUI text = referencesContainer.text; |
| | 83 | |
|
| 8 | 84 | | return model.value != text.text |
| | 85 | | || Math.Abs(model.fontSize - text.fontSize) > float.Epsilon |
| | 86 | | || model.fontAutoSize != text.enableAutoSizing |
| | 87 | | || Math.Abs(model.lineSpacing - text.lineSpacing) > float.Epsilon |
| | 88 | | || new Vector4((int) model.paddingLeft, (int) model.paddingTop, (int) model.paddingRight, (int) model |
| | 89 | | } |
| | 90 | |
|
| | 91 | | protected override void RefreshDCLSize(RectTransform parentTransform = null) |
| | 92 | | { |
| 16 | 93 | | if (parentTransform == null) |
| | 94 | | { |
| 0 | 95 | | parentTransform = referencesContainer.GetComponentInParent<RectTransform>(); |
| | 96 | | } |
| | 97 | |
|
| 16 | 98 | | if (model.adaptWidth || model.adaptHeight) |
| 2 | 99 | | referencesContainer.text.ForceMeshUpdate(false); |
| | 100 | |
|
| 16 | 101 | | Bounds b = referencesContainer.text.textBounds; |
| | 102 | |
|
| | 103 | | float width, height; |
| | 104 | |
|
| 16 | 105 | | if (model.adaptWidth) |
| | 106 | | { |
| 2 | 107 | | width = b.size.x; |
| 2 | 108 | | } |
| | 109 | | else |
| | 110 | | { |
| 14 | 111 | | width = model.width.GetScaledValue(parentTransform.rect.width); |
| | 112 | | } |
| | 113 | |
|
| 16 | 114 | | if (model.adaptHeight) |
| | 115 | | { |
| 2 | 116 | | height = b.size.y; |
| 2 | 117 | | } |
| | 118 | | else |
| | 119 | | { |
| 14 | 120 | | height = model.height.GetScaledValue(parentTransform.rect.height); |
| | 121 | | } |
| | 122 | |
|
| 16 | 123 | | referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width); |
| 16 | 124 | | referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); |
| 16 | 125 | | } |
| | 126 | |
|
| | 127 | | public override void Dispose() |
| | 128 | | { |
| 0 | 129 | | if (referencesContainer != null) |
| 0 | 130 | | Utils.SafeDestroy(referencesContainer.gameObject); |
| | 131 | |
|
| 0 | 132 | | base.Dispose(); |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | private static void ApplyModelChanges(TMP_Text text, Model model) |
| | 136 | | { |
| 8 | 137 | | text.text = model.value; |
| | 138 | |
|
| 8 | 139 | | text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0); |
| 8 | 140 | | text.fontSize = (int) model.fontSize; |
| 8 | 141 | | text.richText = true; |
| 8 | 142 | | text.overflowMode = TextOverflowModes.Overflow; |
| 8 | 143 | | text.enableAutoSizing = model.fontAutoSize; |
| | 144 | |
|
| 8 | 145 | | text.margin = |
| | 146 | | new Vector4 |
| | 147 | | ( |
| | 148 | | (int) model.paddingLeft, |
| | 149 | | (int) model.paddingTop, |
| | 150 | | (int) model.paddingRight, |
| | 151 | | (int) model.paddingBottom |
| | 152 | | ); |
| | 153 | |
|
| 8 | 154 | | text.alignment = TextShape.GetAlignment(model.vTextAlign, model.hTextAlign); |
| 8 | 155 | | text.lineSpacing = model.lineSpacing; |
| | 156 | |
|
| 8 | 157 | | if (model.lineCount != 0) |
| | 158 | | { |
| 2 | 159 | | text.maxVisibleLines = Mathf.Max(model.lineCount, 1); |
| 2 | 160 | | } |
| | 161 | | else |
| | 162 | | { |
| 6 | 163 | | text.maxVisibleLines = int.MaxValue; |
| | 164 | | } |
| | 165 | |
|
| 8 | 166 | | text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing; |
| | 167 | |
|
| 8 | 168 | | if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0) |
| | 169 | | { |
| 2 | 170 | | text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON"); |
| 2 | 171 | | text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor); |
| 2 | 172 | | text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur); |
| 2 | 173 | | } |
| 6 | 174 | | else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON")) |
| | 175 | | { |
| 2 | 176 | | text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON"); |
| | 177 | | } |
| | 178 | |
|
| 8 | 179 | | if (model.outlineWidth > 0f) |
| | 180 | | { |
| 1 | 181 | | text.fontSharedMaterial.EnableKeyword("OUTLINE_ON"); |
| 1 | 182 | | text.outlineWidth = model.outlineWidth; |
| 1 | 183 | | text.outlineColor = model.outlineColor; |
| 1 | 184 | | } |
| 7 | 185 | | else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON")) |
| | 186 | | { |
| 1 | 187 | | text.fontSharedMaterial.DisableKeyword("OUTLINE_ON"); |
| | 188 | | } |
| 7 | 189 | | } |
| | 190 | | } |
| | 191 | | } |