Button.as

Go to the documentation of this file.
00001 import gugga.common.UIComponentEx;
00002 import gugga.tracking.Tracker;
00003 import gugga.utils.DebugUtils;
00004 
00005 [Event("click")]
00006 [Event("rollOver")]
00007 [Event("rollOut")]
00008 [Event("press")]
00009 
00010 class gugga.components.Button extends UIComponentEx
00011 {
00012         //TODO: constants names should be more descriptive. Example - STATE_OFF
00013         
00014         //button states
00015         private static var OFF:String = "off";
00016         private static var ON:String = "on";
00017         private static var OFF_DISABLE:String = "offDisable";
00018         private static var ON_DISABLE:String = "onDisable";
00019         
00020         //mouse states
00021         private static var ROLL:String = "Roll";
00022         private static var DOWN:String = "Down";
00023         private static var NORMAL:String = "";
00024 
00025         //frame where the movie stops for last time
00026         private var mLastStopedFrame:Number;
00027         private var mLastPlayedLabel:String; 
00028         
00029         private var mButtonState:String; 
00030         private var mMouseState:String;
00031         
00032         private var mEnabled:Boolean;
00033         public function get Enabled():Boolean { return mEnabled; }
00034         public function set Enabled(aValue:Boolean):Void 
00035         {
00036                 if(aValue == true)
00037                 {
00038                         enable();
00039                 }
00040                 else
00041                 {
00042                         disable();
00043                 }
00044         }
00045         
00046         private var mSelected : Boolean;
00047         public function get Selected() : Boolean { return mSelected; }
00048         public function set Selected(aValue : Boolean) : Void 
00049         { 
00050                 if(aValue == true)
00051                 {
00052                         select();
00053                 }
00054                 else
00055                 {
00056                         unselect();
00057                 }
00058         }
00059         
00060         private var mTrackClick : Boolean = true;
00061         public function get TrackClick() : Boolean { return mTrackClick; }
00062         public function set TrackClick(aValue:Boolean) : Void { mTrackClick = aValue; }
00063         
00064         function Button()
00065         {
00066                 mTrackableID = this._name;
00067                 
00068                 mButtonState = OFF;
00069                 mMouseState = NORMAL;
00070 
00071                 mLastPlayedLabel = null;
00072                 mLastStopedFrame = null;
00073                 
00074                 useHandCursor = true;
00075                 mSelected = false;
00076                 mEnabled = true;
00077         }
00078         
00079         
00080         function onRelease()
00081         {
00082                 if (mEnabled)
00083                 {
00084                         //check if button is rolled or no
00085                         startTransitonToState(mButtonState, ROLL);
00086                         
00087                         if(mTrackClick)
00088                         {
00089                                 Tracker.getTrackerFor(this).trackClick(this, this._xmouse, this._ymouse);
00090                         }
00091                         
00092                         dispatchEvent({type:"click",target:this});
00093                 }
00094         }
00095 
00096         function onRollOver():Void
00097         {
00098                 startTransitonToState(mButtonState, ROLL);
00099                 dispatchEvent({type:"rollOver", target:this});
00100         }
00101 
00102         function onDragOver():Void
00103         {
00104                 onRollOver();
00105         }
00106         
00107         function onRollOut():Void
00108         {
00109                 startTransitonToState(mButtonState, NORMAL);
00110                 dispatchEvent({type:"rollOut", target:this});
00111         }
00112 
00113         function onDragOut():Void
00114         {
00115                 onRollOut();
00116         }
00117         
00118         function onPress():Void
00119         {
00120                 if (mEnabled)
00121                 {
00122                         startTransitonToState(mButtonState, DOWN);
00123                         dispatchEvent({type:"press", target:this});
00124                 }
00125         }
00126         
00127         function onOver()
00128         {
00129                 onRollOver();
00130         }
00131         
00132         /*function setEnabled(flag:Boolean):Void
00133         {
00134                 enabled = flag;
00135                 _alpha = flag ? 100 : 50;
00136         }*/
00137         
00138         //TODO: all transitions can be separated in methods which can be overriden 
00139         // and can be played independently. 
00140         
00141         public function playRollOver()
00142         {
00143                 startTransitonToState(mButtonState, ROLL);
00144         }
00145         
00146         public function playRollOut()
00147         {
00148                 startTransitonToState(mButtonState, NORMAL);
00149         }
00150         
00151         public function click()
00152         {
00153                 onRelease();
00154         }
00155         
00156         public function disable()
00157         {
00158                 if (mEnabled)
00159                 {
00160                         if (mSelected)
00161                         {
00162                                 startTransitonToState(ON_DISABLE, mMouseState);
00163                         }
00164                         else
00165                         {
00166                                 startTransitonToState(OFF_DISABLE, mMouseState);
00167                         }
00168                         
00169                         
00170                         mEnabled = false;
00171                         useHandCursor = false;
00172                 }
00173         }
00174         
00175         public function enable()
00176         {
00177                 if (!mEnabled)
00178                 {
00179                         if (mSelected)
00180                         {
00181                                 startTransitonToState(ON, mMouseState);
00182                         }
00183                         else
00184                         {
00185                                 startTransitonToState(OFF, mMouseState);
00186                         }
00187                         
00188                         mEnabled = true;
00189                         useHandCursor = true;
00190                 }
00191         }
00192         
00193         public function select():Void
00194         {
00195                 if(!mSelected)
00196                 {
00197                         startTransitonToState(ON, mMouseState);
00198                         mSelected = true;
00199                 }
00200         }
00201         
00202         public function unselect():Void
00203         {
00204                 if(mSelected)
00205                 {
00206                         startTransitonToState(OFF, mMouseState);
00207                         mSelected = false;
00208                 }
00209         }
00210         
00223         private function playLabel(aLabel:String):Boolean
00224         {
00225                 var oldCurFrame:Number = this._currentframe;
00226                 this.gotoAndPlay(aLabel);
00227 
00228                 //mLastStopedFrame is equal to the aLabel frame
00229                 if (mLastStopedFrame == this._currentframe)
00230                 {
00231                         this.gotoAndStop(aLabel);
00232                         mLastPlayedLabel = aLabel;
00233                         return false;
00234                 }
00235                 
00236                 //There is no label aLabel
00237                 if (oldCurFrame == this._currentframe)
00238                 {
00239                         return false;
00240                 }
00241 
00242                 mLastPlayedLabel = aLabel;
00243                 return true;
00244         }
00245         
00249         private function stopPlaying()
00250         {
00251                 this.stop();
00252                 mLastStopedFrame = this._currentframe;
00253                 
00254                 var labelForCurState:String = getLabelForCurrentState();
00255                 if(mLastPlayedLabel != labelForCurState)
00256                 {
00257                         playLabel(labelForCurState);
00258                 }
00259         }
00260         
00261         private function getLabelForCurrentState():String
00262         {
00263                 return mButtonState + mMouseState;
00264         }
00265         
00266         //try to transit between current and next state and 
00267         //if there is no transition, play label for next state
00268         private function startTransitonToState(aNewButtonState:String, aNewMouseState:String)
00269         {
00270                 var labelForCurState:String = getLabelForCurrentState();
00271                 mButtonState = aNewButtonState;
00272                 mMouseState = aNewMouseState;
00273                 var labelForNextState:String = getLabelForCurrentState();
00274                 
00275                 var isPlaying:Boolean = playLabel(labelForCurState + "_" + labelForNextState);
00276 
00277                 if (!isPlaying)
00278                 {
00279                         playLabel(labelForNextState);
00280                 }
00281         }
00282 }

Generated on Fri May 11 17:12:42 2007 for GuggaFramework by  doxygen 1.5.2