diff options
author | chai <chaifix@163.com> | 2020-12-30 20:59:04 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2020-12-30 20:59:04 +0800 |
commit | e9ea621b93fbb58d9edfca8375918791637bbd52 (patch) | |
tree | 19ce3b1c1f2d51eda6878c9d0f2c9edc27f13650 /Client/Assembly-CSharp/TextBox.cs |
+init
Diffstat (limited to 'Client/Assembly-CSharp/TextBox.cs')
-rw-r--r-- | Client/Assembly-CSharp/TextBox.cs | 257 |
1 files changed, 257 insertions, 0 deletions
diff --git a/Client/Assembly-CSharp/TextBox.cs b/Client/Assembly-CSharp/TextBox.cs new file mode 100644 index 0000000..29e98ab --- /dev/null +++ b/Client/Assembly-CSharp/TextBox.cs @@ -0,0 +1,257 @@ +using System; +using System.Collections.Generic; +using System.Text; +using UnityEngine; +using UnityEngine.UI; + +public class TextBox : MonoBehaviour, IFocusHolder +{ + public float TextHeight + { + get + { + return this.outputText.Height; + } + } + + public static readonly HashSet<char> SymbolChars = new HashSet<char> + { + '?', + '!', + ',', + '.', + '\'', + ':', + ';', + '(', + ')', + '/', + '\\', + '%', + '^', + '&', + '-', + '=' + }; + + public string text; + + private string compoText = ""; + + public int characterLimit = -1; + + [SerializeField] + private TextRenderer outputText; + + public SpriteRenderer Background; + + public MeshRenderer Pipe; + + private float pipeBlinkTimer; + + public bool ClearOnFocus; + + public bool ForceUppercase; + + public Button.ButtonClickedEvent OnEnter; + + public Button.ButtonClickedEvent OnChange; + + public Button.ButtonClickedEvent OnFocusLost; + + private TouchScreenKeyboard keyboard; + + public bool AllowSymbols; + + public bool IpMode; + + private Collider2D[] colliders; + + private bool hasFocus; + + private StringBuilder tempTxt = new StringBuilder(); + + public void Start() + { + this.colliders = base.GetComponents<Collider2D>(); + DestroyableSingleton<PassiveButtonManager>.Instance.RegisterOne(this); + if (this.Pipe) + { + this.Pipe.enabled = false; + } + } + + public void OnDestroy() + { + if (this.keyboard != null) + { + this.keyboard.active = false; + this.keyboard = null; + } + if (DestroyableSingleton<PassiveButtonManager>.InstanceExists) + { + DestroyableSingleton<PassiveButtonManager>.Instance.RemoveOne(this); + } + } + + public void Clear() + { + this.SetText(string.Empty, string.Empty); + } + + public void Update() + { + if (!this.hasFocus) + { + return; + } + string inputString = Input.inputString; + if (inputString.Length > 0 || this.compoText != Input.compositionString) + { + if (this.text == null || this.text == "Enter Name") + { + this.text = ""; + } + this.SetText(this.text + inputString, Input.compositionString); + } + if (this.Pipe && this.hasFocus) + { + this.pipeBlinkTimer += Time.deltaTime * 2f; + this.Pipe.enabled = ((int)this.pipeBlinkTimer % 2 == 0); + } + } + + public void GiveFocus() + { + Input.imeCompositionMode = IMECompositionMode.On; + if (this.hasFocus) + { + return; + } + if (this.ClearOnFocus) + { + this.text = string.Empty; + this.compoText = string.Empty; + this.outputText.Text = string.Empty; + } + this.hasFocus = true; + if (TouchScreenKeyboard.isSupported) + { + this.keyboard = TouchScreenKeyboard.Open(this.text); + } + if (this.Background) + { + this.Background.color = Color.green; + } + this.pipeBlinkTimer = 0f; + if (this.Pipe) + { + this.Pipe.transform.localPosition = this.outputText.CursorPos; + } + } + + public void LoseFocus() + { + if (!this.hasFocus) + { + return; + } + Input.imeCompositionMode = IMECompositionMode.Off; + if (this.compoText.Length > 0) + { + this.SetText(this.text + this.compoText, ""); + this.compoText = string.Empty; + } + this.hasFocus = false; + if (this.keyboard != null) + { + this.keyboard.active = false; + this.keyboard = null; + } + if (this.Background) + { + this.Background.color = Color.white; + } + if (this.Pipe) + { + this.Pipe.enabled = false; + } + this.OnFocusLost.Invoke(); + } + + public bool CheckCollision(Vector2 pt) + { + for (int i = 0; i < this.colliders.Length; i++) + { + if (this.colliders[i].OverlapPoint(pt)) + { + return true; + } + } + return false; + } + + public void SetText(string input, string inputCompo = "") + { + bool flag = false; + char c = ' '; + this.tempTxt.Clear(); + foreach (char c2 in input) + { + if (c != ' ' || c2 != ' ') + { + if (c2 == '\r' || c2 == '\n') + { + flag = true; + } + if (c2 == '\b') + { + this.tempTxt.Length = Math.Max(this.tempTxt.Length - 1, 0); + } + if (this.ForceUppercase) + { + c2 = char.ToUpperInvariant(c2); + } + if (this.IsCharAllowed(c2)) + { + this.tempTxt.Append(c2); + c = c2; + } + } + } + if (this.characterLimit > 0) + { + this.tempTxt.Length = Math.Min(this.tempTxt.Length, this.characterLimit); + } + input = this.tempTxt.ToString(); + if (!input.Equals(this.text) || !inputCompo.Equals(this.compoText)) + { + this.text = input; + this.compoText = inputCompo; + this.outputText.Text = this.text + "[FF0000FF]" + this.compoText + "[]"; + this.outputText.RefreshMesh(); + if (this.keyboard != null) + { + this.keyboard.text = this.text; + } + this.OnChange.Invoke(); + } + if (flag) + { + this.OnEnter.Invoke(); + } + if (this.Pipe) + { + this.Pipe.transform.localPosition = this.outputText.CursorPos; + } + } + + public bool IsCharAllowed(char i) + { + if (this.IpMode) + { + return (i >= '0' && i <= '9') || i == '.'; + } + return i == ' ' || (i >= 'A' && i <= 'Z') || (i >= 'a' && i <= 'z') || (i >= '0' && i <= '9') || (i >= 'À' && i <= 'ÿ') || (i >= 'Ѐ' && i <= 'џ') || (i >= 'ㄱ' && i <= 'ㆎ') || (i >= '가' && i <= '힣') || (this.AllowSymbols && TextBox.SymbolChars.Contains(i)); + } +} |