AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
XboxControllerHandlerTest.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 UnityEngine;
5 using UnityEngine.UI;
6 
7 namespace HoloToolkit.Unity.InputModule.Tests
8 {
10  {
11  [Header("Xbox Controller Test Options")]
12  [SerializeField]
13  private float movementSpeedMultiplier = 1f;
14 
15  [SerializeField]
16  private float rotationSpeedMultiplier = 1f;
17 
18  [SerializeField]
19  private XboxControllerMappingTypes resetButton = XboxControllerMappingTypes.XboxY;
20 
21  [SerializeField]
22  private Text debugText = null;
23 
24  private Vector3 initialPosition;
25  private Vector3 newPosition;
26  private Vector3 newRotation;
27 
28  protected virtual void Start()
29  {
30  initialPosition = transform.position;
31  }
32 
33  public override void OnSourceLost(SourceStateEventData eventData)
34  {
35  Debug.LogFormat("Joystick {0} with id: \"{1}\" Disconnected", GamePadName, eventData.SourceId);
36  base.OnSourceLost(eventData);
37  debugText.text = "No Controller Connected";
38  }
39 
40  public override void OnXboxInputUpdate(XboxControllerEventData eventData)
41  {
42  if (string.IsNullOrEmpty(GamePadName))
43  {
44  Debug.LogFormat("Joystick {0} with id: \"{1}\" Connected", eventData.GamePadName, eventData.SourceId);
45  }
46 
47  base.OnXboxInputUpdate(eventData);
48 
49  // Reset our new vectors
50  newPosition = Vector3.zero;
51  newRotation = Vector3.zero;
52 
53  // Assign new Position Data
54  newPosition.x += eventData.XboxLeftStickHorizontalAxis * movementSpeedMultiplier;
55  newPosition.z += eventData.XboxLeftStickVerticalAxis * movementSpeedMultiplier;
56  newPosition.y += eventData.XboxSharedTriggerAxis * movementSpeedMultiplier;
57 
58  transform.position += newPosition;
59 
60  // Assign new rotation data
61  newRotation.y += eventData.XboxRightStickHorizontalAxis * rotationSpeedMultiplier;
62 
63  transform.rotation *= Quaternion.Euler(newRotation);
64 
65  if (XboxControllerMapping.GetButton_Up(resetButton, eventData))
66  {
67  transform.position = initialPosition;
68  }
69 
70  debugText.text =
71  string.Format(
72  "{19}\n" +
73  "LS Horizontal: {0:0.000} Vertical: {1:0.000}\n" +
74  "RS Horizontal: {2:0.000} Vertical: {3:0.000}\n" +
75  "DP Horizontal: {4:0.000} Vertical: {5:0.000}\n" +
76  "Left Trigger: {6:0.000} Right Trigger: {7:0.000} Shared Trigger: {8:0.00}\n" +
77  "A: {9} B: {10} X: {11} Y: {12}\n" +
78  "LB: {13} RB: {14} " +
79  "LS: {15} RS: {16}\n" +
80  "View: {17} Menu: {18}\n",
83  eventData.XboxDpadHorizontalAxis, eventData.XboxDpadVerticalAxis,
84  eventData.XboxLeftTriggerAxis, eventData.XboxRightTriggerAxis, eventData.XboxSharedTriggerAxis,
85  eventData.XboxA_Pressed, eventData.XboxB_Pressed, eventData.XboxX_Pressed, eventData.XboxY_Pressed,
87  eventData.XboxLeftStick_Pressed, eventData.XboxRightStick_Pressed,
88  eventData.XboxView_Pressed, eventData.XboxMenu_Pressed,
89  GamePadName);
90  }
91  }
92 }
uint SourceId
The id of the source the event is from, for instance the hand id.
Defines the controller mapping for the input source.
override void OnXboxInputUpdate(XboxControllerEventData eventData)
override void OnSourceLost(SourceStateEventData eventData)
Describes an source state event that has a source id.
XboxControllerMappingTypes
Xbox Controller axis and button types.
static bool GetButton_Up(XboxControllerMappingTypes buttonType, XboxControllerEventData eventData)