AS3 Button ---- 按扭类

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;

	/**
	 * 按钮类
	 * @author Jave.Lin
	 */	
	public class Button extends Sprite
	{
		private var _text:String;
		private var _isDowned:Boolean=false;
		private var _isOnClickShake:Boolean=true;
		
		public function get isOnClickShake():Boolean{return _isOnClickShake;}
		public function set isOnClickShake(value:Boolean):void
		{
			if(_isOnClickShake!=value)
			{
				_isOnClickShake=value;
				if(_isOnClickShake)
				{
					addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
					addEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
				}
				else
				{
					removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
					removeEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
				}
			}
		}
		
		public function get text():String{return _text;}
		public function set text(value:String):void
		{
			if(_text!=value)
			{
				_text=value;
				refresh();
			}
		}
		
		private var _label:Label;
		
		private var _isAutoSize:Boolean=true;
		
		public function get isAutoSize():Boolean{return _isAutoSize;}
		public function set isAutoSize(value:Boolean):void
		{
			if(_isAutoSize!=value)
			{
				_isAutoSize=value;
				refresh();
			}
		}
		//背景与文字的边框间距
		private var _bgMargin:Number=2;
		
		public function Button($text:String="button")
		{
			this.mouseChildren = false;
			
			text=$text;
			
			initialize();
		}
		
		private function initialize():void
		{
			this.mouseChildren=false;
			
			if(stage)
			{
				onAddedToStageHandler();
			}
			else
			{
				addEventListener(Event.ADDED_TO_STAGE,onAddedToStageHandler);
			}
			
			refresh();
		}
		
		private function onAddedToStageHandler(e:Event=null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onAddedToStageHandler);
			
			addEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStageHandler);
			
			addEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
			//默认抖动的
			if(_isOnClickShake)
			{
				addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
				addEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
			}
		}
		
		private function onMouseUpHandler(e:MouseEvent=null):void
		{
			if(_isDowned)
			{
				_isDowned=false;
				this.x-=1;
				this.y-=1;
			}
		}
		
		private function onMouseDownHandler(e:MouseEvent):void
		{
			if(!_isDowned)
			{
				_isDowned=true;
				this.x+=1;
				this.y+=1;
			}
		}
		
		private function onMouseOutHandler(e:MouseEvent):void
		{
			removeEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
			addEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler);
			
			onMouseUpHandler();
			
			filters=null;
		}
		
		private function onMouseOverHandler(e:MouseEvent):void
		{
			removeEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
			
			filters=[new GlowFilter(0x00ff00,1,3,3,3)];
		}
		
		private function onRemovedFromStageHandler(e:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE,onRemovedFromStageHandler);
			removeEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler);
			removeEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
			removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
			removeEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
		}
		
		private function refresh():void
		{
			if(_label==null)
			{
				//font Verdana
				_label=new Label();
				addChild(_label);
			}
			
			_label.text=text;
			
			//获取字体位置、大小
			var textFieldRect:Rectangle=_label.getBounds(this);
			
			//居中
			_label.x=-textFieldRect.width/2;
			_label.y=-textFieldRect.height/2;
			
			if(_isAutoSize)
			{
				refreshBackground();
			}
		}
		
		protected function refreshBackground():void
		{
			//获取字体位置、大小
			var textFieldRect:Rectangle=_label.getBounds(this);
			
			//背景也居中
			this.graphics.clear();
			this.graphics.beginFill(0x55aa00, 0.5);
			this.graphics.drawRect(
				(-textFieldRect.width/2)-_bgMargin,
				(-textFieldRect.height/2)-_bgMargin,
				textFieldRect.width+(_bgMargin*2),
				textFieldRect.height+(_bgMargin*2)
			);
			this.graphics.endFill();
		}
	}
}


图片:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值