<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Silverlight &#124; WPF &#124; Microsoft.Net &#187; Tiles</title>
	<atom:link href="http://joel.neubeck.net/tag/tiles/feed/" rel="self" type="application/rss+xml" />
	<link>http://joel.neubeck.net</link>
	<description>Simplifing structure without changing results</description>
	<lastBuildDate>Wed, 26 May 2010 18:43:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Silverlight Flipping Tiles Animation</title>
		<link>http://joel.neubeck.net/2008/05/silverlight-flipping-tiles-animation/</link>
		<comments>http://joel.neubeck.net/2008/05/silverlight-flipping-tiles-animation/#comments</comments>
		<pubDate>Fri, 16 May 2008 06:36:30 +0000</pubDate>
		<dc:creator>joel</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[3D Flip]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[DispatcherTimer]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[Tiles]]></category>

		<guid isPermaLink="false">http://joel.neubeck.net/2008/05/silverlight-flipping-tiles-animation/</guid>
		<description><![CDATA[Back a few weeks ago I posted an example of how to create a 3D flip animation similar to the effect seen on an iPhone. Today I will build upon that procedural animation and simulate the flipping tile affect seen in the iTunes album art screen saver, available on a Mac. In my example I [...]]]></description>
			<content:encoded><![CDATA[<p>Back a few weeks ago I posted an example of how to create a <a href="http://joel.neubeck.net/2008/04/silverlight-3d-flip-animation/">3D flip animation</a> similar to the effect seen on an iPhone. Today I will build upon that procedural animation and simulate the flipping tile affect seen in the iTunes album art screen saver, available on a Mac.
<p><iframe src="/wp-content/uploads/2008/05/LeopardSreenSaver/default.html" width="400" height="400"></iframe></p>
<p>In my example I have a total of 16 random tiles displayed from a collection of 42 different images. Any image not currently displayed is available to be randomly selected to replace an existing tile.&nbsp; Here is how I achieve the effect.&nbsp; </p>
<p>The project is comprised of a &#8220;Tile&#8221; UserControl , a class called &#8220;Media&#8221; and two generic lists (one to store references to the 16 tiles and one to store all 42 URI to the images).&nbsp; The first step is to fill my page.xaml grid with 16 tiles.&nbsp;&nbsp; Here is a snip of the code used to fill the grid.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="c-sharp" style="font-family:monospace;">private void BuildCollection()
{
    int row = -1;
    int col = 0;
    for (int i = 0; i &amp;lt; 42; i++)
    {
        Uri imageUri = new Uri(string.Format(&quot;Images/{0}.png&quot;, 
		(i+1).ToString()), UriKind.Relative);
        if (i &amp;lt;= 16)
        {
            Tile tile = new Tile();
            Media media =  new Media(imageUri, true);
            tile.Media = media;
&nbsp;
            if (col % 4 == 0)
            {
                row++;
                col = 0;
            }
            tile.SetValue(Grid.ColumnProperty, col.ToString());
            tile.SetValue(Grid.RowProperty, row.ToString());
            this.LayoutRoot.Children.Add(tile);
&nbsp;
            col++;
            _tiles.Add(tile);
            _images.Add(media);
&nbsp;
        }
        else
            _images.Add(new Media(imageUri, false));
    }
}</pre></td></tr></table></div>

<p>For those of you just getting familiar with the &#8220;Grid&#8221; control you might not be aware that it supports implicitly specifying columns and rows. In this example I leverage this to ensure my tiles form a nice 4&#215;4 grid.&nbsp;&nbsp; </p>
<p>As you can see from the code, I have chosen to store only the URI to the image and not store the image itself. I had some difficulty storing a &#8220;BitmapImage&#8221; object in my collection and retrieving it consistently when I was ready to flip a tile. The URI seems less efficient, was reliable. </p>
<p>Now that I have built up my grid and two collections, I can use a DispacherTimer to randomly select a tile and randomly select an image to be used as the backside of the flip. He is how that code looks. </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="c-sharp" style="font-family:monospace;">void _timer_Tick(object sender, EventArgs e)
{
    System.Random RandNum = new System.Random();
    int randomTile = RandNum.Next(16);
&nbsp;
    bool match = false;
    while(!match)
    {
        int randomImage = RandNum.Next(42);
        if(!_images[randomImage].Displayed)
        {
            _images[randomImage].Displayed = true;
            _tiles[randomTile].Media.Displayed = false;
            _tiles[randomTile].Flip(_images[randomImage]);
            match=true;
            break;
        }
    }
}</pre></td></tr></table></div>

<p>As you can see I am using a boolean property in the &#8220;Media&#8221; object to determine if an image is already displayed. The second level of randomization is there to help ensures that I am giving each image equal chance of getting displayed. </p>
<p>If one were to examine the &#8220;Tile&#8221; UserControl, it looks almost exactly like the code I created for the 3D flip animation example. In&nbsp; this situation instead of firing the flip based on a button click, I use the followings public method called from my main page.xaml DispatcherTimer tick. </p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre></td><td class="code"><pre class="c-sharp" style="font-family:monospace;">public void Flip(Media media)
{
    this.Media = media;
&nbsp;
    if (_Front)
    {
        this.ImageBack.Source = new BitmapImage(_Media.ImageUri);
        _sb.Pause();
        _sb.AutoReverse = false;
        _sb.Begin();
        _Front = false;
    }
    else
    {
        this.ImageFront.Source = new BitmapImage(_Media.ImageUri);
        _sb.Pause();
        _sb.AutoReverse = true;
        _sb.Seek(_tsLastFrame);
        _sb.Resume();
&nbsp;
        _Front = true;
    }
}</pre></td></tr></table></div>

<p>Code: <a onclick="javascript: pageTracker._trackPageview('/code/LeopardSreenSaver.zip');" href="/wp-content/uploads/2008/05/LeopardSreenSaver/LeopardSreenSaver.zip">LeopardSreenSaver.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joel.neubeck.net/2008/05/silverlight-flipping-tiles-animation/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
