PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/SparrowGuIv0_5/src/sparrowGui/utils/PubFun.as

http://sparrowgui.googlecode.com/
ActionScript | 665 lines | 414 code | 53 blank | 198 comment | 130 complexity | 3ea05901cb34fb118c88dc663387af9a MD5 | raw file
  1. package sparrowGui.utils
  2. {
  3. import flash.display.DisplayObject;
  4. import flash.display.DisplayObjectContainer;
  5. import flash.display.InteractiveObject;
  6. import flash.display.MovieClip;
  7. import flash.display.Shape;
  8. import flash.display.SimpleButton;
  9. import flash.display.Sprite;
  10. import flash.events.Event;
  11. import flash.filters.DropShadowFilter;
  12. import flash.geom.Point;
  13. import flash.geom.Rectangle;
  14. import flash.text.TextField;
  15. import flash.utils.ByteArray;
  16. import flash.utils.describeType;
  17. import sparrowGui.item.Item;
  18. /**
  19. * ???????????
  20. * @author Pelephone
  21. */
  22. public class PubFun
  23. {
  24. /**
  25. * ?????????
  26. * @param node ???????
  27. * @param tClass ???????, ??? null, ????????
  28. */
  29. static public function findChild(node:DisplayObjectContainer, tClass:Class=null):DisplayObject{
  30. if(tClass == null) tClass = DisplayObject;
  31. for(var i:int=0; i<node.numChildren; i++){
  32. var disp:DisplayObject = node.getChildAt(i);
  33. if(disp is tClass) return disp;
  34. }
  35. return null;
  36. }
  37. /**
  38. * ????????propName,???values??????????
  39. * ??arr=[{name:1},{name:2},{name:3}]; arr2 = findArr(arr,"name",[2,3]) arr2?[{name:2},{name:3}]
  40. * arr2 = findArr(arr,"name",2) ???{name:2}
  41. */
  42. static public function findArr(arr:Array,propName:String,values:Object):*
  43. {
  44. if((values is Number) || (values is String) || !resArr.length){
  45. for each(var o:Object in arr){
  46. if(o[propName]==values) return o;
  47. }
  48. return null;
  49. }
  50. var resArr:Array = [];
  51. for each(o in arr){
  52. if(values.indexOf(o[propName])>=0) resArr.push(o);
  53. }
  54. return resArr;
  55. }
  56. // ????
  57. public static function SafeRemoveChild(disp:DisplayObject) : void
  58. {
  59. if (disp && disp.parent) disp.parent.removeChild(disp);
  60. }
  61. /**
  62. * ?????????????
  63. * @param s
  64. */
  65. static public function clearDisp(s:DisplayObjectContainer):void
  66. {
  67. if(s) while(s.numChildren) s.removeChildAt(0);
  68. }
  69. /** ???????, ?? 0=??, >0=?????, -1=?????*/
  70. static public function isChild(parent:DisplayObjectContainer, child:DisplayObject):int{
  71. if(!child || !parent) return -1;
  72. var deep:int = 0;
  73. while(child != parent){
  74. child = child.parent;
  75. if(!child) return -1;
  76. deep ++;
  77. }
  78. return deep;
  79. }
  80. /**
  81. * ??2??????
  82. * @param ref ????
  83. * @param disp ?????
  84. * @param align ???? "T" ???,"B" ???,"L" ???,"R" ???,"C" ??????,"M"????
  85. * "S" ????disp????ref??
  86. */
  87. static public function alignRect(ref:DisplayObject, disp:DisplayObject,align:String="T"):void
  88. {
  89. //???
  90. if(align.indexOf("T")>=0){
  91. if(disp.parent == ref) disp.y = 0;
  92. else disp.y = ref.y;
  93. }
  94. //???
  95. if(align.indexOf("B")>=0){
  96. if(disp.parent == ref) disp.y = ref.height - disp.height;
  97. else disp.y = ref.y + ref.height - disp.height;
  98. }
  99. //???
  100. if(align.indexOf("L")>=0){
  101. if(disp.parent == ref) disp.x = 0;
  102. else disp.x = ref.x;
  103. }
  104. //???
  105. if(align.indexOf("R")>=0){
  106. if(disp.parent == ref) disp.x = ref.width - disp.width;
  107. else disp.x = ref.x + ref.width - disp.width;
  108. }
  109. //????
  110. if(align.indexOf("C")>=0){
  111. if(disp.parent == ref) disp.x = ref.width/2 - disp.width/2;
  112. else disp.x = ref.x + ref.width/2 - disp.width/2;
  113. }
  114. //????
  115. if(align.indexOf("M")>=0){
  116. if(disp.parent == ref) disp.y = ref.height/2 - disp.height/2;
  117. else disp.y = ref.y + ref.height/2 - disp.height/2;
  118. }
  119. //????disp???ref??
  120. if(align.indexOf("W")>=0){
  121. disp.width = ref.width;
  122. }
  123. //????disp???ref??
  124. if(align.indexOf("H")>=0){
  125. disp.height = ref.height;
  126. }
  127. }
  128. /**
  129. * ??????????
  130. * @param parent ???
  131. * @param propName ???
  132. * @param propValue ???
  133. * @param childType ????, ????, ??? null ?? ???
  134. * @param exclude ????????
  135. */
  136. static public function setChildrenProperty(parent:DisplayObjectContainer, propName:String, propValue:Object, childType:Class=null, ...exclude):void{
  137. // ????
  138. if(childType == null) childType = DisplayObject;
  139. // ??????
  140. var num:int = parent.numChildren;
  141. for(var index:int=0; index<num; index++){
  142. // ????
  143. var child:DisplayObject = parent.getChildAt(index);
  144. if(! (child is childType) ) continue;
  145. // ????
  146. if(exclude.indexOf(child) >= 0) continue;
  147. // ???????
  148. child[propName] = propValue;
  149. }
  150. }
  151. /**
  152. * ??????????????????
  153. * @param tarDisp ?????
  154. * @param zwDisp ????
  155. * @param properties ????????x,y,?,?
  156. */
  157. static public function addChildToZW(tarDisp:DisplayObject,zwDisp:DisplayObject,copyProps:String="x,y,width,height"):void
  158. {
  159. copyProperties(tarDisp,zwDisp,copyProps);
  160. if(tarDisp.scaleX==0 || tarDisp.scaleY==0){
  161. throw new Error("??????, ?????????"); // ?????0
  162. }
  163. zwDisp.parent.addChild(tarDisp);
  164. zwDisp.parent.swapChildren(zwDisp, tarDisp);
  165. SafeRemoveChild(zwDisp);
  166. }
  167. /**
  168. * ??????????????????item
  169. * @param tarDisp
  170. * @param itemCla ?Class????item
  171. * @return
  172. */
  173. static public function changeSP2Item(tarDisp:DisplayObject,itemCla:Class=null,txt:String=null,vars:Object=null):Item
  174. {
  175. if(!tarDisp || tarDisp is SimpleButton) return null;
  176. itemCla = (itemCla==null)?Item:itemCla;
  177. var parent:DisplayObjectContainer = tarDisp.parent;
  178. var id:int = tarDisp.parent.getChildIndex(tarDisp);
  179. var btn:Item = (new itemCla(null,tarDisp,vars)) as Item;
  180. btn.x = tarDisp.x;
  181. btn.y = tarDisp.y;
  182. tarDisp.x = 0;
  183. tarDisp.y = 0;
  184. btn.name = tarDisp.name;
  185. parent.addChildAt(btn,id);
  186. if(txt) setSimpleButtonText(tarDisp,txt);
  187. return btn;
  188. }
  189. /**
  190. * ???????
  191. */
  192. static public function maxChild(backmc:DisplayObjectContainer):InteractiveObject{
  193. var maxChild:InteractiveObject = findChild(backmc, InteractiveObject) as InteractiveObject;
  194. if(maxChild.width == backmc.width && maxChild.height==backmc.height) return maxChild;
  195. return backmc;
  196. }
  197. /**
  198. * ????????
  199. */
  200. static public function getLocalMousePoint(disp:DisplayObject):Point{
  201. trace("getLocalMousePoint for test");
  202. var x:int = 0;
  203. var y:int = 0;
  204. if(disp && disp.stage){
  205. x = disp.stage.mouseX;
  206. y = disp.stage.mouseY;
  207. }
  208. return disp.globalToLocal( new Point(x, y) );
  209. }
  210. /**
  211. * ??propNameList??????
  212. */
  213. static public function copyProperties(dst:Object, src:Object, propNameList:String):void{
  214. if(!dst || !src || !propNameList) return;
  215. var exp:RegExp = /(\w+)/g;
  216. var arr:Array = propNameList.match( exp );
  217. for each(var prop:String in arr){
  218. dst[prop] = src[prop];
  219. }
  220. }
  221. /**
  222. * ??srcObj??????refObj??????????????????????????????
  223. * (?????public?)
  224. * @param obj
  225. * @param defaultClass
  226. * @param ignoreProps
  227. * @param isStrict ????????srcObj????????0????refObj
  228. * @return
  229. */
  230. static public function parseObjToNew(srcObj:Object, refObj:Object, ignoreProps:Array=null, isStrict:Boolean=false):Object
  231. {
  232. var desc:XMLList = describeType( srcObj )["variable"];
  233. // ?? obj ????? prop
  234. for each(var prop:XML in desc)
  235. {
  236. var propName:String = prop.@name; // ???
  237. var propType:String = prop.@type; // ????
  238. // ???
  239. if(ignoreProps && ignoreProps.indexOf(propName)>=0) continue;
  240. //????
  241. if(isStrict && !srcObj[propName]) continue;
  242. // ?????????
  243. if(refObj.hasOwnProperty(propName)){
  244. refObj[propName] = srcObj[propName];
  245. }
  246. }
  247. return refObj;
  248. }
  249. /**
  250. * ??????
  251. * @param obj1 ???????
  252. * @return Object ?????????
  253. */
  254. public static function copyObj(obj1:Object):Object
  255. {
  256. var byteArray:ByteArray=new ByteArray();
  257. byteArray.writeObject(obj1);
  258. byteArray.position=0;
  259. var obj2:Object=byteArray.readObject();
  260. return obj2;
  261. }
  262. /**
  263. * ????????????,??????????????
  264. * @param obj
  265. * @return
  266. */
  267. public static function killObject(obj:Object):Object
  268. {
  269. for(var strName:String in obj){
  270. if((obj[strName] is Number) || (obj[strName] is String)) continue;
  271. if(obj[strName] is DisplayObject){
  272. SafeRemoveChild(obj[strName] as DisplayObject);
  273. }
  274. obj[strName]=null;
  275. }
  276. return obj;
  277. }
  278. /**
  279. * ????????
  280. * @param btn ????
  281. * @param text ????, ?? null ?, ??????(????????)
  282. * @param isHtml ???html??
  283. * @return ????????
  284. */
  285. static public function setSimpleButtonText(btn:DisplayObject, text:String=null, isHtml:Boolean = false):String{
  286. //
  287. var prevText:String = "";
  288. text = text || "";
  289. //
  290. var list:Array = [ btn["upState"], btn["downState"], btn["overState"], btn["hitTestState"],btn];
  291. if(btn.hasOwnProperty("selectState")) list.push(btn["selectState"]);
  292. for each(var disp:DisplayObject in list)
  293. {
  294. var tf:TextField = disp as TextField;
  295. if(tf == null){
  296. var cont:DisplayObjectContainer = disp as DisplayObjectContainer;
  297. if(cont) tf = findChild(cont, TextField) as TextField;
  298. }
  299. if(tf){
  300. if(isHtml){
  301. tf.htmlText = text;
  302. }else{
  303. tf.text = text;
  304. }
  305. prevText = tf.text;
  306. }
  307. }
  308. //
  309. return text || prevText;
  310. }
  311. /**
  312. * ???????????
  313. * @param src
  314. * @return
  315. */
  316. static public function copy_instance(src:DisplayObject):DisplayObject{
  317. if(src == null) return null;
  318. var disp:DisplayObject = (new (src as Object).constructor ) as DisplayObject;
  319. if(disp is MovieClip){
  320. (disp as MovieClip).gotoAndStop( (src as MovieClip).currentFrame );
  321. }
  322. return disp;
  323. }
  324. /**
  325. * ?????????
  326. */
  327. static public function removeChildrenByType(node:DisplayObjectContainer, tClass:Class=null):void{
  328. if(tClass == null) tClass = DisplayObject;
  329. var i:int = 0;
  330. while(i < node.numChildren)
  331. {
  332. var disp:DisplayObject = node.getChildAt(i);
  333. if(disp is tClass) node.removeChildAt(i);
  334. else i++;
  335. }
  336. }
  337. /**
  338. * ?????????
  339. * @param node ???????
  340. * @param tClass ???????, ??? null, ???????
  341. * @return ??????, ??? null
  342. */
  343. static public function findChildren(node:DisplayObjectContainer, tClass:Class=null):Array{
  344. var arr:Array = new Array;
  345. if(tClass == null) tClass = DisplayObject;
  346. for(var i:int=0; i<node.numChildren; i++){
  347. var disp:DisplayObject = node.getChildAt(i);
  348. if(disp is tClass) arr.push(disp);
  349. }
  350. return (arr.length>0)? arr: null;
  351. }
  352. /**
  353. * ? target ?????, ??????????, ???????
  354. */
  355. static public function moveFront(target:DisplayObject):void{
  356. if(!target || !target.parent) return;
  357. target.parent.setChildIndex(target, target.parent.numChildren-1);
  358. }
  359. // ???????????
  360. static public function disableMouseEvent(parent:DisplayObjectContainer, ...nameList):void{
  361. for each(var name:String in nameList){
  362. var disp:DisplayObject = parent.getChildByName(name);
  363. if(disp is InteractiveObject) (disp as InteractiveObject).mouseEnabled = false;
  364. if(disp is DisplayObjectContainer) (disp as DisplayObjectContainer).mouseChildren = false;
  365. }
  366. }
  367. /**
  368. * ??disp?vars????????
  369. * @param disp ????sprite
  370. * @param rect ???????
  371. * @param odds ???????
  372. * @param color ????
  373. * @param alpha ??alpha?
  374. */
  375. public static function drawHorizLines(disp:Sprite,rect:Rectangle,odds:Array,color:uint=0x000000,thickness:Number=1,alpha:Number=1):void
  376. {
  377. disp.graphics.lineStyle(thickness,color,alpha);
  378. for(var i:int=1;i<(odds.length-1);i++){
  379. var arg:int = rect.height*odds[i];
  380. disp.graphics.moveTo((rect.x),(rect.y+i*arg));
  381. disp.graphics.lineTo((rect.x+rect.width),(rect.y+i*arg))
  382. }
  383. }
  384. /**
  385. * ??disp?vars???????
  386. * @param disp ????sprite
  387. * @param rect ???????
  388. * @param odds ???????
  389. * @param color ????
  390. * @param alpha ??alpha?
  391. */
  392. public static function drawVertLines(disp:Sprite,rect:Rectangle,odds:Array,color:uint=0x000000,thickness:Number=1,alpha:Number=1):void
  393. {
  394. disp.graphics.lineStyle(thickness,color,alpha);
  395. for(var i:int=1;i<(odds.length-1);i++){
  396. var arg:int = rect.width*odds[i];
  397. disp.graphics.moveTo((rect.x+i*arg),(rect.y));
  398. disp.graphics.lineTo((rect.x+i*arg),(rect.y+rect.height))
  399. }
  400. }
  401. //??????
  402. public static function setTextTag(txt:String,tag:String):String
  403. {
  404. return "<" + tag + ">" + txt + "</" + tag + ">";
  405. }
  406. //??????
  407. public static function setTextClass(txt:String,cla:String):String
  408. {
  409. return "<span class='" + cla + "'>" + txt + "</span>";
  410. }
  411. /**
  412. * ???????????
  413. * @param disp
  414. * @param spacing ??
  415. * @param hvArrange ?????0????1???
  416. */
  417. public static function arrangeDisp(disp:DisplayObjectContainer,spacing:int=0,hvArrange:int=1):void
  418. {
  419. var xyArg:String = hvArrange?"y":"x";
  420. var whArg:String = hvArrange?"height":"width";
  421. var tmpXY:int = 0;
  422. for(var i:int;i<disp.numChildren;i++){
  423. var dp:DisplayObject = disp.getChildAt(i);
  424. dp[xyArg] = tmpXY;
  425. tmpXY = tmpXY + dp[whArg] + spacing;
  426. }
  427. }
  428. /**
  429. * ????????????,????????????
  430. * @param disp
  431. * @param perColNum ???x?,???0???,??
  432. * @param colWidth ?????,???0??item???????,???==??
  433. * @param rowHeight ??????,???0??item?????????,???==??
  434. */
  435. public static function arrangeDispRC(disp:DisplayObjectContainer,perColNum:int=1,colWidth:int=0,rowHeight:int=0,spacing:int=0):void
  436. {
  437. var tmpY:int=0,tmpX:int=0;
  438. for(var i:int=0;i<disp.numChildren;i++){
  439. var dp:DisplayObject = disp.getChildAt(i);
  440. if(i && !(i%perColNum) && perColNum!=0){
  441. tmpY = tmpY + spacing + (rowHeight?rowHeight:dp.height);
  442. tmpX = 0;
  443. }
  444. dp.x = tmpX;
  445. tmpX = tmpX + (colWidth || dp.width) + spacing;
  446. dp.y = tmpY;
  447. }
  448. }
  449. /**
  450. * ??????????????
  451. * @param disp ??????
  452. * @param width ???????
  453. * @param odds ??
  454. */
  455. public static function arrangDispByOdds(disp:DisplayObjectContainer,width:int,odds:Array,isWidth:Boolean=false):void
  456. {
  457. var tmpx:int=0;
  458. for(var i:int=0;i<disp.numChildren;i++){
  459. var dp:DisplayObject = disp.getChildAt(i);
  460. if(isWidth) dp.width = Math.round(odds[i]*width);
  461. dp.x = tmpx;
  462. tmpx += Math.round(odds[i]*width);
  463. }
  464. }
  465. /**
  466. * ????????
  467. * DropShadowFilter factory method, used in many of the components.
  468. * @param dist The distance of the shadow.
  469. * @param knockout Whether or not to create a knocked out shadow.
  470. */
  471. public static function getShadow(dist:Number, blur:Number=5, color:uint=0x000000):DropShadowFilter
  472. {
  473. return new DropShadowFilter(dist, 45, color, 1, blur, blur, .3, 1);
  474. }
  475. // ?????????
  476. static private var _sprit:Sprite;
  477. static public function setNextFrameCallback(fn:Function, ...args):void{
  478. if(!_sprit) _sprit = new Sprite;
  479. _sprit.addEventListener(Event.ENTER_FRAME, onEnterFrame);
  480. function onEnterFrame(event:Event):void{
  481. _sprit.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
  482. fn.apply(null, args);
  483. }
  484. }
  485. /**
  486. * ?????????arr?????
  487. * @param arr ????
  488. * @param disp ??????
  489. * @param prefix ?????????
  490. */
  491. static public function batParseArrTodisp(arr:Array,disp:DisplayObjectContainer,prefix:String="txt_arr",isHtml:Boolean=false):void
  492. {
  493. var txt:TextField;
  494. for(var i:int=0;i<arr.length;i++){
  495. if(!disp.getChildByName(prefix+i)) continue;
  496. if(disp.getChildByName(prefix+i) is TextField){
  497. txt = (disp.getChildByName(prefix+i) as TextField);
  498. if(!isHtml) txt.text = String(arr[i]);
  499. else txt.htmlText = String(arr[i]);
  500. }else if(disp[prefix+i] is MovieClip){
  501. (disp[prefix+i] as MovieClip).gotoAndStop(int(arr[i]));
  502. }
  503. }
  504. }
  505. /**
  506. * ?????????data???????
  507. * @param data ????
  508. * @param disp ??????
  509. */
  510. static public function batParseObjTodisp(data:Object,disp:DisplayObject,isHtml:Boolean=false):void
  511. {
  512. var txt:TextField;
  513. var desc:XMLList = describeType( disp )["variable"];
  514. // ?? obj ????? prop
  515. for each(var prop:XML in desc)
  516. {
  517. var propName:String = prop.@name; // ???
  518. var propType:String = prop.@type; // ????
  519. if(!data.hasOwnProperty(propName)) continue;
  520. if(disp[propName] is TextField){
  521. txt = (disp[propName] as TextField);
  522. if(!isHtml) txt.text = String(data[propName]);
  523. else txt.htmlText = String(data[propName]);
  524. }else if(disp[propName] is MovieClip){
  525. (disp[propName] as MovieClip).gotoAndStop(int(data[propName]));
  526. }
  527. }
  528. }
  529. // ?????????????????????????
  530. /**
  531. * ????????????????
  532. * @param datas ????
  533. * @return
  534. */
  535. public static function oddsByDatas(datas:Object):Array
  536. {
  537. var sum:int = 0,i:int=0,odds:Array = [];
  538. // ???????
  539. for each(var s:String in datas){
  540. sum += (s.length==0)?1:s.length;
  541. }
  542. // ?????????????????
  543. for each(s in datas){
  544. odds.push(((s.length==0)?1:s.length)/sum);
  545. }
  546. return odds;
  547. }
  548. /**
  549. * ???????????????refStr
  550. * ?str = "123455"; str = delstr(str,"345");
  551. * ??str? 125;
  552. * @param srcStr
  553. * @param refStr
  554. */
  555. public static function delStr(srcStr:String,refStr:String):String
  556. {
  557. var s1:String = srcStr.substring(0,srcStr.indexOf(refStr));
  558. var s2:String = srcStr.substring((s1.length+refStr.length),srcStr.length);
  559. return s1+s2;
  560. }
  561. /**
  562. * ???
  563. * @param sp ??????
  564. */
  565. public static function drawArrow(dr:int=0,dis:int=11):Shape
  566. {
  567. var shap:Shape = new Shape();
  568. shap.graphics.beginFill(0x666666);
  569. if(dr==0){ //???
  570. shap.graphics.moveTo(0,0);
  571. shap.graphics.lineTo(dis,0);
  572. shap.graphics.lineTo(dis/2,dis);
  573. }
  574. else if(dr==1){ //???
  575. shap.graphics.moveTo(dis/2,0);
  576. shap.graphics.lineTo(0,dis);
  577. shap.graphics.lineTo(dis,dis);
  578. }
  579. else if(dr==2){ //???
  580. shap.graphics.moveTo(dis,0);
  581. shap.graphics.lineTo(dis,dis);
  582. shap.graphics.lineTo(0,dis/2);
  583. }
  584. else if(dr==3){ //???
  585. shap.graphics.moveTo(0,0);
  586. shap.graphics.lineTo(0,dis);
  587. shap.graphics.lineTo(dis,dis/2);
  588. }
  589. shap.graphics.endFill();
  590. return shap;
  591. }
  592. /**
  593. * ?? newObj ?? existObj, ????????, ??? existObj ?? newObj ???, ??????
  594. */
  595. static public function replace(newObj:DisplayObjectContainer, existObj:DisplayObject):void{
  596. existObj.parent.addChild(newObj);
  597. existObj.parent.swapChildren(newObj, existObj);
  598. var x:int = existObj.x;
  599. var y:int = existObj.y;
  600. var w:int = existObj.width;
  601. var h:int = existObj.height;
  602. newObj.addChild(existObj);
  603. existObj.x = 0;
  604. existObj.y = 0;
  605. newObj.x = x;
  606. newObj.y = y;
  607. newObj.width = w;
  608. newObj.height = h;
  609. newObj.name = existObj.name;
  610. }
  611. }
  612. }