/*
Copyright (C) 2008 Webyog Softworks Private Limited
This file is a part of Visifire Charts.
Visifire is a free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
You should have received a copy of the GNU General Public License
along with Visifire Charts. If not, see .
If GPL is not suitable for your products or company, Webyog provides Visifire
under a flexible commercial license designed to meet your specific usage and
distribution requirements. If you have already obtained a commercial license
from Webyog, you can use this file under those license terms.
*/
using System;
using System.Windows;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Collections.Generic;
using Visifire.Charts;
namespace Visifire.Commons
{
///
/// Visifire.Commons.AnimationHelper class
///
internal class AnimationHelper
{
#region Public Methods
#endregion
#region Public Properties
#endregion
#region Public Events And Delegates
#endregion
#region Protected Methods
#endregion
#region Internal Properties
#endregion
#region Private Properties
#endregion
#region Private Delegates
#endregion
#region Private Methods
///
/// Returns list of KeySpline
///
/// Required number of KeySplines
/// List of KeySpline
public static List GenerateKeySplineList(int count)
{
List splines = new List();
for (int i = 0; i < count; i++)
splines.Add(GetKeySpline(new Point(0, 0), new Point(1, 1)));
return splines;
}
///
/// Returns a new KeySpline
///
/// First control point
/// Second control point
/// KeySpline
internal static KeySpline GetKeySpline(Point controlPoint1, Point controlPoint2)
{
return new KeySpline() { ControlPoint1 = controlPoint1, ControlPoint2 = controlPoint2 };
}
///
/// Create DoubleAnimation
///
/// Storyboard parent object
/// Animation target object
/// Property path to animate
/// Animation begin time
/// Frame time collection
/// Target value collection
/// List of KeySpline
/// DoubleAnimationUsingKeyFrames
internal static DoubleAnimationUsingKeyFrames CreateDoubleAnimation(FrameworkElement parentObj, DependencyObject target, String property, Double beginTime, DoubleCollection frameTime, DoubleCollection values, List splines)
{
DoubleAnimationUsingKeyFrames da = new DoubleAnimationUsingKeyFrames();
#if WPF
target.SetValue(FrameworkElement.NameProperty, target.GetType().Name + Guid.NewGuid().ToString().Replace('-', '_'));
Storyboard.SetTargetName(da, target.GetValue(FrameworkElement.NameProperty).ToString());
(parentObj as DataSeries).Chart._rootElement.RegisterName((string)target.GetValue(FrameworkElement.NameProperty), target);
#else
Storyboard.SetTarget(da, target);
#endif
Storyboard.SetTargetProperty(da, new PropertyPath(property));
da.BeginTime = TimeSpan.FromSeconds(beginTime);
for (Int32 index = 0; index < splines.Count; index++)
{
SplineDoubleKeyFrame keyFrame = new SplineDoubleKeyFrame();
keyFrame.KeySpline = splines[index];
keyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(frameTime[index]));
keyFrame.Value = values[index];
da.KeyFrames.Add(keyFrame);
}
return da;
}
#endregion
#region Internal Methods
///
/// Apply Opacity animation to a Marker
///
/// Marker to animate
/// Storyboard parent Object
/// Storyboard reference
/// Animation begin time
/// Target opacity value
/// Storyboard
internal static Storyboard ApplyOpacityAnimation(Marker marker, FrameworkElement parentObj, Storyboard storyboard, Double beginTime, Double targetValue)
{
if (marker != null && parentObj != null)
return ApplyOpacityAnimation(marker.Visual, parentObj, storyboard, beginTime, 0.75, targetValue);
else
return storyboard;
}
///
/// Apply Opacity animation to an object
///
/// Object to animate
/// Storyboard parent Object
/// Storyboard reference
/// Begin time
/// Animation duration
/// Target opacity value
/// Storyboard
internal static Storyboard ApplyOpacityAnimation(FrameworkElement objectToAnimate, FrameworkElement parentObj, Storyboard storyboard, Double beginTime, Double duration, Double targetValue)
{
if (objectToAnimate != null && parentObj != null)
{
DoubleCollection values = Graphics.GenerateDoubleCollection(0, targetValue);
DoubleCollection frameTimes = Graphics.GenerateDoubleCollection(0, duration);
List splines = GenerateKeySplineList
(
new Point(0, 0), new Point(1, 1),
new Point(0, 0), new Point(0.5, 1)
);
objectToAnimate.Opacity = 0;
DoubleAnimationUsingKeyFrames opacityAnimation = AnimationHelper.CreateDoubleAnimation(parentObj, objectToAnimate, "(UIElement.Opacity)", beginTime + 0.5, frameTimes, values, splines);
storyboard.Children.Add(opacityAnimation);
}
return storyboard;
}
///
/// Returns list of KeySpline from point array
///
/// List of points
/// List of KeySpline
internal static List GenerateKeySplineList(params Point[] values)
{
List splines = new List();
for (Int32 i = 0; i < values.Length; i += 2)
splines.Add(GetKeySpline(values[i], values[i + 1]));
return splines;
}
#endregion
#region Internal Events And Delegates
#endregion
#region Data
#endregion
}
}