PageRenderTime 27ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/controls/ButtonBase.as

http://flash-games.googlecode.com/
ActionScript | 357 lines | 216 code | 34 blank | 107 comment | 29 complexity | ead128323269f2da6f8cd6028c1fa703 MD5 | raw file
  1. package slw.controls
  2. {
  3. import flash.display.DisplayObject;
  4. import flash.display.GradientType;
  5. import flash.display.MovieClip;
  6. import flash.display.Shape;
  7. import flash.display.Sprite;
  8. import flash.events.MouseEvent;
  9. import flash.geom.Matrix;
  10. import slw.core.UIComponent;
  11. import slw.events.LoaderEvent;
  12. /**
  13. * ButtonBase
  14. * ???????????4??????4???????4????url??
  15. * @author ddx<br/>
  16. * 2011-7-21
  17. *
  18. */
  19. public class ButtonBase extends UIComponent
  20. {
  21. /**
  22. * "up",??????
  23. */
  24. public static const UP:String="up";
  25. /**
  26. * "over",??????
  27. */
  28. public static const OVER:String="over";
  29. /**
  30. * "down",??????
  31. */
  32. public static const DOWN:String="down";
  33. /**
  34. * "disabled",????
  35. */
  36. public static const DISABLED:String="disabled";
  37. //???????????????????
  38. private var _displayStates:Array=[];
  39. private var _defaultDisplayStates:Array=[];
  40. //????
  41. private var _state:String=UP;
  42. //???????
  43. private var _stateContainer:Sprite;
  44. /**
  45. * ????
  46. * @param displayStates:Array=null ?????????0-4???????????4?????
  47. *
  48. */
  49. public function ButtonBase(displayStates:Array=null)
  50. {
  51. super();
  52. this.listName="ButtonBase";
  53. init();
  54. setSize(100,22);
  55. if(displayStates==null){
  56. _defaultDisplayStates=buildDefaultStates();
  57. this.displayStates=_defaultDisplayStates;
  58. }
  59. this.round=6;
  60. }
  61. /*
  62. -----------------------------------
  63. setters getters
  64. -----------------------------------
  65. */
  66. //
  67. /**
  68. * ?????????0-4???????????4?????????????4????????:[]
  69. * @param value:Array
  70. *
  71. */
  72. public function set displayStates(value:Array):void{
  73. if(_displayStates==value)return;
  74. _displayStates=value;
  75. if(_displayStates!=null&&_displayStates.length>0&&_stateContainer==null){
  76. _stateContainer=new Sprite();
  77. this.buildContainer.addChild(_stateContainer);
  78. }else if((_displayStates==null||_displayStates.length==0)&&_stateContainer!=null){
  79. this.buildContainer.removeChild(_stateContainer);
  80. _stateContainer=null;
  81. }
  82. if(_displayStates!=null&&_displayStates.length!=0){
  83. this.skin=null;
  84. _displayStates.length=Math.min(_displayStates.length,4);
  85. }
  86. setState(_state);
  87. }
  88. //
  89. /**
  90. * ?????????0-4???????????4?????????????4????????:[]
  91. * @param value:Array
  92. *
  93. */
  94. public function get displayStates():Array{
  95. return _displayStates;
  96. }
  97. //
  98. /**
  99. * ??????,??????????????(?:["Skin1","Skin2"]),????????????????,??0?????????,
  100. * ?skin???[]?null????
  101. * @param value:Array
  102. *
  103. */
  104. override public function set skin(value:Array):void{
  105. if(this.skin==value){
  106. return;
  107. }
  108. super.skin=value;
  109. if(this.skin!=null&&this.skin.length>0)
  110. this.displayStates=null;
  111. }
  112. //
  113. /**
  114. * ????url???0-4????????????????4?????
  115. * @param value:Array
  116. *
  117. */
  118. override public function set displayURLs(value:Array):void{
  119. if(this.displayURLs==value)return;
  120. this.addEventListener(LoaderEvent.LOAD_COMPLETE,thisLoadComplete);
  121. super.displayURLs=value;
  122. }
  123. //
  124. /**
  125. * ??????,??? true(??)
  126. * @param value:Boolean
  127. *
  128. */
  129. override public function set enabled(value:Boolean):void{
  130. if(this.enabled==value){
  131. return;
  132. }
  133. super.enabled=value;
  134. if(this.enabled){
  135. setState(UP);
  136. }else{
  137. setState(DISABLED);
  138. }
  139. }
  140. //
  141. /**
  142. * ????,???:0px
  143. * @param value:Number
  144. *
  145. */
  146. override public function set round(value:Number):void{
  147. if(this.round==value)return;
  148. super.round=value;
  149. updateDefaultStates();
  150. }
  151. //
  152. override public function set width(value:Number):void{
  153. if(this.width==value) return;
  154. super.width=value;
  155. updateDefaultStates();
  156. setState(_state);
  157. }
  158. //
  159. override public function set height(value:Number):void{
  160. if(this.height==value) return;
  161. super.height=value;
  162. updateDefaultStates();
  163. setState(_state);
  164. }
  165. /*
  166. -----------------------------------
  167. public methods
  168. -----------------------------------
  169. */
  170. //
  171. /**
  172. * ??????????:ButtonBase.UP?????:<br/>
  173. * <ul>
  174. * <li>ButtonBase.UP ??????<br/>
  175. * <li>ButtonBase.OVER ??????<br/>
  176. * <li>ButtonBase.DOWN ??????<br/>
  177. * <li>ButtonBase.DISABLED ????<br/>
  178. * </ul>
  179. * @param state:String
  180. *
  181. */
  182. public function setState(state:String):void{
  183. var $states:Array=[UP,OVER,DOWN,DISABLED];
  184. if($states.indexOf(state)==-1)
  185. throw new Error("???????????:["+$states+"].");
  186. showState($states.indexOf(state));
  187. if(state==DISABLED){
  188. this.alpha=0.6;
  189. }else{
  190. this.alpha=1;
  191. }
  192. }
  193. //
  194. override public function destroy():void{
  195. this.removeEventListener(LoaderEvent.LOAD_COMPLETE,thisLoadComplete);
  196. removeListeners();
  197. super.destroy();
  198. }
  199. /*
  200. -----------------------------------
  201. private methods
  202. -----------------------------------
  203. */
  204. //???
  205. private function init():void{
  206. this.mouseChildren=false;
  207. addListeners();
  208. }
  209. //????
  210. private function addListeners():void{
  211. this.addEventListener(MouseEvent.MOUSE_OUT,thisMouseOut);
  212. this.addEventListener(MouseEvent.MOUSE_OVER,thisMouseOver);
  213. this.addEventListener(MouseEvent.MOUSE_UP,thisMouseUp);
  214. this.addEventListener(MouseEvent.MOUSE_DOWN,thisMouseDown);
  215. }
  216. //????
  217. public function removeListeners():void{
  218. this.removeEventListener(MouseEvent.MOUSE_OUT,thisMouseOut);
  219. this.removeEventListener(MouseEvent.MOUSE_OVER,thisMouseOver);
  220. this.removeEventListener(MouseEvent.MOUSE_UP,thisMouseUp);
  221. this.removeEventListener(MouseEvent.MOUSE_DOWN,thisMouseDown);
  222. }
  223. //??????
  224. private function buildDefaultStates():Array{
  225. var $states:Array=[];
  226. var $borderColors:Array=[0x999999,0x0099CC,0x0099CC,0x999999];
  227. var $colors:Array=[[0xeeeeee,0xcccccc],[0xeeeeee,0x85C9E0],[0x85C9E0,0xeeeeee],[0xcccccc,0xeeeeee]];
  228. for(var i:int=0;i<4;i++){
  229. $states.push(getState($borderColors[i],$colors[i],this.width,this.height,this.round));
  230. }
  231. return $states;
  232. }
  233. //????
  234. private function getState(borderColor:uint,colors:Array,width:Number,height:Number,round:Number):DisplayObject{
  235. var $m:Matrix=new Matrix();
  236. $m.createGradientBox(width,height,Math.PI/2);
  237. var $s:Shape=new Shape();
  238. $s.graphics.lineStyle(0,borderColor,1,true,"none","round");
  239. $s.graphics.beginGradientFill(GradientType.LINEAR,colors,[1,1],[0,0xff],$m);
  240. $s.graphics.drawRoundRect(0,0,width,height,round,round);
  241. $s.graphics.endFill();
  242. return $s;
  243. }
  244. //??????
  245. private function updateDefaultStates():void{
  246. if(_displayStates==_defaultDisplayStates){
  247. _defaultDisplayStates=buildDefaultStates();
  248. this.displayStates=_defaultDisplayStates;
  249. }
  250. }
  251. //????
  252. private function showState(index:int):void{
  253. var $skins:Array=this.skinTargets;
  254. for(var i:int=0;i<$skins.length;i++){
  255. if($skins[i] is MovieClip)
  256. MovieClip($skins[i]).gotoAndStop(index+1);
  257. if(this.autoSizeBySkin){
  258. this.width=MovieClip($skins[i]).width;
  259. this.height=MovieClip($skins[i]).height;
  260. }else{
  261. MovieClip($skins[i]).width=this.width;
  262. MovieClip($skins[i]).height=this.height;
  263. }
  264. }
  265. if(_stateContainer!=null){
  266. while(_stateContainer.numChildren>0){
  267. _stateContainer.removeChildAt(0);
  268. }
  269. if(_displayStates!=null&&_displayStates[index]!=null)
  270. _stateContainer.addChild(_displayStates[index]);
  271. if(this.autoSizeBySkin){
  272. this.width=_stateContainer.width;
  273. this.height=_stateContainer.height;
  274. }else{
  275. _stateContainer.width=this.width;
  276. _stateContainer.height=this.height;
  277. }
  278. }
  279. }
  280. //????
  281. private function thisMouseOver(e:MouseEvent):void{
  282. if(_state==OVER)return;
  283. _state=OVER;
  284. setState(_state);
  285. }
  286. //????
  287. private function thisMouseOut(e:MouseEvent):void{
  288. if(_state==UP)return;
  289. _state=UP;
  290. setState(_state);
  291. }
  292. //????
  293. private function thisMouseUp(e:MouseEvent):void{
  294. thisMouseOver(e);
  295. }
  296. //????
  297. private function thisMouseDown(e:MouseEvent):void{
  298. if(_state==DOWN)return;
  299. _state=DOWN;
  300. setState(_state);
  301. }
  302. //????????
  303. private function thisLoadComplete(e:LoaderEvent):void{
  304. this.displayStates=e.datas;
  305. this.removeEventListener(LoaderEvent.LOAD_COMPLETE,thisLoadComplete);
  306. }
  307. //????
  308. 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{
  309. if((target as Sprite)==null&&(target as Shape)==null){
  310. throw new Error("target????:Sprite?Shape????????.");
  311. }
  312. target.graphics.clear();
  313. if(width==0||height==0){
  314. return;
  315. }
  316. target.graphics.lineStyle(thickness,color,0,false,"none");
  317. if(filled)
  318. target.graphics.beginFill(color,alpha);
  319. target.graphics.drawRoundRect(x,y,width,height,round,round);
  320. target.graphics.endFill();
  321. }
  322. }
  323. }