| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System.Collections; |
| | 4 | | using DCL.Helpers; |
| | 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 UIInputText : UIShape<UIInputTextRefContainer, UIInputText.Model> |
| | 13 | | { |
| | 14 | | [System.Serializable] |
| | 15 | | public new class Model : UIShape.Model |
| | 16 | | { |
| | 17 | | public float outlineWidth = 0f; |
| 34 | 18 | | public Color outlineColor = Color.white; |
| | 19 | |
|
| 34 | 20 | | public Color color = Color.white; |
| | 21 | |
|
| 34 | 22 | | public float fontSize = 100f; |
| | 23 | | public string font; |
| 34 | 24 | | public string value = ""; |
| 34 | 25 | | public string hTextAlign = "bottom"; |
| 34 | 26 | | public string vTextAlign = "left"; |
| | 27 | | public bool textWrapping = false; |
| | 28 | |
|
| | 29 | | public float shadowBlur = 0f; |
| | 30 | | public float shadowOffsetX = 0f; |
| | 31 | | public float shadowOffsetY = 0f; |
| 34 | 32 | | public Color shadowColor = new Color(1, 1, 1); |
| | 33 | |
|
| | 34 | | public float paddingTop = 0f; |
| | 35 | | public float paddingRight = 0f; |
| | 36 | | public float paddingBottom = 0f; |
| | 37 | | public float paddingLeft = 0f; |
| | 38 | |
|
| | 39 | | public string placeholder; |
| 34 | 40 | | public Color placeholderColor = Color.white; |
| 34 | 41 | | public Color focusedBackground = Color.black; |
| | 42 | |
|
| | 43 | | public string onTextSubmit; |
| | 44 | | public string onChanged; |
| | 45 | | public string onFocus; |
| | 46 | | public string onBlur; |
| | 47 | | public string onTextChanged; |
| | 48 | |
|
| | 49 | | public override BaseModel GetDataFromJSON(string json) => |
| 12 | 50 | | Utils.SafeFromJson<Model>(json); |
| | 51 | |
|
| | 52 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 53 | | { |
| 0 | 54 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.UiInputText) |
| 0 | 55 | | return Utils.SafeUnimplemented<UIInputText, Model>(expected: ComponentBodyPayload.PayloadOneofCase.U |
| | 56 | |
|
| 0 | 57 | | var pb = new Model(); |
| 0 | 58 | | if (pbModel.UiInputText.HasName) pb.name = pbModel.UiInputText.Name; |
| 0 | 59 | | if (pbModel.UiInputText.HasParentComponent) pb.parentComponent = pbModel.UiInputText.ParentComponent; |
| 0 | 60 | | if (pbModel.UiInputText.HasVisible) pb.visible = pbModel.UiInputText.Visible; |
| 0 | 61 | | if (pbModel.UiInputText.HasOpacity) pb.opacity = pbModel.UiInputText.Opacity; |
| 0 | 62 | | if (pbModel.UiInputText.HasHAlign) pb.hAlign = pbModel.UiInputText.HAlign; |
| 0 | 63 | | if (pbModel.UiInputText.HasVAlign) pb.vAlign = pbModel.UiInputText.VAlign; |
| 0 | 64 | | if (pbModel.UiInputText.Width != null) pb.width = SDK6DataMapExtensions.FromProtobuf(pb.width, pbModel.U |
| 0 | 65 | | if (pbModel.UiInputText.Height != null) pb.height = SDK6DataMapExtensions.FromProtobuf(pb.height, pbMode |
| 0 | 66 | | if (pbModel.UiInputText.PositionX != null) pb.positionX = SDK6DataMapExtensions.FromProtobuf(pb.position |
| 0 | 67 | | if (pbModel.UiInputText.PositionY != null) pb.positionY = SDK6DataMapExtensions.FromProtobuf(pb.position |
| 0 | 68 | | if (pbModel.UiInputText.HasIsPointerBlocker) pb.isPointerBlocker = pbModel.UiInputText.IsPointerBlocker; |
| | 69 | |
|
| 0 | 70 | | if (pbModel.UiInputText.HasOutlineWidth) pb.outlineWidth = pbModel.UiInputText.OutlineWidth; |
| 0 | 71 | | if (pbModel.UiInputText.OutlineColor != null) pb.outlineColor = pbModel.UiInputText.OutlineColor.AsUnity |
| 0 | 72 | | if (pbModel.UiInputText.Color != null) pb.color = pbModel.UiInputText.Color.AsUnityColor(); |
| 0 | 73 | | if (pbModel.UiInputText.HasFontSize) pb.fontSize = pbModel.UiInputText.FontSize; |
| 0 | 74 | | if (pbModel.UiInputText.HasFont) pb.font = pbModel.UiInputText.Font; |
| 0 | 75 | | if (pbModel.UiInputText.HasValue) pb.value = pbModel.UiInputText.Value; |
| 0 | 76 | | if (pbModel.UiInputText.HasHTextAlign) pb.hTextAlign = pbModel.UiInputText.HTextAlign; |
| 0 | 77 | | if (pbModel.UiInputText.HasVTextAlign) pb.vTextAlign = pbModel.UiInputText.VTextAlign; |
| 0 | 78 | | if (pbModel.UiInputText.HasTextWrapping) pb.textWrapping = pbModel.UiInputText.TextWrapping; |
| 0 | 79 | | if (pbModel.UiInputText.HasShadowBlur) pb.shadowBlur = pbModel.UiInputText.ShadowBlur; |
| 0 | 80 | | if (pbModel.UiInputText.HasShadowOffsetX) pb.shadowOffsetX = pbModel.UiInputText.ShadowOffsetX; |
| 0 | 81 | | if (pbModel.UiInputText.HasShadowOffsetY) pb.shadowOffsetY = pbModel.UiInputText.ShadowOffsetY; |
| 0 | 82 | | if (pbModel.UiInputText.ShadowColor != null) pb.shadowColor = pbModel.UiInputText.ShadowColor.AsUnityCol |
| 0 | 83 | | if (pbModel.UiInputText.HasPaddingTop) pb.paddingTop = pbModel.UiInputText.PaddingTop; |
| 0 | 84 | | if (pbModel.UiInputText.HasPaddingRight) pb.paddingRight = pbModel.UiInputText.PaddingRight; |
| 0 | 85 | | if (pbModel.UiInputText.HasPaddingBottom) pb.paddingBottom = pbModel.UiInputText.PaddingBottom; |
| 0 | 86 | | if (pbModel.UiInputText.HasPaddingLeft) pb.paddingLeft = pbModel.UiInputText.PaddingLeft; |
| 0 | 87 | | if (pbModel.UiInputText.HasPlaceholder) pb.placeholder = pbModel.UiInputText.Placeholder; |
| 0 | 88 | | if (pbModel.UiInputText.FocusedBackground != null) pb.focusedBackground = pbModel.UiInputText.FocusedBac |
| 0 | 89 | | if (pbModel.UiInputText.HasOnTextSubmit) pb.onTextSubmit = pbModel.UiInputText.OnTextSubmit; |
| 0 | 90 | | if (pbModel.UiInputText.HasOnChanged) pb.onChanged = pbModel.UiInputText.OnChanged; |
| 0 | 91 | | if (pbModel.UiInputText.HasOnFocus) pb.onFocus = pbModel.UiInputText.OnFocus; |
| 0 | 92 | | if (pbModel.UiInputText.HasOnBlur) pb.onBlur = pbModel.UiInputText.OnBlur; |
| 0 | 93 | | if (pbModel.UiInputText.HasOnTextChanged) pb.onTextChanged = pbModel.UiInputText.OnTextChanged; |
| | 94 | |
|
| 0 | 95 | | return pb; |
| | 96 | | } |
| | 97 | | } |
| | 98 | |
|
| 10 | 99 | | public override string referencesContainerPrefabName => "UIInputText"; |
| | 100 | |
|
| 12 | 101 | | public TMP_Text tmpText => referencesContainer.text; |
| 177 | 102 | | public TMP_InputField inputField => referencesContainer.inputField; |
| 0 | 103 | | public RectTransform rectTransform => referencesContainer.rectTransform; |
| | 104 | |
|
| 30 | 105 | | public UIInputText() { model = new Model(); } |
| | 106 | |
|
| 0 | 107 | | public override int GetClassId() { return (int) CLASS_ID.UI_INPUT_TEXT_SHAPE; } |
| | 108 | |
|
| | 109 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 110 | | { |
| 0 | 111 | | Debug.LogError( |
| | 112 | | "Aborted UIContainerRectShape attachment to an entity. UIShapes shouldn't be attached to entities."); |
| 0 | 113 | | } |
| | 114 | |
|
| 0 | 115 | | public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { } |
| | 116 | |
|
| | 117 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 118 | | { |
| | 119 | | //NOTE(Brian): We have to serialize twice now, but in the future we should fix the |
| | 120 | | // client data structure to be like this, so we can serialize all of it in one shot. |
| 12 | 121 | | model = (Model) newModel; |
| | 122 | |
|
| 12 | 123 | | inputField.textViewport = referencesContainer.rectTransform; |
| | 124 | |
|
| 12 | 125 | | UnsuscribeFromEvents(); |
| | 126 | |
|
| 12 | 127 | | inputField.onSelect.AddListener(OnFocus); |
| 12 | 128 | | inputField.onDeselect.AddListener(OnBlur); |
| 12 | 129 | | inputField.onSubmit.AddListener(OnSubmit); |
| 12 | 130 | | inputField.onValueChanged.AddListener(OnChanged); |
| | 131 | |
|
| | 132 | | // We avoid using even yield break; as this instruction skips a frame and we don't want that. |
| 12 | 133 | | if ( !DCLFont.IsFontLoaded(scene, model.font) ) |
| | 134 | | { |
| 0 | 135 | | yield return DCLFont.WaitUntilFontIsReady(scene, model.font); |
| | 136 | | } |
| | 137 | |
|
| 12 | 138 | | DCLFont.SetFontFromComponent(scene, model.font, referencesContainer.text); |
| 12 | 139 | | ApplyModelChanges(tmpText, model); |
| | 140 | |
|
| 12 | 141 | | inputField.text = model.placeholder; |
| 12 | 142 | | inputField.textComponent.color = new Color(model.placeholderColor.r, model.placeholderColor.g, |
| | 143 | | model.placeholderColor.b, model.placeholderColor.a); |
| 12 | 144 | | referencesContainer.bgImage.color = new Color(model.focusedBackground.r, model.focusedBackground.g, |
| | 145 | | model.focusedBackground.b, model.focusedBackground.a); |
| 12 | 146 | | } |
| | 147 | |
|
| | 148 | | public void OnFocus(string call) |
| | 149 | | { |
| 2 | 150 | | if (inputField.text == model.placeholder) |
| | 151 | | { |
| 2 | 152 | | inputField.text = ""; |
| | 153 | | } |
| | 154 | |
|
| 2 | 155 | | inputField.customCaretColor = true; |
| 2 | 156 | | inputField.caretColor = Color.white; |
| 2 | 157 | | Interface.WebInterface.ReportOnFocusEvent(scene.sceneData.sceneNumber, model.onFocus); |
| 2 | 158 | | } |
| | 159 | |
|
| | 160 | | public void OnChanged(string changedText) |
| | 161 | | { |
| | 162 | | // NOTE: OSX is adding the ESC character at the end of the string when ESC is pressed |
| | 163 | | #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX |
| | 164 | | if (changedText.Length > 0 && changedText[changedText.Length - 1] == 27) |
| | 165 | | { |
| | 166 | | changedText = changedText.Substring(0, changedText.Length - 1); |
| | 167 | | inputField.SetTextWithoutNotify(changedText); |
| | 168 | | } |
| | 169 | | #endif |
| | 170 | |
|
| | 171 | | // NOTE: we keep `ReportOnTextInputChangedEvent` for backward compatibility (it won't be called for scenes u |
| 17 | 172 | | Interface.WebInterface.ReportOnTextInputChangedEvent(scene.sceneData.sceneNumber, model.onChanged, changedTe |
| 17 | 173 | | Interface.WebInterface.ReportOnTextInputChangedTextEvent(scene.sceneData.sceneNumber, model.onTextChanged, c |
| 17 | 174 | | } |
| | 175 | |
|
| | 176 | | public void OnBlur(string call) |
| | 177 | | { |
| 2 | 178 | | HideCaret(); |
| 2 | 179 | | Interface.WebInterface.ReportOnBlurEvent(scene.sceneData.sceneNumber, model.onBlur); |
| 2 | 180 | | } |
| | 181 | |
|
| | 182 | | public void HideCaret() |
| | 183 | | { |
| 2 | 184 | | if (string.IsNullOrEmpty(inputField.text)) |
| | 185 | | { |
| 2 | 186 | | inputField.text = model.placeholder; |
| | 187 | | } |
| | 188 | |
|
| 2 | 189 | | inputField.customCaretColor = true; |
| 2 | 190 | | inputField.caretColor = Color.clear; |
| 2 | 191 | | inputField.selectionAnchorPosition = 0; |
| 2 | 192 | | inputField.selectionFocusPosition = 0; |
| 2 | 193 | | inputField.selectionStringAnchorPosition = 0; |
| 2 | 194 | | inputField.selectionStringFocusPosition = 0; |
| 2 | 195 | | } |
| | 196 | |
|
| | 197 | | public void OnSubmit(string call) |
| | 198 | | { |
| 2 | 199 | | bool validString = !string.IsNullOrEmpty(call); |
| | 200 | |
|
| 2 | 201 | | if (call?.Length == 1 && (byte) call[0] == 11) //NOTE(Brian): Trim doesn't work. neither IsNullOrWhitespace. |
| | 202 | | { |
| 0 | 203 | | validString = false; |
| | 204 | | } |
| | 205 | |
|
| 2 | 206 | | if (validString) |
| | 207 | | { |
| | 208 | | // NOTE: we keep `ReportOnTextSubmitEvent` for backward compatibility (it won't be called for scenes usi |
| 2 | 209 | | Interface.WebInterface.ReportOnTextSubmitEvent(scene.sceneData.sceneNumber, model.onTextSubmit, call); |
| 2 | 210 | | Interface.WebInterface.ReportOnTextInputChangedTextEvent(scene.sceneData.sceneNumber, model.onTextChange |
| | 211 | | } |
| 0 | 212 | | else if (scene.isPersistent) // DCL UI Chat text input |
| | 213 | | { |
| 0 | 214 | | inputField.DeactivateInputField(); |
| 0 | 215 | | referencesContainer.mouseCatcher.LockCursor(); |
| | 216 | |
|
| | 217 | | // To avoid focusing the chat in the same frame we unfocused it |
| 0 | 218 | | referencesContainer.inputDetectionPausedFrames = 1; |
| | 219 | | } |
| 0 | 220 | | } |
| | 221 | |
|
| | 222 | | void UnsuscribeFromEvents() |
| | 223 | | { |
| 15 | 224 | | inputField.onSelect.RemoveAllListeners(); |
| 15 | 225 | | inputField.onDeselect.RemoveAllListeners(); |
| 15 | 226 | | inputField.onSubmit.RemoveAllListeners(); |
| 15 | 227 | | inputField.onValueChanged.RemoveAllListeners(); |
| 15 | 228 | | } |
| | 229 | |
|
| | 230 | | public override void Dispose() |
| | 231 | | { |
| 3 | 232 | | UnsuscribeFromEvents(); |
| 3 | 233 | | base.Dispose(); |
| 3 | 234 | | } |
| | 235 | |
|
| | 236 | | private void ApplyModelChanges(TMP_Text text, Model model) |
| | 237 | | { |
| 12 | 238 | | text.text = model.value; |
| | 239 | |
|
| 12 | 240 | | text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0); |
| 12 | 241 | | text.fontSize = (int) model.fontSize; |
| 12 | 242 | | text.richText = true; |
| 12 | 243 | | text.overflowMode = TextOverflowModes.Overflow; |
| | 244 | |
|
| 12 | 245 | | text.margin = |
| | 246 | | new Vector4 |
| | 247 | | ( |
| | 248 | | (int) model.paddingLeft, |
| | 249 | | (int) model.paddingTop, |
| | 250 | | (int) model.paddingRight, |
| | 251 | | (int) model.paddingBottom |
| | 252 | | ); |
| | 253 | |
|
| 12 | 254 | | text.alignment = TextShape.GetAlignment(model.vTextAlign, model.hTextAlign); |
| 12 | 255 | | text.lineSpacing = 0f; |
| | 256 | |
|
| 12 | 257 | | text.maxVisibleLines = int.MaxValue; |
| | 258 | |
|
| | 259 | |
|
| 12 | 260 | | text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing; |
| | 261 | |
|
| 12 | 262 | | if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0) |
| | 263 | | { |
| 0 | 264 | | text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON"); |
| 0 | 265 | | text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor); |
| 0 | 266 | | text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur); |
| | 267 | | } |
| 12 | 268 | | else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON")) |
| | 269 | | { |
| 0 | 270 | | text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON"); |
| | 271 | | } |
| | 272 | |
|
| 12 | 273 | | if (model.outlineWidth > 0f) |
| | 274 | | { |
| 0 | 275 | | text.fontSharedMaterial.EnableKeyword("OUTLINE_ON"); |
| 0 | 276 | | text.outlineWidth = model.outlineWidth; |
| 0 | 277 | | text.outlineColor = model.outlineColor; |
| | 278 | | } |
| 12 | 279 | | else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON")) |
| | 280 | | { |
| 0 | 281 | | text.fontSharedMaterial.DisableKeyword("OUTLINE_ON"); |
| | 282 | | } |
| 12 | 283 | | } |
| | 284 | | } |
| | 285 | | } |