123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- using EZXR.Glass.Core;
- using EZXR.Glass.QRScanner;
- using EZXR.Glass.SixDof;
- using EZXR.Glass.UI;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.UI;
- namespace EZXR.Glass.Inputs
- {
- //[ExecuteInEditMode]
- public class KeyBoardHandlerNew : MonoBehaviour
- {
- public enum KeyboardType
- {
- /// <summary>
- /// 小写字母
- /// </summary>
- LowerCase,
- /// <summary>
- /// 大写字母
- /// </summary>
- Uppercase,
- /// <summary>
- /// 数字
- /// </summary>
- Numbers,
- /// <summary>
- /// 符号
- /// </summary>
- Symbols
- }
- /// <summary>
- /// 当前键盘类型:
- /// </summary>
- public KeyboardType keyboardType;
- public GameObject canvas;
- /// <summary>
- /// 所有按键的父物体
- /// </summary>
- public Transform keysParent;
- /// <summary>
- /// 确认键被按下,用于全局需要通过确认键触发的情况
- /// </summary>
- public UnityAction OnEnterClicked;
- private TMP_InputField curTMPInputField;
- private InputField curInputField;
- public TextMeshProUGUI m_TextMeshPro;
- public Button[] letters;
- public Image capslock;
- /// <summary>
- /// 大写的时候要亮的一个绿灯
- /// </summary>
- public GameObject capslockDot;
- /// <summary>
- /// 数字按钮
- /// </summary>
- public GameObject num;
- public GameObject symbol;
- public GameObject abc;
- /// <summary>
- /// 存放letters中默认的键值
- /// </summary>
- string[] letters4Change;
- /// <summary>
- /// 用于数字键盘,存放与字母键盘一一对应的数字和符号
- /// </summary>
- public string[] num4Change;
- /// <summary>
- /// 用于符号键盘,存放与字母键盘一一对应的符号
- /// </summary>
- public string[] symbols4Change;
- /// <summary>
- /// 0是非按下,1是按下
- /// </summary>
- public Sprite[] capslockStatus;
- public TextMeshProUGUI inputfield;
- Ray ray = new Ray();
- RaycastHit hitInfo;
- /// <summary>
- /// 焦点
- /// </summary>
- public Transform focus;
- /// <summary>
- /// 开始显示焦点的距离
- /// </summary>
- float showFocusDistance = 0.05f;
- /// <summary>
- /// 焦点在此距离的时候不再变小,即localScale为1
- /// </summary>
- float focusNearDistance = 0.01f;
- /// <summary>
- /// 拖动键盘的把手
- /// </summary>
- public Image handler;
- /// <summary>
- /// 0是Normal,1是Enter,2是Press
- /// </summary>
- public Sprite[] handlerStatus;
- public GameObject keyboardHover;
- public SpatialAnchor spatialAnchor;
- private void Awake()
- {
- Button[] buttons = keysParent.GetComponentsInChildren<Button>();
- foreach (Button button in buttons)
- {
- button.onClick.AddListener(() => OnKeyClicked(button));
- }
- abc.SetActive(false);
- letters4Change = new string[letters.Length];
- for (int i = 0; i < letters.Length; i++)
- {
- letters4Change[i] = letters[i].name;
- }
- //扫描按钮事件注册
- QRScannerManager.RegisterCompleteListener(OnScanComplete);
- }
- private void KeyboardRecenterNear(bool enabled = false)
- {
- transform.Recenter(new Vector3(-0.015f, -0.15f, 0.4f), false);
- transform.localScale = new Vector3(0.0001806142f, 0.0001806142f, 1);
- }
- private void KeyboardRecenterFar(bool enabled = false)
- {
- transform.Recenter(new Vector3(-0.015f, -0.15f, 0.6f), false);
- transform.localScale = new Vector3(0.0001806142f, 0.0001806142f, 1);
- }
- void OnEnable()
- {
- //SystemManager.OnPowerPressed += KeyboardRecenter;
- //#if UNITY_EDITOR
- // foreach (SpatialButton button in letters)
- // {
- // button.Text = button.name;
- // }
- // foreach (SpatialButton button in symbols)
- // {
- // button.Text = button.name;
- // }
- //#endif
- }
- private void OnDisable()
- {
- //WristPanel.Left.RemoveListener(KeyboardRecenter);
- //SystemManager.OnPowerPressed -= KeyboardRecenter;
- }
- private void Start()
- {
- if (Application.isPlaying)
- {
- ChangeLettersToLower();
- //ChangeSymbols("#+=");
- }
- }
- // Update is called once per frame
- void Update()
- {
- //if (Input.GetKeyDown(KeyCode.T))
- //{
- // KeyboardRecenter();
- //}
- //transform.rotation = HMDPoseTracker.Instance.Head.rotation * Quaternion.Euler(0, 180, 0);
- if (InputSystem.CurrentActiveControllerType == ControllerType.HandTracking)
- {
- if (Vector3.Distance(transform.position, HMDPoseTracker.Instance.Head.position) > 0.5f)
- {
- InputSystem.leftHand.RaycastInteraction = true;
- InputSystem.rightHand.RaycastInteraction = true;
- }
- else
- {
- InputSystem.leftHand.RaycastInteraction = false;
- InputSystem.rightHand.RaycastInteraction = false;
- }
- }
- if (ARHandManager.rightHand != null && ARHandManager.rightHand.Exist && ARHandManager.rightHand.InView(HandJointType.Index_4))
- {
- ray.origin = ARHandManager.rightHand.GetJointData(HandJointType.Index_4).position;
- ray.direction = -transform.forward;
- }
- else if (ARHandManager.leftHand != null && ARHandManager.leftHand.Exist && ARHandManager.leftHand.InView(HandJointType.Index_4))
- {
- ray.origin = ARHandManager.leftHand.GetJointData(HandJointType.Index_4).position;
- ray.direction = -transform.forward;
- }
- if (Physics.Raycast(ray, out hitInfo))
- {
- if (hitInfo.distance < showFocusDistance)
- {
- focus.gameObject.SetActive(true);
- focus.position = hitInfo.point;
- focus.localScale = Vector3.one * (1f * (Math.Clamp(hitInfo.distance - focusNearDistance, 0, 100)) / (showFocusDistance - focusNearDistance) + 1.0f);
- }
- else
- {
- focus.gameObject.SetActive(false);
- }
- }
- }
- public void Show(InputField inputField)
- {
- if (InputSystem.CurrentActiveControllerType == ControllerType.HandTracking)
- {
- InputSystem.leftHand.RaycastInteraction = false;
- InputSystem.rightHand.RaycastInteraction = false;
- KeyboardRecenterNear();
- }
- else
- {
- KeyboardRecenterFar();
- }
- curInputField = inputField;
- m_TextMeshPro.text = inputField.text;
- gameObject.SetActive(true);
- }
- public void Show(TMP_InputField inputField)
- {
- if (InputSystem.CurrentActiveControllerType == ControllerType.HandTracking)
- {
- InputSystem.leftHand.RaycastInteraction = false;
- InputSystem.rightHand.RaycastInteraction = false;
- KeyboardRecenterNear();
- }
- else
- {
- KeyboardRecenterFar();
- }
- curTMPInputField = inputField;
- m_TextMeshPro.text = inputField.text;
- gameObject.SetActive(true);
- }
- public void Hide()
- {
- StartCoroutine(WaitThenHide());
- }
- IEnumerator WaitThenHide()
- {
- yield return new WaitForSeconds(0.5f);
- InputSystem.leftHand.RaycastInteraction = true;
- InputSystem.rightHand.RaycastInteraction = true;
- curInputField = null;
- curTMPInputField = null;
- gameObject.SetActive(false);
- }
- void ExchangeLetters()
- {
- if (keyboardType != KeyboardType.Uppercase)
- {
- ChangeLettersToUpper();
- }
- else
- {
- ChangeLettersToLower();
- }
- }
- void ChangeLettersToUpper()
- {
- keyboardType = KeyboardType.Uppercase;
- for (int i = 0; i < letters.Length; i++)
- {
- letters[i].name = letters4Change[i].ToUpper();
- letters[i].GetComponentInChildren<TextMeshProUGUI>().text = letters[i].name;
- }
- //CapsLock按下状态
- capslock.sprite = capslockStatus[1];
- capslockDot.SetActive(true);
- }
- void ChangeLettersToLower()
- {
- keyboardType = KeyboardType.LowerCase;
- for (int i = 0; i < letters.Length; i++)
- {
- letters[i].name = letters4Change[i].ToLower();
- letters[i].GetComponentInChildren<TextMeshProUGUI>().text = letters[i].name;
- }
- //CapsLock非按下状态
- capslock.sprite = capslockStatus[0];
- capslockDot.SetActive(false);
- }
- void ToggleNum()
- {
- //keyboardType = KeyboardType.Numbers;
- //num.sprite = capslockStatus[1];
- for (int i = 0; i < letters.Length; i++)
- {
- letters[i].name = num4Change[i];
- letters[i].GetComponentInChildren<TextMeshProUGUI>().text = num4Change[i];
- }
- }
- void ToggleSymbol()
- {
- //keyboardType = KeyboardType.Symbols;
- //symbol.sprite = capslockStatus[1];
- for (int i = 0; i < letters.Length; i++)
- {
- letters[i].name = symbols4Change[i];
- letters[i].GetComponentInChildren<TextMeshProUGUI>().text = symbols4Change[i];
- }
- }
- //void ChangeSymbols(string target = "")
- //{
- // if (symbol.name == "123" || target == "#+=")
- // {
- // symbol.name = "#+=";
- // symbol.GetComponentInChildren<TextMeshProUGUI>().text = symbol.name;
- // for (int i = 0; i < symbols.Length; i++)
- // {
- // symbols[i].name = num4Change[i];
- // symbols[i].GetComponentInChildren<TextMeshProUGUI>().text = num4Change[i];
- // }
- // }
- // else if (symbol.name == "#+=" || target == "123")
- // {
- // symbol.name = "123";
- // symbol.GetComponentInChildren<TextMeshProUGUI>().text = symbol.name;
- // for (int i = 0; i < symbols.Length; i++)
- // {
- // symbols[i].name = symbols4Change[i];
- // symbols[i].GetComponentInChildren<TextMeshProUGUI>().text = symbols4Change[i];
- // }
- // }
- //}
- public void OnKeyHoverEnter(string key)
- {
- Debug.Log("OnKeyHoverEnter: " + key);
- }
- public void OnStartPress(string key)
- {
- Debug.Log("OnStartPress: " + key);
- }
- public void OnPressing(string key)
- {
- Debug.Log("OnPressing: " + key);
- }
- //OnKeyDown
- public void OnKeyDown(string key)
- {
- Debug.Log("OnKeyDown: " + key);
- }
- //OnKeyUp
- public void OnKeyUp(string key)
- {
- Debug.Log("OnKeyUp: " + key);
- }
- public void OnEndPress(string key)
- {
- Debug.Log("OnEndPress: " + key);
- }
- public void OnKeyClicked(Button sender)
- {
- switch (sender.name)
- {
- case "Del":
- if (m_TextMeshPro.text.Length > 0)
- {
- m_TextMeshPro.text = m_TextMeshPro.text.Substring(0, m_TextMeshPro.text.Length - 1);
- }
- break;
- case "Clear":
- m_TextMeshPro.text = "";
- break;
- case "CapsLock":
- //num.sprite = capslockStatus[0];
- //symbol.sprite = capslockStatus[0];
- ExchangeLetters();
- break;
- case "123":
- //if (keyboardType == KeyboardType.Numbers)
- //{
- // //num.sprite = capslockStatus[0];
- // ChangeLettersToLower();
- //}
- //else
- capslock.transform.parent.gameObject.SetActive(false);
- num.SetActive(false);
- symbol.SetActive(false);
- abc.SetActive(true);
- {
- //capslock.sprite = capslockStatus[0];
- //capslockDot.SetActive(false);
- //symbol.sprite = capslockStatus[0];
- ToggleNum();
- }
- break;
- case "#+=":
- //if (keyboardType == KeyboardType.Symbols)
- //{
- // //symbol.sprite = capslockStatus[0];
- // ChangeLettersToLower();
- //}
- //else
- capslock.transform.parent.gameObject.SetActive(false);
- num.SetActive(false);
- symbol.SetActive(false);
- abc.SetActive(true);
- {
- //capslock.sprite = capslockStatus[0];
- //capslockDot.SetActive(false);
- //num.sprite = capslockStatus[0];
- ToggleSymbol();
- }
- break;
- case "ABC":
- capslock.transform.parent.gameObject.SetActive(true);
- num.SetActive(true);
- symbol.SetActive(true);
- abc.SetActive(false);
- if (keyboardType == KeyboardType.Uppercase)
- {
- ChangeLettersToUpper();
- }
- else
- {
- ChangeLettersToLower();
- }
- break;
- case "QR":
- StartScan();
- break;
- case "Return":
- if (OnEnterClicked != null)
- {
- OnEnterClicked.Invoke();
- }
- else
- {
- //m_TextMeshPro.text += "\r\n";
- }
- break;
- case "Keyboard":
- Hide();
- break;
- default:
- m_TextMeshPro.text += sender.name;
- break;
- }
- SetInputField(m_TextMeshPro.text);
- }
- void SetInputField(string text)
- {
- if (m_TextMeshPro != null)
- m_TextMeshPro.text = text;
- if (curInputField != null)
- {
- curInputField.text = text;
- }
- if (curTMPInputField != null)
- {
- curTMPInputField.text = text;
- }
- }
- public void OnKeyHoverExit(string key)
- {
- Debug.Log("OnKeyHoverExit: " + key);
- }
- /// <summary>
- /// 0是normal,1是hover,2是press
- /// </summary>
- /// <param name="eventType"></param>
- public void OnHandlerEvent(int eventType)
- {
- handler.sprite = handlerStatus[eventType];
- keyboardHover.SetActive(eventType == 2 ? true : false);
- spatialAnchor.lookToHead = keyboardHover.activeSelf;
- }
- /// <summary>
- /// 点击扫描按钮调用此处
- /// </summary>
- public void StartScan()
- {
- canvas.SetActive(false);
- QRScannerManager.RequestOpen();
- InputSystem.leftHand.RaycastInteraction = true;
- InputSystem.rightHand.RaycastInteraction = true;
- }
- /// <summary>
- /// 扫描完毕回调此处
- /// </summary>
- /// <param name="status"></param>
- /// <param name="content"></param>
- private void OnScanComplete(bool status, string content)
- {
- Debug.Log("QRScannerDemo, Scanning Complete: " + content);
- if (status)
- {
- InputSystem.leftHand.RaycastInteraction = false;
- InputSystem.rightHand.RaycastInteraction = false;
- if (canvas != null)
- canvas.SetActive(true);
- SetInputField(content);
- }
- else
- {
- if (canvas != null)
- canvas.SetActive(true);
- Toast.Show("未识别到二维码");
- }
- }
- }
- }
|