| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System.Collections; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.Components |
| | 9 | | { |
| | 10 | | public class UIInputText : UIShape<UIInputTextRefContainer, UIInputText.Model> |
| | 11 | | { |
| | 12 | | [System.Serializable] |
| | 13 | | new public class Model : UIShape.Model |
| | 14 | | { |
| | 15 | | public float outlineWidth = 0f; |
| 34 | 16 | | public Color outlineColor = Color.white; |
| | 17 | |
|
| 34 | 18 | | public Color color = Color.white; |
| | 19 | |
|
| 34 | 20 | | public float fontSize = 100f; |
| | 21 | | public string font; |
| 34 | 22 | | public string value = ""; |
| 34 | 23 | | public string hTextAlign = "bottom"; |
| 34 | 24 | | public string vTextAlign = "left"; |
| | 25 | | public bool textWrapping = false; |
| | 26 | |
|
| | 27 | | public float shadowBlur = 0f; |
| | 28 | | public float shadowOffsetX = 0f; |
| | 29 | | public float shadowOffsetY = 0f; |
| 34 | 30 | | public Color shadowColor = new Color(1, 1, 1); |
| | 31 | |
|
| | 32 | | public float paddingTop = 0f; |
| | 33 | | public float paddingRight = 0f; |
| | 34 | | public float paddingBottom = 0f; |
| | 35 | | public float paddingLeft = 0f; |
| | 36 | |
|
| | 37 | | public string placeholder; |
| 34 | 38 | | public Color placeholderColor = Color.white; |
| 34 | 39 | | public Color focusedBackground = Color.black; |
| | 40 | |
|
| | 41 | | public string onTextSubmit; |
| | 42 | | public string onChanged; |
| | 43 | | public string onFocus; |
| | 44 | | public string onBlur; |
| | 45 | | public string onTextChanged; |
| | 46 | |
|
| | 47 | | public override BaseModel GetDataFromJSON(string json) |
| | 48 | | { |
| 12 | 49 | | Model model = Utils.SafeFromJson<Model>(json); |
| 12 | 50 | | return model; |
| | 51 | | } |
| | 52 | | } |
| | 53 | |
|
| 10 | 54 | | public override string referencesContainerPrefabName => "UIInputText"; |
| | 55 | |
|
| 0 | 56 | | public TMP_Text tmpText => referencesContainer.text; |
| 0 | 57 | | public TMP_InputField inputField => referencesContainer.inputField; |
| 0 | 58 | | public RectTransform rectTransform => referencesContainer.rectTransform; |
| | 59 | |
|
| 30 | 60 | | public UIInputText() { model = new Model(); } |
| | 61 | |
|
| 0 | 62 | | public override int GetClassId() { return (int) CLASS_ID.UI_INPUT_TEXT_SHAPE; } |
| | 63 | |
|
| | 64 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 65 | | { |
| 0 | 66 | | Debug.LogError( |
| | 67 | | "Aborted UIContainerRectShape attachment to an entity. UIShapes shouldn't be attached to entities."); |
| 0 | 68 | | } |
| | 69 | |
|
| 0 | 70 | | public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { } |
| | 71 | |
|
| | 72 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 73 | | { |
| | 74 | | //NOTE(Brian): We have to serialize twice now, but in the future we should fix the |
| | 75 | | // client data structure to be like this, so we can serialize all of it in one shot. |
| 12 | 76 | | model = (Model) newModel; |
| | 77 | |
|
| 12 | 78 | | inputField.textViewport = referencesContainer.rectTransform; |
| | 79 | |
|
| 12 | 80 | | UnsuscribeFromEvents(); |
| | 81 | |
|
| 12 | 82 | | inputField.onSelect.AddListener(OnFocus); |
| 12 | 83 | | inputField.onDeselect.AddListener(OnBlur); |
| 12 | 84 | | inputField.onSubmit.AddListener(OnSubmit); |
| 12 | 85 | | inputField.onValueChanged.AddListener(OnChanged); |
| | 86 | |
|
| | 87 | | // We avoid using even yield break; as this instruction skips a frame and we don't want that. |
| 12 | 88 | | if ( !DCLFont.IsFontLoaded(scene, model.font) ) |
| | 89 | | { |
| 0 | 90 | | yield return DCLFont.WaitUntilFontIsReady(scene, model.font); |
| | 91 | | } |
| | 92 | |
|
| 12 | 93 | | DCLFont.SetFontFromComponent(scene, model.font, referencesContainer.text); |
| 12 | 94 | | ApplyModelChanges(tmpText, model); |
| | 95 | |
|
| 12 | 96 | | inputField.text = model.placeholder; |
| 12 | 97 | | inputField.textComponent.color = new Color(model.placeholderColor.r, model.placeholderColor.g, |
| | 98 | | model.placeholderColor.b, model.placeholderColor.a); |
| 12 | 99 | | referencesContainer.bgImage.color = new Color(model.focusedBackground.r, model.focusedBackground.g, |
| | 100 | | model.focusedBackground.b, model.focusedBackground.a); |
| 12 | 101 | | } |
| | 102 | |
|
| | 103 | | public void OnFocus(string call) |
| | 104 | | { |
| 2 | 105 | | if (inputField.text == model.placeholder) |
| | 106 | | { |
| 2 | 107 | | inputField.text = ""; |
| | 108 | | } |
| | 109 | |
|
| 2 | 110 | | inputField.customCaretColor = true; |
| 2 | 111 | | inputField.caretColor = Color.white; |
| 2 | 112 | | Interface.WebInterface.ReportOnFocusEvent(scene.sceneData.id, model.onFocus); |
| 2 | 113 | | } |
| | 114 | |
|
| | 115 | | public void OnChanged(string changedText) |
| | 116 | | { |
| | 117 | | // NOTE: OSX is adding the ESC character at the end of the string when ESC is pressed |
| | 118 | | #if UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX |
| | 119 | | if (changedText.Length > 0 && changedText[changedText.Length - 1] == 27) |
| | 120 | | { |
| | 121 | | changedText = changedText.Substring(0, changedText.Length - 1); |
| | 122 | | inputField.SetTextWithoutNotify(changedText); |
| | 123 | | } |
| | 124 | | #endif |
| | 125 | |
|
| | 126 | | // NOTE: we keep `ReportOnTextInputChangedEvent` for backward compatibility (it won't be called for scenes u |
| 17 | 127 | | Interface.WebInterface.ReportOnTextInputChangedEvent(scene.sceneData.id, model.onChanged, changedText); |
| 17 | 128 | | Interface.WebInterface.ReportOnTextInputChangedTextEvent(scene.sceneData.id, model.onTextChanged, changedTex |
| 17 | 129 | | } |
| | 130 | |
|
| | 131 | | public void OnBlur(string call) |
| | 132 | | { |
| 2 | 133 | | HideCaret(); |
| 2 | 134 | | Interface.WebInterface.ReportOnBlurEvent(scene.sceneData.id, model.onBlur); |
| 2 | 135 | | } |
| | 136 | |
|
| | 137 | | public void HideCaret() |
| | 138 | | { |
| 2 | 139 | | if (string.IsNullOrEmpty(inputField.text)) |
| | 140 | | { |
| 2 | 141 | | inputField.text = model.placeholder; |
| | 142 | | } |
| | 143 | |
|
| 2 | 144 | | inputField.customCaretColor = true; |
| 2 | 145 | | inputField.caretColor = Color.clear; |
| 2 | 146 | | inputField.selectionAnchorPosition = 0; |
| 2 | 147 | | inputField.selectionFocusPosition = 0; |
| 2 | 148 | | inputField.selectionStringAnchorPosition = 0; |
| 2 | 149 | | inputField.selectionStringFocusPosition = 0; |
| 2 | 150 | | } |
| | 151 | |
|
| | 152 | | public void OnSubmit(string call) |
| | 153 | | { |
| 2 | 154 | | bool validString = !string.IsNullOrEmpty(call); |
| | 155 | |
|
| 2 | 156 | | if (call?.Length == 1 && (byte) call[0] == 11) //NOTE(Brian): Trim doesn't work. neither IsNullOrWhitespace. |
| | 157 | | { |
| 0 | 158 | | validString = false; |
| | 159 | | } |
| | 160 | |
|
| 2 | 161 | | if (validString) |
| | 162 | | { |
| | 163 | | // NOTE: we keep `ReportOnTextSubmitEvent` for backward compatibility (it won't be called for scenes usi |
| 2 | 164 | | Interface.WebInterface.ReportOnTextSubmitEvent(scene.sceneData.id, model.onTextSubmit, call); |
| 2 | 165 | | Interface.WebInterface.ReportOnTextInputChangedTextEvent(scene.sceneData.id, model.onTextChanged, call, |
| 2 | 166 | | } |
| 0 | 167 | | else if (scene.isPersistent) // DCL UI Chat text input |
| | 168 | | { |
| 0 | 169 | | inputField.DeactivateInputField(); |
| 0 | 170 | | referencesContainer.mouseCatcher.LockCursor(); |
| | 171 | |
|
| | 172 | | // To avoid focusing the chat in the same frame we unfocused it |
| 0 | 173 | | referencesContainer.inputDetectionPausedFrames = 1; |
| | 174 | | } |
| 0 | 175 | | } |
| | 176 | |
|
| | 177 | | void UnsuscribeFromEvents() |
| | 178 | | { |
| 15 | 179 | | inputField.onSelect.RemoveAllListeners(); |
| 15 | 180 | | inputField.onDeselect.RemoveAllListeners(); |
| 15 | 181 | | inputField.onSubmit.RemoveAllListeners(); |
| 15 | 182 | | inputField.onValueChanged.RemoveAllListeners(); |
| 15 | 183 | | } |
| | 184 | |
|
| | 185 | | public override void Dispose() |
| | 186 | | { |
| 3 | 187 | | UnsuscribeFromEvents(); |
| 3 | 188 | | base.Dispose(); |
| 3 | 189 | | } |
| | 190 | |
|
| | 191 | | private void ApplyModelChanges(TMP_Text text, Model model) |
| | 192 | | { |
| 12 | 193 | | text.text = model.value; |
| | 194 | |
|
| 12 | 195 | | text.color = new Color(model.color.r, model.color.g, model.color.b, model.visible ? model.opacity : 0); |
| 12 | 196 | | text.fontSize = (int) model.fontSize; |
| 12 | 197 | | text.richText = true; |
| 12 | 198 | | text.overflowMode = TextOverflowModes.Overflow; |
| | 199 | |
|
| 12 | 200 | | text.margin = |
| | 201 | | new Vector4 |
| | 202 | | ( |
| | 203 | | (int) model.paddingLeft, |
| | 204 | | (int) model.paddingTop, |
| | 205 | | (int) model.paddingRight, |
| | 206 | | (int) model.paddingBottom |
| | 207 | | ); |
| | 208 | |
|
| 12 | 209 | | text.alignment = TextShape.GetAlignment(model.vTextAlign, model.hTextAlign); |
| 12 | 210 | | text.lineSpacing = 0f; |
| | 211 | |
|
| 12 | 212 | | text.maxVisibleLines = int.MaxValue; |
| | 213 | |
|
| | 214 | |
|
| 12 | 215 | | text.enableWordWrapping = model.textWrapping && !text.enableAutoSizing; |
| | 216 | |
|
| 12 | 217 | | if (model.shadowOffsetX != 0 || model.shadowOffsetY != 0) |
| | 218 | | { |
| 0 | 219 | | text.fontSharedMaterial.EnableKeyword("UNDERLAY_ON"); |
| 0 | 220 | | text.fontSharedMaterial.SetColor("_UnderlayColor", model.shadowColor); |
| 0 | 221 | | text.fontSharedMaterial.SetFloat("_UnderlaySoftness", model.shadowBlur); |
| 0 | 222 | | } |
| 12 | 223 | | else if (text.fontSharedMaterial.IsKeywordEnabled("UNDERLAY_ON")) |
| | 224 | | { |
| 0 | 225 | | text.fontSharedMaterial.DisableKeyword("UNDERLAY_ON"); |
| | 226 | | } |
| | 227 | |
|
| 12 | 228 | | if (model.outlineWidth > 0f) |
| | 229 | | { |
| 0 | 230 | | text.fontSharedMaterial.EnableKeyword("OUTLINE_ON"); |
| 0 | 231 | | text.outlineWidth = model.outlineWidth; |
| 0 | 232 | | text.outlineColor = model.outlineColor; |
| 0 | 233 | | } |
| 12 | 234 | | else if (text.fontSharedMaterial.IsKeywordEnabled("OUTLINE_ON")) |
| | 235 | | { |
| 0 | 236 | | text.fontSharedMaterial.DisableKeyword("OUTLINE_ON"); |
| | 237 | | } |
| 12 | 238 | | } |
| | 239 | | } |
| | 240 | | } |