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