PointerViewerKeyboard.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // /******************************************************************************
  2. // * File: PointerViewerKeyboard.cs
  3. // * Copyright (c) 2023 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
  4. // *
  5. // *
  6. // ******************************************************************************/
  7. using QCHT.Interactions.Core;
  8. using QCHT.Interactions.Hands;
  9. using QCHT.Samples.Menu;
  10. using UnityEngine;
  11. using UnityEngine.XR.Interaction.Toolkit;
  12. namespace QCHT.Samples.XRKeyboard
  13. {
  14. public class PointerViewerKeyboard : PointerViewer
  15. {
  16. private Transform _leftHand, _righHand;
  17. [SerializeField] private float distanceToDesactivate = 0.5f;
  18. private Transform _keyboardTransform = null;
  19. public Transform KeyboardTransform
  20. {
  21. get => _keyboardTransform;
  22. set => _keyboardTransform = value;
  23. }
  24. [SerializeField] private bool _shouldHideHands = false;
  25. private bool _shouldShowRightHandsAndRays = true;
  26. private bool _shouldShowLeftHandsAndRays = true;
  27. private void Start()
  28. {
  29. _leftHand = XRHandTrackingManager.GetOrCreate().LeftHand.transform;
  30. _righHand = XRHandTrackingManager.GetOrCreate().RightHand.transform;
  31. }
  32. // Update is called once per frame
  33. protected override void Update()
  34. {
  35. if (!updateRays) return;
  36. //base.Update();
  37. var currentShowRight = _shouldShowRightHandsAndRays;
  38. var currentShowLeft = _shouldShowLeftHandsAndRays;
  39. if (_keyboardTransform == null)
  40. return;
  41. var keyboardPosition = _keyboardTransform.position;
  42. _shouldShowRightHandsAndRays =
  43. !(Vector3.Distance(keyboardPosition, _righHand.position) <= distanceToDesactivate);
  44. _shouldShowLeftHandsAndRays =
  45. !(Vector3.Distance(keyboardPosition, _leftHand.position) <= distanceToDesactivate);
  46. leftController.SetActive(_shouldShowLeftHandsAndRays && leftIsTracked.IsInProgress());
  47. rightController.SetActive(_shouldShowRightHandsAndRays && rightIsTracked.IsInProgress());
  48. if (_shouldHideHands)
  49. {
  50. //Hide hands
  51. if (currentShowLeft != _shouldShowLeftHandsAndRays)
  52. {
  53. if (_leftHand.TryGetComponent(out XRBaseController xrController) && xrController.model != null &&
  54. xrController.model.TryGetComponent(out IHideable hideable))
  55. {
  56. if (_shouldShowLeftHandsAndRays)
  57. hideable.Show();
  58. else
  59. hideable.Hide();
  60. }
  61. }
  62. if (currentShowRight != _shouldShowRightHandsAndRays)
  63. {
  64. if (_righHand.TryGetComponent(out XRBaseController xrController) && xrController.model != null &&
  65. xrController.model.TryGetComponent(out IHideable hideable))
  66. {
  67. if (_shouldShowRightHandsAndRays)
  68. hideable.Show();
  69. else
  70. hideable.Hide();
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }