using Tango.Editors; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Media.Media3D; /// /// /// A collection of and extension methods. /// internal static class UIElementExtension { /// /// Starts the double animation. /// /// The element. /// The property. /// The animation. /// To. /// From. /// internal static DoubleAnimation StartDoubleAnimation(this IAnimatable element, DependencyProperty property, ConfigurationAnimation animation, double to, double? from = null) { DoubleAnimation ani = new DoubleAnimation(); ani.AccelerationRatio = animation.AccelerationRatio; ani.DecelerationRatio = animation.DecelerationRatio; if (animation.EasingFunction != null) { ani.EasingFunction = animation.EasingFunction; } if (from != null && !double.IsNaN(from.Value)) { ani.From = from.Value; } ani.Duration = new Duration(animation.Duration); if (double.IsNaN((double)(element as DependencyObject).GetValue(property)) || animation.Duration == TimeSpan.FromSeconds(0)) { element.BeginAnimation(property, null); (element as DependencyObject).SetValue(property, to); return ani; } ani.Completed += (x, y) => { element.BeginAnimation(property, null); (element as DependencyObject).SetValue(property, to); animation.RaiseAnimationCompleted(element); }; if (!double.IsNaN(to)) { ani.To = to; element.BeginAnimation(property, ani); } else { element.BeginAnimation(property, null, HandoffBehavior.Compose); (element as DependencyObject).SetValue(property, to); } return ani; } internal static ColorAnimation StartColorAnimation(this IAnimatable element, DependencyProperty property, ConfigurationAnimation animation, Color to) { ColorAnimation ani = new ColorAnimation(); ani.AccelerationRatio = animation.AccelerationRatio; ani.DecelerationRatio = animation.DecelerationRatio; if (animation.EasingFunction != null) { ani.EasingFunction = animation.EasingFunction; } ani.Duration = new Duration(animation.Duration); ani.Completed += (x, y) => { element.BeginAnimation(property, null); (element as DependencyObject).SetValue(property, to); animation.RaiseAnimationCompleted(element); }; ani.To = to; element.BeginAnimation(property, ani); return ani; } /// /// Gets the double animation. /// /// The element. /// The property. /// The animation. /// To. /// From. /// internal static AnimationSetup GetDoubleAnimation(this UIElement element, DependencyProperty property, ConfigurationAnimation animation, double to, double? from = null) { DoubleAnimation ani = new DoubleAnimation(); ani.AccelerationRatio = animation.AccelerationRatio; ani.DecelerationRatio = animation.DecelerationRatio; if (animation.EasingFunction != null) { ani.EasingFunction = animation.EasingFunction; } if (from != null && !double.IsNaN(from.Value)) { ani.From = from.Value; } ani.Duration = new Duration(animation.Duration); if (double.IsNaN((double)element.GetValue(property)) || animation.Duration == TimeSpan.FromSeconds(0)) { //element.BeginAnimation(property, null); //element.SetValue(property, to); return new AnimationSetup(ani, element, property); } ani.Completed += (x, y) => { Storyboard story = new Storyboard(); element.BeginAnimation(property, null); element.SetValue(property, to); animation.RaiseAnimationCompleted(element); }; if (!double.IsNaN(to)) { ani.To = to; //element.BeginAnimation(property, ani); } else { //element.BeginAnimation(property, null, HandoffBehavior.Compose); //element.SetValue(property, to); } return new AnimationSetup(ani, element, property); } /// /// Starts the int32 animation. /// /// The element. /// The property. /// The animation. /// To. /// internal static Int32Animation StartInt32Animation(this UIElement element, DependencyProperty property, ConfigurationAnimation animation, Int32 to) { Int32Animation ani = new Int32Animation(); ani.AccelerationRatio = animation.AccelerationRatio; ani.DecelerationRatio = animation.DecelerationRatio; if (animation.EasingFunction != null) { ani.EasingFunction = animation.EasingFunction; } ani.Duration = new Duration(animation.Duration); ani.Completed += (x, y) => { element.BeginAnimation(property, null); element.SetValue(property, to); animation.RaiseAnimationCompleted(element); }; if (!double.IsNaN(to)) { ani.To = to; element.BeginAnimation(property, ani); } else { element.BeginAnimation(property, null, HandoffBehavior.Compose); element.SetValue(property, to); } return ani; } /// /// Starts the point animation. /// /// The element. /// The property. /// The animation. /// To. /// internal static PointAnimation StartPointAnimation(this UIElement element, DependencyProperty property, ConfigurationAnimation animation, Point to) { PointAnimation ani = new PointAnimation(); ani.AccelerationRatio = animation.AccelerationRatio; ani.DecelerationRatio = animation.DecelerationRatio; if (animation.EasingFunction != null) { ani.EasingFunction = animation.EasingFunction; } ani.To = to; ani.Duration = new Duration(animation.Duration); ani.Completed += (x, y) => { element.BeginAnimation(property, null); element.SetValue(property, to); animation.RaiseAnimationCompleted(element); }; element.BeginAnimation(property, ani); return ani; } /// /// Starts the point3 d animation. /// /// The element. /// The property. /// The animation. /// To. /// internal static Point3DAnimation StartPoint3DAnimation(this IAnimatable element, DependencyProperty property, ConfigurationAnimation animation, Point3D to) { Point3DAnimation ani = new Point3DAnimation(); ani.AccelerationRatio = animation.AccelerationRatio; ani.DecelerationRatio = animation.DecelerationRatio; if (animation.EasingFunction != null) { ani.EasingFunction = animation.EasingFunction; } ani.To = to; ani.Duration = new Duration(animation.Duration); ani.Completed += (x, y) => { element.BeginAnimation(property, null); (element as DependencyObject).SetValue(property, to); animation.RaiseAnimationCompleted(element); }; element.BeginAnimation(property, ani); return ani; } /// /// Starts the vector3 d animation. /// /// The element. /// The property. /// The animation. /// To. /// internal static Vector3DAnimation StartVector3DAnimation(this IAnimatable element, DependencyProperty property, ConfigurationAnimation animation, Vector3D to) { Vector3DAnimation ani = new Vector3DAnimation(); ani.AccelerationRatio = animation.AccelerationRatio; ani.DecelerationRatio = animation.DecelerationRatio; if (animation.EasingFunction != null) { ani.EasingFunction = animation.EasingFunction; } ani.To = to; ani.Duration = new Duration(animation.Duration); ani.Completed += (x, y) => { element.BeginAnimation(property, null); (element as DependencyObject).SetValue(property, to); animation.RaiseAnimationCompleted(element); }; element.BeginAnimation(property, ani); return ani; } }