SyntaxHighlighter

Monday, June 13, 2011

The Lonely WAIF Project

The lonely waif project is a animation library written entirely in F#.
I'll just take a brief moment to explain the name. If you look up waif
on Wikipedia you'll find  that it's more or less defined as such:
waif (from the Old French guaif, stray beast)[1] is a living creature removed,
by hardship, loss or other helpless circumstance, from his original surroundings.
I like to think of this projects original surroundings as the C# version
and well, its lonely because I'm currently the only contributor and user. :)

At this point you're probably wondering a few things...
a) Why an animation library in F#?
b) Why not just use the C# library from F#?

Well, I already use the library in my C#/F# polyglot project at work.
It's nice and all, but I'm trying to reduce my code footprint.
And unfortunately this mixed support is not helping very much.
Besides, I'm working on a really clean and simple visualization
component which will allow me to visualize time series, having a way
to animate the interactivity of that time series would be even nicer.
Which brings us to the main reason why I'm doing it in F#.
Trying off the shelf reporting controls is nice until you want that
extra level of sophistication only to find yourself stuck. I really
want that google charting level of sophistication but I know I will
not come close to getting it with componentone charting controls.
After all, google is not using off the shelf charting controls. :)

Now that we understand the name of this library and the
motivation behind it, let's take a look at it.








If you head over to google code where the project is hosted you'll
find two F# projects.
1) The animation library
2) WPF test application which uses the library

Let's take a look at one of the sample snippets.

- Say you have a window with a simple rectangle shape on it
and you want to animate the rotation of the angle.
- Or maybe you want to simply move it across the screen.

Here's what the code might look like :

let uri = new System.Uri("/AnimationTest;component/MainView.xaml",
                                           UriKind.Relative)
let window =  Application.LoadComponent(uri) :?> Window
let animator = new Animator() // animation time!
(* Grab UIElements *)
let rect = window.FindName("rectangle") :?> Rectangle


(* ===== Rotate Rectangle ===== *)
let rtf = castAs(rect.RenderTransform).Children.[2] :?> RotateTransform
 (* Angle *)
(* DependencyObject - rotate transform *)
animator.AnimateDouble(rtf,
(* DependencyProperty *)
    RotateTransform.AngleProperty,
(* EventHandler - callback function *)
    null,
(* Duration - in milliseconds *)
    250.0,
(* Acceleration *)
   None,
(* Deceleration *) 
   None,
(* From *)
   None,
(* To *) 
   Some(90.0)) |> ignore)

(* ===== Translate Rectangle ===== *)
let ttf = castAs(rect.RenderTransform).Children.[0] :?> TranslateTransform
(* Y Axis *)
animator.AnimateDouble(ttf,
   TranslateTransform.YProperty,
   null,250.0,None,None,None,Some(90.0)) |> ignore)

There are a few more samples in the sample application but that's really
all there is to it. I use the C# library all the time for creating custom
controls and widgets to use in my WPF applications.
Now I get to use it in F#! Over time I'll be adding more to the library
so it has total parity with the C# library.

Enjoy!

No comments:

Post a Comment