Resumé

Updated Silverlight 2.0 Tweener (Of T)

Cristian Merighi () 0.00

Sudden new version of my Tweener for Silverlight 2.0 (Beta 1). It now supports interpolations on Colors and Points.
This article is obsolete. Some functionalities might not work anymore. Comments are disabled.
warning! This article has to be considered outdated.
Please refer to here for an updated version of the problem.

Right one day after my Silverlight 2.0 Tweener, I'm here updating the whole API. I cite myself...

The future steps will see [...] ...Color and Point support

Here they are.
To include those types and to easily keep track of their specific value during the entire motion process (see CurrentPosition property in TweenMotionChangedEventArgs class) I've been thinking about using generics.
That's why we're now facing a Tweener<T> or Tweener(Of T)!

Henceforth we'll handle different objects and the code posted yesterday has to be reviewed.
Let's rewrite the previous' post sample:

//[...] omitted code (extract)

private void UserControl_Loaded(object sender, RoutedEventArgs e)

{

    ITween<Color> tween6 = Tweener<Color>.CreateTween((SolidColorBrush)cnv.Background, SolidColorBrush.ColorProperty, Elastic.EaseOut, Colors.Red, Colors.Black, TimeSpan.FromSeconds(2D));

    ITween<double> tween3 = Tweener.CreateTween(cnv, TweenProperty.Angle, Elastic.EaseOut, 0D, -45D, TimeSpan.FromSeconds(4D));

    ITween<double> tween = Tweener.CreateTween(cnv, TweenProperty.ScaleY, Bounce.EaseOut, 1, 0.5, TimeSpan.FromSeconds(3D));

    ITween<double> tween5 = Tweener.CreateTween(cnv, TweenProperty.AngleX, Linear.EaseNone, 0, -60D, TimeSpan.FromSeconds(3D));

    ITween<Point> tween2 = Tweener<Point>.CreateTween(seg, LineSegment.PointProperty, Circ.EaseOutIn, seg.Point, new Point(seg.Point.X, seg.Point.Y+100), TimeSpan.FromSeconds(.9D));

 

    tween.MotionStarted += new EventHandler(tween_MotionStarted);

    tween.MotionFinished += new EventHandler(tween_MotionFinished);

    tween.MotionChanged += new TweenMotionChangedEventHandler<double>(tween_MotionChanged);

}

 

void tween_MotionChanged(object sender, TweenMotionChangedEventArgs<double> e)

{

    txt.Text += string.Format("\n[{0:T}] value {1}", DateTime.Now, e.CurrentPosition);

}

 //[...] omitted code

 

The previous code refers to the following XAML body (as you'll soon see: no artistic willingness, just naked objects to test on):

<Canvas Background="Black" Width="200" Height="100" x:Name="cnv" VerticalAlignment="Center" HorizontalAlignment="Center">

    <Canvas.RenderTransform>

    <MatrixTransform Matrix="1,1,-1,1,0,0" />

    </Canvas.RenderTransform>

</Canvas>

<Path Stroke="Black" StrokeThickness="1" Fill="Aqua">

    <Path.Data>

        <PathGeometry>

            <PathFigure StartPoint="510,50">

                <LineSegment Point="211,70" />

                <LineSegment Point="660,170" x:Name="seg" />

                <LineSegment Point="510,50" />

            </PathFigure>

        </PathGeometry>

    </Path.Data>

</Path>

<TextBlock x:Name="txt" TextWrapping="NoWrap"

              Text="debug:" Width="Auto" Foreground="Red" FontSize="11" FontFamily="Courier New" />

Remark: the generic type T for Tweener is constrained to have parameterless constructor (see below).
Since we're handling ValueTypes (which all provide parameterless constructor) this constraint is pretty ...loose, is anyway useful to trick the compiler in order to check the real Type of T. This permits to make core decisions during the tweening process (as you'll see downloading the relative code).

/// <summary>

/// Static class that provides tweening functionalities for Silverlight.

/// </summary>

/// <typeparam name="T">The Type of the property to tween.</typeparam>

public static class Tweener<T> where T : new()

Updated Visual Studio projects (Tweener + Testing Silverlight App) are zipped and available here:

zip file « download code

Take care. Bye.

feedback
 

Syndicate

Author

Cristian Merighi facebook twitter google+ youtube

Latest articles

Top rated

Archive

Where am I?

Author

Cristian Merighi facebook twitter google+ youtube

I'm now reading

Feeds