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