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