123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- using UnityEngine.Serialization;
- using UnityEngine.UI;
- namespace CommonUnity3D.UGUI
- {
- public class TextLayerInputField : DisplayNodeInteractive, IEventSystemHandler, IUpdateSelectedHandler, ISubmitHandler, ICanvasElement, IInputField
- {
- //--------------------------------------------------------------------
- // property //
- private string m_Text = string.Empty;
- private char m_AsteriskChar = '*';
- private int m_CharacterLimit = 100;
- private InputField.ContentType m_ContentType = InputField.ContentType.Standard;
- private InputField.CharacterValidation m_CharacterValidation = InputField.CharacterValidation.None;
- private InputField.InputType m_InputType = InputField.InputType.Standard;
- private InputField.LineType m_LineType = InputField.LineType.SingleLine;
- private TouchScreenKeyboardType m_KeyboardType = TouchScreenKeyboardType.Default;
- private ITextComponent m_TextComponent;
- private Graphic m_Placeholder;
- private bool m_ShowCaret = true;
- private float m_CaretBlinkRate = 0.4f;
- public Action<string> event_EndEdit { get; set; }
- public Action<string> event_ValueChanged { get; set; }
- //--------------------------------------------------------------------
- // runtime //
- private Event m_ProcessingEvent = new Event();
- private TouchScreenKeyboard m_Keyboard = null;
- private string m_OriginalText = string.Empty;
- private bool m_WasCanceled = false;
- private bool m_ShouldActivateNextUpdate = false;
- private bool m_AllowInput = false;
- private CaretSprite m_CaretSprite;
- //--------------------------------------------------------------------
- public string Text
- {
- get
- {
- if (this.m_Keyboard != null && this.m_Keyboard.active && !this.InPlaceEditing() && EventSystem.current.currentSelectedGameObject == base.gameObject)
- {
- return this.m_Keyboard.text;
- }
- return this.m_Text;
- }
- set
- {
- if (this.Text == value)
- {
- return;
- }
- this.m_Text = value;
- if (!Application.isPlaying)
- {
- this.SendOnValueChangedAndUpdateLabel();
- return;
- }
- else
- {
- if (this.m_Keyboard != null)
- {
- this.m_Keyboard.text = this.m_Text;
- }
- this.SendOnValueChangedAndUpdateLabel();
- }
- }
- }
- public bool isFocused
- {
- get { return this.m_AllowInput; }
- }
- public InputField.InputType inputType
- {
- get { return m_InputType; }
- set { m_InputType = value; }
- }
- public InputField.ContentType contentType
- {
- get { return m_ContentType; }
- set { m_ContentType = value; }
- }
- public InputField.LineType lineType
- {
- get { return this.m_LineType; }
- set
- {
- if (this.m_LineType != value)
- {
- this.m_LineType = value;
- this.SetToCustomIfContentTypeIsNot(new InputField.ContentType[]
- {
- InputField.ContentType.Standard,
- InputField.ContentType.Autocorrected
- });
- }
- }
- }
- public bool multiLine
- {
- get { return m_LineType != InputField.LineType.SingleLine; }
- }
- public TouchScreenKeyboardType keyboardType
- {
- get { return m_KeyboardType; }
- set { m_KeyboardType = value; }
- }
- public int characterLimit
- {
- get { return m_CharacterLimit; }
- set { m_CharacterLimit = value; }
- }
- public InputField.CharacterValidation characterValidation
- {
- get { return m_CharacterValidation; }
- set { m_CharacterValidation = value; }
- }
- public char asteriskChar
- {
- get { return this.m_AsteriskChar; }
- set { this.m_AsteriskChar = value; }
- }
- public bool isShowCaret
- {
- get { return this.m_ShowCaret; }
- set { this.m_ShowCaret = value; }
- }
- public float caretBlinkRate
- {
- get { return m_CaretBlinkRate; }
- set { m_CaretBlinkRate = value; }
- }
- //--------------------------------------------------------------------
- public ITextComponent TextComponent
- {
- get { return m_TextComponent; }
- set { m_TextComponent = value; }
- }
- public Graphic Placeholder
- {
- get { return m_Placeholder; }
- set { m_Placeholder = value; }
- }
- //--------------------------------------------------------------------
- public override void OnDeselect(BaseEventData eventData)
- {
- base.OnDeselect(eventData);
- this.DeactivateInputField();
- }
- public override void OnSelect(BaseEventData eventData)
- {
- base.OnSelect(eventData);
- this.ActivateInputField();
- }
- public override void OnPointerClick(PointerEventData eventData)
- {
- base.OnPointerClick(eventData);
- this.ActivateInputField();
- }
- public override void OnPointerDown(PointerEventData eventData)
- {
- EventSystem.current.SetSelectedGameObject(base.gameObject, eventData);
- bool allowInput = this.m_AllowInput;
- base.OnPointerDown(eventData);
- if (!this.InPlaceEditing() && (this.m_Keyboard == null || !this.m_Keyboard.active))
- {
- this.OnSelect(eventData);
- return;
- }
- this.UpdateLabel();
- eventData.Use();
- }
- #if UNITY_EDITOR
- protected override void OnValidate()
- {
- base.OnValidate();
- this.EnforceContentType();
- if (!this.IsActive())
- {
- return;
- }
- this.UpdateLabel();
- }
- #endif
- //--------------------------------------------------------------------
- #region _text_append_
- protected virtual bool Append(char input)
- {
- if (input == '\0')
- {
- return false;
- }
- if (!this.InPlaceEditing())
- {
- return false;
- }
- if (this.characterValidation != InputField.CharacterValidation.None)
- {
- input = this.Validate(this.Text, input);
- }
- if (input == '\0')
- {
- return false;
- }
- string add = input.ToString();
- if (UnityEngine.Application.platform == RuntimePlatform.IPhonePlayer)
- {
- }
- else if (this.characterLimit > 0 && (this.Text.Length + add.Length) >= this.characterLimit)
- {
- return false;
- }
- this.m_Text += add;
- this.SendOnValueChanged();
- return true;
- }
- protected virtual bool DeleteAll()
- {
- if (m_Text.Length > 0)
- {
- this.m_Text = string.Empty;
- this.SendOnValueChanged();
- return true;
- }
- return false;
- }
- protected virtual bool Delete()
- {
- if (m_Text.Length > 0)
- {
- this.m_Text = m_Text.Substring(0, m_Text.Length - 1);
- this.SendOnValueChanged();
- return true;
- }
- return false;
- }
- protected virtual bool IsValidChar(char c)
- {
- return c != '\u007f';
- }
- protected char Validate(string text, char ch)
- {
- int pos = text.Length;
- if (this.characterValidation == InputField.CharacterValidation.None || !base.enabled)
- {
- return ch;
- }
- if (this.characterValidation == InputField.CharacterValidation.Integer || this.characterValidation == InputField.CharacterValidation.Decimal)
- {
- if (pos != 0 || text.Length <= 0 || text[0] != '-')
- {
- if (ch >= '0' && ch <= '9')
- {
- return ch;
- }
- if (ch == '-' && pos == 0)
- {
- return ch;
- }
- if (ch == '.' && this.characterValidation == InputField.CharacterValidation.Decimal && !text.Contains("."))
- {
- return ch;
- }
- }
- }
- else
- {
- if (this.characterValidation == InputField.CharacterValidation.Alphanumeric)
- {
- if (ch >= 'A' && ch <= 'Z')
- {
- return ch;
- }
- if (ch >= 'a' && ch <= 'z')
- {
- return ch;
- }
- if (ch >= '0' && ch <= '9')
- {
- return ch;
- }
- }
- else
- {
- if (this.characterValidation == InputField.CharacterValidation.Name)
- {
- char c = (text.Length <= 0) ? ' ' : text[Mathf.Clamp(pos, 0, text.Length - 1)];
- char c2 = (text.Length <= 0) ? '\n' : text[Mathf.Clamp(pos + 1, 0, text.Length - 1)];
- if (char.IsLetter(ch))
- {
- if (char.IsLower(ch) && c == ' ')
- {
- return char.ToUpper(ch);
- }
- if (char.IsUpper(ch) && c != ' ' && c != '\'')
- {
- return char.ToLower(ch);
- }
- return ch;
- }
- else
- {
- if (ch == '\'')
- {
- if (c != ' ' && c != '\'' && c2 != '\'' && !text.Contains("'"))
- {
- return ch;
- }
- }
- else
- {
- if (ch == ' ' && c != ' ' && c != '\'' && c2 != ' ' && c2 != '\'')
- {
- return ch;
- }
- }
- }
- }
- else
- {
- if (this.characterValidation == InputField.CharacterValidation.EmailAddress)
- {
- if (ch >= 'A' && ch <= 'Z')
- {
- return ch;
- }
- if (ch >= 'a' && ch <= 'z')
- {
- return ch;
- }
- if (ch >= '0' && ch <= '9')
- {
- return ch;
- }
- if (ch == '@' && text.IndexOf('@') == -1)
- {
- return ch;
- }
- if ("!#$%&'*+-/=?^_`{|}~".IndexOf(ch) != -1)
- {
- return ch;
- }
- if (ch == '.')
- {
- char c3 = (text.Length <= 0) ? ' ' : text[Mathf.Clamp(pos, 0, text.Length - 1)];
- char c4 = (text.Length <= 0) ? '\n' : text[Mathf.Clamp(pos + 1, 0, text.Length - 1)];
- if (c3 != '.' && c4 != '.')
- {
- return ch;
- }
- }
- }
- }
- }
- }
- return '\0';
- }
- protected void UpdateLabel()
- {
- if (this.m_TextComponent != null)
- {
- string text = this.Text;
- string text2 = this.Text;
- if (this.inputType == InputField.InputType.Password)
- {
- text2 = new string(this.asteriskChar, text.Length);
- }
- bool flag = string.IsNullOrEmpty(text);
- if (this.m_Placeholder != null)
- {
- this.m_Placeholder.enabled = flag;
- }
- this.m_TextComponent.Text = text2;
- }
- }
- //--------------------------------------------------------------------
- private void SendOnValueChangedAndUpdateLabel()
- {
- this.SendOnValueChanged();
- this.UpdateLabel();
- }
- private void SendOnValueChanged()
- {
- if (this.event_ValueChanged != null)
- {
- this.event_ValueChanged.Invoke(this.Text);
- }
- }
- protected void SendOnSubmit()
- {
- if (UnityEngine.Application.platform == RuntimePlatform.IPhonePlayer)
- {
- if (this.characterLimit > 0 && this.m_Text.Length > this.characterLimit)
- {
- this.m_Text = this.m_Text.Substring(0, this.characterLimit);
- }
- }
- if (this.event_EndEdit != null)
- {
- this.event_EndEdit.Invoke(this.m_Text);
- }
- }
- private void EnforceContentType()
- {
- switch (this.contentType)
- {
- case InputField.ContentType.Standard:
- this.m_InputType = InputField.InputType.Standard;
- this.m_KeyboardType = TouchScreenKeyboardType.Default;
- this.m_CharacterValidation = InputField.CharacterValidation.None;
- return;
- case InputField.ContentType.Autocorrected:
- this.m_InputType = InputField.InputType.AutoCorrect;
- this.m_KeyboardType = TouchScreenKeyboardType.Default;
- this.m_CharacterValidation = InputField.CharacterValidation.None;
- return;
- case InputField.ContentType.IntegerNumber:
- this.m_LineType = InputField.LineType.SingleLine;
- this.m_InputType = InputField.InputType.Standard;
- this.m_KeyboardType = TouchScreenKeyboardType.NumberPad;
- this.m_CharacterValidation = InputField.CharacterValidation.Integer;
- return;
- case InputField.ContentType.DecimalNumber:
- this.m_LineType = InputField.LineType.SingleLine;
- this.m_InputType = InputField.InputType.Standard;
- this.m_KeyboardType = TouchScreenKeyboardType.NumbersAndPunctuation;
- this.m_CharacterValidation = InputField.CharacterValidation.Decimal;
- return;
- case InputField.ContentType.Alphanumeric:
- this.m_LineType = InputField.LineType.SingleLine;
- this.m_InputType = InputField.InputType.Standard;
- this.m_KeyboardType = TouchScreenKeyboardType.ASCIICapable;
- this.m_CharacterValidation = InputField.CharacterValidation.Alphanumeric;
- return;
- case InputField.ContentType.Name:
- this.m_LineType = InputField.LineType.SingleLine;
- this.m_InputType = InputField.InputType.Standard;
- this.m_KeyboardType = TouchScreenKeyboardType.Default;
- this.m_CharacterValidation = InputField.CharacterValidation.Name;
- return;
- case InputField.ContentType.EmailAddress:
- this.m_LineType = InputField.LineType.SingleLine;
- this.m_InputType = InputField.InputType.Standard;
- this.m_KeyboardType = TouchScreenKeyboardType.EmailAddress;
- this.m_CharacterValidation = InputField.CharacterValidation.EmailAddress;
- return;
- case InputField.ContentType.Password:
- this.m_LineType = InputField.LineType.SingleLine;
- this.m_InputType = InputField.InputType.Password;
- this.m_KeyboardType = TouchScreenKeyboardType.Default;
- this.m_CharacterValidation = InputField.CharacterValidation.None;
- return;
- case InputField.ContentType.Pin:
- this.m_LineType = InputField.LineType.SingleLine;
- this.m_InputType = InputField.InputType.Password;
- this.m_KeyboardType = TouchScreenKeyboardType.NumberPad;
- this.m_CharacterValidation = InputField.CharacterValidation.Integer;
- return;
- default:
- return;
- }
- }
- private void SetToCustomIfContentTypeIsNot(params InputField.ContentType[] allowedContentTypes)
- {
- if (this.contentType == InputField.ContentType.Custom)
- {
- return;
- }
- for (int i = 0; i < allowedContentTypes.Length; i++)
- {
- if (this.contentType == allowedContentTypes[i])
- {
- return;
- }
- }
- this.contentType = InputField.ContentType.Custom;
- }
- private void SetToCustom()
- {
- if (this.contentType == InputField.ContentType.Custom)
- {
- return;
- }
- this.contentType = InputField.ContentType.Custom;
- }
- #endregion
- //--------------------------------------------------------------------
- #region _keyboard_
- private bool InPlaceEditing()
- {
- return !TouchScreenKeyboard.isSupported;
- }
- public void ProcessEvent(Event e)
- {
- this.KeyPressed(e);
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="evt"></param>
- /// <returns>finish</returns>
- protected bool KeyPressed(Event evt)
- {
- KeyCode keyCode = evt.keyCode;
- switch (evt.keyCode)
- {
- case KeyCode.KeypadEnter:
- if (this.lineType != InputField.LineType.MultiLineNewline)
- {
- return true;
- }
- break;
- case KeyCode.Delete:
- if (DeleteAll())
- {
- this.UpdateLabel();
- }
- return false;
- case KeyCode.Backspace:
- if (this.Delete())
- {
- this.UpdateLabel();
- }
- return false;
- }
- char c = evt.character;
- if (!this.multiLine && (c == '\t' || c == '\r' || c == '\n'))
- {
- return false;
- }
- if (c == '\r' || c == '\u0003')
- {
- c = '\n';
- }
- if (this.IsValidChar(c))
- {
- this.Append(c);
- this.UpdateLabel();
- }
- return false;
- }
- public virtual void OnUpdateSelected(BaseEventData eventData)
- {
- if (!this.isFocused)
- {
- return;
- }
- while (Event.PopEvent(this.m_ProcessingEvent))
- {
- if (this.m_ProcessingEvent.rawType == EventType.KeyDown)
- {
- bool finish = this.KeyPressed(this.m_ProcessingEvent);
- if (finish)
- {
- this.DeactivateInputField();
- break;
- }
- }
- }
- eventData.Use();
- }
- protected virtual void OnTouchKeoboardFinish(TouchScreenKeyboard keyboard)
- {
- string text = keyboard.text;
- if (this.m_Text != text)
- {
- this.m_Text = string.Empty;
- for (int i = 0; i < text.Length; i++)
- {
- char c = text[i];
- if (c == '\r' || c == '\u0003')
- {
- c = '\n';
- }
- if (this.characterValidation != InputField.CharacterValidation.None)
- {
- c = this.Validate(this.m_Text, c);
- }
- if (c == '\n')
- {
- keyboard.text = this.m_Text;
- this.OnDeselect(null);
- return;
- }
- if (c != '\0')
- {
- this.m_Text += c;
- }
- }
- if (UnityEngine.Application.platform == RuntimePlatform.IPhonePlayer)
- {
- }
- else if (this.characterLimit > 0 && this.m_Text.Length > this.characterLimit)
- {
- this.m_Text = this.m_Text.Substring(0, this.characterLimit);
- }
- int length = this.m_Text.Length;
- if (this.m_Text != text)
- {
- keyboard.text = this.m_Text;
- }
- this.SendOnValueChangedAndUpdateLabel();
- }
- }
- //--------------------------------------------------------------------
- public void ActivateInputField()
- {
- if (this.m_TextComponent == null || !this.IsActive() || !this.IsInteractable())
- {
- return;
- }
- if (this.m_Keyboard != null && !this.m_Keyboard.active)
- {
- this.m_Keyboard.active = true;
- this.m_Keyboard.text = this.m_Text;
- }
- this.m_ShouldActivateNextUpdate = true;
- }
- private void ActivateInputFieldInternal()
- {
- if (EventSystem.current.currentSelectedGameObject != base.gameObject)
- {
- EventSystem.current.SetSelectedGameObject(base.gameObject);
- }
- if (TouchScreenKeyboard.isSupported)
- {
- this.m_Keyboard = TouchScreenKeyboard.Open(
- this.m_Text,
- this.keyboardType,
- this.inputType == InputField.InputType.AutoCorrect,
- this.multiLine,
- this.inputType == InputField.InputType.Password);
- }
- else
- {
- Input.imeCompositionMode = IMECompositionMode.On;
- }
- this.m_OriginalText = this.Text;
- this.m_WasCanceled = false;
- this.m_AllowInput = true;
- }
- public void DeactivateInputField()
- {
- if (!this.m_AllowInput)
- {
- return;
- }
- this.m_AllowInput = false;
- if (this.m_TextComponent != null && this.IsInteractable())
- {
- if (this.m_WasCanceled)
- {
- this.Text = this.m_OriginalText;
- }
- if (this.m_Keyboard != null)
- {
- this.m_Keyboard.active = false;
- this.m_Keyboard = null;
- }
- this.SendOnSubmit();
- Input.imeCompositionMode = IMECompositionMode.Auto;
- }
- }
- #endregion
- //--------------------------------------------------------------------
- protected virtual void LateUpdate()
- {
- if (this.m_ShouldActivateNextUpdate)
- {
- if (!this.isFocused)
- {
- this.ActivateInputFieldInternal();
- this.m_ShouldActivateNextUpdate = false;
- return;
- }
- this.m_ShouldActivateNextUpdate = false;
- }
- UpdateCaret();
- if (this.InPlaceEditing() || !this.isFocused)
- {
- return;
- }
- if (this.m_Keyboard == null || !this.m_Keyboard.active)
- {
- if (this.m_Keyboard != null && this.m_Keyboard.wasCanceled)
- {
- this.m_WasCanceled = true;
- }
- this.OnDeselect(null);
- return;
- }
- this.OnTouchKeoboardFinish(m_Keyboard);
- if (this.m_Keyboard.done)
- {
- if (this.m_Keyboard.wasCanceled)
- {
- this.m_WasCanceled = true;
- }
- this.OnDeselect(null);
- }
- }
- //--------------------------------------------------------------------
- public void GraphicUpdateComplete()
- {
- }
- public void LayoutComplete()
- {
- }
- public void OnSubmit(BaseEventData eventData)
- {
- }
- public void Rebuild(CanvasUpdate executing)
- {
- }
- //--------------------------------------------------------------------
- private void UpdateCaret()
- {
- if (isShowCaret && isFocused)
- {
- if (m_TextComponent != null)
- {
- if (m_CaretSprite == null)
- {
- m_CaretSprite = new CaretSprite();
- Binding.AddChild(m_CaretSprite);
- }
- var cb = m_TextComponent.LastCaretPosition;
- cb.x += m_TextComponent.Binding.X;
- cb.y += m_TextComponent.Binding.Y;
- m_CaretSprite.Bounds2D = cb;
- m_CaretSprite.Visible = (cb.width > 0);
- m_CaretSprite.UpdateCaret(m_CaretBlinkRate);
- }
- }
- else
- {
- if (m_CaretSprite != null)
- {
- m_CaretSprite.Visible = false;
- }
- }
- }
- private class CaretSprite : TintSprite
- {
- private float m_CaretSEC;
- private int m_CaretTimer;
- internal CaretSprite() : base("caret") { }
- internal bool UpdateCaret(float blinkRate)
- {
- var delta = UnityEngine.Time.deltaTime;
- m_CaretSEC += delta;
- if (m_CaretSEC > blinkRate)
- {
- m_CaretSEC = m_CaretSEC % blinkRate;
- m_CaretTimer++;
- }
- this.VisibleInParent = (m_CaretTimer % 2 == 0);
- return this.VisibleInParent;
- }
- }
- }
- }
|