PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/controls/Label.as

http://flash-games.googlecode.com/
ActionScript | 383 lines | 207 code | 18 blank | 158 comment | 23 complexity | 6a47a0ee27c584c655769ee6b85beb78 MD5 | raw file
  1. package slw.controls
  2. {
  3. import flash.display.DisplayObject;
  4. import flash.display.Loader;
  5. import flash.display.LoaderInfo;
  6. import flash.display.Sprite;
  7. import flash.events.Event;
  8. import flash.net.URLRequest;
  9. import flash.system.ApplicationDomain;
  10. import flash.system.LoaderContext;
  11. import flash.text.TextField;
  12. import flash.text.TextFieldAutoSize;
  13. import flash.text.TextFormat;
  14. import flash.utils.getDefinitionByName;
  15. import slw.core.UIComponent;
  16. /**
  17. * Label
  18. * ?????????????????
  19. * @author ddx<br/>
  20. * 2011-7-18
  21. *
  22. */
  23. public class Label extends UIComponent
  24. {
  25. /**
  26. * "top"??????
  27. */
  28. public static const TOP:String="top";
  29. /**
  30. * "bottom"??????
  31. */
  32. public static const BOTTOM:String="bottom";
  33. /**
  34. * "left"??????
  35. */
  36. public static const LEFT:String="left";
  37. /**
  38. * "right"??????
  39. */
  40. public static const RIGHT:String="right";
  41. //????
  42. private var _text:String=null;
  43. //???????
  44. private var _gap:Number=5;
  45. //??
  46. private var _textField:TextField;
  47. //??????
  48. private var _icon:DisplayObject
  49. //????????
  50. private var _iconStr:String=null;
  51. //????
  52. private var _iconURL:String=null;
  53. //????
  54. private var _position:String="";
  55. //????
  56. private var _format:TextFormat;
  57. //??????
  58. private var _defaultFormat:TextFormat;
  59. //????
  60. private var _textColor:uint=0x0;
  61. /**
  62. * ????
  63. * @param text:String="Label" ????
  64. * @param icon*=null ????
  65. * @param position:String="left" ??????
  66. *
  67. */
  68. public function Label(text:String="Label",icon:*=null,position:String=LEFT)
  69. {
  70. super();
  71. this.listName="Label";
  72. init();
  73. setProperties(text,icon,position);
  74. }
  75. /*
  76. -----------------------------------
  77. setters getters
  78. -----------------------------------
  79. */
  80. //
  81. /**
  82. * ????????TextField.defaultTextFormat
  83. * @param value:TextFormat
  84. *
  85. */
  86. public function set defaultTextFormat(value:TextFormat):void{
  87. if(_defaultFormat==value){
  88. return;
  89. }
  90. _defaultFormat=value;
  91. if(_textField!=null)
  92. _textField.defaultTextFormat=_defaultFormat;
  93. _format=null;
  94. updateDisplayList();
  95. }
  96. //
  97. /**
  98. * ????????TextField.defaultTextFormat
  99. * @param value:TextFormat
  100. *
  101. */
  102. public function get defaultTextFormat():TextFormat{
  103. return _defaultFormat;
  104. }
  105. //
  106. /**
  107. * ???? ???:0x0
  108. * @param value:uint
  109. *
  110. */
  111. public function set textColor(value:uint):void{
  112. if(_textColor==value){
  113. return;
  114. }
  115. _textColor=value;
  116. if(_textField!=null)
  117. _textField.textColor=_textColor;
  118. }
  119. //
  120. /**
  121. * ???? ???:0x0
  122. * @param value:uint
  123. *
  124. */
  125. public function get textColor():uint{
  126. return _textColor;
  127. }
  128. //
  129. /**
  130. * ????,???:"Label"?????""?null???????
  131. * @param value:String
  132. *
  133. */
  134. public function set text(value:String):void{
  135. if(_text==value){
  136. return;
  137. }
  138. _text=value;
  139. if(_text==""||_text==null){
  140. if(_textField!=null)
  141. this.buildContainer.removeChild(_textField);
  142. _textField=null;
  143. }else if(_textField==null){
  144. _textField=new TextField();
  145. _textField.autoSize=TextFieldAutoSize.LEFT;
  146. _textField.textColor=_textColor;
  147. if(_defaultFormat!=null)
  148. _textField.defaultTextFormat=_defaultFormat;
  149. this.buildContainer.addChild(_textField);
  150. }
  151. updateDisplayList();
  152. }
  153. //
  154. /**
  155. * ????,???:"Label"?????""?null???????
  156. * @param value:String
  157. *
  158. */
  159. public function get text():String{
  160. return _text;
  161. }
  162. //
  163. /**
  164. * ????????????????????????null?????,???:null
  165. * @param value:*
  166. *
  167. */
  168. public function set icon(value:*):void{
  169. if(_icon==value||_iconStr==value){
  170. return;
  171. }
  172. if(_icon!=null)
  173. this.buildContainer.removeChild(_icon);
  174. _icon=null;
  175. _iconStr=null;
  176. if(value is String){
  177. _iconStr=value;
  178. var $c:Class=getDefinitionByName(_iconStr) as Class;
  179. _icon=new $c();
  180. }else{
  181. _icon=value;
  182. }
  183. if(_icon!=null){
  184. this.buildContainer.addChild(_icon);
  185. }else{
  186. _iconURL=null;
  187. }
  188. updateDisplayList();
  189. }
  190. //
  191. /**
  192. * ????????????????????????null?????,???:null
  193. * @param value:*
  194. *
  195. */
  196. public function get icon():*{
  197. return _icon==null?_iconStr:_icon;
  198. }
  199. //
  200. /**
  201. * ??URL?????????icon???????????????(?Loader??,??Loader.content??)
  202. * @param value:String
  203. * @see <br/>
  204. * <a href='http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/display/Loader.html#contentLoaderInfo' target='_blank'>Loader.content</a>
  205. *
  206. */
  207. public function set iconURL(value:String):void{
  208. if(_iconURL==value){
  209. return;
  210. }
  211. _iconURL=value;
  212. var $context:LoaderContext=new LoaderContext();
  213. $context.applicationDomain=ApplicationDomain.currentDomain;
  214. var $loader:Loader=new Loader();
  215. $loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderComplete);
  216. $loader.load(new URLRequest(_iconURL),$context);
  217. }
  218. //
  219. /**
  220. * ??URL?????????icon???????????????(?Loader??,??Loader.content??)
  221. * @param value:String
  222. * @see <br/>
  223. * <a href='http://help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/flash/display/Loader.html#contentLoaderInfo' target='_blank'>Loader.content</a>
  224. *
  225. */
  226. public function get iconURL():String{
  227. return _iconURL;
  228. }
  229. //
  230. /**
  231. * ?????????:Label.LEFT,?????:<br/>
  232. * <ul>
  233. * <li>Label.TOP ????????<br/>
  234. * <li>Label.BOTTOM ????????<br/>
  235. * <li>Label.LEFT ????????<br/>
  236. * <li>Label.RIGHT ????????<br/>
  237. * </ul>
  238. * @param value:String
  239. *
  240. */
  241. public function set position(value:String):void{
  242. if(_position==value){
  243. return;
  244. }
  245. _position=value;
  246. updateDisplayList();
  247. }
  248. //
  249. /**
  250. * ?????????:Label.LEFT,?????:<br/>
  251. * <ul>
  252. * <li>Label.TOP ????????<br/>
  253. * <li>Label.BOTTOM ????????<br/>
  254. * <li>Label.LEFT ????????<br/>
  255. * <li>Label.RIGHT ????????<br/>
  256. * </ul>
  257. * @param value:String
  258. *
  259. */
  260. public function get position():String{
  261. return _position;
  262. }
  263. //
  264. /**
  265. * ???????????????? 0
  266. * @param value:Number
  267. *
  268. */
  269. override public function set round(value:Number):void{
  270. }
  271. /*
  272. -----------------------------------
  273. public methods
  274. -----------------------------------
  275. */
  276. //
  277. /**
  278. * ????????
  279. * @param format:TextFormat
  280. *
  281. */
  282. public function setTextFormat(format:TextFormat):void{
  283. _format=format;
  284. updateDisplayList();
  285. }
  286. /*
  287. -----------------------------------
  288. private methods
  289. -----------------------------------
  290. */
  291. //???
  292. private function init():void{
  293. this.mouseChildren=false;
  294. this.mouseEnabled=false;
  295. }
  296. //????
  297. private function updateDisplayList():void{
  298. if(_icon!=null){
  299. _icon.x=0;
  300. _icon.y=0;
  301. }
  302. if(_textField!=null){
  303. _textField.text=_text==null?"":_text;
  304. if(_format!=null)
  305. _textField.setTextFormat(_format);
  306. _textField.x=0;
  307. _textField.y=0;
  308. }
  309. if(_icon!=null&&_textField!=null){
  310. switch(_position){
  311. case TOP:
  312. _textField.y=_icon.y+_icon.height+_gap;
  313. layoutX(_textField,_icon);
  314. break;
  315. case BOTTOM:
  316. _icon.y=_textField.y+_textField.height+_gap;
  317. layoutX(_textField,_icon);
  318. break;
  319. case LEFT:
  320. _textField.x=_icon.x+_icon.width+_gap;
  321. layoutY(_textField,_icon);
  322. break;
  323. case RIGHT:
  324. _icon.x=_textField.x+_textField.width+_gap;
  325. layoutY(_textField,_icon);
  326. break;
  327. }
  328. }
  329. checkoutSize();
  330. }
  331. //??????
  332. private function setProperties(text:String,icon:*,position:String):void{
  333. _position=position;
  334. this.text=text;
  335. this.icon=icon;
  336. updateDisplayList();
  337. }
  338. //x???
  339. private function layoutX(...args):void{
  340. for(var i:int=0;i<args.length;i++){
  341. if(args[i].width<this.buildContainer.width){
  342. args[i].x=(this.buildContainer.width-args[i].width)/2;
  343. }
  344. }
  345. }
  346. //y???
  347. private function layoutY(...args):void{
  348. for(var i:int=0;i<args.length;i++){
  349. if(args[i].height<this.buildContainer.height){
  350. args[i].y=(this.buildContainer.height-args[i].height)/2;
  351. }
  352. }
  353. }
  354. //??????
  355. private function loaderComplete(e:Event):void{
  356. e.target.removeEventListener(Event.COMPLETE,loaderComplete);
  357. this.icon=LoaderInfo(e.target).content;
  358. }
  359. //????
  360. private function checkoutSize():void{
  361. this.width=this.buildContainer.width;
  362. this.height=this.buildContainer.height;
  363. }
  364. }
  365. }