GestureManager.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Windows.Perception.Spatial;
  2. using Windows.UI.Input.Spatial;
  3. using Urho.SharpReality;
  4. namespace Urho
  5. {
  6. public class GesturesManager
  7. {
  8. readonly StereoApplication app;
  9. readonly SpatialStationaryFrameOfReference referenceFrame;
  10. readonly SpatialGestureRecognizer tap = new SpatialGestureRecognizer(SpatialGestureSettings.Tap | SpatialGestureSettings.DoubleTap);
  11. readonly SpatialGestureRecognizer manipulationTranslate = new SpatialGestureRecognizer(SpatialGestureSettings.ManipulationTranslate);
  12. readonly SpatialGestureRecognizer hold = new SpatialGestureRecognizer(SpatialGestureSettings.Hold);
  13. //TODO: handle Navigation/Rails (SpatialGestureSettings)
  14. public GesturesManager(StereoApplication app, SpatialStationaryFrameOfReference referenceFrame)
  15. {
  16. this.app = app;
  17. this.referenceFrame = referenceFrame;
  18. tap.Tapped += Tap_Tapped;
  19. hold.HoldCanceled += Hold_HoldCanceled;
  20. hold.HoldCompleted += Hold_HoldCompleted;
  21. hold.HoldStarted += Hold_HoldStarted;
  22. manipulationTranslate.ManipulationCanceled += ManipulationTranslate_ManipulationCanceled;
  23. manipulationTranslate.ManipulationCompleted += ManipulationTranslate_ManipulationCompleted;
  24. manipulationTranslate.ManipulationStarted += ManipulationTranslate_ManipulationStarted;
  25. manipulationTranslate.ManipulationUpdated += ManipulationTranslate_ManipulationUpdated;
  26. }
  27. public void HandleInteraction(SpatialInteraction interaction)
  28. {
  29. if (app.EnableGestureTapped)
  30. tap.CaptureInteraction(interaction);
  31. if (app.EnableGestureHold)
  32. hold.CaptureInteraction(interaction);
  33. if (app.EnableGestureManipulation)
  34. manipulationTranslate.CaptureInteraction(interaction);
  35. }
  36. void ManipulationTranslate_ManipulationUpdated(SpatialGestureRecognizer sender, SpatialManipulationUpdatedEventArgs args)
  37. {
  38. var data = args.TryGetCumulativeDelta(referenceFrame.CoordinateSystem);
  39. if (data != null)
  40. {
  41. var vector = new Vector3(data.Translation.X, data.Translation.Y, -data.Translation.Z);
  42. Application.InvokeOnMain(() => app.OnGestureManipulationUpdated(vector));
  43. }
  44. }
  45. void ManipulationTranslate_ManipulationStarted(SpatialGestureRecognizer sender, SpatialManipulationStartedEventArgs args)
  46. {
  47. Application.InvokeOnMain(() => app.OnGestureManipulationStarted());
  48. }
  49. void ManipulationTranslate_ManipulationCompleted(SpatialGestureRecognizer sender, SpatialManipulationCompletedEventArgs args)
  50. {
  51. var data = args.TryGetCumulativeDelta(referenceFrame.CoordinateSystem);
  52. if (data != null)
  53. {
  54. var vector = new Vector3(data.Translation.X, data.Translation.Y, -data.Translation.Z);
  55. Application.InvokeOnMain(() => app.OnGestureManipulationCompleted(vector));
  56. }
  57. }
  58. void ManipulationTranslate_ManipulationCanceled(SpatialGestureRecognizer sender, SpatialManipulationCanceledEventArgs args)
  59. {
  60. Application.InvokeOnMain(() => app.OnGestureManipulationCanceled());
  61. }
  62. void Hold_HoldStarted(SpatialGestureRecognizer sender, SpatialHoldStartedEventArgs args)
  63. {
  64. Application.InvokeOnMain(() => app.OnGestureHoldStarted());
  65. }
  66. void Hold_HoldCompleted(SpatialGestureRecognizer sender, SpatialHoldCompletedEventArgs args)
  67. {
  68. Application.InvokeOnMain(() => app.OnGestureHoldCompleted());
  69. }
  70. void Hold_HoldCanceled(SpatialGestureRecognizer sender, SpatialHoldCanceledEventArgs args)
  71. {
  72. Application.InvokeOnMain(() => app.OnGestureHoldCanceled());
  73. }
  74. void Tap_Tapped(SpatialGestureRecognizer sender, SpatialTappedEventArgs args)
  75. {
  76. if (args.TapCount == 1)
  77. Application.InvokeOnMain(() => app.OnGestureTapped());
  78. if (args.TapCount == 2)
  79. Application.InvokeOnMain(() => app.OnGestureDoubleTapped());
  80. }
  81. }
  82. }