Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.4 KiB

  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: UIElement that responds to VR hands and generates UnityEvents
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using UnityEngine.Events;
  8. using UnityEngine.UI;
  9. using System;
  10. namespace Valve.VR.InteractionSystem
  11. {
  12. //-------------------------------------------------------------------------
  13. [RequireComponent( typeof( Interactable ) )]
  14. public class UIElement : MonoBehaviour
  15. {
  16. public CustomEvents.UnityEventHand onHandClick;
  17. private Hand currentHand;
  18. //-------------------------------------------------
  19. void Awake()
  20. {
  21. Button button = GetComponent<Button>();
  22. if ( button )
  23. {
  24. button.onClick.AddListener( OnButtonClick );
  25. }
  26. }
  27. //-------------------------------------------------
  28. private void OnHandHoverBegin( Hand hand )
  29. {
  30. currentHand = hand;
  31. InputModule.instance.HoverBegin( gameObject );
  32. ControllerButtonHints.ShowButtonHint( hand, Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger );
  33. }
  34. //-------------------------------------------------
  35. private void OnHandHoverEnd( Hand hand )
  36. {
  37. InputModule.instance.HoverEnd( gameObject );
  38. ControllerButtonHints.HideButtonHint( hand, Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger );
  39. currentHand = null;
  40. }
  41. //-------------------------------------------------
  42. private void HandHoverUpdate( Hand hand )
  43. {
  44. if ( hand.GetStandardInteractionButtonDown() )
  45. {
  46. InputModule.instance.Submit( gameObject );
  47. ControllerButtonHints.HideButtonHint( hand, Valve.VR.EVRButtonId.k_EButton_SteamVR_Trigger );
  48. }
  49. }
  50. //-------------------------------------------------
  51. private void OnButtonClick()
  52. {
  53. onHandClick.Invoke( currentHand );
  54. }
  55. }
  56. #if UNITY_EDITOR
  57. //-------------------------------------------------------------------------
  58. [UnityEditor.CustomEditor( typeof( UIElement ) )]
  59. public class UIElementEditor : UnityEditor.Editor
  60. {
  61. //-------------------------------------------------
  62. // Custom Inspector GUI allows us to click from within the UI
  63. //-------------------------------------------------
  64. public override void OnInspectorGUI()
  65. {
  66. DrawDefaultInspector();
  67. UIElement uiElement = (UIElement)target;
  68. if ( GUILayout.Button( "Click" ) )
  69. {
  70. InputModule.instance.Submit( uiElement.gameObject );
  71. }
  72. }
  73. }
  74. #endif
  75. }