AS3 CheckBox ---- 复选框类

CheckBox类:

package
{
	import controlsEvents.CheckBoxEvent;
	
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;

	/**
	 * 复选框类
	 * @author Jave.Lin
	 */	
	public class CheckBox extends Control
	{
		private var _label:Label;
		private var _tick:Shape;
		private var _checked:Boolean=false;
		private var _isDowned:Boolean=false;
		private var _outlineGlowFilter:GlowFilter;
		private var _innerlineGlowFilter:GlowFilter;
		
		public function get text():String
		{
			return _label.text;
		}
		
		public function set text(value:String):void
		{
			if(_label.text!=value)
			{
				_label.text=value;
				_label.y=-5/2;
			}
		}
		
		public function get checked():Boolean
		{
			return _checked;
		}
		
		public function set checked(value:Boolean):void
		{
			if(_checked!=value)
			{
				_checked=value;
				refreshBackground();
				dispatchEvent(new CheckBoxEvent(CheckBoxEvent.CHECKED_CHANGED));
			}
		}
		
		public function CheckBox()
		{
			super();
		}
		
		protected override function initialize():void
		{
			this.mouseChildren=false;
			
			_outlineGlowFilter=new GlowFilter(0x00ff00,1,3,3,2);
			_innerlineGlowFilter=new GlowFilter(0x00ff00,1,3,3,2,1,true);
			
			_label=new Label();
			_label.x=10;
			addChild(_label);
			
			_tick=new Shape();
			addChild(_tick);
			
			if(stage)
			{
				onAddedToStageHandler();
			}
			else
			{
				addEventListener(Event.ADDED_TO_STAGE,onAddedToStageHandler);
			}
			
			refreshBackground();
			
			_tick.filters=[_outlineGlowFilter];
		}
		
		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);
			addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownHandler);
			addEventListener(MouseEvent.MOUSE_UP,onMouseUpHandler);
			addEventListener(MouseEvent.CLICK,onClickHandler);
		}
		
		private function onClickHandler(e:MouseEvent):void
		{
			checked=!checked;
		}
		
		private function onMouseUpHandler(e:MouseEvent=null):void
		{
			if(_isDowned)
			{
				_isDowned=false;
				_label.x-=1;
				_label.y-=1;
			}
		}
		
		private function onMouseDownHandler(e:MouseEvent):void
		{
			if(!_isDowned)
			{
				_isDowned=true;
				_label.x+=1;
				_label.y+=1;
			}
		}
		
		private function onMouseOutHandler(e:MouseEvent):void
		{
			removeEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
			addEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler);
			
			onMouseUpHandler();
			
			_tick.filters=[_outlineGlowFilter];
		}
		
		private function onMouseOverHandler(e:MouseEvent):void
		{
			removeEventListener(MouseEvent.MOUSE_OVER,onMouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT,onMouseOutHandler);
			
			_tick.filters=[_outlineGlowFilter,_innerlineGlowFilter];
		}
		
		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);
			removeEventListener(MouseEvent.CLICK,onClickHandler);
		}
		
		protected override function refreshBackground():void
		{
			_tick.graphics.clear();
			
			_tick.graphics.beginFill(0x00ff00,0.01);
			_tick.graphics.drawCircle(5,5,4);
			_tick.graphics.endFill();
			
			_tick.graphics.lineStyle(1);
			_tick.graphics.moveTo(0,0);
			_tick.graphics.lineTo(10,0);
			_tick.graphics.lineTo(10,10);
			_tick.graphics.lineTo(0,10);
			_tick.graphics.lineTo(0,0);
			
			if(_checked)
			{
				_tick.graphics.lineStyle(2);
				_tick.graphics.moveTo(2,5);
				_tick.graphics.lineTo(4,8);
				_tick.graphics.lineTo(8,2);
			}
		}
	}
}

测试类:

package test
{
	import controlsEvents.CheckBoxEvent;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	
	public class CheckBoxTest extends Sprite
	{
		private var author:Label;
		private var cb:CheckBox;
		
		public function CheckBoxTest()
		{
			super();
			stage.color=0xccccbb;
			stage.frameRate=60;
			stage.align=StageAlign.TOP_LEFT;
			stage.scaleMode=StageScaleMode.NO_SCALE;
			
			author=new Label();
			addChild(author);
			author.textColor=0x00ff00;
			author.fontSize=24;
			author.x=100;
			author.y=50;
			author.text="作者:Jave.Lin";
			
			cb=new CheckBox();
			addChild(cb);
			
			cb.x=100;
			cb.y=100;
			
			cb.text="testCheckBox";
			
			cb.addEventListener(CheckBoxEvent.CHECKED_CHANGED,onChanged);
		}
		
		private function onChanged(e:CheckBoxEvent):void
		{
			trace(cb.checked);
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值