// </Snippet_GamePiece_PublicProperties> /*******************************************/ // <Snippet_GamePiece_ProcessInertia> #region ProcessInertia public void ProcessInertia() { if (processInertia) { inertiaProcessor.Process(Timestamp); } }
public override void OnApplyTemplate() { // Set up the ContentPresenter. _content = GetTemplateChild("PART_Content") as ContentPresenter; _translate = new TranslateTransform(); _scale = new ScaleTransform(); _content.RenderTransform = new TransformGroup { Children = new TransformCollection { _scale, _translate } }; _content.RenderTransformOrigin = new Point(.5, .5); // Set up the spring animation. _spring = new Storyboard { Duration = TimeSpan.FromMilliseconds(200), FillBehavior = FillBehavior.Stop, DecelerationRatio = .6 }; _spring.Completed += Spring_Completed; _springScaleX = new DoubleAnimation { Duration = _spring.Duration, To = 1, DecelerationRatio = _spring.DecelerationRatio }; Storyboard.SetTarget(_springScaleX, _content); Storyboard.SetTargetProperty(_springScaleX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)")); _spring.Children.Add(_springScaleX); _springScaleY = new DoubleAnimation { Duration = _spring.Duration, To = 1, DecelerationRatio = _spring.DecelerationRatio }; Storyboard.SetTarget(_springScaleY, _content); Storyboard.SetTargetProperty(_springScaleY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)")); _spring.Children.Add(_springScaleY); _springTranslateX = new DoubleAnimation { Duration = _spring.Duration, To = 0, DecelerationRatio = _spring.DecelerationRatio }; Storyboard.SetTarget(_springTranslateX, _content); Storyboard.SetTargetProperty(_springTranslateX, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.X)")); _spring.Children.Add(_springTranslateX); _springTranslateY = new DoubleAnimation { Duration = _spring.Duration, To = 0, DecelerationRatio = _spring.DecelerationRatio }; Storyboard.SetTarget(_springTranslateY, _content); Storyboard.SetTargetProperty(_springTranslateY, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[1].(TranslateTransform.Y)")); _spring.Children.Add(_springTranslateY); _spring.Begin(this, true); ClipToBounds = true; // Set up the ManipulationProcessor. _contentManipulationProcessor = new ManipulationProcessor2D(Manipulations2D.Scale | Manipulations2D.TranslateX | Manipulations2D.TranslateY); _contentManipulationProcessor.Started += ContentManipulationProcessor_Affine2DManipulationStarted; _contentManipulationProcessor.Delta += ContentManipulationProcessor_Affine2DManipulationDelta; _contentManipulationProcessor.Completed += ContentManipulationProcessor_Affine2DManipulationCompleted; // Set up the InertiaProcessor. _inertiaTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(30) }; _inertiaTimer.Tick += (sender, e) => _inertiaProcessor.Process(DateTime.UtcNow.Ticks); _inertiaProcessor = new InertiaProcessor2D(); _inertiaProcessor.Delta += ContentManipulationProcessor_Affine2DManipulationDelta; _inertiaProcessor.Completed += (sender, e) => _inertiaTimer.Stop(); _inertiaProcessor.TranslationBehavior.DesiredDeceleration = (float)(96 * 1.5 * .001 * .001); #warning ElasticMargin has been deprecated. //// _inertiaProcessor.ElasticMargin = new Thickness(25); base.OnApplyTemplate(); }
/// <summary> /// Here when the inertia timer ticks. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnTimerTick(object sender, EventArgs e) { inertiaProcessor.Process(Timestamp); }