/controls/QTextField.as
ActionScript | 848 lines | 444 code | 71 blank | 333 comment | 75 complexity | fa7f6405fdcc0d8bc877fae139bc1c93 MD5 | raw file
- package slw.controls
- {
- import flash.display.DisplayObject;
- import flash.display.MovieClip;
- import flash.display.Shape;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.FocusEvent;
- import flash.events.MouseEvent;
- import flash.filters.GlowFilter;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.text.TextFieldType;
- import flash.text.TextFormat;
- import flash.ui.Mouse;
- import flash.ui.MouseCursor;
-
- import slw.core.UIComponent;
- import slw.events.LoaderEvent;
-
-
- /**
- * QTextField
- * ??????????????????????
- * <p>
- * <listing>
- * var t:QTextField=new QTextField("This is a component QTextField.");
- this.addChild(t);
- t.move(100,100);
-
- t.focusedActive=true;
- t.border=true;
- t.borderColor=0x0099CC;
- t.borderAlpha=0.6;
- t.defaultTextFormat=new TextFormat(null,12,0x0);
- t.displayStates=array;
-
- t.border=true;
- t.background=true;
- t.multiline=true;
-
- function getDisplay():Sprite{
- var s:Sprite=new Sprite();
- s.graphics.beginFill(0xcccccc+Math.random()*0xffffff,1);
- s.graphics.drawRect(0,0,100,100);
- s.graphics.endFill();
-
- return s;
- }
- * </listing>
- * </p>
- * @author ddx<br/>
- * 2011-7-19
- *
- */
- public class QTextField extends UIComponent
- {
- /**
- * "normal",??????
- */
- public static const NORMAL:String="normal";
- /**
- * "focused",????????
- */
- public static const FOCUSED:String="focused";
- //???????
- private var _state:String=NORMAL;
- private var _autoSize:String=TextFieldAutoSize.NONE;
-
- //????
- private var _displayStates:Array=[];
- //??????
- private var _focusedFilters:Array=[new GlowFilter(0x0099CC,1,2,2,2,3,false,false)];
-
- //???????????????????
- private var _textField:TextField;
- private var _borderContainer:Sprite;
- private var _bgContainer:Sprite;
- private var _stateContainer:Sprite;
-
- //???html????????
- private var _text:String;
- private var _htmlText:String;
- private var _restrict:String;
-
- //????????????????????????,?????
- private var _multiline:Boolean;
- private var _border:Boolean;
- private var _focusedActive:Boolean;
- private var _background:Boolean;
- private var _editabled:Boolean;
- private var _displayAsPassword:Boolean;
-
- //????????????alpha???????????????alpha
- private var _textColor:uint=0x0;
- private var _borderColor:uint=0x0099CC;
- private var _borderAlpha:Number=0.6;
- private var _backgroundColor:uint=0xeeeeee;
- private var _backgroundAlpha:Number=1;
-
- //??????
- private var _defaultFormat:TextFormat;
-
- //????
- private var _width:Number=0;
- private var _height:Number=0;
- /**
- * ????
- * @param text:String="" ?????
- *
- */
- public function QTextField(text:String="")
- {
- super();
- this.listName="QTextField";
-
- init();
-
- setSize(100,100);
- this.text=text;
- this.round=10;
- }
-
- /*
- -----------------------------------
- setters getters
- -----------------------------------
- */
- //
- /**
- * ?????????TextField.text
- * @param value:String
- *
- */
- public function set text(value:String):void{
- if(_text==value)return;
-
- _text=value;
- _htmlText=null;
-
- if(_autoSize!=TextFieldAutoSize.NONE){
- this.width=_textField.width;
- this.height=_textField.height;
- }else{
- updateDisplayList();
- }
- }
- //
- /**
- * ?????????TextField.text
- * @param value:String
- *
- */
- public function get text():String{
- return _text;
- }
- //
- /**
- * ?????html????TextField.htmlText
- * @param value:String
- *
- */
- public function set htmlText(value:String):void{
- if(_htmlText==value)return;
-
- _htmlText=value;
- _text=null;
-
- if(_autoSize!=TextFieldAutoSize.NONE){
- this.width=_textField.width;
- this.height=_textField.height;
- }else{
- updateDisplayList();
- }
- }
- //
- /**
- * ?????html????TextField.htmlText
- * @param value:String
- *
- */
- public function get htmlText():String{
- return _htmlText;
- }
- //
- /**
- * ???????,???:false
- * @param value:Boolean
- *
- */
- public function set multiline(value:Boolean):void{
- if(_multiline==value)return;
-
- _multiline=value;
-
- _textField.multiline=_multiline;
- _textField.wordWrap=_multiline;
-
- if(_autoSize!=TextFieldAutoSize.NONE){
- this.width=_textField.width;
- this.height=_textField.height;
- }else{
- updateDisplayList();
- }
- }
- //
- /**
- * ???????,???:false
- * @param value:Boolean
- *
- */
- public function get multiline():Boolean{
- return _multiline;
- }
- //
- /**
- * ??????????:false
- * @param value:Boolean
- *
- */
- public function set border(value:Boolean):void{
- if(_border==value)return;
-
- _border=value;
-
- if(_border&&_borderContainer==null){
- _borderContainer=new Sprite();
- _borderContainer.mouseEnabled=false;
- this.addChild(_borderContainer);
-
- if(_border)
- drawBorder(_borderContainer,_borderColor,_borderAlpha,this.width,this.height,this.round);
-
- if(_state==FOCUSED)setState(_state);
- }else if(_borderContainer!=null){
- this.removeChild(_borderContainer);
- _borderContainer=null;
- }
- }
- //
- /**
- * ??????????:false
- * @param value:Boolean
- *
- */
- public function get border():Boolean{
- return _border;
- }
- //
- /**
- * ????????:0x0099CC
- * @param value:uint
- *
- */
- public function set borderColor(value:uint):void{
- if(_borderColor==value)return;
-
- _borderColor=value;
-
- if(_border)
- drawBorder(_borderContainer,_borderColor,_borderAlpha,this.width,this.height,this.round);
- }
- //
- /**
- * ????????:0x0099CC
- * @param value:uint
- *
- */
- public function get borderColor():uint{
- return _borderColor;
- }
- //
- /**
- * ???????????:0.6
- * @param value:Number
- *
- */
- public function set borderAlpha(value:Number):void{
- if(_borderAlpha==value)return;
-
- _borderAlpha=value;
-
- if(_border)
- drawBorder(_borderContainer,_borderColor,_borderAlpha,this.width,this.height,this.round);
- }
- //
- /**
- * ???????,???:0.6
- * @param value:Number
- *
- */
- public function get borderAlpha():Number{
- return _borderAlpha;
- }
- //
- /**
- * ?????????????????displayStates?????????????(????????????)?
- * ?????????????skin???????????????????????????????????:10px
- * @param value:Number
- *
- */
- override public function set round(value:Number):void{
- if(this.round==value)return;
-
- super.round=value;
-
- if(_border)
- drawBorder(_borderContainer,_borderColor,_borderAlpha,this.width,this.height,this.round);
- if(_background)
- drawRect(_bgContainer,0,_backgroundColor,_backgroundAlpha,0,0,this.width,this.height,this.round,true);
- }
- //
- /**
- * ????????????:false
- * @param value:Boolean
- *
- */
- public function set background(value:Boolean):void{
- if(_background==value)return;
-
- _background=value;
-
- if(_background){
- if(_bgContainer==null){
- _bgContainer=new Sprite();
- _bgContainer.mouseEnabled=false;
- this.buildContainer.addChildAt(_bgContainer,0);
- }
- }else if(_bgContainer!=null){
- this.buildContainer.removeChild(_bgContainer);
- _bgContainer==null;
- }
-
- if(_background)
- drawRect(_bgContainer,0,_backgroundColor,_backgroundAlpha,0,0,this.width,this.height,this.round,true);
- }
- //
- /**
- * ????????????:false
- * @param value:Boolean
- *
- */
- public function get background():Boolean{
- return _background;
- }
- //
- /**
- * ????????:0xeeeeee
- * @param value:uint
- *
- */
- public function set backgroundColor(value:uint):void{
- if(_backgroundColor==value)return;
-
- _backgroundColor=value;
-
- if(_background)
- drawRect(_bgContainer,0,_backgroundColor,_backgroundAlpha,0,0,this.width,this.height,this.round,true);
- }
- //
- /**
- * ????????:0xeeeeee
- * @param value:uint
- *
- */
- public function get backgroundColor():uint{
- return _backgroundColor;
- }
- //
- /**
- * ?????,???:0.2
- * @param value:Number
- *
- */
- public function set backgroundAlpha(value:Number):void{
- if(_backgroundAlpha==value)return;
-
- _backgroundAlpha=value;
-
- if(_background)
- drawRect(_bgContainer,0,_backgroundColor,_backgroundAlpha,0,0,this.width,this.height,this.round,true);
- }
- //
- /**
- * ?????,???:0.2
- * @param value:Number
- *
- */
- public function get backgroundAlpha():Number{
- return _backgroundAlpha;
- }
- //
- /**
- * ????????:0x0,?TextField.textColor
- * @param value:uint
- *
- */
- public function set textColor(value:uint):void{
- if(_textColor==value)return;
-
- _textColor=value;
- _textField.textColor=_textColor;
- }
- //
- /**
- * ????????:0x0,?TextField.textColor
- * @param value:uint
- *
- */
- public function get textColor():uint{
- return _textColor;
- }
- //
- /**
- * ??????,???null???new TextFormat(null,12,0x0,false,false,false,"","","left",0,0,0,0)
- * @param value:TextFormat
- *
- */
- public function set defaultTextFormat(value:TextFormat):void{
- if(_defaultFormat==value)return;
-
- _defaultFormat=value;
- if(_defaultFormat==null)
- _defaultFormat=new TextFormat(null,12,0x0,false,false,false,"","","left",0,0,0,0);
-
- if(_autoSize!=TextFieldAutoSize.NONE){
- this.width=_textField.width;
- this.height=_textField.height;
- }else{
- updateDisplayList();
- }
- }
- //
- /**
- * ??????,???null???new TextFormat(null,12,0x0,false,false,false,"","","left",0,0,0,0)
- * @param value:TextFormat
- *
- */
- public function get defaultTextFormat():TextFormat{
- return _defaultFormat;
- }
- //
- /**
- * ??????????????:TextFieldAutoSize.NONE??TextField.autoSize
- * @param value:String
- *
- */
- public function set autoSize(value:String):void{
- if(_autoSize==value)return;
-
- _autoSize=value;
- _textField.autoSize=_autoSize;
-
- if(_autoSize!=TextFieldAutoSize.NONE){
- this.width=_textField.width;
- this.height=_textField.height;
-
- _textField.addEventListener(Event.CHANGE,textFieldChange);
- }else{
- updateDisplayList();
- _textField.removeEventListener(Event.CHANGE,textFieldChange);
- }
- }
- //
- /**
- * ??????????????:TextFieldAutoSize.NONE??TextField.autoSize
- * @param value:String
- *
- */
- public function get autoSize():String{
- return _autoSize;
- }
- //
- /**
- * ?????????????????????:null??TextField.restrict
- * @param value:String
- *
- */
- public function set restrict(value:String):void{
- if(_restrict==value)return;
-
- _restrict=value;
- _textField.restrict=_restrict;
-
- updateDisplayList();
- }
- //
- /**
- * ?????????????????????:null??TextField.restrict
- * @param value:String
- *
- */
- public function get restrict():String{
- return _restrict;
- }
- //
- /**
- * ???????????:false
- * @param value:Boolean
- *
- */
- public function set editabled(value:Boolean):void{
- if(_editabled==value)return;
-
- _editabled=value;
-
- if(_editabled){
- _textField.type=TextFieldType.INPUT;
- }else{
- _textField.type=TextFieldType.DYNAMIC;
- }
- }
- //
- /**
- * ???????????:false
- * @param value:Boolean
- *
- */
- public function get editabled():Boolean{
- return _editabled;
- }
- //
- /**
- * ?????????????:false,?TextField.displayAsPassword
- * @param value:Boolean
- *
- */
- public function set displayAsPassword(value:Boolean):void{
- if(_displayAsPassword==value)return;
-
- _displayAsPassword=value;
-
- _textField.displayAsPassword=_displayAsPassword;
-
- updateDisplayList();
- }
- //
- /**
- * ?????????????:false,?TextField.displayAsPassword
- * @param value:Boolean
- *
- */
- public function get displayAsPassword():Boolean{
- return _displayAsPassword;
- }
- //
- /**
- * ??????????,,?skin???????????1?2??????????????,
- * ??displayStates?displayStates[0]?displayStates[1]?????????????,???:false
- * @param value:Boolean
- *
- */
- public function set focusedActive(value:Boolean):void{
- if(_focusedActive==value)return;
-
- _focusedActive=value;
- if(_focusedActive){
- _textField.addEventListener(FocusEvent.FOCUS_IN,textFieldFocusIn);
- _textField.addEventListener(FocusEvent.FOCUS_OUT,textFieldFocusOut);
- }else{
- _textField.removeEventListener(FocusEvent.FOCUS_IN,textFieldFocusIn);
- _textField.removeEventListener(FocusEvent.FOCUS_OUT,textFieldFocusOut);
- }
-
- if(_state==FOCUSED)setState(_state);
- }
- //
- /**
- * ??????????,,?skin???????????1?2??????????????,
- * ??displayStates?displayStates[0]?displayStates[1]?????????????,???:false
- * @param value:Boolean
- *
- */
- public function get focusedActive():Boolean{
- return _focusedActive;
- }
- //
- /**
- * ?????????
- * @param value:Boolean
- *
- */
- override public function set focused(value:Boolean):void{
- if(this.focused==value)return;
-
- if(value){
- this.stage.focus=_textField;
- _textField.setSelection(_textField.length,_textField.length);
- }else{
- this.stage.focus=null;
- }
- }
- //
- /**
- * ?????????
- * @param value:Boolean
- *
- */
- override public function get focused():Boolean{
- return this.stage==null?false:this.stage.focus==_textField;
- }
- //
- /**
- * focusedActive?true???????????:[new GlowFilter(0x0099CC,1,2,2,2,3,false,false)]
- * @param value:Array
- *
- */
- public function set focusedFilters(value:Array):void{
- if(_focusedFilters==value)return;
-
- _focusedFilters=value;
-
- if(_state==FOCUSED)setState(_state);
- }
- //
- /**
- * focusedActive?true???????????:[new GlowFilter(0x0099CC,1,2,2,2,3,false,false)]
- * @param value:Array
- *
- */
- public function get focusedFilters():Array{
- return _focusedFilters;
- }
- //
- /**
- * ?????????????????????????????DisplayObject?????????????2?????????:[]
- * @param value:Array
- *
- */
- public function set displayStates(value:Array):void{
- if(_displayStates==value)return;
-
- _displayStates=value;
-
- if(_displayStates==null||_displayStates.length==0){
- if(_stateContainer!=null){
- this.buildContainer.removeChild(_stateContainer);
- _stateContainer=null;
- }
- }else{
- _displayStates.length=Math.min(_displayStates.length,2);
- if(_stateContainer==null){
- _stateContainer=new Sprite();
- _stateContainer.mouseChildren=false;
- this.buildContainer.addChildAt(_stateContainer,0);
- }
- setState(_state);
-
- if(this.autoSizeBySkin){
- this.width=_stateContainer.width;
- this.height=_stateContainer.height;
- }
- }
- }
- //
- /**
- * ?????????????????????????????DisplayObject?????????????2?????????:[]
- * @param value:Array
- *
- */
- public function get displayStates():Array{
- return _displayStates;
- }
- //
- /**
- * ????url????????????????????????,?????????????????????2?????????:[]
- * @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;
- }
- //
- override public function set width(value:Number):void{
- if(_autoSize!=TextFieldAutoSize.NONE){
- value=_textField.width;
- }
- super.width=value;
- if(_width==value){
- return;
- }
- _width=value;
-
- _textField.width=value;
-
- if(_border)
- drawBorder(_borderContainer,_borderColor,_borderAlpha,this.width,this.height,this.round);
- }
- //
- override public function set height(value:Number):void{
- if(_autoSize!=TextFieldAutoSize.NONE){
- value=_textField.height;
- }
- super.height=value;
- if(_height==value){
- return;
- }
- _height=value;
-
- _textField.height=value;
-
- if(_border)
- drawBorder(_borderContainer,_borderColor,_borderAlpha,this.width,this.height,this.round);
- }
- /*
- -----------------------------------
- public methods
- -----------------------------------
- */
- //
- /**
- * ????????TextField.setTextFormat
- * @param format:TextFormat
- * @param startIndex:int=0
- * @param endIndex:int=int.MAX_VALUE
- *
- */
- public function setTextFormat(format:TextFormat,startIndex:int=0,endIndex:int=int.MAX_VALUE):void{
- _textField.setTextFormat(format,startIndex,endIndex);
- }
- //
- override public function destroy():void{
- _textField.removeEventListener(FocusEvent.FOCUS_IN,textFieldFocusIn);
- _textField.removeEventListener(FocusEvent.FOCUS_OUT,textFieldFocusOut);
- _textField.removeEventListener(Event.CHANGE,textFieldChange);
-
- this.removeEventListener(LoaderEvent.LOAD_COMPLETE,thisLoadComplete);
- super.destroy();
- }
- /*
- -----------------------------------
- private methods
- -----------------------------------
- */
- //???
- private function init():void{
- _textField=new TextField();
- this.buildContainer.addChild(_textField);
-
- _defaultFormat=new TextFormat(null,12,0x0,false,false,false,"","","left",0,0,0,0);
- }
- //????
- private function updateDisplayList():void{
- if(_defaultFormat!=null){
- _textField.defaultTextFormat=_defaultFormat;
- }
- if(_text!=null){
- _textField.text=_text;
- }else if(_htmlText!=null){
- _textField.htmlText=_htmlText;
- }
-
- if(_borderContainer!=null)
- this.setChildIndex(_borderContainer,this.numChildren-1);
- }
- //????
- private function setState(state:String):void{
- if(!_focusedActive){
- return;
- }
- switch(state){
- case NORMAL:
- showState(0);
- break;
- case FOCUSED:
- showState(1);
- break;
- default:
- throw new Error("QTextField ?????:["+NORMAL+","+FOCUSED+"]??");
- }
- }
- //????
- private function showState(index:int):void{
- var $targets:Array=this.skinTargets;
- for(var i:int=0;i<$targets.length;i++){
- if(($targets[i] as MovieClip)!=null){
- MovieClip($targets[i]).gotoAndStop(index+1);
- }
- }
-
- if(_displayStates[index] is DisplayObject){
- if(_stateContainer!=null){
- while(_stateContainer.numChildren>0){
- _stateContainer.removeChildAt(0);
- }
- _stateContainer.addChild(_displayStates[index]);
- }
- }
-
- if(_border){
- if(index==0){
- if(_borderContainer!=null)_borderContainer.filters=[];
- }else if(index==1){
- if(_borderContainer!=null)_borderContainer.filters=_focusedFilters;
- }
- }
- }
- //????
- private function drawBorder(target:Sprite,color:uint,alpha:Number=1,width:Number=1,height:Number=1,round:Number=0):void{
- target.graphics.clear();
- target.graphics.lineStyle(1,color,alpha,true,"none","round");
- target.graphics.drawRoundRect(0,0,width-1,height-1,round,round);
- }
- //????
- 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();
- }
- //????
- private function textFieldFocusIn(e:FocusEvent):void{
- if(_state==FOCUSED)return;
-
- _state=FOCUSED;
- setState(_state);
- }
- //????
- private function textFieldFocusOut(e:FocusEvent):void{
- if(_state==NORMAL)return;
-
- _state=NORMAL;
- setState(_state);
- }
- //?????????
- private function textFieldChange(e:Event):void{
- this.width=_textField.width;
- this.height=_textField.height;
- }
- //????????
- private function thisLoadComplete(e:LoaderEvent):void{
- this.displayStates=e.datas;
- this.removeEventListener(LoaderEvent.LOAD_COMPLETE,thisLoadComplete);
- }
- }
- }