/controls/ButtonBase.as
ActionScript | 357 lines | 216 code | 34 blank | 107 comment | 29 complexity | ead128323269f2da6f8cd6028c1fa703 MD5 | raw file
- package slw.controls
- {
- import flash.display.DisplayObject;
- import flash.display.GradientType;
- import flash.display.MovieClip;
- import flash.display.Shape;
- import flash.display.Sprite;
- import flash.events.MouseEvent;
- import flash.geom.Matrix;
-
- import slw.core.UIComponent;
- import slw.events.LoaderEvent;
-
-
- /**
- * ButtonBase
- * ???????????4??????4???????4????url??
- * @author ddx<br/>
- * 2011-7-21
- *
- */
- public class ButtonBase extends UIComponent
- {
- /**
- * "up",??????
- */
- public static const UP:String="up";
- /**
- * "over",??????
- */
- public static const OVER:String="over";
- /**
- * "down",??????
- */
- public static const DOWN:String="down";
- /**
- * "disabled",????
- */
- public static const DISABLED:String="disabled";
- //???????????????????
- private var _displayStates:Array=[];
- private var _defaultDisplayStates:Array=[];
- //????
- private var _state:String=UP;
-
- //???????
- private var _stateContainer:Sprite;
- /**
- * ????
- * @param displayStates:Array=null ?????????0-4???????????4?????
- *
- */
- public function ButtonBase(displayStates:Array=null)
- {
- super();
- this.listName="ButtonBase";
-
- init();
-
- setSize(100,22);
-
- if(displayStates==null){
- _defaultDisplayStates=buildDefaultStates();
- this.displayStates=_defaultDisplayStates;
- }
- this.round=6;
- }
-
- /*
- -----------------------------------
- setters getters
- -----------------------------------
- */
- //
- /**
- * ?????????0-4???????????4?????????????4????????:[]
- * @param value:Array
- *
- */
- public function set displayStates(value:Array):void{
- if(_displayStates==value)return;
-
- _displayStates=value;
-
- if(_displayStates!=null&&_displayStates.length>0&&_stateContainer==null){
- _stateContainer=new Sprite();
- this.buildContainer.addChild(_stateContainer);
- }else if((_displayStates==null||_displayStates.length==0)&&_stateContainer!=null){
- this.buildContainer.removeChild(_stateContainer);
- _stateContainer=null;
- }
-
- if(_displayStates!=null&&_displayStates.length!=0){
- this.skin=null;
- _displayStates.length=Math.min(_displayStates.length,4);
- }
-
- setState(_state);
- }
- //
- /**
- * ?????????0-4???????????4?????????????4????????:[]
- * @param value:Array
- *
- */
- public function get displayStates():Array{
- return _displayStates;
- }
- //
- /**
- * ??????,??????????????(?:["Skin1","Skin2"]),????????????????,??0?????????,
- * ?skin???[]?null????
- * @param value:Array
- *
- */
- override public function set skin(value:Array):void{
- if(this.skin==value){
- return;
- }
- super.skin=value;
- if(this.skin!=null&&this.skin.length>0)
- this.displayStates=null;
- }
- //
- /**
- * ????url???0-4????????????????4?????
- * @param value:Array
- *
- */
- override public function set displayURLs(value:Array):void{
- if(this.displayURLs==value)return;
-
- this.addEventListener(LoaderEvent.LOAD_COMPLETE,thisLoadComplete);
- super.displayURLs=value;
- }
- //
- /**
- * ??????,??? true(??)
- * @param value:Boolean
- *
- */
- override public function set enabled(value:Boolean):void{
- if(this.enabled==value){
- return;
- }
- super.enabled=value;
-
- if(this.enabled){
- setState(UP);
- }else{
- setState(DISABLED);
- }
- }
- //
- /**
- * ????,???:0px
- * @param value:Number
- *
- */
- override public function set round(value:Number):void{
- if(this.round==value)return;
-
- super.round=value;
-
- updateDefaultStates();
- }
- //
- override public function set width(value:Number):void{
- if(this.width==value) return;
-
- super.width=value;
-
- updateDefaultStates();
- setState(_state);
- }
- //
- override public function set height(value:Number):void{
- if(this.height==value) return;
-
- super.height=value;
-
- updateDefaultStates();
- setState(_state);
- }
- /*
- -----------------------------------
- public methods
- -----------------------------------
- */
- //
- /**
- * ??????????:ButtonBase.UP?????:<br/>
- * <ul>
- * <li>ButtonBase.UP ??????<br/>
- * <li>ButtonBase.OVER ??????<br/>
- * <li>ButtonBase.DOWN ??????<br/>
- * <li>ButtonBase.DISABLED ????<br/>
- * </ul>
- * @param state:String
- *
- */
- public function setState(state:String):void{
- var $states:Array=[UP,OVER,DOWN,DISABLED];
- if($states.indexOf(state)==-1)
- throw new Error("???????????:["+$states+"].");
-
- showState($states.indexOf(state));
-
- if(state==DISABLED){
- this.alpha=0.6;
- }else{
- this.alpha=1;
- }
- }
- //
- override public function destroy():void{
- this.removeEventListener(LoaderEvent.LOAD_COMPLETE,thisLoadComplete);
- removeListeners();
- super.destroy();
- }
- /*
- -----------------------------------
- private methods
- -----------------------------------
- */
- //???
- private function init():void{
- this.mouseChildren=false;
-
- addListeners();
- }
- //????
- private function addListeners():void{
- this.addEventListener(MouseEvent.MOUSE_OUT,thisMouseOut);
- this.addEventListener(MouseEvent.MOUSE_OVER,thisMouseOver);
- this.addEventListener(MouseEvent.MOUSE_UP,thisMouseUp);
- this.addEventListener(MouseEvent.MOUSE_DOWN,thisMouseDown);
- }
- //????
- public function removeListeners():void{
- this.removeEventListener(MouseEvent.MOUSE_OUT,thisMouseOut);
- this.removeEventListener(MouseEvent.MOUSE_OVER,thisMouseOver);
- this.removeEventListener(MouseEvent.MOUSE_UP,thisMouseUp);
- this.removeEventListener(MouseEvent.MOUSE_DOWN,thisMouseDown);
- }
- //??????
- private function buildDefaultStates():Array{
- var $states:Array=[];
- var $borderColors:Array=[0x999999,0x0099CC,0x0099CC,0x999999];
- var $colors:Array=[[0xeeeeee,0xcccccc],[0xeeeeee,0x85C9E0],[0x85C9E0,0xeeeeee],[0xcccccc,0xeeeeee]];
- for(var i:int=0;i<4;i++){
- $states.push(getState($borderColors[i],$colors[i],this.width,this.height,this.round));
- }
-
- return $states;
- }
- //????
- private function getState(borderColor:uint,colors:Array,width:Number,height:Number,round:Number):DisplayObject{
- var $m:Matrix=new Matrix();
- $m.createGradientBox(width,height,Math.PI/2);
-
- var $s:Shape=new Shape();
- $s.graphics.lineStyle(0,borderColor,1,true,"none","round");
- $s.graphics.beginGradientFill(GradientType.LINEAR,colors,[1,1],[0,0xff],$m);
- $s.graphics.drawRoundRect(0,0,width,height,round,round);
- $s.graphics.endFill();
-
- return $s;
- }
- //??????
- private function updateDefaultStates():void{
- if(_displayStates==_defaultDisplayStates){
- _defaultDisplayStates=buildDefaultStates();
- this.displayStates=_defaultDisplayStates;
- }
- }
- //????
- private function showState(index:int):void{
- var $skins:Array=this.skinTargets;
- for(var i:int=0;i<$skins.length;i++){
- if($skins[i] is MovieClip)
- MovieClip($skins[i]).gotoAndStop(index+1);
-
- if(this.autoSizeBySkin){
- this.width=MovieClip($skins[i]).width;
- this.height=MovieClip($skins[i]).height;
- }else{
- MovieClip($skins[i]).width=this.width;
- MovieClip($skins[i]).height=this.height;
- }
- }
-
- if(_stateContainer!=null){
- while(_stateContainer.numChildren>0){
- _stateContainer.removeChildAt(0);
- }
- if(_displayStates!=null&&_displayStates[index]!=null)
- _stateContainer.addChild(_displayStates[index]);
-
- if(this.autoSizeBySkin){
- this.width=_stateContainer.width;
- this.height=_stateContainer.height;
- }else{
- _stateContainer.width=this.width;
- _stateContainer.height=this.height;
- }
- }
- }
- //????
- private function thisMouseOver(e:MouseEvent):void{
- if(_state==OVER)return;
-
- _state=OVER;
- setState(_state);
- }
- //????
- private function thisMouseOut(e:MouseEvent):void{
- if(_state==UP)return;
-
- _state=UP;
- setState(_state);
- }
- //????
- private function thisMouseUp(e:MouseEvent):void{
- thisMouseOver(e);
- }
- //????
- private function thisMouseDown(e:MouseEvent):void{
- if(_state==DOWN)return;
-
- _state=DOWN;
- setState(_state);
- }
- //????????
- private function thisLoadComplete(e:LoaderEvent):void{
- this.displayStates=e.datas;
-
- this.removeEventListener(LoaderEvent.LOAD_COMPLETE,thisLoadComplete);
- }
- //????
- private function drawRect(target:*,thickness:Number=0,color:uint=0,alpha:Number=1,x:Number=0,y:Number=0,width:Number=1,height:Number=1,round:Number=0,filled:Boolean=false):void{
- if((target as Sprite)==null&&(target as Shape)==null){
- throw new Error("target????:Sprite?Shape????????.");
- }
- target.graphics.clear();
- if(width==0||height==0){
- return;
- }
-
- target.graphics.lineStyle(thickness,color,0,false,"none");
- if(filled)
- target.graphics.beginFill(color,alpha);
- target.graphics.drawRoundRect(x,y,width,height,round,round);
- target.graphics.endFill();
- }
- }
- }