AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
DebugPanelControllerInfo.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using UnityEngine;
6 
7 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
8 using System.Collections.Generic;
9 using UnityEngine.XR.WSA.Input;
10 #endif
11 
12 namespace HoloToolkit.Unity
13 {
14  public class DebugPanelControllerInfo : MonoBehaviour
15  {
16 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
17  private class ControllerState
18  {
19  public InteractionSourceHandedness Handedness;
20  public Vector3 PointerPosition;
21  public Quaternion PointerRotation;
22  public Vector3 GripPosition;
23  public Quaternion GripRotation;
24  public bool Grasped;
25  public bool MenuPressed;
26  public bool SelectPressed;
27  public float SelectPressedAmount;
28  public bool ThumbstickPressed;
29  public Vector2 ThumbstickPosition;
30  public bool TouchpadPressed;
31  public bool TouchpadTouched;
32  public Vector2 TouchpadPosition;
33  }
34 
35  private Dictionary<uint, ControllerState> controllers;
36 #endif
37 
38  // Text display label game objects
39  public TextMesh LeftInfoTextPointerPosition;
40  public TextMesh LeftInfoTextPointerRotation;
41  public TextMesh LeftInfoTextGripPosition;
42  public TextMesh LeftInfoTextGripRotation;
43  public TextMesh LeftInfoTextGripGrasped;
44  public TextMesh LeftInfoTextMenuPressed;
45  public TextMesh LeftInfoTextTriggerPressed;
49  public TextMesh LeftInfoTextTouchpadPressed;
50  public TextMesh LeftInfoTextTouchpadTouched;
54  public TextMesh RightInfoTextGripPosition;
55  public TextMesh RightInfoTextGripRotation;
56  public TextMesh RightInfoTextGripGrasped;
57  public TextMesh RightInfoTextMenuPressed;
58  public TextMesh RightInfoTextTriggerPressed;
65 
66  private void Awake()
67  {
68 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
69  controllers = new Dictionary<uint, ControllerState>();
70 
71  InteractionManager.InteractionSourceDetected += InteractionManager_InteractionSourceDetected;
72 
73  InteractionManager.InteractionSourceLost += InteractionManager_InteractionSourceLost;
74  InteractionManager.InteractionSourceUpdated += InteractionManager_InteractionSourceUpdated;
75 #endif
76  }
77 
78  private void Start()
79  {
80  if (DebugPanel.Instance != null)
81  {
82  DebugPanel.Instance.RegisterExternalLogCallback(GetControllerInfo);
83  }
84  }
85 
86 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
87  private void InteractionManager_InteractionSourceDetected(InteractionSourceDetectedEventArgs obj)
88  {
89  Debug.LogFormat("{0} {1} Detected", obj.state.source.handedness, obj.state.source.kind);
90 
91  if (obj.state.source.kind == InteractionSourceKind.Controller && !controllers.ContainsKey(obj.state.source.id))
92  {
93  controllers.Add(obj.state.source.id, new ControllerState { Handedness = obj.state.source.handedness });
94  }
95  }
96 
97  private void InteractionManager_InteractionSourceLost(InteractionSourceLostEventArgs obj)
98  {
99  Debug.LogFormat("{0} {1} Lost", obj.state.source.handedness, obj.state.source.kind);
100 
101  controllers.Remove(obj.state.source.id);
102  }
103 
104  private void InteractionManager_InteractionSourceUpdated(InteractionSourceUpdatedEventArgs obj)
105  {
106  ControllerState controllerState;
107  if (controllers.TryGetValue(obj.state.source.id, out controllerState))
108  {
109  obj.state.sourcePose.TryGetPosition(out controllerState.PointerPosition, InteractionSourceNode.Pointer);
110  obj.state.sourcePose.TryGetRotation(out controllerState.PointerRotation, InteractionSourceNode.Pointer);
111  obj.state.sourcePose.TryGetPosition(out controllerState.GripPosition, InteractionSourceNode.Grip);
112  obj.state.sourcePose.TryGetRotation(out controllerState.GripRotation, InteractionSourceNode.Grip);
113 
114  controllerState.Grasped = obj.state.grasped;
115  controllerState.MenuPressed = obj.state.menuPressed;
116  controllerState.SelectPressed = obj.state.selectPressed;
117  controllerState.SelectPressedAmount = obj.state.selectPressedAmount;
118  controllerState.ThumbstickPressed = obj.state.thumbstickPressed;
119  controllerState.ThumbstickPosition = obj.state.thumbstickPosition;
120  controllerState.TouchpadPressed = obj.state.touchpadPressed;
121  controllerState.TouchpadTouched = obj.state.touchpadTouched;
122  controllerState.TouchpadPosition = obj.state.touchpadPosition;
123  }
124  }
125 #endif
126 
127  private string GetControllerInfo()
128  {
129  string toReturn = string.Empty;
130 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
131  foreach (ControllerState controllerState in controllers.Values)
132  {
133  // Debug message
134  toReturn += string.Format("Hand: {0}\nPointer: Position: {1} Rotation: {2}\n" +
135  "Grip: Position: {3} Rotation: {4}\nGrasped: {5} " +
136  "MenuPressed: {6}\nSelect: Pressed: {7} PressedAmount: {8}\n" +
137  "Thumbstick: Pressed: {9} Position: {10}\nTouchpad: Pressed: {11} " +
138  "Touched: {12} Position: {13}\n\n",
139  controllerState.Handedness, controllerState.PointerPosition, controllerState.PointerRotation.eulerAngles,
140  controllerState.GripPosition, controllerState.GripRotation.eulerAngles, controllerState.Grasped,
141  controllerState.MenuPressed, controllerState.SelectPressed, controllerState.SelectPressedAmount,
142  controllerState.ThumbstickPressed, controllerState.ThumbstickPosition, controllerState.TouchpadPressed,
143  controllerState.TouchpadTouched, controllerState.TouchpadPosition);
144 
145  // Text label display
146  if (controllerState.Handedness.Equals(InteractionSourceHandedness.Left))
147  {
148  LeftInfoTextPointerPosition.text = controllerState.Handedness.ToString();
149  LeftInfoTextPointerRotation.text = controllerState.PointerRotation.ToString();
150  LeftInfoTextGripPosition.text = controllerState.GripPosition.ToString();
151  LeftInfoTextGripRotation.text = controllerState.GripRotation.ToString();
152  LeftInfoTextGripGrasped.text = controllerState.Grasped.ToString();
153  LeftInfoTextMenuPressed.text = controllerState.MenuPressed.ToString();
154  LeftInfoTextTriggerPressed.text = controllerState.SelectPressed.ToString();
155  LeftInfoTextTriggerPressedAmount.text = controllerState.SelectPressedAmount.ToString();
156  LeftInfoTextThumbstickPressed.text = controllerState.ThumbstickPressed.ToString();
157  LeftInfoTextThumbstickPosition.text = controllerState.ThumbstickPosition.ToString();
158  LeftInfoTextTouchpadPressed.text = controllerState.TouchpadPressed.ToString();
159  LeftInfoTextTouchpadTouched.text = controllerState.TouchpadTouched.ToString();
160  LeftInfoTextTouchpadPosition.text = controllerState.TouchpadPosition.ToString();
161  }
162  else if (controllerState.Handedness.Equals(InteractionSourceHandedness.Right))
163  {
164  RightInfoTextPointerPosition.text = controllerState.PointerPosition.ToString();
165  RightInfoTextPointerRotation.text = controllerState.PointerRotation.ToString();
166  RightInfoTextGripPosition.text = controllerState.GripPosition.ToString();
167  RightInfoTextGripRotation.text = controllerState.GripRotation.ToString();
168  RightInfoTextGripGrasped.text = controllerState.Grasped.ToString();
169  RightInfoTextMenuPressed.text = controllerState.MenuPressed.ToString();
170  RightInfoTextTriggerPressed.text = controllerState.SelectPressed.ToString();
171  RightInfoTextTriggerPressedAmount.text = controllerState.SelectPressedAmount.ToString();
172  RightInfoTextThumbstickPressed.text = controllerState.ThumbstickPressed.ToString();
173  RightInfoTextThumbstickPosition.text = controllerState.ThumbstickPosition.ToString();
174  RightInfoTextTouchpadPressed.text = controllerState.TouchpadPressed.ToString();
175  RightInfoTextTouchpadTouched.text = controllerState.TouchpadTouched.ToString();
176  RightInfoTextTouchpadPosition.text = controllerState.TouchpadPosition.ToString();
177  }
178  }
179 #endif
180  return toReturn.Substring(0, Math.Max(0, toReturn.Length - 2));
181  }
182  }
183 }
Script to control writing the Debug.Log output to a control.
Definition: DebugPanel.cs:12