VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_ControllerInputManager.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEngine.XR;
5 
6 namespace AVR.Core {
7  /// <summary>
8  /// Manages inputs of a controller such as button presses and track-pad inputs.
9  /// </summary>
10  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_core_1_1_a_v_r___controller_input_manager.html")]
12  {
13  /// <summary>
14  /// Boolean-returning events of a controller.
15  /// </summary>
16  public enum BoolEvent {
17  PRIMARY2DAXIS_TOUCH, // Primary Axis touched
18  PRIMARY2DAXIS_TOUCH_LEFT, // Primary Axis touched on the left side
19  PRIMARY2DAXIS_TOUCH_RIGHT, // Primary Axis touched on the right side
20  PRIMARY2DAXIS_TOUCH_LEFTORRIGHT, // Primary Axis touched on the left or right side
21  PRIMARY2DAXIS_TOUCH_MIDDLE, // Primary Axis touched in the middle
22  PRIMARY2DAXIS_CLICK, // Primary Axis clicked
23  PRIMARY2DAXIS_CLICK_LEFT, // Primary Axis clicked on the left side
24  PRIMARY2DAXIS_CLICK_RIGHT, // Primary Axis clicked on the right side
25  PRIMARY2DAXIS_CLICK_LEFTORRIGHT, // Primary Axis clicked on the left or right side
26  PRIMARY2DAXIS_CLICK_MIDDLE, // Primary Axis clicked in the middle
27  PRIMARY2DAXIS_CLICKDOWN, // Primary Axis clicked
28  PRIMARY2DAXIS_CLICKDOWN_LEFT, // Primary Axis clicked down on the left side
29  PRIMARY2DAXIS_CLICKDOWN_RIGHT, // Primary Axis clicked down on the right side
30  PRIMARY2DAXIS_CLICKDOWN_LEFTORRIGHT,// Primary Axis clicked down on the left or right side
31  PRIMARY2DAXIS_CLICKDOWN_MIDDLE, // Primary Axis clicked down in the middle
32  TRIGGER_ONTRIGGERDOWN, // Trigger pressed
33  TRIGGER_TRIGGER, // Trigger status
34  TRIGGER_ONTRIGGERUP, // Trigger released
35  MENUBUTTON, // Menubutton status
36  MENUBUTTON_DOWN, // Menubutton pressed
37  MENUBUTTON_UP, // Menubutton released
38  GRIP_GRIP, // Grip status
39  GRIP_ONGRIPDOWN, // Grip pressed
40  GRIP_ONGRIPUP, // Grip released
41  ANY_CANCEL, // Any event that might cancel something (triggerdown, menubuttondown, primaryaxis click)
42  ALWAYS_TRUE, // Is always true
43  ALWAYS_FALSE // Is always false
44  //TODO etc.
45  }
46 
47  /// <summary>
48  /// Floating-Point-number-returning events of a controller.
49  /// </summary>
50  public enum FloatEvent
51  {
52  PRIMARY2DAXIS_X,
53  PRIMARY2DAXIS_Y
54  }
55 
56  /// <summary>
57  /// 2D vector returning events of a controller.
58  /// </summary>
59  public enum Vec2Event
60  {
61  PRIMARY2DAXIS
62  }
63 
64  /// <summary>
65  /// True if the primary2D axis is being touched by the user
66  /// </summary>
67  public bool primary2DAxisTouch {
68  get {
69  controller.inputDevice.TryGetFeatureValue(CommonUsages.primary2DAxisTouch, out bool o);
70  return o;
71  }
72  }
73 
74  /// <summary>
75  /// True if the primary2D axis is being clicked
76  /// </summary>
77  public bool primary2DAxisClick {
78  get {
79  controller.inputDevice.TryGetFeatureValue(CommonUsages.primary2DAxisClick, out bool o);
80  return o;
81  }
82  }
83 
84  /// <summary>
85  /// True on the frame that the primary 2D axis is pressed down
86  /// </summary>
87  public bool primary2DAxisClickDown {
88  get {
89  controller.inputDevice.TryGetFeatureValue(CommonUsages.primary2DAxisClick, out bool o);
90  return o && !lastAxisClick;
91  }
92  }
93 
94  /// <summary>
95  /// True on the frame that the primary 2D axis is released
96  /// </summary>
97  public bool primary2DAxisClickUp {
98  get {
99  controller.inputDevice.TryGetFeatureValue(CommonUsages.primary2DAxisClick, out bool o);
100  return !o && lastAxisClick;
101  }
102  }
103 
104  /// <summary>
105  /// 2D value returned by the primary 2D axis.
106  /// </summary>
107  public Vector2 primary2DAxis {
108  get {
109  controller.inputDevice.TryGetFeatureValue(CommonUsages.primary2DAxis, out Vector2 o);
110  return o;
111  }
112  }
113 
114  /// <summary>
115  /// True if the trigger button is being pressed
116  /// </summary>
117  public bool triggerButton {
118  get {
119  controller.inputDevice.TryGetFeatureValue(CommonUsages.triggerButton, out bool o);
120  return o;
121  }
122  }
123 
124  /// <summary>
125  /// True on the frame the trigger-button is pressed down
126  /// </summary>
127  public bool triggerDown
128  {
129  get
130  {
131  controller.inputDevice.TryGetFeatureValue(CommonUsages.triggerButton, out bool o);
132  return o && !lastTrigger;
133  }
134  }
135 
136  /// <summary>
137  /// True on the frame the trigger-button is released
138  /// </summary>
139  public bool triggerUp
140  {
141  get
142  {
143  controller.inputDevice.TryGetFeatureValue(CommonUsages.triggerButton, out bool o);
144  return !o && lastTrigger;
145  }
146  }
147 
148  /// <summary>
149  /// True if the menu-button is pressed
150  /// </summary>
151  public bool menuButton
152  {
153  get
154  {
155  controller.inputDevice.TryGetFeatureValue(CommonUsages.menuButton, out bool o);
156  return o;
157  }
158  }
159 
160  /// <summary>
161  /// True on the frame the menubutton is pressed down
162  /// </summary>
163  public bool menuButtonDown
164  {
165  get
166  {
167  controller.inputDevice.TryGetFeatureValue(CommonUsages.menuButton, out bool o);
168  return o && ! lastMenuButton;
169  }
170  }
171 
172  /// <summary>
173  /// True on the frame the menubutton is released
174  /// </summary>
175  public bool menuButtonUp
176  {
177  get
178  {
179  controller.inputDevice.TryGetFeatureValue(CommonUsages.menuButton, out bool o);
180  return !o && lastMenuButton;
181  }
182  }
183 
184  /// <summary>
185  /// True if the grip is pressed
186  /// </summary>
187  public bool grip
188  {
189  get {
190  controller.inputDevice.TryGetFeatureValue(CommonUsages.gripButton, out bool o);
191  return o;
192  }
193  }
194 
195  /// <summary>
196  /// True on the frame the grip is pressed down
197  /// </summary>
198  public bool gripDown
199  {
200  get
201  {
202  controller.inputDevice.TryGetFeatureValue(CommonUsages.gripButton, out bool o);
203  return o && !lastGrip;
204  }
205  }
206 
207  /// <summary>
208  /// True on the frame the grip is released
209  /// </summary>
210  public bool gripUp
211  {
212  get
213  {
214  controller.inputDevice.TryGetFeatureValue(CommonUsages.gripButton, out bool o);
215  return !o && lastGrip;
216  }
217  }
218 
219  private bool lastTrigger = false;
220  private bool lastMenuButton = false;
221  private bool lastAxisClick = false;
222  private bool lastGrip = false;
223 
224  protected void LateUpdate()
225  {
226  controller.inputDevice.TryGetFeatureValue(CommonUsages.triggerButton, out lastTrigger);
227  controller.inputDevice.TryGetFeatureValue(CommonUsages.menuButton, out lastMenuButton);
228  controller.inputDevice.TryGetFeatureValue(CommonUsages.primary2DAxisClick, out lastAxisClick);
229  }
230 
231  /// <summary>
232  /// Returns the status of a given BoolEvent
233  /// </summary>
234  public bool getEventStatus(BoolEvent type)
235  {
236 #if AVR_NET
237  if(!IsOwner) return false;
238 #endif
239  switch (type)
240  {
241  case BoolEvent.PRIMARY2DAXIS_CLICK : { return primary2DAxisClick; }
242  case BoolEvent.PRIMARY2DAXIS_CLICK_LEFT: { return primary2DAxisClick && primary2DAxis.x < -0.5; }
243  case BoolEvent.PRIMARY2DAXIS_CLICK_RIGHT: { return primary2DAxisClick && primary2DAxis.x > 0.5; }
244  case BoolEvent.PRIMARY2DAXIS_CLICK_LEFTORRIGHT: { return primary2DAxisClick && Mathf.Abs(primary2DAxis.x) > 0.5; }
245  case BoolEvent.PRIMARY2DAXIS_CLICK_MIDDLE: { return primary2DAxisClick && Mathf.Abs(primary2DAxis.x) < 0.5; }
246  case BoolEvent.PRIMARY2DAXIS_CLICKDOWN: { return primary2DAxisClickDown; }
247  case BoolEvent.PRIMARY2DAXIS_CLICKDOWN_LEFT: { return primary2DAxisClickDown && primary2DAxis.x < -0.5; }
248  case BoolEvent.PRIMARY2DAXIS_CLICKDOWN_RIGHT: { return primary2DAxisClickDown && primary2DAxis.x > 0.5; }
249  case BoolEvent.PRIMARY2DAXIS_CLICKDOWN_LEFTORRIGHT: { return primary2DAxisClickDown && Mathf.Abs(primary2DAxis.x) > 0.5; }
250  case BoolEvent.PRIMARY2DAXIS_CLICKDOWN_MIDDLE: { return primary2DAxisClickDown && Mathf.Abs(primary2DAxis.x) < 0.5; }
251  case BoolEvent.PRIMARY2DAXIS_TOUCH: { return primary2DAxisTouch; }
252  case BoolEvent.PRIMARY2DAXIS_TOUCH_LEFT: { return primary2DAxisTouch && primary2DAxis.x < -0.5; }
253  case BoolEvent.PRIMARY2DAXIS_TOUCH_RIGHT: { return primary2DAxisTouch && primary2DAxis.x > 0.5; }
254  case BoolEvent.PRIMARY2DAXIS_TOUCH_LEFTORRIGHT: { return primary2DAxisTouch && Mathf.Abs(primary2DAxis.x) > 0.5; }
255  case BoolEvent.PRIMARY2DAXIS_TOUCH_MIDDLE: { return primary2DAxisTouch && Mathf.Abs(primary2DAxis.x) < 0.5; }
256  case BoolEvent.TRIGGER_TRIGGER: { return triggerButton; }
257  case BoolEvent.TRIGGER_ONTRIGGERDOWN: { return triggerDown; }
258  case BoolEvent.TRIGGER_ONTRIGGERUP: { return triggerUp; }
259  case BoolEvent.MENUBUTTON: { return menuButton; }
260  case BoolEvent.MENUBUTTON_DOWN: { return menuButtonDown; }
261  case BoolEvent.MENUBUTTON_UP: { return menuButtonUp; }
262  case BoolEvent.GRIP_GRIP: { return grip; }
263  case BoolEvent.GRIP_ONGRIPDOWN: { return gripDown; }
264  case BoolEvent.GRIP_ONGRIPUP: { return gripUp; }
265  case BoolEvent.ANY_CANCEL: { return menuButtonDown || triggerDown || primary2DAxisClick; }
266  case BoolEvent.ALWAYS_TRUE: { return true; }
267  case BoolEvent.ALWAYS_FALSE: { return false; }
268  default : { AVR_DevConsole.cwarn("getEventStatus does not recoginze value "+type, this); break; }
269  }
270  return false;
271  }
272 
273  /// <summary>
274  /// Returns the status of a given FloatEvent
275  /// </summary>
276  public float getEventStatus(FloatEvent type)
277  {
278  switch (type)
279  {
280  case FloatEvent.PRIMARY2DAXIS_X : { return primary2DAxis.x; }
281  case FloatEvent.PRIMARY2DAXIS_Y: { return primary2DAxis.y; }
282  default: { AVR_DevConsole.cwarn("getEventStatus does not recoginze value " + type, this); break; }
283  }
284  return -1.0f;
285  }
286 
287  /// <summary>
288  /// Returns the status of a given Vec2Event
289  /// </summary>
290  public Vector2 getEventStatus(Vec2Event type)
291  {
292  switch (type)
293  {
294  case Vec2Event.PRIMARY2DAXIS : { return primary2DAxis; }
295  default: { AVR_DevConsole.cwarn("getEventStatus does not recoginze value " + type, this); break; }
296  }
297  return Vector2.zero;
298  }
299  }
300 }
BoolEvent
Boolean-returning events of a controller.
Vector2 getEventStatus(Vec2Event type)
Returns the status of a given Vec2Event
FloatEvent
Floating-Point-number-returning events of a controller.
Manages inputs of a controller such as button presses and track-pad inputs.
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
float getEventStatus(FloatEvent type)
Returns the status of a given FloatEvent
bool getEventStatus(BoolEvent type)
Returns the status of a given BoolEvent
AVR_Component specifically attatched to an AVR_Controller. Needs to have an AVR_Controller on this ga...
Vec2Event
2D vector returning events of a controller.