VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_UIInteractionProvider.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 
5 using AVR.Core;
6 
7 namespace AVR.UI {
8  /// <summary>
9  /// NOTE: If your EventSystem object has a InputSystemUIInputModule (new Unity input system) then any settings you set on the UIInteractionProvider will have *NO* effect. You will have to set them through the new Unity Input system.
10  /// Alternatively, simply use a regular StandaloneInputModule (you can ignore the red error message it display) instead of the InputSystemUIInputModule and make sure you have `Project Settings > Player > Active Input Handling` set to "Both".
11  /// This class provides UI Interaction through a VR controller.
12  /// Requires a AVR_UIRay to be set.
13  /// </summary>
14  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_u_i_1_1_a_v_r___u_i_interaction_provider.html")]
16  {
17  /// <summary>
18  /// The currently active interactionprovider. NOTE: only one interactionprovider will be active at a time.
19  /// </summary>
20  public static AVR_UIInteractionProvider currentActive
21  {
22  get { return _currentActive; }
23  set {
24  _currentActive = value;
25  VRInput.Instance.setEventCamera(value?.UIRay?.UICamera);
26  }
27  }
29 
31  public AVR_ControllerInputManager.BoolEvent mouseButton0Click;
32  [AVR.Core.Attributes.FoldoutGroup("Events")]
35  public AVR_ControllerInputManager.BoolEvent mouseButton0Up;
36  [AVR.Core.Attributes.FoldoutGroup("Events")]
39  public AVR_ControllerInputManager.BoolEvent mouseButton1Down;
40  [AVR.Core.Attributes.FoldoutGroup("Events")]
42 
43  [Header("Settings")]
44  /// <summary>
45  /// Only show UIRay when hovering over a canvas or always.
46  /// </summary>
47  public bool show_ray_only_on_hover = false;
48 
49  [Header("Pointer")]
50  /// <summary>
51  /// Pointer ray of this interactionprovider. Is required for the interactionprovider to work.
52  /// </summary>
53  public AVR_UIRay UIRay;
54 
55  // We use this variable as a timer in set().
56  private float stime = 0.0f;
57 
58  protected override void OnEnable() {
59  base.OnEnable();
60  stime = Time.realtimeSinceStartup;
61  set();
62  }
63 
64  protected override void OnDisable() {
65  base.OnDisable();
66  unset();
67  }
68 
69  protected override void Awake()
70  {
71  base.Awake();
72  UIRay = GetComponentInChildren<AVR_UIRay>();
73 
74  if(!UIRay) {
75  AVR_DevConsole.cerror("UIInteractionProvider requires an AVR_UIRay to function!", this);
76  Destroy(this);
77  }
78  }
79 
80  // Sets this UIPorvider as the currently active one.
81  void set() {
82 #if AVR_NET
83  if (IsOnline && !IsOwner) return;
84 #endif
85 
86  // TODO: This is a shitty workaround. AVR_Controller.Awake() calls *after* this, so inputManager is not set on the first frame.
87  // We are retrying for 1 second.
88  if (Time.realtimeSinceStartup - stime < 1.0 && !controller.inputManager) {
89  Invoke("set", 0.0f);
90  return;
91  }
92 
93  if(currentActive) {
94  AVR_DevConsole.warn("Attempted to set UIInteractionProvider \"" + gameObject.name + "\" but another UIInteractionprovider is already active! Only one can be active at a time.");
95  currentActive.enabled = false;
96  }
97  else if(!controller.inputManager) {
98  AVR_DevConsole.error("AVR_UIInteractionProvider references a controller that has no inputmanager!");
99  return;
100  }
101  else if(!VRInput.Instance) {
102  AVR_DevConsole.error("AVR_UIInteractionProvider can only be used with a VRInput component!");
103  return;
104  }
105  currentActive = this;
106  }
107 
108  // Un-Sets this UIPorvider as the currently active one.
109  void unset() {
110 #if AVR_NET
111  if (IsOnline && !IsOwner) return;
112 #endif
113 
114  currentActive = null;
115  }
116 
117  void Update() {
118 #if AVR_NET
119  if (IsOnline && !IsOwner) return;
120 #endif
121  // Here we show/hide the UIRay if needed and also set the length of the ray to the distance to the canvas.
122  if(AVR_Canvas.active_canvases?.Count>0) {
123  float min_dist = float.PositiveInfinity;
124  AVR_Canvas closest_canv = AVR_Canvas.active_canvases[0];
125 
126  foreach(AVR_Canvas canv in AVR_Canvas.active_canvases) {
127  if(canv.isInteractible && canv.GetPlane().Raycast(new Ray(UIRay.UICamera.transform.position, UIRay.UICamera.transform.forward), out float dist)) {
128  min_dist = Mathf.Min(dist, min_dist);
129  closest_canv = canv;
130  }
131  }
132  UIRay.max_length = min_dist;
133  UIRay.canvasNormal = -closest_canv.transform.forward;
134  UIRay.show();
135  }
136  else {
137  UIRay.max_length = 30;
138  if (show_ray_only_on_hover) UIRay.hide();
139  }
140  }
141  }
142 }
BoolEvent
Boolean-returning events of a controller.
Represents a UI pointer-ray.
Definition: AVR_UIRay.cs:12
NOTE: If your EventSystem object has a InputSystemUIInputModule (new Unity input system) then any set...
static List< AVR_Canvas > active_canvases
A list of all AVR_Canvases that the user is currently interacting with. NOTE: This is not the same as...
Definition: AVR_Canvas.cs:38
bool isInteractible
If set to false, the UIInteractionprovider will disregard this canvas entirely.
Definition: AVR_Canvas.cs:22
AVR_ControllerInputManager.BoolEvent mouseButton1Click
Manages inputs of a controller such as button presses and track-pad inputs.
static VRInput Instance
Singleton instance of this class. There should only be one VRInput component.
Definition: VRInput.cs:19
AVR_ControllerInputManager.BoolEvent mouseButton0Down
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
AVR_ControllerInputManager.BoolEvent mouseButton1Up
Assigns given attributes to a foldout group in the inspector. The way these are drawn is determined b...
Plane GetPlane()
Returns the 3d plane this canvas lies in
Definition: AVR_Canvas.cs:121
AVR_Component specifically attatched to an AVR_Controller. Needs to have an AVR_Controller on this ga...
This class overrides the base Input with Input from our VR components, thus enabling UI Interaction w...
Definition: VRInput.cs:14
AVR_UIRay UIRay
Pointer ray of this interactionprovider. Is required for the interactionprovider to work...
Represents a canvas for VR interaction purposes. Allows for interaction with the AVR_UIInteractionPro...
Definition: AVR_Canvas.cs:17
static AVR_UIInteractionProvider _currentActive
Camera UICamera
Camera that is the eventCamera for the underlying inputsystem. If left blank, a Camera with preset va...
Definition: AVR_UIRay.cs:18