/// <summary> /// Arranges the <see cref="UIElement"/> for hte specified <see cref="Rect"/> /// bounds. /// </summary> /// <param name="element"> /// The <see cref="UIElement"/> to arrange. /// </param> /// <param name="bounds"> /// The <see cref="Rect"/> bound for the element. /// </param> protected void ArrangeElement(UIElement element, Rect bounds) { if (element == null) { return; } if (!renderingListener.IsListening) { renderingListener.StartListening(); } ItemAnimationData animationData = GetAnimationData(element); if (animationData == null) { animationData = new ItemAnimationData(); SetAnimationData(element, animationData); element.RenderTransform = animationData.Transform; animationData.Current = OnElementAdded(element, bounds); } animationData.Target = bounds.Location; element.Arrange(bounds); }
private void OnRendering(object sender, EventArgs eventArgs) { double dampening = Dampening; double attractionFactor = Attraction * .01; double variation = Variation; bool hasChanges = false; for (int i = 0; i < InternalChildren.Count; i++) { ItemAnimationData animationData = GetAnimationData(Children[i]); if (animationData != null) { attractionFactor *= 1 + (variation * animationData.RandomSeed - .5); Vector currentVelocity = animationData.LocationVelocity; Point newLocation; Vector newVelocity; Vector diff = animationData.Offset; if (diff.Length > MinimumValueDelta || currentVelocity.Length > MinimumValueDelta) { newVelocity = currentVelocity * (1 - dampening); newVelocity += diff * attractionFactor; if (currentVelocity.Length > TerminalVelocity) { newVelocity *= TerminalVelocity / currentVelocity.Length; } newLocation = animationData.Current + newVelocity; hasChanges = true; } else { newLocation = animationData.Target; newVelocity = new Vector(); } animationData.Current = newLocation; animationData.LocationVelocity = newVelocity; Vector transformVector = animationData.Current - animationData.Target; animationData.Transform.X = transformVector.X; animationData.Transform.Y = transformVector.Y; } } // Stop listening if no changes for all elements if (!hasChanges) { renderingListener.StopListening(); } }
private static void SetAnimationData(DependencyObject element, ItemAnimationData value) { if (element == null) { throw new ArgumentNullException("element"); } if (value == null) { throw new ArgumentNullException("value"); } element.SetValue(AnimationDataProperty, value); }
/// <summary> /// Invoked when the System.Windows.Media.VisualCollection of a visual object /// is modified. /// </summary> /// <param name="visualAdded"> /// The System.Windows.Media.Visual that was added to the collection. /// </param> /// <param name="visualRemoved"> /// The System.Windows.Media.Visual that was removed from the collection. /// </param> protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved) { if (visualRemoved == null) { return; } ItemAnimationData animationData = GetAnimationData(visualRemoved); if (animationData == null) { return; } visualRemoved.ClearValue(AnimationDataProperty); }