FrontWheelTrackerEditor.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Globalization;
  3. using Tracking;
  4. using UnityEditor;
  5. using UnityEngine;
  6. [CustomEditor(typeof(FrontWheelTracker))]
  7. // ReSharper disable once CheckNamespace
  8. public class FrontWheelTrackerEditor : Editor
  9. {
  10. private SerializedProperty radiusProperty;
  11. private FrontWheelTracker fwt;
  12. private void OnEnable()
  13. {
  14. fwt = (FrontWheelTracker) target;
  15. if (fwt.isActiveAndEnabled)
  16. {
  17. radiusProperty = serializedObject.FindProperty("trackerDistanceToWheelCenterPoint");
  18. fwt.Init();
  19. }
  20. }
  21. public override void OnInspectorGUI()
  22. {
  23. serializedObject.Update();
  24. fwt.bicycleTransform = (Transform) EditorGUILayout.ObjectField("Bicycle Transform", fwt.bicycleTransform,
  25. typeof(Transform), false);
  26. if (fwt.isActiveAndEnabled)
  27. {
  28. EditorGUILayout.Space();
  29. EditorGUILayout.LabelField("Attach Vive Tracker to Wheel and press Button to calibrate");
  30. if (GUILayout.Button("Calibrate"))fwt.Calibrate();
  31. EditorGUILayout.Space();
  32. }
  33. fwt.trackerDistanceToWheelCenterPoint = EditorGUILayout.FloatField(
  34. new GUIContent("Tracker Distance to Hub",
  35. "Distance of Vive Tracker to The Center of the wheel hub on the x,z plane of the tracker"),
  36. fwt.trackerDistanceToWheelCenterPoint);
  37. if (!fwt.isActiveAndEnabled) return;
  38. EditorGUILayout.LabelField("Rotate Wheel to 90 degrees to calibrate radius");
  39. if (GUILayout.Button("Calibrate Radius")) CalibrateRadius();
  40. EditorGUILayout.Space();
  41. EditorGUILayout.LabelField("Data", EditorStyles.boldLabel);
  42. EditorGUILayout.LabelField("Zero Rot:", fwt.ZeroRot.ToString());
  43. EditorGUILayout.LabelField("Zero Pos:", fwt.ZeroPos.ToString());
  44. var rotation = fwt.Rotation;
  45. var position = fwt.Position;
  46. EditorGUILayout.Space();
  47. EditorGUILayout.LabelField("Relative Position", fwt.Position.ToString());
  48. EditorGUILayout.LabelField("Relative Rotation", fwt.Rotation.ToString());
  49. EditorGUILayout.LabelField("Calculated Steer Rotation",
  50. fwt.CalculatedSteerRotation.ToString(CultureInfo.CurrentCulture));
  51. }
  52. //TODO: call when not playing
  53. private void CalibrateRadius()
  54. {
  55. var posOnBikePlane = Vector3.ProjectOnPlane(fwt.Position, fwt.bicycleTransform.up);
  56. radiusProperty.floatValue = posOnBikePlane.x / Mathf.Cos(90 * Mathf.Deg2Rad);
  57. serializedObject.ApplyModifiedProperties();
  58. }
  59. }