using System; 
 | 
using System.Collections.Generic; 
 | 
using System.Text; 
 | 
using System.Text.RegularExpressions; 
 | 
using UnityEngine.Events; 
 | 
using vnxbqy.UI; 
 | 
namespace UnityEngine.UI 
 | 
{ 
 | 
    [AddComponentMenu("UI/NumKeyBoard")] 
 | 
    [RequireComponent(typeof(RectTransform))] 
 | 
    public class NumKeyBoard : MonoBehaviour 
 | 
    { 
 | 
        [SerializeField] 
 | 
        NumKeyBoardType m_Type; 
 | 
        public NumKeyBoardType type 
 | 
        { 
 | 
            get { return m_Type; } 
 | 
        } 
 | 
        [SerializeField] 
 | 
        bool deActiveConfirm = false; 
 | 
        [SerializeField] 
 | 
        private uint m_Min; 
 | 
        public uint min 
 | 
        { 
 | 
            get { return m_Min; } 
 | 
            set { m_Min = value; } 
 | 
        } 
 | 
        [SerializeField] 
 | 
        private uint m_Max; 
 | 
        public uint max { get { return m_Max; } set { m_Max = value; } } 
 | 
        [SerializeField] 
 | 
        private int m_NumLimit; 
 | 
        public int limit { get { return m_NumLimit; } set { m_NumLimit = value; } } 
 | 
        [SerializeField] 
 | 
        private Button[] m_NumBtns = new Button[10]; 
 | 
        [SerializeField] 
 | 
        private Button okBtn; 
 | 
        [SerializeField] 
 | 
        private Button cancelBtn; 
 | 
        [Serializable] 
 | 
        public class NumKeyboardEvent : UnityEvent { } 
 | 
        private NumKeyboardEvent m_OnValueChange = new NumKeyboardEvent(); 
 | 
        public NumKeyboardEvent onValueChange 
 | 
        { 
 | 
            get { return m_OnValueChange; } 
 | 
        } 
 | 
        [Serializable] 
 | 
        public class NumKeyboardConfirmEvent : UnityEvent<bool> { } 
 | 
        private NumKeyboardConfirmEvent m_OnConfirm = new NumKeyboardConfirmEvent(); 
 | 
        public NumKeyboardConfirmEvent onConfirm 
 | 
        { 
 | 
            get { return m_OnConfirm; } 
 | 
        } 
 | 
        public bool displayOutValue = true; 
 | 
        private StringBuilder m_Value = new StringBuilder(); 
 | 
  
 | 
        [SerializeField] ClickScreenOtherSpace m_ClickOther; 
 | 
  
 | 
        public string Value 
 | 
        { 
 | 
            get { return m_Value.ToString(); } 
 | 
            set 
 | 
            { 
 | 
                switch (type) 
 | 
                { 
 | 
                    case NumKeyBoardType.Math: 
 | 
                        { 
 | 
                            if (!Regex.IsMatch(value, "[0-9]+")) 
 | 
                            { 
 | 
                                throw new Exception("No Correct Format!"); 
 | 
                            } 
 | 
                            m_IntValue = uint.Parse(value); 
 | 
                            m_RealIntValue = m_IntValue; 
 | 
                        } 
 | 
                        break; 
 | 
                    case NumKeyBoardType.Password: 
 | 
                        { 
 | 
                            if (!Regex.IsMatch(value, "[0-9]*")) 
 | 
                            { 
 | 
                                throw new Exception("No Correct Format!"); 
 | 
                            } 
 | 
                        } 
 | 
                        break; 
 | 
                } 
 | 
                m_Value.Length = 0; 
 | 
                m_Value.Append(value); 
 | 
                SetTargetTextValue(); 
 | 
            } 
 | 
        } 
 | 
        private uint m_IntValue = 0; 
 | 
        private uint m_RealIntValue = 0; 
 | 
        private bool resetValue = false; 
 | 
        /// <summary> 
 | 
        /// 目标显示文本组件 
 | 
        /// </summary> 
 | 
        public Text targetText; 
 | 
  
 | 
        private void Awake() 
 | 
        { 
 | 
            for (int i = 0; i < m_NumBtns.Length; i++) 
 | 
            { 
 | 
                if (m_NumBtns[i] != null) 
 | 
                { 
 | 
                    uint index = (uint)i; 
 | 
                    m_NumBtns[i].onClick.AddListener(() => 
 | 
                    { 
 | 
                        OnClickNum(index); 
 | 
                    }); 
 | 
                } 
 | 
            } 
 | 
            okBtn.onClick.AddListener(OnOkBtn); 
 | 
            cancelBtn.onClick.AddListener(OnCancelBtn); 
 | 
            ResetValue(); 
 | 
            if (m_ClickOther != null) 
 | 
            { 
 | 
                m_ClickOther.AddListener(OnOkBtn); 
 | 
            } 
 | 
        } 
 | 
  
 | 
        private void ResetValue() 
 | 
        { 
 | 
            switch (type) 
 | 
            { 
 | 
                case NumKeyBoardType.Math: 
 | 
                    m_IntValue = (uint)Mathf.Max(0, min); 
 | 
                    m_RealIntValue = m_IntValue; 
 | 
                    m_Value.Length = 0; 
 | 
                    m_Value.Append(m_IntValue); 
 | 
                    break; 
 | 
                case NumKeyBoardType.Password: 
 | 
                    m_Value.Length = 0; 
 | 
                    break; 
 | 
            } 
 | 
            SetTargetTextValue(); 
 | 
            resetValue = true; 
 | 
        } 
 | 
  
 | 
        private void OnEnable() 
 | 
        { 
 | 
            resetValue = true; 
 | 
        } 
 | 
  
 | 
        private void OnCancelBtn() 
 | 
        { 
 | 
            ResetValue(); 
 | 
            onValueChange.Invoke(); 
 | 
            onConfirm.Invoke(false); 
 | 
        } 
 | 
  
 | 
        private void OnOkBtn() 
 | 
        { 
 | 
            CheckValue(); 
 | 
            onConfirm.Invoke(true); 
 | 
            if (deActiveConfirm) 
 | 
            { 
 | 
                gameObject.SetActive(false); 
 | 
            } 
 | 
        } 
 | 
  
 | 
        private void OnClickNum(uint index) 
 | 
        { 
 | 
            switch (type) 
 | 
            { 
 | 
                case NumKeyBoardType.Math: 
 | 
                    { 
 | 
                        if (resetValue) 
 | 
                        { 
 | 
                            m_RealIntValue = index; 
 | 
                            if (max > 0 && index > max) 
 | 
                            { 
 | 
                                index = max; 
 | 
                            } 
 | 
                            if (!displayOutValue && min > 0 && index < min) 
 | 
                            { 
 | 
                                index = min; 
 | 
                            } 
 | 
                            m_IntValue = index; 
 | 
                            resetValue = false; 
 | 
                        } 
 | 
                        else 
 | 
                        { 
 | 
                            uint _value = m_RealIntValue * 10 + index; 
 | 
                            m_RealIntValue = _value; 
 | 
                            if (max > 0 && _value > max) 
 | 
                            { 
 | 
                                _value = max; 
 | 
                            } 
 | 
                            var maxlength = LengthOf(max); 
 | 
                            var length = LengthOf(_value); 
 | 
                            if (min > 0 && _value < min && (!displayOutValue || (max > 0 && length == maxlength))) 
 | 
                            { 
 | 
                                _value = min; 
 | 
                            } 
 | 
                            if (_value == m_IntValue) 
 | 
                            { 
 | 
                                return; 
 | 
                            } 
 | 
                            m_IntValue = _value; 
 | 
                        } 
 | 
                        m_Value.Length = 0; 
 | 
                        m_Value.Append(m_IntValue); 
 | 
                        SetTargetTextValue(); 
 | 
                    } 
 | 
                    break; 
 | 
                case NumKeyBoardType.Password: 
 | 
                    if (limit != 0 && m_Value.Length >= limit) 
 | 
                    { 
 | 
                        return; 
 | 
                    } 
 | 
                    m_Value.Append(index); 
 | 
                    SetTargetTextValue(); 
 | 
                    break; 
 | 
            } 
 | 
            m_OnValueChange.Invoke(); 
 | 
        } 
 | 
  
 | 
        private void CheckValue() 
 | 
        { 
 | 
            if (type == NumKeyBoardType.Math) 
 | 
            { 
 | 
                var value = 0; 
 | 
                int.TryParse(Value,out value); 
 | 
                if (max > 0 && value > max) 
 | 
                { 
 | 
                    Value = max.ToString(); 
 | 
                } 
 | 
                if (min > 0 && value < min) 
 | 
                { 
 | 
                    Value = min.ToString(); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
  
 | 
        void SetTargetTextValue() 
 | 
        { 
 | 
            if (targetText != null) 
 | 
            { 
 | 
                targetText.text = Value; 
 | 
            } 
 | 
        } 
 | 
  
 | 
        static int LengthOf(uint value) 
 | 
        { 
 | 
            for (int i = 0; i <= 10; i++) 
 | 
            { 
 | 
                if (value / (ulong)Mathf.Pow(10, i) < 1) 
 | 
                { 
 | 
                    return i; 
 | 
                } 
 | 
            } 
 | 
            return 0; 
 | 
        } 
 | 
    } 
 | 
    public enum NumKeyBoardType 
 | 
    { 
 | 
        Math, 
 | 
        Password, 
 | 
    } 
 | 
} 
 |