Tuesday, April 13, 2010

Simple WPF Animation

Since I want to be able to visually demonstrate solutions to the sliding-block puzzle, I wanted a way to animate the WPF display. Below is part of the solution I've come up with. I don't know a huge amount about WPF, so this may look pretty off the wall and grim to experienced WPF-ers. If so, leave a comment and I'll try to make corrections.

(Presented “as-is” and without warranty or implied fitness; use at your own risk.)

First, the timer code:
type Animator () =

let timer =
new System.Windows.Threading.DispatcherTimer()

let stop () =
if timer.IsEnabled then timer.Stop()

let animate (fCallback:unit->bool)
e =
if not(fCallback()) then stop()

member this.Start (fCallback:unit->bool)
(interval:int) =
timer.Tick.Add (animate fCallback)
timer.Interval <- new System.TimeSpan(0,0,0,0,interval)

member this.Stop () =

And a simple use that makes random moves (to be placed within MainWindow or whatever class displays the Fifteen puzzle):
  let Animate  =
let animator = new Animator()
let rand = new Random()
(fun interval ->
(fun () ->
moveTile(rand.Next 4)

No comments: