<?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>indieas &#187; states</title>
	<atom:link href="http://www.indieas.org/tag/states/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.indieas.org</link>
	<description>independent actionscript developer team</description>
	<lastBuildDate>Tue, 19 Oct 2010 13:43:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Reinvent the wheel or how to program a button in AS3</title>
		<link>http://www.indieas.org/2009/09/reinvent-the-wheel-or-how-to-program-a-button-in-as3/</link>
		<comments>http://www.indieas.org/2009/09/reinvent-the-wheel-or-how-to-program-a-button-in-as3/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 12:15:07 +0000</pubDate>
		<dc:creator>mich</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[button]]></category>
		<category><![CDATA[disable]]></category>
		<category><![CDATA[label]]></category>
		<category><![CDATA[states]]></category>

		<guid isPermaLink="false">http://www.indieas.org/?p=47</guid>
		<description><![CDATA[Something that you learn at the very beginning of developing flash projects is how to create a button. There is this practical Button symbol where you can define the common up, over and down states of a button. This is great but has two major downsides: you can not change anything at runtime like a [...]]]></description>
			<content:encoded><![CDATA[<p>Something that you learn at the very beginning of developing flash projects is how to create a button. There is this practical Button symbol where you can define the common up, over and down states of a button. This is great but has two major downsides:</p>
<ol>
<li>you can not change anything at runtime like a label.</li>
<li>there is no disabled state.</li>
</ol>
<p>So I started to find a way to solve this. After a day playing around I came up with the following solution;</p>
<p>As it needs different states I was looking for a solution to encapsulate these states behind a single class and the StateManager was born. I setup the StateManager first with all the different states:</p>
<pre class="brush: as3; title: ; notranslate">
stateManager = new StateManager();
stateManager.addState( &quot;up&quot;, ButtonUpDraft ); stateManager.addState( &quot;down&quot;, ButtonDownDraft );
stateManager.addState( &quot;over&quot;, ButtonOverDraft );
</pre>
<p>ButtonUpDraft, ButtonDownDraft, ButtonOverDraft are simple MovieClips exported from Flash that are instantiate inside the StateManager. Each one of these MovieClips contain a MovieClip called &#8220;bg&#8221; and a TextField called &#8220;label&#8221;. So to adjust for example the width of the &#8220;bg&#8221; on every state I can call:</p>
<pre class="brush: as3; title: ; notranslate">stateManager.bg.width = 230;</pre>
<p>To change the current state I simply can call:</p>
<pre class="brush: as3; title: ; notranslate">stateManager.state = &quot;up&quot;;</pre>
<p>This way my Button class has not to deal with the individual states and can adjust properties of state assets over multiple states with one command. So my final Button class looks something like this:</p>
<pre class="brush: as3; title: ; notranslate">
package org.indieas.widgets {
 import flash.events.MouseEvent;
 import flash.text.AntiAliasType;
 import flash.text.TextFieldAutoSize;

 import org.indieas.theme.ButtonDisableDraft;
 import org.indieas.theme.ButtonDownDraft;
 import org.indieas.theme.ButtonOverDraft;
 import org.indieas.theme.ButtonUpDraft;
 import org.indieas.widgets.states.AbstractStatesWidget;

 public class Button extends AbstractStatesWidget {

 private var deltaWidth:Number;
 private var _resizeByLabel:Boolean = false;

 public function Button() {
 super();

 stateManager.state = &quot;up&quot;;

 deltaWidth = stateManager.bg.width - stateManager.label.width;

 stateManager.label.autoSize = TextFieldAutoSize.LEFT;
 stateManager.label.multiline = false;
 stateManager.label.wordWrap = false;
 stateManager.label.mouseEnabled = false;

 addEventListener( MouseEvent.MOUSE_UP, onUp );
 addEventListener( MouseEvent.MOUSE_OVER, onOver );
 addEventListener( MouseEvent.MOUSE_OUT, onOut );
 addEventListener( MouseEvent.MOUSE_DOWN, onDown );

 useHandCursor = buttonMode = true;
 }

 protected override function initStates():void {
 stateManager.addState( &quot;up&quot;, ButtonUpDraft );
 stateManager.addState( &quot;over&quot;, ButtonOverDraft );
 stateManager.addState( &quot;down&quot;, ButtonDownDraft );
 stateManager.addState( &quot;disable&quot;, ButtonDisableDraft );
 }

 public function set label( value:String ):void {
 stateManager.label.text = value;
 if( _resizeByLabel ) {
 width = stateManager.label.width + deltaWidth;
 } else {
 width = stateManager.bg.width;
 }
 }

 public function get label():String {
 return stateManager.label.text;
 }

 public function set enable( value:Boolean ):void {
 mouseEnabled = mouseChildren = value;
 if( value ) {
 stateManager.state = &quot;up&quot;;
 } else {
 stateManager.state = &quot;disable&quot;;
 }
 }

 public function get enable():Boolean {
 return stateManager.state != &quot;disable&quot;;
 }

 public override function set width(value:Number):void {
 stateManager.bg.width = value;
 stateManager.label.x = ( value - stateManager.label.width ) / 2;
 }

 public override function set height(value:Number):void {
 stateManager.bg.height = value;
 stateManager.label.y = ( ( value - stateManager.label.height ) / 2 ) + 1;
 }

 private function onUp( me:MouseEvent ):void {
 stateManager.state = &quot;over&quot;;
 }
 private function onOver( me:MouseEvent ):void {
 stateManager.state = &quot;over&quot;;
 }
 private function onOut( me:MouseEvent ):void {
 if( enable ) stateManager.state = &quot;up&quot;;
 }
 private function onDown( me:MouseEvent ):void {
 stateManager.state = &quot;down&quot;;
 }
 }
}
</pre>
<p>The advantage is obvious as there is no big mess with different states and you can focus only on the functionality of the button. So at the end of the day a friend, how had nothing to do with as, asked me what i was doing today. My answer was in a way disappointing as I said I just programmed a button&#8230; the whole day.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.indieas.org/2009/09/reinvent-the-wheel-or-how-to-program-a-button-in-as3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

