From 7ec315f364c9726eea2111f19e5cd31fc3b574e1 Mon Sep 17 00:00:00 2001 From: Lindsay Kubasik Date: Tue, 4 Dec 2018 13:00:21 -0800 Subject: [PATCH] Interactiontracker sample updates (#308) * Updating InteractionTracker Samples & adding SDK 17763 Making incremental updates to samples using InteractionTracker. -Add mousewheel & pen functionality to PullToAnimate -Add pen and mouse click functionality to Curtain -Update Curtain to use NaturalMotion InertiaModifiers -Add pen & mousewheel support to Interactions3D -Fix bugs resulting from failed TryRedirect* calls -Add runtime & compile-time checks for version-dependent features --- SampleGallery/MainPage.xaml.cs | 3 +- SampleGallery/SampleGallery.csproj | 2 +- .../Samples/SDK 14393/Curtain/Curtain.cs | 71 ++++++++++++++----- .../SDK 14393/PullToAnimate/PullToAnimate.cs | 17 ++++- .../Behaviors/InteractionBehavior.cs | 2 +- .../Interactions3D/Interactions3D.cs | 16 +++-- .../PullToRefresh/PullToRefresh.xaml.cs | 17 ++--- SamplesCommon/SamplesCommon/CommonBuild.props | 1 + 8 files changed, 92 insertions(+), 37 deletions(-) diff --git a/SampleGallery/MainPage.xaml.cs b/SampleGallery/MainPage.xaml.cs index 3155d9a..8762b71 100644 --- a/SampleGallery/MainPage.xaml.cs +++ b/SampleGallery/MainPage.xaml.cs @@ -322,7 +322,8 @@ namespace CompositionSampleGallery _14393, // Anniversary Update (1607) _15063, // Creators Update (1703) _16299, // Fall Creators Update - _17134 // Version 1803 + _17134, // Version 1803 + _17763 // Version 1810 }; public RuntimeSupportedSDKs() diff --git a/SampleGallery/SampleGallery.csproj b/SampleGallery/SampleGallery.csproj index 3aa7bb2..8c594fc 100644 --- a/SampleGallery/SampleGallery.csproj +++ b/SampleGallery/SampleGallery.csproj @@ -15,7 +15,7 @@ 512 {A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} CompositionSampleGallery_TemporaryKey.pfx - 10.0.17134.0 + 10.0.17763.0 10.0.15063.0 True Always diff --git a/SampleGallery/Samples/SDK 14393/Curtain/Curtain.cs b/SampleGallery/Samples/SDK 14393/Curtain/Curtain.cs index 9904574..0dbf883 100644 --- a/SampleGallery/Samples/SDK 14393/Curtain/Curtain.cs +++ b/SampleGallery/Samples/SDK 14393/Curtain/Curtain.cs @@ -13,6 +13,7 @@ //********************************************************* using ExpressionBuilder; +using System; using System.Numerics; using Windows.Foundation; using Windows.UI.Composition; @@ -59,9 +60,9 @@ namespace CompositionSampleGallery _interactionSource = VisualInteractionSource.Create(_root); _interactionSource.PositionYSourceMode = InteractionSourceMode.EnabledWithInertia; - _interactionSource.ManipulationRedirectionMode = VisualInteractionSourceRedirectionMode.CapableTouchpadOnly; _tracker.InteractionSources.Add(_interactionSource); _tracker.MaxPosition = new Vector3(0, (float)Root.ActualHeight, 0); + SetDefaultInertia(); // // Use the Tacker's Position (negated) to apply to the Offset of the Image. @@ -72,19 +73,41 @@ namespace CompositionSampleGallery private void ActivateSpringForce() { - var dampingConstant = 5; - var springConstant = 20; - var modifier = InteractionTrackerInertiaMotion.Create(_compositor); +#if SDKVERSION_15063 + // + // On newer builds, use the Spring NaturalMotion + // + if (MainPage.RuntimeCapabilities.IsSdkVersionRuntimeSupported(RuntimeSupportedSDKs.SDKVERSION._15063)) + { + var modifier = InteractionTrackerInertiaNaturalMotion.Create(_compositor); + var springAnimation = _compositor.CreateSpringScalarAnimation(); + springAnimation.Period = TimeSpan.FromSeconds(.15); + springAnimation.DampingRatio = .4f; + springAnimation.FinalValue = 0.0f; - // Set the condition to true (always) - modifier.SetCondition((BooleanNode)true); + modifier.Condition = _compositor.CreateExpressionAnimation("true"); + modifier.NaturalMotion = springAnimation; + _tracker.ConfigurePositionYInertiaModifiers(new InteractionTrackerInertiaModifier[] { modifier }); + } + // + // On older builds, use a custom force that behaves like a spring + // + else +#endif + { + var dampingConstant = 5; + var springConstant = 20; + var modifier = InteractionTrackerInertiaMotion.Create(_compositor); - // Define a spring-like force, anchored at position 0. - var target = ExpressionValues.Target.CreateInteractionTrackerTarget(); - modifier.SetMotion((-target.Position.Y * springConstant) - (dampingConstant * target.PositionVelocityInPixelsPerSecond.Y)); + // Set the condition to true (always) + modifier.SetCondition((BooleanNode)true); - _tracker.ConfigurePositionYInertiaModifiers(new InteractionTrackerInertiaModifier[] { modifier }); + // Define a spring-like force, anchored at position 0. + var target = ExpressionValues.Target.CreateInteractionTrackerTarget(); + modifier.SetMotion((-target.Position.Y * springConstant) - (dampingConstant * target.PositionVelocityInPixelsPerSecond.Y)); + _tracker.ConfigurePositionYInertiaModifiers(new InteractionTrackerInertiaModifier[] { modifier }); + } } private void Grid_SizeChanged(object sender, SizeChangedEventArgs e) { @@ -156,17 +179,33 @@ namespace CompositionSampleGallery _tracker.ConfigurePositionYInertiaModifiers(new InteractionTrackerInertiaModifier[] { modifier }); } - private void ClearInertiaModifiers() + // + // Create a snap point in the "down" position using default inertia + // + private void SetDefaultInertia() { - _tracker.ConfigurePositionYInertiaModifiers(null); + var modifier = InteractionTrackerInertiaRestingValue.Create(_compositor); + modifier.RestingValue = _compositor.CreateExpressionAnimation("0"); + modifier.Condition = _compositor.CreateExpressionAnimation("true"); + _tracker.ConfigurePositionYInertiaModifiers(new InteractionTrackerInertiaModifier[] { modifier }); } private void Root_PointerPressed(object sender, PointerRoutedEventArgs e) { - if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch) + if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse) { - // Tell the system to use the gestures from this pointer point (if it can). - _interactionSource.TryRedirectForManipulation(e.GetCurrentPoint(Root)); + _tracker.TryUpdatePositionWithAdditionalVelocity(new Vector3(0.0f, 1000.0f, 0.0f)); + } + else + { + try + { + _interactionSource.TryRedirectForManipulation(e.GetCurrentPoint(Root)); + } + catch (Exception) + { + //catch to avoid app crash based on unauthorized input + } } } @@ -177,7 +216,7 @@ namespace CompositionSampleGallery switch (((ListBox)sender).SelectedIndex) { case 0: - ClearInertiaModifiers(); + SetDefaultInertia(); break; case 1: diff --git a/SampleGallery/Samples/SDK 14393/PullToAnimate/PullToAnimate.cs b/SampleGallery/Samples/SDK 14393/PullToAnimate/PullToAnimate.cs index 21ddb17..b62f7f5 100644 --- a/SampleGallery/Samples/SDK 14393/PullToAnimate/PullToAnimate.cs +++ b/SampleGallery/Samples/SDK 14393/PullToAnimate/PullToAnimate.cs @@ -241,6 +241,13 @@ namespace CompositionSampleGallery _interactionSource = VisualInteractionSource.Create(_backgroundVisual); _interactionSource.PositionYSourceMode = InteractionSourceMode.EnabledWithInertia; + +#if SDKVERSION_17763 + if (MainPage.RuntimeCapabilities.IsSdkVersionRuntimeSupported(RuntimeSupportedSDKs.SDKVERSION._17763)) + { + _interactionSource.ManipulationRedirectionMode = VisualInteractionSourceRedirectionMode.CapableTouchpadAndPointerWheel; + } +#endif _tracker.InteractionSources.Add(_interactionSource); var trackerNode = _tracker.GetReference(); @@ -372,12 +379,16 @@ namespace CompositionSampleGallery private void Pointer_Pressed(object sender, PointerRoutedEventArgs e) { - if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch) + try { - //Capture pointer to system for gestures _interactionSource.TryRedirectForManipulation(e.GetCurrentPoint(Root)); } - + catch (Exception) + { + // + // Catch to avoid app crash based on unauthorized input. + // + } } private Compositor _compositor; diff --git a/SampleGallery/Samples/SDK 14393/SwipeScroller/Behaviors/InteractionBehavior.cs b/SampleGallery/Samples/SDK 14393/SwipeScroller/Behaviors/InteractionBehavior.cs index 0e850f1..c1cceed 100644 --- a/SampleGallery/Samples/SDK 14393/SwipeScroller/Behaviors/InteractionBehavior.cs +++ b/SampleGallery/Samples/SDK 14393/SwipeScroller/Behaviors/InteractionBehavior.cs @@ -115,7 +115,7 @@ namespace CompositionSampleGallery.Samples.SDK_14393.SwipeScroller.Behaviors { interactionSource.TryRedirectForManipulation(a.GetCurrentPoint(s as UIElement)); } - catch (UnauthorizedAccessException) + catch (Exception) { // Ignoring the failed redirect to prevent app crashing } diff --git a/SampleGallery/Samples/SDK 15063/Interactions3D/Interactions3D.cs b/SampleGallery/Samples/SDK 15063/Interactions3D/Interactions3D.cs index 73cd0c3..741c226 100644 --- a/SampleGallery/Samples/SDK 15063/Interactions3D/Interactions3D.cs +++ b/SampleGallery/Samples/SDK 15063/Interactions3D/Interactions3D.cs @@ -89,7 +89,12 @@ namespace CompositionSampleGallery _interactionSource = VisualInteractionSource.Create(_rootContainer); _interactionSource.ScaleSourceMode = InteractionSourceMode.EnabledWithInertia; _interactionSource.PositionXSourceMode = InteractionSourceMode.EnabledWithInertia; - +#if SDKVERSION_17763 + if (MainPage.RuntimeCapabilities.IsSdkVersionRuntimeSupported(RuntimeSupportedSDKs.SDKVERSION._17763)) + { + _interactionSource.ManipulationRedirectionMode = VisualInteractionSourceRedirectionMode.CapableTouchpadAndPointerWheel; + } +#endif _tracker = InteractionTracker.CreateWithOwner(_compositor, this); _tracker.MinScale = 0.6f; _tracker.MaxScale = 5.0f; @@ -661,14 +666,15 @@ namespace CompositionSampleGallery } private void Root_PointerPressed(object sender, PointerRoutedEventArgs e) { - if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch) + try { - // Tell the system to use the gestures from this pointer point (if it can). _interactionSource.TryRedirectForManipulation(e.GetCurrentPoint(Root)); - - // Stop ambient animations _tracker.TryUpdatePositionBy(Vector3.Zero); } + catch (Exception) + { + return; + } } public void CustomAnimationStateEntered(InteractionTracker sender, InteractionTrackerCustomAnimationStateEnteredArgs args) diff --git a/SampleGallery/Samples/SDK 15063/PullToRefresh/PullToRefresh.xaml.cs b/SampleGallery/Samples/SDK 15063/PullToRefresh/PullToRefresh.xaml.cs index cf0a146..182a172 100644 --- a/SampleGallery/Samples/SDK 15063/PullToRefresh/PullToRefresh.xaml.cs +++ b/SampleGallery/Samples/SDK 15063/PullToRefresh/PullToRefresh.xaml.cs @@ -113,17 +113,14 @@ namespace CompositionSampleGallery private void Window_PointerPressed(object sender, PointerRoutedEventArgs e) { - if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Touch) + try { - try - { - // Tell the system to use the gestures from this pointer point (if it can). - _interactionSource.TryRedirectForManipulation(e.GetCurrentPoint(null)); - } - catch (UnauthorizedAccessException) - { - // Ignoring the failed redirect to prevent app crashing - } + // Tell the system to use the gestures from this pointer point (if it can). + _interactionSource.TryRedirectForManipulation(e.GetCurrentPoint(null)); + } + catch (Exception) + { + // Ignoring the failed redirect to prevent app crashing } } diff --git a/SamplesCommon/SamplesCommon/CommonBuild.props b/SamplesCommon/SamplesCommon/CommonBuild.props index 8fe0252..0aa0161 100644 --- a/SamplesCommon/SamplesCommon/CommonBuild.props +++ b/SamplesCommon/SamplesCommon/CommonBuild.props @@ -13,6 +13,7 @@ $(DefineConstants);SDKVERSION_15063 $(DefineConstants);SDKVERSION_16299 $(DefineConstants);SDKVERSION_17134 + $(DefineConstants);SDKVERSION_17763