<?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>SHAZAML! &#187; ShowGlobalCounterBehavior</title>
	<atom:link href="http://www.shazaml.com/archives/tag/showglobalcounterbehavior/feed" rel="self" type="application/rss+xml" />
	<link>http://www.shazaml.com</link>
	<description>The Blog for Design &#38; Development Superheroes</description>
	<lastBuildDate>Wed, 28 Jul 2010 21:02:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Video: Creating a Silverlight 3 Casual Game using Blend 3 and Triggers, Actions, and Behaviors</title>
		<link>http://www.shazaml.com/archives/video-silverlight-casual-game</link>
		<comments>http://www.shazaml.com/archives/video-silverlight-casual-game#comments</comments>
		<pubDate>Mon, 09 Nov 2009 15:19:11 +0000</pubDate>
		<dc:creator>Mark Tucker</dc:creator>
				<category><![CDATA[Hidden Object Game]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[Behavior]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[ChangePropertyAction]]></category>
		<category><![CDATA[ContinuousPlayMediaBehavior]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[GlobalCounterMinReachedTrigger]]></category>
		<category><![CDATA[GoToStateAction]]></category>
		<category><![CDATA[IncrementGlobalCounterAction]]></category>
		<category><![CDATA[InvokeCommandAction]]></category>
		<category><![CDATA[MagnifierOverBehavior]]></category>
		<category><![CDATA[MouseCursorBehavior]]></category>
		<category><![CDATA[ParticlesBehavior]]></category>
		<category><![CDATA[PlaySoundAction]]></category>
		<category><![CDATA[RemoveElementAction]]></category>
		<category><![CDATA[SetGlobalCounterAction]]></category>
		<category><![CDATA[SetInteractionPropertyAction]]></category>
		<category><![CDATA[ShowGlobalCounterBehavior]]></category>
		<category><![CDATA[Trigger]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[Visual State Manager]]></category>
		<category><![CDATA[VSM]]></category>

		<guid isPermaLink="false">http://www.shazaml.com/?p=333</guid>
		<description><![CDATA[Video presentation from Desert Code Camp 2009 - Silverlight 3 Casual Game]]></description>
			<content:encoded><![CDATA[<p>On Saturday, November 7, 2009 at 9:00am I presented at the Desert Code Camp in Phoenix, AZ. The topic of my presentation was Silverlight casual game development. In less than an hour I demonstrated how to use triggers, actions, and behaviors in Blend 3 to create a hidden object game. Because all code was contained in the TABs (Triggers, Actions, and Behaviors) there was no code behind for the main UserControl.</p>
<p>Here is a link to the <a title="Video - Creating a Silverlight 3 Casual Game using Blend 3 and Triggers, Actions, and Behaviors" href="http://www.shazaml.com/downloads/DesertCodeCamp2009-Silverlight3CasualGame.wmv">presentation video</a> in WMV format.</p>
<p>If you like you can see <a href="http://www.shazaml.com/archives/creating-a-hidden-object-game-in-silverlight-3">all the episodes</a> and follow along with the tutorial.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shazaml.com/archives/video-silverlight-casual-game/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
<enclosure url="http://www.shazaml.com/downloads/DesertCodeCamp2009-Silverlight3CasualGame.wmv" length="63294285" type="video/x-ms-wmv" />
		</item>
		<item>
		<title>Hidden Object: Episode 10 – Counting Items Found with Counter Triggers, Actions &amp; a Behavior</title>
		<link>http://www.shazaml.com/archives/hidden-object-episode-10</link>
		<comments>http://www.shazaml.com/archives/hidden-object-episode-10#comments</comments>
		<pubDate>Sun, 04 Oct 2009 07:10:46 +0000</pubDate>
		<dc:creator>Mark Tucker</dc:creator>
				<category><![CDATA[Hidden Object Game]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[Behavior]]></category>
		<category><![CDATA[Blend]]></category>
		<category><![CDATA[games]]></category>
		<category><![CDATA[GlobalCounterChangedTrigger]]></category>
		<category><![CDATA[GlobalCounterMaxReachedTrigger]]></category>
		<category><![CDATA[GlobalCounterMinReachedTrigger]]></category>
		<category><![CDATA[GoToStateAction]]></category>
		<category><![CDATA[IncrementGlobalCounterAction]]></category>
		<category><![CDATA[SetGlobalCounterAction]]></category>
		<category><![CDATA[ShowGlobalCounterBehavior]]></category>
		<category><![CDATA[Trigger]]></category>
		<category><![CDATA[Visual State Manager]]></category>
		<category><![CDATA[VSM]]></category>

		<guid isPermaLink="false">http://www.shazaml.com/?p=267</guid>
		<description><![CDATA[Using a custom behavior, triggers, and actions we are able to count the number of items found and change state when the last item is found.]]></description>
			<content:encoded><![CDATA[<p>In episode 9 of <a href="http://www.shazaml.com/archives/creating-a-hidden-object-game-in-silverlight-3">Creating a Hidden Object Game is Silverlight 3</a> we added additional screens to the game. In this episode, we will add the Win screen and a collection of triggers, actions, &amp; behaviors that work with a global counter.</p>
<p>The Win screen is shown after all 13 items have been clicked. What we need is an integer counter that subtracts one for each item clicked and when the count gets to zero change the state of the <em>MainPage</em> UserControl to show the screen.</p>
<p>Similar to what we did in the last episode, create a new Canvas called <em>winCanvas</em> and position it to the left of the UserControl on the artboard.</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec1.png" alt="" /></p>
<p>In the States panel, add the WinScreen state to the ScreenStates group:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec2.png" alt="" /></p>
<p>With recording mode on for the WinScreen state, set the Top and Left properties of <em>winCanvas</em> to 0.</p>
<p>All we need to do is figure out a way to trigger a state change to show this screen.</p>
<p>The solution is a variation of the <a href="http://electricbeach.org/?p=349">Global State Behavior sample</a> by Christian Schormann which uses a <a href="http://msdn.microsoft.com/en-us/library/ms954629.aspx">Singleton</a> object containing a Dictionary to hold values based on a key (or tag).</p>
<p>Let&#8217;s go step-by-step through the use of the GlobalCounter classes before we look at any code.</p>
<p>The counter store (GlobalCounterStore) has an empty dictionary keyed using a CounterKey. Some trigger invokes SetGlobalCounterAction which sets the initial values for Value, MinValue, and MaxValue in a dictionary item with a CounterKey of &#8220;HiddenItems&#8221;. In this case the trigger that sets the values is an EventTrigger on the <em>MainPage</em> UserControl for the Loaded event:</p>
<p><a href="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec3.png"><img class="alignnone size-medium wp-image-253" title="100409_0710_HiddenObjec3.png" src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec3-300x157.png" alt="100409_0710_HiddenObjec3.png" width="300" height="157" /></a> </p>
<p> </p>
<p>Any time that an entry is changed in the counter store, the Changed event is raised which passes Key, Value, MinValue, and MaxValue. Both the ShowGlobalCounterBehavior and the GlobalCounterChangedTrigger handle this event. Since every instance of the GlobalCounterChangedTrigger &amp; ShowGlobalCounterBehavior handle the Changed event they need to have a CounterKey property to determine if they should take any action for that event. In the case of ShowGlobalCounterBehavior, the associated TextBlock&#8217;s Text property is set to Value. For the GlobalCounterChangedTrigger any actions are invoked. In this episode, the GlobalCounterChangedTrigger is not used but is included for completeness.</p>
<p>Now that the counter store has been initialized with values, we can use the IncrementGlobalCounterAction to increment the value. In this case we are incrementing the value by -1 which updates the value in the counter store causing the Changed event to fire which is handled by the ShowGlobalCounterBehavior which sets the text on the TextBlock.</p>
<p><a href="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec4.png"><img class="alignnone size-medium wp-image-254" title="100409_0710_HiddenObjec4.png" src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec4-300x229.png" alt="100409_0710_HiddenObjec4.png" width="300" height="229" /></a></p>
<p> </p>
<p>The IncrementGlobalCounterAction is invoked for each Path object clicked until the counter reaches the MinValue:</p>
<p><a href="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec5.png"><img class="alignnone size-medium wp-image-255" title="100409_0710_HiddenObjec5.png" src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec5-300x149.png" alt="100409_0710_HiddenObjec5.png" width="300" height="149" /></a></p>
<p> </p>
<p>The Changed event is fired to update the TextBlock, but the MinValueReached event is also fired. This is handled by the GlobalCounterMinReachedTrigger which compares its CounterKey with the Key passed in the event and if they match then the trigger invokes its actions. For this episode, there is a GoToStateAction associated with the UserControl that responds to the GlobalCounterMinReachedTrigger and sets the current state to WinScreen which shows <em>winCanvas</em>.</p>
<p>In cases where you want an incrementing counter instead of a decrementing one, pass a positive value for the IncrementCounterValueBy property of IncrementGlobalCounterAction and use the GlobalCounterMaxReachedTrigger.</p>
<p>The global counter classes can be used for many things including a lives counter, health, shield strength, or to keep track of a player&#8217;s score.</p>
<p>Here are a class diagram of the GlobalCounterStore and related classes:</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec6.png" alt="" /></p>
<p>Besides the implementation of the Singleton pattern, the most interesting code can be found in the SaveToStore and IncrementCounter methods of GlobalCounterStore:</p>
<pre class="brush: csharp; gutter: false;">
internal sealed class GlobalCounterStore
{
    private Dictionary&lt;string, GlobalCounterItem&gt; store;

    internal void SaveToStore(string key, int value, int minValue, int maxValue)
    {
        if (key == null)
            return;
        var item = new GlobalCounterItem() {Value = value, MinValue = minValue, MaxValue = maxValue };

        if (store.ContainsKey(key))
        {
            store[key] = item;
        }
        else
        {
            store.Add(key, item);
        }

        OnChanged(key, item);
    }

    internal void IncrementCounter(string key, int incrementBy)
    {
        if ((key == null) || !store.ContainsKey(key))
            return;

        int newValue = store[key].Value + incrementBy;
        store[key].Value = newValue;

        OnChanged(key, store[key]);

        if (newValue &gt;= store[key].MaxValue)
            OnMaxValueReached(key, store[key]);

        if (newValue &lt;= store[key].MinValue)
            OnMinValueReached(key, store[key]);
    }
}
</pre>
<p> </p>
<p>The SetGlobalCounterAction is a TriggerAction that defines four dependency properties for CounterKey, CounterValue, CounterMinValue, and CounterMaxValue. The Invoke method calls the SaveToStore method on GlobalCounterStore.</p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec7.png" alt="" /></p>
<p>The IncrementGlobalCounterAction is a TargetedTriggerAction that defines dependency properties for CounterKey and IncrementCounterValueBy. It&#8217;s Invoke method calls the IncrementCounter method on GlobalCounterStore.</p>
<p> </p>
<p>The three triggers are very similar in that each defines a CounterKey dependency property and each adds a handler to an event raised by GlobalCounterStore:</p>
<p><a href="http://www.shazaml.com/wp-content/uploads/2009/10/GlobalCounterTriggers.png"><img class="alignnone size-full wp-image-268" title="GlobalCounterTriggers" src="http://www.shazaml.com/wp-content/uploads/2009/10/GlobalCounterTriggers.png" alt="GlobalCounterTriggers" width="266" height="679" /></a></p>
<p> </p>
<p>In the case of GlobalCounterMinReachedTrigger, the OnAttached method adds a handler for the GlobalCounterStore&#8217;s MinValueReached event whereas the handler is removed in the OnDetaching method:</p>
<pre class="brush: csharp; gutter: false;">
public class GlobalCounterMinReachedTrigger : TriggerBase&lt;DependencyObject&gt;
{
    protected override void OnAttached()
    {
        base.OnAttached();
        GlobalCounterStore.Instance.MinValueReached += Instance_MinValueReached;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        GlobalCounterStore.Instance.MinValueReached -= Instance_MinValueReached;
    }

    void Instance_MinValueReached(object sender, GlobalCounterEventArgs e)
    {
        if (CounterKey == null)
            return;

        if (CounterKey == e.Key)
            this.InvokeActions(e);
    }
}
</pre>
<p> </p>
<p>When the MinValueReached event is raised due to a change in the GlobalCounterStore to any dictionary entry regardless of its key, the passed key (e.Key) is compared to the CounterKey property. If the values match, then the actions for this trigger are invoked.</p>
<p> </p>
<p>ShowGlobalCounterBehavior is a behavior that can be attached to a TextBlock control and functions is a similar way as the triggers:</p>
<p> </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec9.png" alt="" /></p>
<p> </p>
<p>It defines the CounterKey property and handles the Changed event raised by GlobalCounterKey:</p>
<pre class="brush: csharp; gutter: false;">
public class ShowGlobalCounterBehavior : Behavior&lt;TextBlock&gt;
{
    protected override void OnAttached()
    {
        base.OnAttached();
        GlobalCounterStore.Instance.Changed += Instance_Changed;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        GlobalCounterStore.Instance.Changed -= Instance_Changed;
    }
 
    void Instance_Changed(object sender, GlobalCounterEventArgs e)
    {
        if (CounterKey == e.Key)
        {
            if (e.Value &gt; e.MaxValue)
                AssociatedObject.Text = e.MaxValue.ToString();
            else if (e.Value &lt; e.MinValue)
                AssociatedObject.Text = e.MinValue.ToString();
            else
                AssociatedObject.Text = e.Value.ToString();
        }
    }
}
</pre>
<p> </p>
<p>It is possible to increment the Value in the counter store by more than 1 so I added checks so that the TextBlock will never show values greater than MaxValue or less than MinValue.</p>
<p> </p>
<p>Now it is time to use these classes in Blend. Drop an instance of SetGlobalCounterAction on the UserControl, a GoToStateAction on LayoutRoot, and a IncrementGlobalCounterAction on a Path:</p>
<p> </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec10.png" alt="" /></p>
<p> </p>
<p>The values for SetGlobalCounterAction are set as follows:</p>
<p> </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec11.png" alt="" /></p>
<p> </p>
<p>The new GoToStateAction uses GlobalCounterMinReachedTrigger and sets the StateName to WinScreen:</p>
<p> </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec12.png" alt="" /></p>
<p> </p>
<p>Each path will have an IncrementGlobalCounterAction set with these values:</p>
<p> </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec13.png" alt="" /></p>
<p> </p>
<p> </p>
<p>Finally, we add a TextBlock to our Canvas to hold the count of remaining items and add the ShowGlobalCounterBehavior to it:</p>
<p> </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec14.png" alt="" /></p>
<p> </p>
<p> <a href="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec15.png"><img class="alignnone size-medium wp-image-265" title="100409_0710_HiddenObjec15.png" src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec15-300x156.png" alt="100409_0710_HiddenObjec15.png" width="300" height="156" /></a></p>
<p> </p>
<p> </p>
<p>The only property to set on the behavior is the CounterKey: </p>
<p><img src="http://www.shazaml.com/wp-content/uploads/2009/10/100409_0710_HiddenObjec16.png" alt="" /></p>
<p> </p>
<p> </p>
<p>Once we add IncrementGlobalCounterAction to each Path, we can run the game and as we find items the counter will decrement by 1. When the value reaches 0, then the winning screen is shown.</p>
<p> </p>
<p><a href="http://www.shazaml.com/downloads/ClutteredCubeSource10.zip">Source code</a></p>
<p><a href="http://www.shazaml.com/hidden-object-episode-10-demo">Demo</a></p>
<p> </p>
<p>In the <a href=" http://www.shazaml.com/archives/hidden-object-episode-11 ">next episode</a> we will update the Particles Behavior to allow particle custom shapes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.shazaml.com/archives/hidden-object-episode-10/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
