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