123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine.SceneManagement;
- using NibiruAxis;
- namespace Nxr.Internal
- {
- public enum m_eKeyBoard : byte
- {
- none = 0,
- Add = 1,
- Delete = 2,
- Space = 3,
- ChangePage = 4,
- ToggleCase = 5,
- Submit = 6,
- }
- /// <summary>
- ///
- /// </summary>
- public class NibiruKeyBoard
- {
- public static NibiruKeyBoard m_instance = null;
- public static NibiruKeyBoard Instance
- {
- get
- {
- if (m_instance == null || m_instance.m_objSelf == null)
- m_instance = new NibiruKeyBoard();
- return m_instance;
- }
- }
- public GameObject m_objSelf = null;
- private Transform keyBoardTransform = null;
- private NibiruKeyBoardSingle[] m_arrCKeyBoardSingle = null;
- private Text m_textKeyBoard = null;
- /// <summary>
- /// 文本框显示字符串
- /// </summary>
- private string m_strKeyBoard = "";
- private RaycastHit hit;
- private int m_dwPageIndex = 0;
- private int m_dwToggleCase = 0;
- private Text m_text;
- /// <summary>
- /// 最大输入长度
- /// </summary>
- private int m_dwMaxSize = 30;
- /// <summary>
- /// 初始字符串
- /// </summary>
- private string m_strInitChar = " Nibiru KeyBoard ";
- private bool isShowing = false;
- public NibiruKeyBoard()
- {
- // Debug.Log("------>Create NibiruKeyBoard");
- m_objSelf = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Keyboard/NibiruKeyBoard"));
- m_objSelf.SetActive(false);
- keyBoardTransform = m_objSelf.transform;
- keyBoardTransform.position = new Vector3(1000, 1000, 1000);
- m_arrCKeyBoardSingle = keyBoardTransform.GetComponentsInChildren<NibiruKeyBoardSingle>();
- m_textKeyBoard = keyBoardTransform.Find("KeyPanel/InputBg/text_KeyBoard").GetComponent<Text>();
- m_textKeyBoard.text = m_strInitChar;
- isShowing = false;
- }
- void ReInit()
- {
- m_textKeyBoard.text = m_strInitChar;
- m_strKeyBoard = "";
- m_dwPageIndex = 0;
- m_dwToggleCase = 0;
- ShowPage(m_dwPageIndex, m_dwToggleCase);
- }
- /// <summary>
- /// Set UI.Text which the input content will display on.
- /// </summary>
- /// <param name="_text"></param>
- public void SetText(Text _text)
- {
- m_text = _text;
- m_textKeyBoard.text = m_text.text;
- m_strKeyBoard = m_text.text;
- }
- /// <summary>
- /// Show Keyboard
- /// </summary>
- /// <param name="_pageIndex">PageIndex : 0=alphabet,1=number</param>
- public void Show(int _pageIndex = 0)
- {
- Show(_pageIndex, new Vector3(0, -0.3f, 1), new Vector3(30, 0, 0));
- }
- /// <summary>
- /// Show Keyboard
- /// </summary>
- /// <param name="_pageIndex">PageIndex : 0=alphabet,1=number</param>
- /// <param name="position">keyBoardTransform's position</param>
- /// <param name="rotation">keyBoardTransform's rotation</param>
- public void Show(int _pageIndex, Vector3 position, Vector3 rotation)
- {
- if (isShowing)
- {
- Debug.Log("NibiruKeyBoard is Showing.");
- return;
- }
- m_objSelf.SetActive(true);
- isShowing = true;
- // 设置坐标和旋转
- keyBoardTransform.position = position;
- keyBoardTransform.Rotate(rotation);
- List<KeyBoardInfo> m_listKeyBoard = NibiruCoreStaticDataManager.instance.GetKeyBoardInfoByPage(_pageIndex % 2);
- for (int i = 0; i < m_arrCKeyBoardSingle.Length; i++)
- {
- if (i < m_listKeyBoard.Count)
- {
- m_arrCKeyBoardSingle[i].gameObject.SetActive(true);
- if ((m_eKeyBoard)m_listKeyBoard[i].m_bType == m_eKeyBoard.Add)
- {
- string _strChar = Char(int.Parse(m_listKeyBoard[i].m_strShow_1));
- m_arrCKeyBoardSingle[i].m_textKey.text = _strChar;
- m_arrCKeyBoardSingle[i].m_strChar = _strChar;
- }
- else
- {
- m_arrCKeyBoardSingle[i].m_textKey.text = m_listKeyBoard[i].m_strShow_1;
- m_arrCKeyBoardSingle[i].m_strChar = m_listKeyBoard[i].m_strShow_1;
- }
- m_arrCKeyBoardSingle[i].m_imageKey.GetComponent<RectTransform>().sizeDelta = new Vector2(m_listKeyBoard[i].m_dwScaleX, m_listKeyBoard[i].m_dwScaleY);
- m_arrCKeyBoardSingle[i].m_trsSelf.localPosition = new Vector3(m_listKeyBoard[i].m_dwPosX, m_listKeyBoard[i].m_dwPosY, 22);
- m_arrCKeyBoardSingle[i].m_CKeyBoardInfo = m_listKeyBoard[i];
- m_arrCKeyBoardSingle[i].m_colliderKey.size = new Vector3(m_listKeyBoard[i].m_dwScaleX, m_listKeyBoard[i].m_dwScaleY, 1);
- }
- else
- {
- m_arrCKeyBoardSingle[i].gameObject.SetActive(false);
- }
- }
- }
- void ShowPage(int _pageIndex, int _toggleCase)
- {
- List<KeyBoardInfo> m_listKeyBoard = NibiruCoreStaticDataManager.instance.GetKeyBoardInfoByPage(_pageIndex % 2);
- for (int i = 0; i < m_arrCKeyBoardSingle.Length; i++)
- {
- if (i < m_listKeyBoard.Count)
- {
- m_arrCKeyBoardSingle[i].gameObject.SetActive(true);
- if ((m_eKeyBoard)m_listKeyBoard[i].m_bType == m_eKeyBoard.Add)
- {
- string _strChar = Char(int.Parse(_toggleCase % 2 == 0 ? m_listKeyBoard[i].m_strShow_1 : m_listKeyBoard[i].m_strShow_2));
- m_arrCKeyBoardSingle[i].m_textKey.text = _strChar;
- m_arrCKeyBoardSingle[i].m_strChar = _strChar;
- }
- else
- {
- m_arrCKeyBoardSingle[i].m_textKey.text = _toggleCase % 2 == 0 ? m_listKeyBoard[i].m_strShow_1 : m_listKeyBoard[i].m_strShow_2;
- m_arrCKeyBoardSingle[i].m_strChar = _toggleCase % 2 == 0 ? m_listKeyBoard[i].m_strShow_1 : m_listKeyBoard[i].m_strShow_2;
- }
- m_arrCKeyBoardSingle[i].m_imageKey.GetComponent<RectTransform>().sizeDelta = new Vector2(m_listKeyBoard[i].m_dwScaleX, m_listKeyBoard[i].m_dwScaleY);
- m_arrCKeyBoardSingle[i].m_trsSelf.localPosition = new Vector3(m_listKeyBoard[i].m_dwPosX, m_listKeyBoard[i].m_dwPosY, 22);
- m_arrCKeyBoardSingle[i].m_CKeyBoardInfo = m_listKeyBoard[i];
- m_arrCKeyBoardSingle[i].m_colliderKey.size = new Vector3(m_listKeyBoard[i].m_dwScaleX, m_listKeyBoard[i].m_dwScaleY, 1);
- }
- else
- {
- m_arrCKeyBoardSingle[i].gameObject.SetActive(false);
- }
- }
- }
- public void OnPressEnterByCamera()
- {
- Transform mTransform = NxrViewer.Instance.GetHead().transform;
- if (Physics.Raycast(mTransform.position, mTransform.TransformDirection(Vector3.forward), out hit))
- {
- OnPressEnter(hit.collider.gameObject);
- }
- }
- public void OnPressEnterByQuat()
- {
- if(NxrControllerHelper.ControllerRaycastObject != null)
- {
- OnPressEnter(NxrControllerHelper.ControllerRaycastObject);
- }
- }
- public void OnPressEnterByMouse()
- {
- Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
- if (Physics.Raycast(ray, out hit))
- {
- OnPressEnter(hit.collider.gameObject);
- }
- }
- public void OnPressEnter(GameObject hit)
- {
- if (hit.name.Substring(0, 3) == "Key")
- {
- NibiruKeyBoardSingle _CKeyBoardSingle = hit.GetComponent<NibiruKeyBoardSingle>();
- m_eKeyBoard keyType = (m_eKeyBoard)_CKeyBoardSingle.m_CKeyBoardInfo.m_bType;
- switch (keyType)
- {
- case m_eKeyBoard.Add:
- if (m_strKeyBoard.Length >= m_dwMaxSize)
- return;
- m_strKeyBoard += _CKeyBoardSingle.m_strChar;
- m_textKeyBoard.text = m_strKeyBoard;
- break;
- case m_eKeyBoard.ChangePage:
- m_dwPageIndex++;
- m_dwToggleCase = 0;
- ShowPage(m_dwPageIndex, m_dwToggleCase);
- break;
- case m_eKeyBoard.Delete:
- if (m_strKeyBoard.Length > 0)
- {
- m_strKeyBoard = m_strKeyBoard.Substring(0, m_strKeyBoard.Length - 1);
- m_textKeyBoard.text = m_strKeyBoard;
- }
- break;
- case m_eKeyBoard.Space:
- if (m_strKeyBoard.Length >= m_dwMaxSize)
- return;
- m_strKeyBoard += " ";
- m_textKeyBoard.text = m_strKeyBoard;
- break;
- case m_eKeyBoard.Submit:
- ReInit();
- Dismiss();
- break;
- case m_eKeyBoard.ToggleCase:
- m_dwToggleCase++;
- ShowPage(m_dwPageIndex, m_dwToggleCase);
- break;
- }
- if (m_text != null && keyType != m_eKeyBoard.Submit)
- {
- m_text.text = m_strKeyBoard;
- }
- }
- }
- /// <summary>
- /// Dismiss keyboard
- /// </summary>
- public void Dismiss()
- {
- if (!isShowing) return;
- isShowing = false;
- m_objSelf.SetActive(false);
- GameObject.DestroyImmediate(m_objSelf);
- m_objSelf = null;
- if (keyBoardTransform != null)
- {
- // 很远不可见
- keyBoardTransform.position = new Vector3(1000, 1000, 1000);
- keyBoardTransform.rotation = new Quaternion(0, 0, 0, 0);
- }
- }
- /// <summary>
- /// Determine whether the keyboard is displayed.
- /// </summary>
- /// <returns></returns>
- public bool isShown()
- { return isShowing; }
- public string Char(int asciiCode)
- {
- if (asciiCode >= 0 && asciiCode <= 255)
- {
- return "" + (char)asciiCode;
- }
- else
- {
- throw new System.Exception("ASCII code is not valid. ");
- }
- }
- /// <summary>
- /// Get current input content
- /// </summary>
- /// <returns></returns>
- public string GetKeyBoardString()
- {
- return m_strKeyBoard;
- }
- /// <summary>
- /// Get keyboard's transform
- /// </summary>
- /// <returns></returns>
- public Transform GetKeyBoardTransform()
- {
- return keyBoardTransform;
- }
- }
- }
|