NibiruKeyBoard.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine.SceneManagement;
  6. using NibiruAxis;
  7. namespace Nxr.Internal
  8. {
  9. public enum m_eKeyBoard : byte
  10. {
  11. none = 0,
  12. Add = 1,
  13. Delete = 2,
  14. Space = 3,
  15. ChangePage = 4,
  16. ToggleCase = 5,
  17. Submit = 6,
  18. }
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. public class NibiruKeyBoard
  23. {
  24. public static NibiruKeyBoard m_instance = null;
  25. public static NibiruKeyBoard Instance
  26. {
  27. get
  28. {
  29. if (m_instance == null || m_instance.m_objSelf == null)
  30. m_instance = new NibiruKeyBoard();
  31. return m_instance;
  32. }
  33. }
  34. public GameObject m_objSelf = null;
  35. private Transform keyBoardTransform = null;
  36. private NibiruKeyBoardSingle[] m_arrCKeyBoardSingle = null;
  37. private Text m_textKeyBoard = null;
  38. /// <summary>
  39. /// 文本框显示字符串
  40. /// </summary>
  41. private string m_strKeyBoard = "";
  42. private RaycastHit hit;
  43. private int m_dwPageIndex = 0;
  44. private int m_dwToggleCase = 0;
  45. private Text m_text;
  46. /// <summary>
  47. /// 最大输入长度
  48. /// </summary>
  49. private int m_dwMaxSize = 30;
  50. /// <summary>
  51. /// 初始字符串
  52. /// </summary>
  53. private string m_strInitChar = " Nibiru KeyBoard ";
  54. private bool isShowing = false;
  55. public NibiruKeyBoard()
  56. {
  57. // Debug.Log("------>Create NibiruKeyBoard");
  58. m_objSelf = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Keyboard/NibiruKeyBoard"));
  59. m_objSelf.SetActive(false);
  60. keyBoardTransform = m_objSelf.transform;
  61. keyBoardTransform.position = new Vector3(1000, 1000, 1000);
  62. m_arrCKeyBoardSingle = keyBoardTransform.GetComponentsInChildren<NibiruKeyBoardSingle>();
  63. m_textKeyBoard = keyBoardTransform.Find("KeyPanel/InputBg/text_KeyBoard").GetComponent<Text>();
  64. m_textKeyBoard.text = m_strInitChar;
  65. isShowing = false;
  66. }
  67. void ReInit()
  68. {
  69. m_textKeyBoard.text = m_strInitChar;
  70. m_strKeyBoard = "";
  71. m_dwPageIndex = 0;
  72. m_dwToggleCase = 0;
  73. ShowPage(m_dwPageIndex, m_dwToggleCase);
  74. }
  75. /// <summary>
  76. /// Set UI.Text which the input content will display on.
  77. /// </summary>
  78. /// <param name="_text"></param>
  79. public void SetText(Text _text)
  80. {
  81. m_text = _text;
  82. m_textKeyBoard.text = m_text.text;
  83. m_strKeyBoard = m_text.text;
  84. }
  85. /// <summary>
  86. /// Show Keyboard
  87. /// </summary>
  88. /// <param name="_pageIndex">PageIndex : 0=alphabet,1=number</param>
  89. public void Show(int _pageIndex = 0)
  90. {
  91. Show(_pageIndex, new Vector3(0, -0.3f, 1), new Vector3(30, 0, 0));
  92. }
  93. /// <summary>
  94. /// Show Keyboard
  95. /// </summary>
  96. /// <param name="_pageIndex">PageIndex : 0=alphabet,1=number</param>
  97. /// <param name="position">keyBoardTransform's position</param>
  98. /// <param name="rotation">keyBoardTransform's rotation</param>
  99. public void Show(int _pageIndex, Vector3 position, Vector3 rotation)
  100. {
  101. if (isShowing)
  102. {
  103. Debug.Log("NibiruKeyBoard is Showing.");
  104. return;
  105. }
  106. m_objSelf.SetActive(true);
  107. isShowing = true;
  108. // 设置坐标和旋转
  109. keyBoardTransform.position = position;
  110. keyBoardTransform.Rotate(rotation);
  111. List<KeyBoardInfo> m_listKeyBoard = NibiruCoreStaticDataManager.instance.GetKeyBoardInfoByPage(_pageIndex % 2);
  112. for (int i = 0; i < m_arrCKeyBoardSingle.Length; i++)
  113. {
  114. if (i < m_listKeyBoard.Count)
  115. {
  116. m_arrCKeyBoardSingle[i].gameObject.SetActive(true);
  117. if ((m_eKeyBoard)m_listKeyBoard[i].m_bType == m_eKeyBoard.Add)
  118. {
  119. string _strChar = Char(int.Parse(m_listKeyBoard[i].m_strShow_1));
  120. m_arrCKeyBoardSingle[i].m_textKey.text = _strChar;
  121. m_arrCKeyBoardSingle[i].m_strChar = _strChar;
  122. }
  123. else
  124. {
  125. m_arrCKeyBoardSingle[i].m_textKey.text = m_listKeyBoard[i].m_strShow_1;
  126. m_arrCKeyBoardSingle[i].m_strChar = m_listKeyBoard[i].m_strShow_1;
  127. }
  128. m_arrCKeyBoardSingle[i].m_imageKey.GetComponent<RectTransform>().sizeDelta = new Vector2(m_listKeyBoard[i].m_dwScaleX, m_listKeyBoard[i].m_dwScaleY);
  129. m_arrCKeyBoardSingle[i].m_trsSelf.localPosition = new Vector3(m_listKeyBoard[i].m_dwPosX, m_listKeyBoard[i].m_dwPosY, 22);
  130. m_arrCKeyBoardSingle[i].m_CKeyBoardInfo = m_listKeyBoard[i];
  131. m_arrCKeyBoardSingle[i].m_colliderKey.size = new Vector3(m_listKeyBoard[i].m_dwScaleX, m_listKeyBoard[i].m_dwScaleY, 1);
  132. }
  133. else
  134. {
  135. m_arrCKeyBoardSingle[i].gameObject.SetActive(false);
  136. }
  137. }
  138. }
  139. void ShowPage(int _pageIndex, int _toggleCase)
  140. {
  141. List<KeyBoardInfo> m_listKeyBoard = NibiruCoreStaticDataManager.instance.GetKeyBoardInfoByPage(_pageIndex % 2);
  142. for (int i = 0; i < m_arrCKeyBoardSingle.Length; i++)
  143. {
  144. if (i < m_listKeyBoard.Count)
  145. {
  146. m_arrCKeyBoardSingle[i].gameObject.SetActive(true);
  147. if ((m_eKeyBoard)m_listKeyBoard[i].m_bType == m_eKeyBoard.Add)
  148. {
  149. string _strChar = Char(int.Parse(_toggleCase % 2 == 0 ? m_listKeyBoard[i].m_strShow_1 : m_listKeyBoard[i].m_strShow_2));
  150. m_arrCKeyBoardSingle[i].m_textKey.text = _strChar;
  151. m_arrCKeyBoardSingle[i].m_strChar = _strChar;
  152. }
  153. else
  154. {
  155. m_arrCKeyBoardSingle[i].m_textKey.text = _toggleCase % 2 == 0 ? m_listKeyBoard[i].m_strShow_1 : m_listKeyBoard[i].m_strShow_2;
  156. m_arrCKeyBoardSingle[i].m_strChar = _toggleCase % 2 == 0 ? m_listKeyBoard[i].m_strShow_1 : m_listKeyBoard[i].m_strShow_2;
  157. }
  158. m_arrCKeyBoardSingle[i].m_imageKey.GetComponent<RectTransform>().sizeDelta = new Vector2(m_listKeyBoard[i].m_dwScaleX, m_listKeyBoard[i].m_dwScaleY);
  159. m_arrCKeyBoardSingle[i].m_trsSelf.localPosition = new Vector3(m_listKeyBoard[i].m_dwPosX, m_listKeyBoard[i].m_dwPosY, 22);
  160. m_arrCKeyBoardSingle[i].m_CKeyBoardInfo = m_listKeyBoard[i];
  161. m_arrCKeyBoardSingle[i].m_colliderKey.size = new Vector3(m_listKeyBoard[i].m_dwScaleX, m_listKeyBoard[i].m_dwScaleY, 1);
  162. }
  163. else
  164. {
  165. m_arrCKeyBoardSingle[i].gameObject.SetActive(false);
  166. }
  167. }
  168. }
  169. public void OnPressEnterByCamera()
  170. {
  171. Transform mTransform = NxrViewer.Instance.GetHead().transform;
  172. if (Physics.Raycast(mTransform.position, mTransform.TransformDirection(Vector3.forward), out hit))
  173. {
  174. OnPressEnter(hit.collider.gameObject);
  175. }
  176. }
  177. public void OnPressEnterByQuat()
  178. {
  179. if(NxrControllerHelper.ControllerRaycastObject != null)
  180. {
  181. OnPressEnter(NxrControllerHelper.ControllerRaycastObject);
  182. }
  183. }
  184. public void OnPressEnterByMouse()
  185. {
  186. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  187. if (Physics.Raycast(ray, out hit))
  188. {
  189. OnPressEnter(hit.collider.gameObject);
  190. }
  191. }
  192. public void OnPressEnter(GameObject hit)
  193. {
  194. if (hit.name.Substring(0, 3) == "Key")
  195. {
  196. NibiruKeyBoardSingle _CKeyBoardSingle = hit.GetComponent<NibiruKeyBoardSingle>();
  197. m_eKeyBoard keyType = (m_eKeyBoard)_CKeyBoardSingle.m_CKeyBoardInfo.m_bType;
  198. switch (keyType)
  199. {
  200. case m_eKeyBoard.Add:
  201. if (m_strKeyBoard.Length >= m_dwMaxSize)
  202. return;
  203. m_strKeyBoard += _CKeyBoardSingle.m_strChar;
  204. m_textKeyBoard.text = m_strKeyBoard;
  205. break;
  206. case m_eKeyBoard.ChangePage:
  207. m_dwPageIndex++;
  208. m_dwToggleCase = 0;
  209. ShowPage(m_dwPageIndex, m_dwToggleCase);
  210. break;
  211. case m_eKeyBoard.Delete:
  212. if (m_strKeyBoard.Length > 0)
  213. {
  214. m_strKeyBoard = m_strKeyBoard.Substring(0, m_strKeyBoard.Length - 1);
  215. m_textKeyBoard.text = m_strKeyBoard;
  216. }
  217. break;
  218. case m_eKeyBoard.Space:
  219. if (m_strKeyBoard.Length >= m_dwMaxSize)
  220. return;
  221. m_strKeyBoard += " ";
  222. m_textKeyBoard.text = m_strKeyBoard;
  223. break;
  224. case m_eKeyBoard.Submit:
  225. ReInit();
  226. Dismiss();
  227. break;
  228. case m_eKeyBoard.ToggleCase:
  229. m_dwToggleCase++;
  230. ShowPage(m_dwPageIndex, m_dwToggleCase);
  231. break;
  232. }
  233. if (m_text != null && keyType != m_eKeyBoard.Submit)
  234. {
  235. m_text.text = m_strKeyBoard;
  236. }
  237. }
  238. }
  239. /// <summary>
  240. /// Dismiss keyboard
  241. /// </summary>
  242. public void Dismiss()
  243. {
  244. if (!isShowing) return;
  245. isShowing = false;
  246. m_objSelf.SetActive(false);
  247. GameObject.DestroyImmediate(m_objSelf);
  248. m_objSelf = null;
  249. if (keyBoardTransform != null)
  250. {
  251. // 很远不可见
  252. keyBoardTransform.position = new Vector3(1000, 1000, 1000);
  253. keyBoardTransform.rotation = new Quaternion(0, 0, 0, 0);
  254. }
  255. }
  256. /// <summary>
  257. /// Determine whether the keyboard is displayed.
  258. /// </summary>
  259. /// <returns></returns>
  260. public bool isShown()
  261. { return isShowing; }
  262. public string Char(int asciiCode)
  263. {
  264. if (asciiCode >= 0 && asciiCode <= 255)
  265. {
  266. return "" + (char)asciiCode;
  267. }
  268. else
  269. {
  270. throw new System.Exception("ASCII code is not valid. ");
  271. }
  272. }
  273. /// <summary>
  274. /// Get current input content
  275. /// </summary>
  276. /// <returns></returns>
  277. public string GetKeyBoardString()
  278. {
  279. return m_strKeyBoard;
  280. }
  281. /// <summary>
  282. /// Get keyboard's transform
  283. /// </summary>
  284. /// <returns></returns>
  285. public Transform GetKeyBoardTransform()
  286. {
  287. return keyBoardTransform;
  288. }
  289. }
  290. }