| | 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 TextShape.Model textModel; |
| | 16 | | public string placeholder; |
| 34 | 17 | | public Color placeholderColor = Color.white; |
| 34 | 18 | | public Color focusedBackground = Color.black; |
| | 19 | |
|
| | 20 | | public string onTextSubmit; |
| | 21 | | public string onChanged; |
| | 22 | | public string onFocus; |
| | 23 | | public string onBlur; |
| | 24 | |
|
| | 25 | | public override BaseModel GetDataFromJSON(string json) |
| | 26 | | { |
| 12 | 27 | | Model model = Utils.SafeFromJson<Model>(json); |
| 12 | 28 | | model.textModel = Utils.SafeFromJson<TextShape.Model>(json); |
| 12 | 29 | | return model; |
| | 30 | | } |
| | 31 | | } |
| | 32 | |
|
| 10 | 33 | | public override string referencesContainerPrefabName => "UIInputText"; |
| | 34 | |
|
| 0 | 35 | | public TMP_Text tmpText => referencesContainer.text; |
| 0 | 36 | | public TMP_InputField inputField => referencesContainer.inputField; |
| 0 | 37 | | public RectTransform rectTransform => referencesContainer.rectTransform; |
| | 38 | |
|
| 30 | 39 | | public UIInputText() { model = new Model(); } |
| | 40 | |
|
| 0 | 41 | | public override int GetClassId() { return (int) CLASS_ID.UI_INPUT_TEXT_SHAPE; } |
| | 42 | |
|
| | 43 | | public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null) |
| | 44 | | { |
| 0 | 45 | | Debug.LogError( |
| | 46 | | "Aborted UIContainerRectShape attachment to an entity. UIShapes shouldn't be attached to entities."); |
| 0 | 47 | | } |
| | 48 | |
|
| 0 | 49 | | public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null) { } |
| | 50 | |
|
| | 51 | | public override IEnumerator ApplyChanges(BaseModel newModel) |
| | 52 | | { |
| | 53 | | //NOTE(Brian): We have to serialize twice now, but in the future we should fix the |
| | 54 | | // client data structure to be like this, so we can serialize all of it in one shot. |
| 12 | 55 | | model = (Model) newModel; |
| | 56 | |
|
| 12 | 57 | | inputField.textViewport = referencesContainer.rectTransform; |
| | 58 | |
|
| 12 | 59 | | UnsuscribeFromEvents(); |
| | 60 | |
|
| 12 | 61 | | inputField.onSelect.AddListener(OnFocus); |
| 12 | 62 | | inputField.onDeselect.AddListener(OnBlur); |
| 12 | 63 | | inputField.onSubmit.AddListener(OnSubmit); |
| 12 | 64 | | inputField.onValueChanged.AddListener(OnChanged); |
| | 65 | |
|
| 12 | 66 | | yield return DCL.Components.TextShape.ApplyModelChanges(scene, tmpText, model.textModel); |
| | 67 | |
|
| 12 | 68 | | inputField.text = model.placeholder; |
| 12 | 69 | | inputField.textComponent.color = new Color(model.placeholderColor.r, model.placeholderColor.g, |
| | 70 | | model.placeholderColor.b, model.placeholderColor.a); |
| 12 | 71 | | referencesContainer.bgImage.color = new Color(model.focusedBackground.r, model.focusedBackground.g, |
| | 72 | | model.focusedBackground.b, model.focusedBackground.a); |
| 12 | 73 | | } |
| | 74 | |
|
| | 75 | | public void OnFocus(string call) |
| | 76 | | { |
| 3 | 77 | | if (inputField.text == model.placeholder) |
| | 78 | | { |
| 2 | 79 | | inputField.text = ""; |
| | 80 | | } |
| | 81 | |
|
| 3 | 82 | | inputField.customCaretColor = true; |
| 3 | 83 | | inputField.caretColor = Color.white; |
| 3 | 84 | | Interface.WebInterface.ReportOnFocusEvent(scene.sceneData.id, model.onFocus); |
| 3 | 85 | | } |
| | 86 | |
|
| 34 | 87 | | public void OnChanged(string changedText) { Interface.WebInterface.ReportOnTextInputChangedEvent(scene.sceneData |
| | 88 | |
|
| | 89 | | public void OnBlur(string call) |
| | 90 | | { |
| 1 | 91 | | HideCaret(); |
| 1 | 92 | | Interface.WebInterface.ReportOnBlurEvent(scene.sceneData.id, model.onBlur); |
| 1 | 93 | | } |
| | 94 | |
|
| | 95 | | public void HideCaret() |
| | 96 | | { |
| 1 | 97 | | if (string.IsNullOrEmpty(inputField.text)) |
| | 98 | | { |
| 1 | 99 | | inputField.text = model.placeholder; |
| | 100 | | } |
| | 101 | |
|
| 1 | 102 | | inputField.customCaretColor = true; |
| 1 | 103 | | inputField.caretColor = Color.clear; |
| 1 | 104 | | inputField.selectionAnchorPosition = 0; |
| 1 | 105 | | inputField.selectionFocusPosition = 0; |
| 1 | 106 | | inputField.selectionStringAnchorPosition = 0; |
| 1 | 107 | | inputField.selectionStringFocusPosition = 0; |
| 1 | 108 | | } |
| | 109 | |
|
| | 110 | | public void OnSubmit(string call) |
| | 111 | | { |
| 1 | 112 | | bool validString = !string.IsNullOrEmpty(tmpText.text); |
| | 113 | |
|
| 1 | 114 | | if (tmpText.text.Length == 1 && (byte) tmpText.text[0] == 11) //NOTE(Brian): Trim doesn't work. neither IsNu |
| | 115 | | { |
| 0 | 116 | | validString = false; |
| | 117 | | } |
| | 118 | |
|
| 1 | 119 | | if (validString) |
| | 120 | | { |
| 1 | 121 | | Interface.WebInterface.ReportOnTextSubmitEvent(scene.sceneData.id, model.onTextSubmit, tmpText.text); |
| | 122 | |
|
| 1 | 123 | | ForceFocus(); |
| 1 | 124 | | } |
| 0 | 125 | | else if (scene.isPersistent) // DCL UI Chat text input |
| | 126 | | { |
| 0 | 127 | | inputField.DeactivateInputField(); |
| 0 | 128 | | referencesContainer.mouseCatcher.LockCursor(); |
| | 129 | |
|
| | 130 | | // To avoid focusing the chat in the same frame we unfocused it |
| 0 | 131 | | referencesContainer.inputDetectionPausedFrames = 1; |
| | 132 | | } |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | public void ForceFocus() |
| | 136 | | { |
| 1 | 137 | | inputField.text = ""; |
| 1 | 138 | | inputField.caretColor = Color.white; |
| 1 | 139 | | inputField.Select(); |
| 1 | 140 | | inputField.ActivateInputField(); |
| 1 | 141 | | } |
| | 142 | |
|
| | 143 | | void UnsuscribeFromEvents() |
| | 144 | | { |
| 25 | 145 | | inputField.onSelect.RemoveAllListeners(); |
| 25 | 146 | | inputField.onDeselect.RemoveAllListeners(); |
| 25 | 147 | | inputField.onSubmit.RemoveAllListeners(); |
| 25 | 148 | | inputField.onValueChanged.RemoveAllListeners(); |
| 25 | 149 | | } |
| | 150 | |
|
| | 151 | | public override void Dispose() |
| | 152 | | { |
| 13 | 153 | | UnsuscribeFromEvents(); |
| 13 | 154 | | base.Dispose(); |
| 13 | 155 | | } |
| | 156 | | } |
| | 157 | | } |