PageRenderTime 1948ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 1ms

/controls/QTextField.as

http://flash-games.googlecode.com/
ActionScript | 848 lines | 444 code | 71 blank | 333 comment | 75 complexity | fa7f6405fdcc0d8bc877fae139bc1c93 MD5 | raw file
  1. package slw.controls
  2. {
  3. import flash.display.DisplayObject;
  4. import flash.display.MovieClip;
  5. import flash.display.Shape;
  6. import flash.display.Sprite;
  7. import flash.events.Event;
  8. import flash.events.FocusEvent;
  9. import flash.events.MouseEvent;
  10. import flash.filters.GlowFilter;
  11. import flash.text.TextField;
  12. import flash.text.TextFieldAutoSize;
  13. import flash.text.TextFieldType;
  14. import flash.text.TextFormat;
  15. import flash.ui.Mouse;
  16. import flash.ui.MouseCursor;
  17. import slw.core.UIComponent;
  18. import slw.events.LoaderEvent;
  19. /**
  20. * QTextField
  21. * ??????????????????????
  22. * <p>
  23. * <listing>
  24. * var t:QTextField=new QTextField("This is a component QTextField.");
  25. this.addChild(t);
  26. t.move(100,100);
  27. t.focusedActive=true;
  28. t.border=true;
  29. t.borderColor=0x0099CC;
  30. t.borderAlpha=0.6;
  31. t.defaultTextFormat=new TextFormat(null,12,0x0);
  32. t.displayStates=array;
  33. t.border=true;
  34. t.background=true;
  35. t.multiline=true;
  36. function getDisplay():Sprite{
  37. var s:Sprite=new Sprite();
  38. s.graphics.beginFill(0xcccccc+Math.random()*0xffffff,1);
  39. s.graphics.drawRect(0,0,100,100);
  40. s.graphics.endFill();
  41. return s;
  42. }
  43. * </listing>
  44. * </p>
  45. * @author ddx<br/>
  46. * 2011-7-19
  47. *
  48. */
  49. public class QTextField extends UIComponent
  50. {
  51. /**
  52. * "normal",??????
  53. */
  54. public static const NORMAL:String="normal";
  55. /**
  56. * "focused",????????
  57. */
  58. public static const FOCUSED:String="focused";
  59. //???????
  60. private var _state:String=NORMAL;
  61. private var _autoSize:String=TextFieldAutoSize.NONE;
  62. //????
  63. private var _displayStates:Array=[];
  64. //??????
  65. private var _focusedFilters:Array=[new GlowFilter(0x0099CC,1,2,2,2,3,false,false)];
  66. //???????????????????
  67. private var _textField:TextField;
  68. private var _borderContainer:Sprite;
  69. private var _bgContainer:Sprite;
  70. private var _stateContainer:Sprite;
  71. //???html????????
  72. private var _text:String;
  73. private var _htmlText:String;
  74. private var _restrict:String;
  75. //????????????????????????,?????
  76. private var _multiline:Boolean;
  77. private var _border:Boolean;
  78. private var _focusedActive:Boolean;
  79. private var _background:Boolean;
  80. private var _editabled:Boolean;
  81. private var _displayAsPassword:Boolean;
  82. //????????????alpha???????????????alpha
  83. private var _textColor:uint=0x0;
  84. private var _borderColor:uint=0x0099CC;
  85. private var _borderAlpha:Number=0.6;
  86. private var _backgroundColor:uint=0xeeeeee;
  87. private var _backgroundAlpha:Number=1;
  88. //??????
  89. private var _defaultFormat:TextFormat;
  90. //????
  91. private var _width:Number=0;
  92. private var _height:Number=0;
  93. /**
  94. * ????
  95. * @param text:String="" ?????
  96. *
  97. */
  98. public function QTextField(text:String="")
  99. {
  100. super();
  101. this.listName="QTextField";
  102. init();
  103. setSize(100,100);
  104. this.text=text;
  105. this.round=10;
  106. }
  107. /*
  108. -----------------------------------
  109. setters getters
  110. -----------------------------------
  111. */
  112. //
  113. /**
  114. * ?????????TextField.text
  115. * @param value:String
  116. *
  117. */
  118. public function set text(value:String):void{
  119. if(_text==value)return;
  120. _text=value;
  121. _htmlText=null;
  122. if(_autoSize!=TextFieldAutoSize.NONE){
  123. this.width=_textField.width;
  124. this.height=_textField.height;
  125. }else{
  126. updateDisplayList();
  127. }
  128. }
  129. //
  130. /**
  131. * ?????????TextField.text
  132. * @param value:String
  133. *
  134. */
  135. public function get text():String{
  136. return _text;
  137. }
  138. //
  139. /**
  140. * ?????html????TextField.htmlText
  141. * @param value:String
  142. *
  143. */
  144. public function set htmlText(value:String):void{
  145. if(_htmlText==value)return;
  146. _htmlText=value;
  147. _text=null;
  148. if(_autoSize!=TextFieldAutoSize.NONE){
  149. this.width=_textField.width;
  150. this.height=_textField.height;
  151. }else{
  152. updateDisplayList();
  153. }
  154. }
  155. //
  156. /**
  157. * ?????html????TextField.htmlText
  158. * @param value:String
  159. *
  160. */
  161. public function get htmlText():String{
  162. return _htmlText;
  163. }
  164. //
  165. /**
  166. * ???????,???:false
  167. * @param value:Boolean
  168. *
  169. */
  170. public function set multiline(value:Boolean):void{
  171. if(_multiline==value)return;
  172. _multiline=value;
  173. _textField.multiline=_multiline;
  174. _textField.wordWrap=_multiline;
  175. if(_autoSize!=TextFieldAutoSize.NONE){
  176. this.width=_textField.width;
  177. this.height=_textField.height;
  178. }else{
  179. updateDisplayList();
  180. }
  181. }
  182. //
  183. /**
  184. * ???????,???:false
  185. * @param value:Boolean
  186. *
  187. */
  188. public function get multiline():Boolean{
  189. return _multiline;
  190. }
  191. //
  192. /**
  193. * ??????????:false
  194. * @param value:Boolean
  195. *
  196. */
  197. public function set border(value:Boolean):void{
  198. if(_border==value)return;
  199. _border=value;
  200. if(_border&&_borderContainer==null){
  201. _borderContainer=new Sprite();
  202. _borderContainer.mouseEnabled=false;
  203. this.addChild(_borderContainer);
  204. if(_border)
  205. drawBorder(_borderContainer,_borderColor,_borderAlpha,this.width,this.height,this.round);
  206. if(_state==FOCUSED)setState(_state);
  207. }else if(_borderContainer!=null){
  208. this.removeChild(_borderContainer);
  209. _borderContainer=null;
  210. }
  211. }
  212. //
  213. /**
  214. * ??????????:false
  215. * @param value:Boolean
  216. *
  217. */
  218. public function get border():Boolean{
  219. return _border;
  220. }
  221. //
  222. /**
  223. * ????????:0x0099CC
  224. * @param value:uint
  225. *
  226. */
  227. public function set borderColor(value:uint):void{
  228. if(_borderColor==value)return;
  229. _borderColor=value;
  230. if(_border)
  231. drawBorder(_borderContainer,_borderColor,_borderAlpha,this.width,this.height,this.round);
  232. }
  233. //
  234. /**
  235. * ????????:0x0099CC
  236. * @param value:uint
  237. *
  238. */
  239. public function get borderColor():uint{
  240. return _borderColor;
  241. }
  242. //
  243. /**
  244. * ???????????:0.6
  245. * @param value:Number
  246. *
  247. */
  248. public function set borderAlpha(value:Number):void{
  249. if(_borderAlpha==value)return;
  250. _borderAlpha=value;
  251. if(_border)
  252. drawBorder(_borderContainer,_borderColor,_borderAlpha,this.width,this.height,this.round);
  253. }
  254. //
  255. /**
  256. * ???????,???:0.6
  257. * @param value:Number
  258. *
  259. */
  260. public function get borderAlpha():Number{
  261. return _borderAlpha;
  262. }
  263. //
  264. /**
  265. * ?????????????????displayStates?????????????(????????????)?
  266. * ?????????????skin???????????????????????????????????:10px
  267. * @param value:Number
  268. *
  269. */
  270. override public function set round(value:Number):void{
  271. if(this.round==value)return;
  272. super.round=value;
  273. if(_border)
  274. drawBorder(_borderContainer,_borderColor,_borderAlpha,this.width,this.height,this.round);
  275. if(_background)
  276. drawRect(_bgContainer,0,_backgroundColor,_backgroundAlpha,0,0,this.width,this.height,this.round,true);
  277. }
  278. //
  279. /**
  280. * ????????????:false
  281. * @param value:Boolean
  282. *
  283. */
  284. public function set background(value:Boolean):void{
  285. if(_background==value)return;
  286. _background=value;
  287. if(_background){
  288. if(_bgContainer==null){
  289. _bgContainer=new Sprite();
  290. _bgContainer.mouseEnabled=false;
  291. this.buildContainer.addChildAt(_bgContainer,0);
  292. }
  293. }else if(_bgContainer!=null){
  294. this.buildContainer.removeChild(_bgContainer);
  295. _bgContainer==null;
  296. }
  297. if(_background)
  298. drawRect(_bgContainer,0,_backgroundColor,_backgroundAlpha,0,0,this.width,this.height,this.round,true);
  299. }
  300. //
  301. /**
  302. * ????????????:false
  303. * @param value:Boolean
  304. *
  305. */
  306. public function get background():Boolean{
  307. return _background;
  308. }
  309. //
  310. /**
  311. * ????????:0xeeeeee
  312. * @param value:uint
  313. *
  314. */
  315. public function set backgroundColor(value:uint):void{
  316. if(_backgroundColor==value)return;
  317. _backgroundColor=value;
  318. if(_background)
  319. drawRect(_bgContainer,0,_backgroundColor,_backgroundAlpha,0,0,this.width,this.height,this.round,true);
  320. }
  321. //
  322. /**
  323. * ????????:0xeeeeee
  324. * @param value:uint
  325. *
  326. */
  327. public function get backgroundColor():uint{
  328. return _backgroundColor;
  329. }
  330. //
  331. /**
  332. * ?????,???:0.2
  333. * @param value:Number
  334. *
  335. */
  336. public function set backgroundAlpha(value:Number):void{
  337. if(_backgroundAlpha==value)return;
  338. _backgroundAlpha=value;
  339. if(_background)
  340. drawRect(_bgContainer,0,_backgroundColor,_backgroundAlpha,0,0,this.width,this.height,this.round,true);
  341. }
  342. //
  343. /**
  344. * ?????,???:0.2
  345. * @param value:Number
  346. *
  347. */
  348. public function get backgroundAlpha():Number{
  349. return _backgroundAlpha;
  350. }
  351. //
  352. /**
  353. * ????????:0x0,?TextField.textColor
  354. * @param value:uint
  355. *
  356. */
  357. public function set textColor(value:uint):void{
  358. if(_textColor==value)return;
  359. _textColor=value;
  360. _textField.textColor=_textColor;
  361. }
  362. //
  363. /**
  364. * ????????:0x0,?TextField.textColor
  365. * @param value:uint
  366. *
  367. */
  368. public function get textColor():uint{
  369. return _textColor;
  370. }
  371. //
  372. /**
  373. * ??????,???null???new TextFormat(null,12,0x0,false,false,false,"","","left",0,0,0,0)
  374. * @param value:TextFormat
  375. *
  376. */
  377. public function set defaultTextFormat(value:TextFormat):void{
  378. if(_defaultFormat==value)return;
  379. _defaultFormat=value;
  380. if(_defaultFormat==null)
  381. _defaultFormat=new TextFormat(null,12,0x0,false,false,false,"","","left",0,0,0,0);
  382. if(_autoSize!=TextFieldAutoSize.NONE){
  383. this.width=_textField.width;
  384. this.height=_textField.height;
  385. }else{
  386. updateDisplayList();
  387. }
  388. }
  389. //
  390. /**
  391. * ??????,???null???new TextFormat(null,12,0x0,false,false,false,"","","left",0,0,0,0)
  392. * @param value:TextFormat
  393. *
  394. */
  395. public function get defaultTextFormat():TextFormat{
  396. return _defaultFormat;
  397. }
  398. //
  399. /**
  400. * ??????????????:TextFieldAutoSize.NONE??TextField.autoSize
  401. * @param value:String
  402. *
  403. */
  404. public function set autoSize(value:String):void{
  405. if(_autoSize==value)return;
  406. _autoSize=value;
  407. _textField.autoSize=_autoSize;
  408. if(_autoSize!=TextFieldAutoSize.NONE){
  409. this.width=_textField.width;
  410. this.height=_textField.height;
  411. _textField.addEventListener(Event.CHANGE,textFieldChange);
  412. }else{
  413. updateDisplayList();
  414. _textField.removeEventListener(Event.CHANGE,textFieldChange);
  415. }
  416. }
  417. //
  418. /**
  419. * ??????????????:TextFieldAutoSize.NONE??TextField.autoSize
  420. * @param value:String
  421. *
  422. */
  423. public function get autoSize():String{
  424. return _autoSize;
  425. }
  426. //
  427. /**
  428. * ?????????????????????:null??TextField.restrict
  429. * @param value:String
  430. *
  431. */
  432. public function set restrict(value:String):void{
  433. if(_restrict==value)return;
  434. _restrict=value;
  435. _textField.restrict=_restrict;
  436. updateDisplayList();
  437. }
  438. //
  439. /**
  440. * ?????????????????????:null??TextField.restrict
  441. * @param value:String
  442. *
  443. */
  444. public function get restrict():String{
  445. return _restrict;
  446. }
  447. //
  448. /**
  449. * ???????????:false
  450. * @param value:Boolean
  451. *
  452. */
  453. public function set editabled(value:Boolean):void{
  454. if(_editabled==value)return;
  455. _editabled=value;
  456. if(_editabled){
  457. _textField.type=TextFieldType.INPUT;
  458. }else{
  459. _textField.type=TextFieldType.DYNAMIC;
  460. }
  461. }
  462. //
  463. /**
  464. * ???????????:false
  465. * @param value:Boolean
  466. *
  467. */
  468. public function get editabled():Boolean{
  469. return _editabled;
  470. }
  471. //
  472. /**
  473. * ?????????????:false,?TextField.displayAsPassword
  474. * @param value:Boolean
  475. *
  476. */
  477. public function set displayAsPassword(value:Boolean):void{
  478. if(_displayAsPassword==value)return;
  479. _displayAsPassword=value;
  480. _textField.displayAsPassword=_displayAsPassword;
  481. updateDisplayList();
  482. }
  483. //
  484. /**
  485. * ?????????????:false,?TextField.displayAsPassword
  486. * @param value:Boolean
  487. *
  488. */
  489. public function get displayAsPassword():Boolean{
  490. return _displayAsPassword;
  491. }
  492. //
  493. /**
  494. * ??????????,,?skin???????????1?2??????????????,
  495. * ??displayStates?displayStates[0]?displayStates[1]?????????????,???:false
  496. * @param value:Boolean
  497. *
  498. */
  499. public function set focusedActive(value:Boolean):void{
  500. if(_focusedActive==value)return;
  501. _focusedActive=value;
  502. if(_focusedActive){
  503. _textField.addEventListener(FocusEvent.FOCUS_IN,textFieldFocusIn);
  504. _textField.addEventListener(FocusEvent.FOCUS_OUT,textFieldFocusOut);
  505. }else{
  506. _textField.removeEventListener(FocusEvent.FOCUS_IN,textFieldFocusIn);
  507. _textField.removeEventListener(FocusEvent.FOCUS_OUT,textFieldFocusOut);
  508. }
  509. if(_state==FOCUSED)setState(_state);
  510. }
  511. //
  512. /**
  513. * ??????????,,?skin???????????1?2??????????????,
  514. * ??displayStates?displayStates[0]?displayStates[1]?????????????,???:false
  515. * @param value:Boolean
  516. *
  517. */
  518. public function get focusedActive():Boolean{
  519. return _focusedActive;
  520. }
  521. //
  522. /**
  523. * ?????????
  524. * @param value:Boolean
  525. *
  526. */
  527. override public function set focused(value:Boolean):void{
  528. if(this.focused==value)return;
  529. if(value){
  530. this.stage.focus=_textField;
  531. _textField.setSelection(_textField.length,_textField.length);
  532. }else{
  533. this.stage.focus=null;
  534. }
  535. }
  536. //
  537. /**
  538. * ?????????
  539. * @param value:Boolean
  540. *
  541. */
  542. override public function get focused():Boolean{
  543. return this.stage==null?false:this.stage.focus==_textField;
  544. }
  545. //
  546. /**
  547. * focusedActive?true???????????:[new GlowFilter(0x0099CC,1,2,2,2,3,false,false)]
  548. * @param value:Array
  549. *
  550. */
  551. public function set focusedFilters(value:Array):void{
  552. if(_focusedFilters==value)return;
  553. _focusedFilters=value;
  554. if(_state==FOCUSED)setState(_state);
  555. }
  556. //
  557. /**
  558. * focusedActive?true???????????:[new GlowFilter(0x0099CC,1,2,2,2,3,false,false)]
  559. * @param value:Array
  560. *
  561. */
  562. public function get focusedFilters():Array{
  563. return _focusedFilters;
  564. }
  565. //
  566. /**
  567. * ?????????????????????????????DisplayObject?????????????2?????????:[]
  568. * @param value:Array
  569. *
  570. */
  571. public function set displayStates(value:Array):void{
  572. if(_displayStates==value)return;
  573. _displayStates=value;
  574. if(_displayStates==null||_displayStates.length==0){
  575. if(_stateContainer!=null){
  576. this.buildContainer.removeChild(_stateContainer);
  577. _stateContainer=null;
  578. }
  579. }else{
  580. _displayStates.length=Math.min(_displayStates.length,2);
  581. if(_stateContainer==null){
  582. _stateContainer=new Sprite();
  583. _stateContainer.mouseChildren=false;
  584. this.buildContainer.addChildAt(_stateContainer,0);
  585. }
  586. setState(_state);
  587. if(this.autoSizeBySkin){
  588. this.width=_stateContainer.width;
  589. this.height=_stateContainer.height;
  590. }
  591. }
  592. }
  593. //
  594. /**
  595. * ?????????????????????????????DisplayObject?????????????2?????????:[]
  596. * @param value:Array
  597. *
  598. */
  599. public function get displayStates():Array{
  600. return _displayStates;
  601. }
  602. //
  603. /**
  604. * ????url????????????????????????,?????????????????????2?????????:[]
  605. * @param value:Array
  606. *
  607. */
  608. override public function set displayURLs(value:Array):void{
  609. if(this.displayURLs==value)return;
  610. this.addEventListener(LoaderEvent.LOAD_COMPLETE,thisLoadComplete);
  611. super.displayURLs=value;
  612. }
  613. //
  614. override public function set width(value:Number):void{
  615. if(_autoSize!=TextFieldAutoSize.NONE){
  616. value=_textField.width;
  617. }
  618. super.width=value;
  619. if(_width==value){
  620. return;
  621. }
  622. _width=value;
  623. _textField.width=value;
  624. if(_border)
  625. drawBorder(_borderContainer,_borderColor,_borderAlpha,this.width,this.height,this.round);
  626. }
  627. //
  628. override public function set height(value:Number):void{
  629. if(_autoSize!=TextFieldAutoSize.NONE){
  630. value=_textField.height;
  631. }
  632. super.height=value;
  633. if(_height==value){
  634. return;
  635. }
  636. _height=value;
  637. _textField.height=value;
  638. if(_border)
  639. drawBorder(_borderContainer,_borderColor,_borderAlpha,this.width,this.height,this.round);
  640. }
  641. /*
  642. -----------------------------------
  643. public methods
  644. -----------------------------------
  645. */
  646. //
  647. /**
  648. * ????????TextField.setTextFormat
  649. * @param format:TextFormat
  650. * @param startIndex:int=0
  651. * @param endIndex:int=int.MAX_VALUE
  652. *
  653. */
  654. public function setTextFormat(format:TextFormat,startIndex:int=0,endIndex:int=int.MAX_VALUE):void{
  655. _textField.setTextFormat(format,startIndex,endIndex);
  656. }
  657. //
  658. override public function destroy():void{
  659. _textField.removeEventListener(FocusEvent.FOCUS_IN,textFieldFocusIn);
  660. _textField.removeEventListener(FocusEvent.FOCUS_OUT,textFieldFocusOut);
  661. _textField.removeEventListener(Event.CHANGE,textFieldChange);
  662. this.removeEventListener(LoaderEvent.LOAD_COMPLETE,thisLoadComplete);
  663. super.destroy();
  664. }
  665. /*
  666. -----------------------------------
  667. private methods
  668. -----------------------------------
  669. */
  670. //???
  671. private function init():void{
  672. _textField=new TextField();
  673. this.buildContainer.addChild(_textField);
  674. _defaultFormat=new TextFormat(null,12,0x0,false,false,false,"","","left",0,0,0,0);
  675. }
  676. //????
  677. private function updateDisplayList():void{
  678. if(_defaultFormat!=null){
  679. _textField.defaultTextFormat=_defaultFormat;
  680. }
  681. if(_text!=null){
  682. _textField.text=_text;
  683. }else if(_htmlText!=null){
  684. _textField.htmlText=_htmlText;
  685. }
  686. if(_borderContainer!=null)
  687. this.setChildIndex(_borderContainer,this.numChildren-1);
  688. }
  689. //????
  690. private function setState(state:String):void{
  691. if(!_focusedActive){
  692. return;
  693. }
  694. switch(state){
  695. case NORMAL:
  696. showState(0);
  697. break;
  698. case FOCUSED:
  699. showState(1);
  700. break;
  701. default:
  702. throw new Error("QTextField ?????:["+NORMAL+","+FOCUSED+"]??");
  703. }
  704. }
  705. //????
  706. private function showState(index:int):void{
  707. var $targets:Array=this.skinTargets;
  708. for(var i:int=0;i<$targets.length;i++){
  709. if(($targets[i] as MovieClip)!=null){
  710. MovieClip($targets[i]).gotoAndStop(index+1);
  711. }
  712. }
  713. if(_displayStates[index] is DisplayObject){
  714. if(_stateContainer!=null){
  715. while(_stateContainer.numChildren>0){
  716. _stateContainer.removeChildAt(0);
  717. }
  718. _stateContainer.addChild(_displayStates[index]);
  719. }
  720. }
  721. if(_border){
  722. if(index==0){
  723. if(_borderContainer!=null)_borderContainer.filters=[];
  724. }else if(index==1){
  725. if(_borderContainer!=null)_borderContainer.filters=_focusedFilters;
  726. }
  727. }
  728. }
  729. //????
  730. private function drawBorder(target:Sprite,color:uint,alpha:Number=1,width:Number=1,height:Number=1,round:Number=0):void{
  731. target.graphics.clear();
  732. target.graphics.lineStyle(1,color,alpha,true,"none","round");
  733. target.graphics.drawRoundRect(0,0,width-1,height-1,round,round);
  734. }
  735. //????
  736. 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{
  737. if((target as Sprite)==null&&(target as Shape)==null){
  738. throw new Error("target????:Sprite?Shape????????.");
  739. }
  740. target.graphics.clear();
  741. if(width==0||height==0){
  742. return;
  743. }
  744. target.graphics.lineStyle(thickness,color,0,false,"none");
  745. if(filled)
  746. target.graphics.beginFill(color,alpha);
  747. target.graphics.drawRoundRect(x,y,width,height,round,round);
  748. target.graphics.endFill();
  749. }
  750. //????
  751. private function textFieldFocusIn(e:FocusEvent):void{
  752. if(_state==FOCUSED)return;
  753. _state=FOCUSED;
  754. setState(_state);
  755. }
  756. //????
  757. private function textFieldFocusOut(e:FocusEvent):void{
  758. if(_state==NORMAL)return;
  759. _state=NORMAL;
  760. setState(_state);
  761. }
  762. //?????????
  763. private function textFieldChange(e:Event):void{
  764. this.width=_textField.width;
  765. this.height=_textField.height;
  766. }
  767. //????????
  768. private function thisLoadComplete(e:LoaderEvent):void{
  769. this.displayStates=e.datas;
  770. this.removeEventListener(LoaderEvent.LOAD_COMPLETE,thisLoadComplete);
  771. }
  772. }
  773. }