| | 1 | | using System; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Models; |
| | 4 | | using System.Collections; |
| | 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 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) => |
| 4 | 46 | | Utils.SafeFromJson<Model>(json); |
| | 47 | |
|
| | 48 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 49 | | { |
| 0 | 50 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.UiText) |
| 0 | 51 | | return Utils.SafeUnimplemented<UIText, Model>(expected: ComponentBodyPayload.PayloadOneofCase.UiText |
| | 52 | |
|
| 0 | 53 | | var pb = new Model(); |
| 0 | 54 | | if (pbModel.UiText.HasName) pb.name = pbModel.UiText.Name; |
| 0 | 55 | | if (pbModel.UiText.HasParentComponent) pb.parentComponent = pbModel.UiText.ParentComponent; |
| 0 | 56 | | if (pbModel.UiText.HasVisible) pb.visible = pbModel.UiText.Visible; |
| 0 | 57 | | if (pbModel.UiText.HasOpacity) pb.opacity = pbModel.UiText.Opacity; |
| 0 | 58 | | if (pbModel.UiText.HasHAlign) pb.hAlign = pbModel.UiText.HAlign; |
| 0 | 59 | | if (pbModel.UiText.HasVAlign) pb.vAlign = pbModel.UiText.VAlign; |
| 0 | 60 | | if (pbModel.UiText.Width != null) pb.width = SDK6DataMapExtensions.FromProtobuf(pb.width, pbModel.UiText |
| 0 | 61 | | if (pbModel.UiText.Height != null) pb.height = SDK6DataMapExtensions.FromProtobuf(pb.height, pbModel.UiT |
| 0 | 62 | | if (pbModel.UiText.PositionX != null) pb.positionX = SDK6DataMapExtensions.FromProtobuf(pb.positionX, pb |
| 0 | 63 | | if (pbModel.UiText.PositionY != null) pb.positionY = SDK6DataMapExtensions.FromProtobuf(pb.positionY, pb |
| 0 | 64 | | if (pbModel.UiText.HasIsPointerBlocker) pb.isPointerBlocker = pbModel.UiText.IsPointerBlocker; |
| | 65 | |
|
| 0 | 66 | | if (pbModel.UiText.HasOutlineWidth) pb.outlineWidth = pbModel.UiText.OutlineWidth; |
| 0 | 67 | | if (pbModel.UiText.OutlineColor != null) pb.outlineColor = pbModel.UiText.OutlineColor.AsUnityColor(); |
| 0 | 68 | | if (pbModel.UiText.Color != null) pb.color = pbModel.UiText.Color.AsUnityColor(); |
| 0 | 69 | | if (pbModel.UiText.HasAdaptWidth) pb.adaptWidth = pbModel.UiText.AdaptWidth; |
| 0 | 70 | | if (pbModel.UiText.HasAdaptHeight) pb.adaptHeight = pbModel.UiText.AdaptHeight; |
| 0 | 71 | | if (pbModel.UiText.HasFontSize) pb.fontSize = pbModel.UiText.FontSize; |
| 0 | 72 | | if (pbModel.UiText.HasFontAutoSize) pb.fontAutoSize = pbModel.UiText.FontAutoSize; |
| 0 | 73 | | if (pbModel.UiText.HasFont) pb.font = pbModel.UiText.Font; |
| 0 | 74 | | if (pbModel.UiText.HasValue) pb.value = pbModel.UiText.Value; |
| 0 | 75 | | if (pbModel.UiText.HasLineSpacing) pb.lineSpacing = pbModel.UiText.LineSpacing; |
| 0 | 76 | | if (pbModel.UiText.HasLineCount) pb.lineCount = pbModel.UiText.LineCount; |
| 0 | 77 | | if (pbModel.UiText.HasHTextAlign) pb.hTextAlign = pbModel.UiText.HTextAlign; |
| 0 | 78 | | if (pbModel.UiText.HasVTextAlign) pb.vTextAlign = pbModel.UiText.VTextAlign; |
| 0 | 79 | | if (pbModel.UiText.HasTextWrapping) pb.textWrapping = pbModel.UiText.TextWrapping; |
| 0 | 80 | | if (pbModel.UiText.HasShadowBlur) pb.shadowBlur = pbModel.UiText.ShadowBlur; |
| 0 | 81 | | if (pbModel.UiText.HasShadowOffsetX) pb.shadowOffsetX = pbModel.UiText.ShadowOffsetX; |
| 0 | 82 | | if (pbModel.UiText.HasShadowOffsetY) pb.shadowOffsetY = pbModel.UiText.ShadowOffsetY; |
| 0 | 83 | | if (pbModel.UiText.ShadowColor != null) pb.shadowColor = pbModel.UiText.ShadowColor.AsUnityColor(); |
| 0 | 84 | | if (pbModel.UiText.HasPaddingTop) pb.paddingTop = pbModel.UiText.PaddingTop; |
| 0 | 85 | | if (pbModel.UiText.HasPaddingRight) pb.paddingRight = pbModel.UiText.PaddingRight; |
| 0 | 86 | | if (pbModel.UiText.HasPaddingBottom) pb.paddingBottom = pbModel.UiText.PaddingBottom; |
| 0 | 87 | | if (pbModel.UiText.HasPaddingLeft) pb.paddingLeft = pbModel.UiText.PaddingLeft; |
| | 88 | |
|
| 0 | 89 | | return pb; |
| | 90 | | } |
| | 91 | | } |
| | 92 | |
|
| 4 | 93 | | public override string referencesContainerPrefabName => "UIText"; |
| | 94 | |
|
| 4 | 95 | | public UIText() |
| | 96 | | { |
| 4 | 97 | | model = new Model(); |
| 4 | 98 | | } |
| | 99 | |
|
| | 100 | | public override int GetClassId() => |
| 0 | 101 | | (int) CLASS_ID.UI_TEXT_SHAPE; |
| | 102 | |
|
| 0 | 103 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) { Debug.LogError("Abo |
| | 104 | |
|
| 0 | 105 | | public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { } |
| | 106 | |
|
| | 107 | | public override IEnumerator ApplyChanges(BaseModel baseModel) |
| | 108 | | { |
| 8 | 109 | | model = (Model) baseModel; |
| | 110 | |
|
| | 111 | | // We avoid using even yield break; as this instruction skips a frame and we don't want that. |
| 8 | 112 | | if ( !DCLFont.IsFontLoaded(scene, model.font) ) |
| 0 | 113 | | yield return DCLFont.WaitUntilFontIsReady(scene, model.font); |
| | 114 | |
|
| 8 | 115 | | DCLFont.SetFontFromComponent(scene, model.font, referencesContainer.text); |
| 8 | 116 | | bool shouldMarkDirty = ShouldMarkDirty(); |
| | 117 | |
|
| 8 | 118 | | ApplyModelChanges(referencesContainer.text, model); |
| 16 | 119 | | if (shouldMarkDirty) MarkLayoutDirty(); |
| 8 | 120 | | } |
| | 121 | | private bool ShouldMarkDirty() |
| | 122 | | { |
| 8 | 123 | | TextMeshProUGUI text = referencesContainer.text; |
| | 124 | |
|
| 8 | 125 | | return model.value != text.text |
| | 126 | | || Math.Abs(model.fontSize - text.fontSize) > float.Epsilon |
| | 127 | | || model.fontAutoSize != text.enableAutoSizing |
| | 128 | | || Math.Abs(model.lineSpacing - text.lineSpacing) > float.Epsilon |
| | 129 | | || new Vector4((int) model.paddingLeft, (int) model.paddingTop, (int) model.paddingRight, (int) model |
| | 130 | | } |
| | 131 | |
|
| | 132 | | protected override void RefreshDCLSize(RectTransform parentTransform = null) |
| | 133 | | { |
| 16 | 134 | | if (parentTransform == null) |
| | 135 | | { |
| 0 | 136 | | parentTransform = referencesContainer.GetComponentInParent<RectTransform>(); |
| | 137 | | } |
| | 138 | |
|
| 16 | 139 | | if (model.adaptWidth || model.adaptHeight) |
| 2 | 140 | | referencesContainer.text.ForceMeshUpdate(false); |
| | 141 | |
|
| 16 | 142 | | Bounds b = referencesContainer.text.textBounds; |
| | 143 | |
|
| | 144 | | float width, height; |
| | 145 | |
|
| 16 | 146 | | if (model.adaptWidth) |
| | 147 | | { |
| 2 | 148 | | width = b.size.x; |
| | 149 | | } |
| | 150 | | else |
| | 151 | | { |
| 14 | 152 | | width = model.width.GetScaledValue(parentTransform.rect.width); |
| | 153 | | } |
| | 154 | |
|
| 16 | 155 | | if (model.adaptHeight) |
| | 156 | | { |
| 2 | 157 | | height = b.size.y; |
| | 158 | | } |
| | 159 | | else |
| | 160 | | { |
| 14 | 161 | | height = model.height.GetScaledValue(parentTransform.rect.height); |
| | 162 | | } |
| | 163 | |
|
| 16 | 164 | | referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width); |
| 16 | 165 | | referencesContainer.layoutElementRT.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); |
| 16 | 166 | | } |
| | 167 | |
|
| | 168 | | public override void Dispose() |
| | 169 | | { |
| 0 | 170 | | if (referencesContainer != null) |
| 0 | 171 | | Utils.SafeDestroy(referencesContainer.gameObject); |
| | 172 | |
|
| 0 | 173 | | base.Dispose(); |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | private static void ApplyModelChanges(TMP_Text text, Model model) |
| | 177 | | { |
| 8 | 178 | | text.text = model.value; |
| | 179 | |
|
| 8 | 180 | | text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0); |
| 8 | 181 | | text.fontSize = (int) model.fontSize; |
| 8 | 182 | | text.richText = true; |
| 8 | 183 | | text.overflowMode = TextOverflowModes.Overflow; |
| 8 | 184 | | text.enableAutoSizing = model.fontAutoSize; |
| | 185 | |
|
| 8 | 186 | | text.margin = |
| | 187 | | new Vector4 |
| | 188 | | ( |
| | 189 | | (int) model.paddingLeft, |
| | 190 | | (int) model.paddingTop, |
| | 191 | | (int) model.paddingRight, |
| | 192 | | (int) model.paddingBottom |
| | 193 | | ); |
| | 194 | |
|
| 8 | 195 | | text.alignment = TextShape.GetAlignment(model.vTextAlign, model.hTextAlign); |
| 8 | 196 | | text.lineSpacing = model.lineSpacing; |
| | 197 | |
|
| 8 | 198 | | if (model.lineCount != 0) |
| | 199 | | { |
| 2 | 200 | | text.maxVisibleLines = Mathf.Max(model.lineCount, 1); |
| | 201 | | } |
| | 202 | | else |
| | 203 | | { |
| 6 | 204 | | text.maxVisibleLines = int.MaxValue; |
| | 205 | | } |
| | 206 | |
|
| 8 | 207 | | text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing; |
| | 208 | |
|
| 8 | 209 | | if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0) |
| | 210 | | { |
| 2 | 211 | | text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON"); |
| 2 | 212 | | text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor); |
| 2 | 213 | | text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur); |
| | 214 | | } |
| 6 | 215 | | else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON")) |
| | 216 | | { |
| 2 | 217 | | text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON"); |
| | 218 | | } |
| | 219 | |
|
| 8 | 220 | | if (model.outlineWidth > 0f) |
| | 221 | | { |
| 1 | 222 | | text.fontSharedMaterial.EnableKeyword("OUTLINE_ON"); |
| 1 | 223 | | text.outlineWidth = model.outlineWidth; |
| 1 | 224 | | text.outlineColor = model.outlineColor; |
| | 225 | | } |
| 7 | 226 | | else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON")) |
| | 227 | | { |
| 0 | 228 | | text.fontSharedMaterial.DisableKeyword("OUTLINE_ON"); |
| | 229 | | } |
| 7 | 230 | | } |
| | 231 | | } |
| | 232 | | } |