KeyBoardHandlerNew.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. using EZXR.Glass.Core;
  2. using EZXR.Glass.QRScanner;
  3. using EZXR.Glass.SixDof;
  4. using EZXR.Glass.UI;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using TMPro;
  9. using UnityEngine;
  10. using UnityEngine.Events;
  11. using UnityEngine.UI;
  12. namespace EZXR.Glass.Inputs
  13. {
  14. //[ExecuteInEditMode]
  15. public class KeyBoardHandlerNew : MonoBehaviour
  16. {
  17. public enum KeyboardType
  18. {
  19. /// <summary>
  20. /// 小写字母
  21. /// </summary>
  22. LowerCase,
  23. /// <summary>
  24. /// 大写字母
  25. /// </summary>
  26. Uppercase,
  27. /// <summary>
  28. /// 数字
  29. /// </summary>
  30. Numbers,
  31. /// <summary>
  32. /// 符号
  33. /// </summary>
  34. Symbols
  35. }
  36. /// <summary>
  37. /// 当前键盘类型:
  38. /// </summary>
  39. public KeyboardType keyboardType;
  40. public GameObject canvas;
  41. /// <summary>
  42. /// 所有按键的父物体
  43. /// </summary>
  44. public Transform keysParent;
  45. /// <summary>
  46. /// 确认键被按下,用于全局需要通过确认键触发的情况
  47. /// </summary>
  48. public UnityAction OnEnterClicked;
  49. private TMP_InputField curTMPInputField;
  50. private InputField curInputField;
  51. public TextMeshProUGUI m_TextMeshPro;
  52. public Button[] letters;
  53. public Image capslock;
  54. /// <summary>
  55. /// 大写的时候要亮的一个绿灯
  56. /// </summary>
  57. public GameObject capslockDot;
  58. /// <summary>
  59. /// 数字按钮
  60. /// </summary>
  61. public GameObject num;
  62. public GameObject symbol;
  63. public GameObject abc;
  64. /// <summary>
  65. /// 存放letters中默认的键值
  66. /// </summary>
  67. string[] letters4Change;
  68. /// <summary>
  69. /// 用于数字键盘,存放与字母键盘一一对应的数字和符号
  70. /// </summary>
  71. public string[] num4Change;
  72. /// <summary>
  73. /// 用于符号键盘,存放与字母键盘一一对应的符号
  74. /// </summary>
  75. public string[] symbols4Change;
  76. /// <summary>
  77. /// 0是非按下,1是按下
  78. /// </summary>
  79. public Sprite[] capslockStatus;
  80. public TextMeshProUGUI inputfield;
  81. Ray ray = new Ray();
  82. RaycastHit hitInfo;
  83. /// <summary>
  84. /// 焦点
  85. /// </summary>
  86. public Transform focus;
  87. /// <summary>
  88. /// 开始显示焦点的距离
  89. /// </summary>
  90. float showFocusDistance = 0.05f;
  91. /// <summary>
  92. /// 焦点在此距离的时候不再变小,即localScale为1
  93. /// </summary>
  94. float focusNearDistance = 0.01f;
  95. /// <summary>
  96. /// 拖动键盘的把手
  97. /// </summary>
  98. public Image handler;
  99. /// <summary>
  100. /// 0是Normal,1是Enter,2是Press
  101. /// </summary>
  102. public Sprite[] handlerStatus;
  103. public GameObject keyboardHover;
  104. public SpatialAnchor spatialAnchor;
  105. private void Awake()
  106. {
  107. Button[] buttons = keysParent.GetComponentsInChildren<Button>();
  108. foreach (Button button in buttons)
  109. {
  110. button.onClick.AddListener(() => OnKeyClicked(button));
  111. }
  112. abc.SetActive(false);
  113. letters4Change = new string[letters.Length];
  114. for (int i = 0; i < letters.Length; i++)
  115. {
  116. letters4Change[i] = letters[i].name;
  117. }
  118. //扫描按钮事件注册
  119. QRScannerManager.RegisterCompleteListener(OnScanComplete);
  120. }
  121. private void KeyboardRecenterNear(bool enabled = false)
  122. {
  123. transform.Recenter(new Vector3(-0.015f, -0.15f, 0.4f), false);
  124. transform.localScale = new Vector3(0.0001806142f, 0.0001806142f, 1);
  125. }
  126. private void KeyboardRecenterFar(bool enabled = false)
  127. {
  128. transform.Recenter(new Vector3(-0.015f, -0.15f, 0.6f), false);
  129. transform.localScale = new Vector3(0.0001806142f, 0.0001806142f, 1);
  130. }
  131. void OnEnable()
  132. {
  133. //SystemManager.OnPowerPressed += KeyboardRecenter;
  134. //#if UNITY_EDITOR
  135. // foreach (SpatialButton button in letters)
  136. // {
  137. // button.Text = button.name;
  138. // }
  139. // foreach (SpatialButton button in symbols)
  140. // {
  141. // button.Text = button.name;
  142. // }
  143. //#endif
  144. }
  145. private void OnDisable()
  146. {
  147. //WristPanel.Left.RemoveListener(KeyboardRecenter);
  148. //SystemManager.OnPowerPressed -= KeyboardRecenter;
  149. }
  150. private void Start()
  151. {
  152. if (Application.isPlaying)
  153. {
  154. ChangeLettersToLower();
  155. //ChangeSymbols("#+=");
  156. }
  157. }
  158. // Update is called once per frame
  159. void Update()
  160. {
  161. //if (Input.GetKeyDown(KeyCode.T))
  162. //{
  163. // KeyboardRecenter();
  164. //}
  165. //transform.rotation = HMDPoseTracker.Instance.Head.rotation * Quaternion.Euler(0, 180, 0);
  166. if (InputSystem.CurrentActiveControllerType == ControllerType.HandTracking)
  167. {
  168. if (Vector3.Distance(transform.position, HMDPoseTracker.Instance.Head.position) > 0.5f)
  169. {
  170. InputSystem.leftHand.RaycastInteraction = true;
  171. InputSystem.rightHand.RaycastInteraction = true;
  172. }
  173. else
  174. {
  175. InputSystem.leftHand.RaycastInteraction = false;
  176. InputSystem.rightHand.RaycastInteraction = false;
  177. }
  178. }
  179. if (ARHandManager.rightHand != null && ARHandManager.rightHand.Exist && ARHandManager.rightHand.InView(HandJointType.Index_4))
  180. {
  181. ray.origin = ARHandManager.rightHand.GetJointData(HandJointType.Index_4).position;
  182. ray.direction = -transform.forward;
  183. }
  184. else if (ARHandManager.leftHand != null && ARHandManager.leftHand.Exist && ARHandManager.leftHand.InView(HandJointType.Index_4))
  185. {
  186. ray.origin = ARHandManager.leftHand.GetJointData(HandJointType.Index_4).position;
  187. ray.direction = -transform.forward;
  188. }
  189. if (Physics.Raycast(ray, out hitInfo))
  190. {
  191. if (hitInfo.distance < showFocusDistance)
  192. {
  193. focus.gameObject.SetActive(true);
  194. focus.position = hitInfo.point;
  195. focus.localScale = Vector3.one * (1f * (Math.Clamp(hitInfo.distance - focusNearDistance, 0, 100)) / (showFocusDistance - focusNearDistance) + 1.0f);
  196. }
  197. else
  198. {
  199. focus.gameObject.SetActive(false);
  200. }
  201. }
  202. }
  203. public void Show(InputField inputField)
  204. {
  205. if (InputSystem.CurrentActiveControllerType == ControllerType.HandTracking)
  206. {
  207. InputSystem.leftHand.RaycastInteraction = false;
  208. InputSystem.rightHand.RaycastInteraction = false;
  209. KeyboardRecenterNear();
  210. }
  211. else
  212. {
  213. KeyboardRecenterFar();
  214. }
  215. curInputField = inputField;
  216. m_TextMeshPro.text = inputField.text;
  217. gameObject.SetActive(true);
  218. }
  219. public void Show(TMP_InputField inputField)
  220. {
  221. if (InputSystem.CurrentActiveControllerType == ControllerType.HandTracking)
  222. {
  223. InputSystem.leftHand.RaycastInteraction = false;
  224. InputSystem.rightHand.RaycastInteraction = false;
  225. KeyboardRecenterNear();
  226. }
  227. else
  228. {
  229. KeyboardRecenterFar();
  230. }
  231. curTMPInputField = inputField;
  232. m_TextMeshPro.text = inputField.text;
  233. gameObject.SetActive(true);
  234. }
  235. public void Hide()
  236. {
  237. StartCoroutine(WaitThenHide());
  238. }
  239. IEnumerator WaitThenHide()
  240. {
  241. yield return new WaitForSeconds(0.5f);
  242. InputSystem.leftHand.RaycastInteraction = true;
  243. InputSystem.rightHand.RaycastInteraction = true;
  244. curInputField = null;
  245. curTMPInputField = null;
  246. gameObject.SetActive(false);
  247. }
  248. void ExchangeLetters()
  249. {
  250. if (keyboardType != KeyboardType.Uppercase)
  251. {
  252. ChangeLettersToUpper();
  253. }
  254. else
  255. {
  256. ChangeLettersToLower();
  257. }
  258. }
  259. void ChangeLettersToUpper()
  260. {
  261. keyboardType = KeyboardType.Uppercase;
  262. for (int i = 0; i < letters.Length; i++)
  263. {
  264. letters[i].name = letters4Change[i].ToUpper();
  265. letters[i].GetComponentInChildren<TextMeshProUGUI>().text = letters[i].name;
  266. }
  267. //CapsLock按下状态
  268. capslock.sprite = capslockStatus[1];
  269. capslockDot.SetActive(true);
  270. }
  271. void ChangeLettersToLower()
  272. {
  273. keyboardType = KeyboardType.LowerCase;
  274. for (int i = 0; i < letters.Length; i++)
  275. {
  276. letters[i].name = letters4Change[i].ToLower();
  277. letters[i].GetComponentInChildren<TextMeshProUGUI>().text = letters[i].name;
  278. }
  279. //CapsLock非按下状态
  280. capslock.sprite = capslockStatus[0];
  281. capslockDot.SetActive(false);
  282. }
  283. void ToggleNum()
  284. {
  285. //keyboardType = KeyboardType.Numbers;
  286. //num.sprite = capslockStatus[1];
  287. for (int i = 0; i < letters.Length; i++)
  288. {
  289. letters[i].name = num4Change[i];
  290. letters[i].GetComponentInChildren<TextMeshProUGUI>().text = num4Change[i];
  291. }
  292. }
  293. void ToggleSymbol()
  294. {
  295. //keyboardType = KeyboardType.Symbols;
  296. //symbol.sprite = capslockStatus[1];
  297. for (int i = 0; i < letters.Length; i++)
  298. {
  299. letters[i].name = symbols4Change[i];
  300. letters[i].GetComponentInChildren<TextMeshProUGUI>().text = symbols4Change[i];
  301. }
  302. }
  303. //void ChangeSymbols(string target = "")
  304. //{
  305. // if (symbol.name == "123" || target == "#+=")
  306. // {
  307. // symbol.name = "#+=";
  308. // symbol.GetComponentInChildren<TextMeshProUGUI>().text = symbol.name;
  309. // for (int i = 0; i < symbols.Length; i++)
  310. // {
  311. // symbols[i].name = num4Change[i];
  312. // symbols[i].GetComponentInChildren<TextMeshProUGUI>().text = num4Change[i];
  313. // }
  314. // }
  315. // else if (symbol.name == "#+=" || target == "123")
  316. // {
  317. // symbol.name = "123";
  318. // symbol.GetComponentInChildren<TextMeshProUGUI>().text = symbol.name;
  319. // for (int i = 0; i < symbols.Length; i++)
  320. // {
  321. // symbols[i].name = symbols4Change[i];
  322. // symbols[i].GetComponentInChildren<TextMeshProUGUI>().text = symbols4Change[i];
  323. // }
  324. // }
  325. //}
  326. public void OnKeyHoverEnter(string key)
  327. {
  328. Debug.Log("OnKeyHoverEnter: " + key);
  329. }
  330. public void OnStartPress(string key)
  331. {
  332. Debug.Log("OnStartPress: " + key);
  333. }
  334. public void OnPressing(string key)
  335. {
  336. Debug.Log("OnPressing: " + key);
  337. }
  338. //OnKeyDown
  339. public void OnKeyDown(string key)
  340. {
  341. Debug.Log("OnKeyDown: " + key);
  342. }
  343. //OnKeyUp
  344. public void OnKeyUp(string key)
  345. {
  346. Debug.Log("OnKeyUp: " + key);
  347. }
  348. public void OnEndPress(string key)
  349. {
  350. Debug.Log("OnEndPress: " + key);
  351. }
  352. public void OnKeyClicked(Button sender)
  353. {
  354. switch (sender.name)
  355. {
  356. case "Del":
  357. if (m_TextMeshPro.text.Length > 0)
  358. {
  359. m_TextMeshPro.text = m_TextMeshPro.text.Substring(0, m_TextMeshPro.text.Length - 1);
  360. }
  361. break;
  362. case "Clear":
  363. m_TextMeshPro.text = "";
  364. break;
  365. case "CapsLock":
  366. //num.sprite = capslockStatus[0];
  367. //symbol.sprite = capslockStatus[0];
  368. ExchangeLetters();
  369. break;
  370. case "123":
  371. //if (keyboardType == KeyboardType.Numbers)
  372. //{
  373. // //num.sprite = capslockStatus[0];
  374. // ChangeLettersToLower();
  375. //}
  376. //else
  377. capslock.transform.parent.gameObject.SetActive(false);
  378. num.SetActive(false);
  379. symbol.SetActive(false);
  380. abc.SetActive(true);
  381. {
  382. //capslock.sprite = capslockStatus[0];
  383. //capslockDot.SetActive(false);
  384. //symbol.sprite = capslockStatus[0];
  385. ToggleNum();
  386. }
  387. break;
  388. case "#+=":
  389. //if (keyboardType == KeyboardType.Symbols)
  390. //{
  391. // //symbol.sprite = capslockStatus[0];
  392. // ChangeLettersToLower();
  393. //}
  394. //else
  395. capslock.transform.parent.gameObject.SetActive(false);
  396. num.SetActive(false);
  397. symbol.SetActive(false);
  398. abc.SetActive(true);
  399. {
  400. //capslock.sprite = capslockStatus[0];
  401. //capslockDot.SetActive(false);
  402. //num.sprite = capslockStatus[0];
  403. ToggleSymbol();
  404. }
  405. break;
  406. case "ABC":
  407. capslock.transform.parent.gameObject.SetActive(true);
  408. num.SetActive(true);
  409. symbol.SetActive(true);
  410. abc.SetActive(false);
  411. if (keyboardType == KeyboardType.Uppercase)
  412. {
  413. ChangeLettersToUpper();
  414. }
  415. else
  416. {
  417. ChangeLettersToLower();
  418. }
  419. break;
  420. case "QR":
  421. StartScan();
  422. break;
  423. case "Return":
  424. if (OnEnterClicked != null)
  425. {
  426. OnEnterClicked.Invoke();
  427. }
  428. else
  429. {
  430. //m_TextMeshPro.text += "\r\n";
  431. }
  432. break;
  433. case "Keyboard":
  434. Hide();
  435. break;
  436. default:
  437. m_TextMeshPro.text += sender.name;
  438. break;
  439. }
  440. SetInputField(m_TextMeshPro.text);
  441. }
  442. void SetInputField(string text)
  443. {
  444. if (m_TextMeshPro != null)
  445. m_TextMeshPro.text = text;
  446. if (curInputField != null)
  447. {
  448. curInputField.text = text;
  449. }
  450. if (curTMPInputField != null)
  451. {
  452. curTMPInputField.text = text;
  453. }
  454. }
  455. public void OnKeyHoverExit(string key)
  456. {
  457. Debug.Log("OnKeyHoverExit: " + key);
  458. }
  459. /// <summary>
  460. /// 0是normal,1是hover,2是press
  461. /// </summary>
  462. /// <param name="eventType"></param>
  463. public void OnHandlerEvent(int eventType)
  464. {
  465. handler.sprite = handlerStatus[eventType];
  466. keyboardHover.SetActive(eventType == 2 ? true : false);
  467. spatialAnchor.lookToHead = keyboardHover.activeSelf;
  468. }
  469. /// <summary>
  470. /// 点击扫描按钮调用此处
  471. /// </summary>
  472. public void StartScan()
  473. {
  474. canvas.SetActive(false);
  475. QRScannerManager.RequestOpen();
  476. InputSystem.leftHand.RaycastInteraction = true;
  477. InputSystem.rightHand.RaycastInteraction = true;
  478. }
  479. /// <summary>
  480. /// 扫描完毕回调此处
  481. /// </summary>
  482. /// <param name="status"></param>
  483. /// <param name="content"></param>
  484. private void OnScanComplete(bool status, string content)
  485. {
  486. Debug.Log("QRScannerDemo, Scanning Complete: " + content);
  487. if (status)
  488. {
  489. InputSystem.leftHand.RaycastInteraction = false;
  490. InputSystem.rightHand.RaycastInteraction = false;
  491. if (canvas != null)
  492. canvas.SetActive(true);
  493. SetInputField(content);
  494. }
  495. else
  496. {
  497. if (canvas != null)
  498. canvas.SetActive(true);
  499. Toast.Show("未识别到二维码");
  500. }
  501. }
  502. }
  503. }