/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
- package sparrowGui.utils
- {
- import flash.display.DisplayObject;
- import flash.display.DisplayObjectContainer;
- import flash.display.InteractiveObject;
- import flash.display.MovieClip;
- import flash.display.Shape;
- import flash.display.SimpleButton;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.filters.DropShadowFilter;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- import flash.text.TextField;
- import flash.utils.ByteArray;
- import flash.utils.describeType;
-
- import sparrowGui.item.Item;
-
- /**
- * ???????????
- * @author Pelephone
- */
- public class PubFun
- {
- /**
- * ?????????
- * @param node ???????
- * @param tClass ???????, ??? null, ????????
- */
- static public function findChild(node:DisplayObjectContainer, tClass:Class=null):DisplayObject{
- if(tClass == null) tClass = DisplayObject;
- for(var i:int=0; i<node.numChildren; i++){
- var disp:DisplayObject = node.getChildAt(i);
- if(disp is tClass) return disp;
- }
- return null;
- }
-
- /**
- * ????????propName,???values??????????
- * ??arr=[{name:1},{name:2},{name:3}]; arr2 = findArr(arr,"name",[2,3]) arr2?[{name:2},{name:3}]
- * arr2 = findArr(arr,"name",2) ???{name:2}
- */
- static public function findArr(arr:Array,propName:String,values:Object):*
- {
- if((values is Number) || (values is String) || !resArr.length){
- for each(var o:Object in arr){
- if(o[propName]==values) return o;
- }
- return null;
- }
- var resArr:Array = [];
- for each(o in arr){
- if(values.indexOf(o[propName])>=0) resArr.push(o);
- }
- return resArr;
- }
-
- // ????
- public static function SafeRemoveChild(disp:DisplayObject) : void
- {
- if (disp && disp.parent) disp.parent.removeChild(disp);
- }
-
- /**
- * ?????????????
- * @param s
- */
- static public function clearDisp(s:DisplayObjectContainer):void
- {
- if(s) while(s.numChildren) s.removeChildAt(0);
- }
-
- /** ???????, ?? 0=??, >0=?????, -1=?????*/
- static public function isChild(parent:DisplayObjectContainer, child:DisplayObject):int{
- if(!child || !parent) return -1;
- var deep:int = 0;
- while(child != parent){
- child = child.parent;
- if(!child) return -1;
- deep ++;
- }
- return deep;
- }
-
- /**
- * ??2??????
- * @param ref ????
- * @param disp ?????
- * @param align ???? "T" ???,"B" ???,"L" ???,"R" ???,"C" ??????,"M"????
- * "S" ????disp????ref??
- */
- static public function alignRect(ref:DisplayObject, disp:DisplayObject,align:String="T"):void
- {
- //???
- if(align.indexOf("T")>=0){
- if(disp.parent == ref) disp.y = 0;
- else disp.y = ref.y;
- }
- //???
- if(align.indexOf("B")>=0){
- if(disp.parent == ref) disp.y = ref.height - disp.height;
- else disp.y = ref.y + ref.height - disp.height;
- }
- //???
- if(align.indexOf("L")>=0){
- if(disp.parent == ref) disp.x = 0;
- else disp.x = ref.x;
- }
- //???
- if(align.indexOf("R")>=0){
- if(disp.parent == ref) disp.x = ref.width - disp.width;
- else disp.x = ref.x + ref.width - disp.width;
- }
- //????
- if(align.indexOf("C")>=0){
- if(disp.parent == ref) disp.x = ref.width/2 - disp.width/2;
- else disp.x = ref.x + ref.width/2 - disp.width/2;
- }
- //????
- if(align.indexOf("M")>=0){
- if(disp.parent == ref) disp.y = ref.height/2 - disp.height/2;
- else disp.y = ref.y + ref.height/2 - disp.height/2;
- }
- //????disp???ref??
- if(align.indexOf("W")>=0){
- disp.width = ref.width;
- }
- //????disp???ref??
- if(align.indexOf("H")>=0){
- disp.height = ref.height;
- }
- }
-
- /**
- * ??????????
- * @param parent ???
- * @param propName ???
- * @param propValue ???
- * @param childType ????, ????, ??? null ?? ???
- * @param exclude ????????
- */
- static public function setChildrenProperty(parent:DisplayObjectContainer, propName:String, propValue:Object, childType:Class=null, ...exclude):void{
-
- // ????
- if(childType == null) childType = DisplayObject;
-
- // ??????
- var num:int = parent.numChildren;
- for(var index:int=0; index<num; index++){
-
- // ????
- var child:DisplayObject = parent.getChildAt(index);
- if(! (child is childType) ) continue;
-
- // ????
- if(exclude.indexOf(child) >= 0) continue;
-
- // ???????
- child[propName] = propValue;
- }
- }
-
- /**
- * ??????????????????
- * @param tarDisp ?????
- * @param zwDisp ????
- * @param properties ????????x,y,?,?
- */
- static public function addChildToZW(tarDisp:DisplayObject,zwDisp:DisplayObject,copyProps:String="x,y,width,height"):void
- {
- copyProperties(tarDisp,zwDisp,copyProps);
- if(tarDisp.scaleX==0 || tarDisp.scaleY==0){
- throw new Error("??????, ?????????"); // ?????0
- }
-
- zwDisp.parent.addChild(tarDisp);
- zwDisp.parent.swapChildren(zwDisp, tarDisp);
- SafeRemoveChild(zwDisp);
- }
-
- /**
- * ??????????????????item
- * @param tarDisp
- * @param itemCla ?Class????item
- * @return
- */
- static public function changeSP2Item(tarDisp:DisplayObject,itemCla:Class=null,txt:String=null,vars:Object=null):Item
- {
- if(!tarDisp || tarDisp is SimpleButton) return null;
- itemCla = (itemCla==null)?Item:itemCla;
- var parent:DisplayObjectContainer = tarDisp.parent;
- var id:int = tarDisp.parent.getChildIndex(tarDisp);
- var btn:Item = (new itemCla(null,tarDisp,vars)) as Item;
- btn.x = tarDisp.x;
- btn.y = tarDisp.y;
- tarDisp.x = 0;
- tarDisp.y = 0;
- btn.name = tarDisp.name;
- parent.addChildAt(btn,id);
- if(txt) setSimpleButtonText(tarDisp,txt);
- return btn;
- }
-
- /**
- * ???????
- */
- static public function maxChild(backmc:DisplayObjectContainer):InteractiveObject{
- var maxChild:InteractiveObject = findChild(backmc, InteractiveObject) as InteractiveObject;
- if(maxChild.width == backmc.width && maxChild.height==backmc.height) return maxChild;
- return backmc;
- }
-
- /**
- * ????????
- */
- static public function getLocalMousePoint(disp:DisplayObject):Point{
- trace("getLocalMousePoint for test");
- var x:int = 0;
- var y:int = 0;
- if(disp && disp.stage){
- x = disp.stage.mouseX;
- y = disp.stage.mouseY;
- }
- return disp.globalToLocal( new Point(x, y) );
- }
-
- /**
- * ??propNameList??????
- */
- static public function copyProperties(dst:Object, src:Object, propNameList:String):void{
- if(!dst || !src || !propNameList) return;
- var exp:RegExp = /(\w+)/g;
- var arr:Array = propNameList.match( exp );
- for each(var prop:String in arr){
- dst[prop] = src[prop];
- }
- }
-
- /**
- * ??srcObj??????refObj??????????????????????????????
- * (?????public?)
- * @param obj
- * @param defaultClass
- * @param ignoreProps
- * @param isStrict ????????srcObj????????0????refObj
- * @return
- */
- static public function parseObjToNew(srcObj:Object, refObj:Object, ignoreProps:Array=null, isStrict:Boolean=false):Object
- {
- var desc:XMLList = describeType( srcObj )["variable"];
- // ?? obj ????? prop
- for each(var prop:XML in desc)
- {
- var propName:String = prop.@name; // ???
- var propType:String = prop.@type; // ????
-
- // ???
- if(ignoreProps && ignoreProps.indexOf(propName)>=0) continue;
- //????
- if(isStrict && !srcObj[propName]) continue;
-
- // ?????????
- if(refObj.hasOwnProperty(propName)){
- refObj[propName] = srcObj[propName];
- }
- }
- return refObj;
- }
-
- /**
- * ??????
- * @param obj1 ???????
- * @return Object ?????????
- */
- public static function copyObj(obj1:Object):Object
- {
- var byteArray:ByteArray=new ByteArray();
- byteArray.writeObject(obj1);
-
- byteArray.position=0;
- var obj2:Object=byteArray.readObject();
- return obj2;
- }
-
- /**
- * ????????????,??????????????
- * @param obj
- * @return
- */
- public static function killObject(obj:Object):Object
- {
- for(var strName:String in obj){
- if((obj[strName] is Number) || (obj[strName] is String)) continue;
- if(obj[strName] is DisplayObject){
- SafeRemoveChild(obj[strName] as DisplayObject);
- }
- obj[strName]=null;
- }
- return obj;
- }
-
- /**
- * ????????
- * @param btn ????
- * @param text ????, ?? null ?, ??????(????????)
- * @param isHtml ???html??
- * @return ????????
- */
- static public function setSimpleButtonText(btn:DisplayObject, text:String=null, isHtml:Boolean = false):String{
-
- //
- var prevText:String = "";
- text = text || "";
-
- //
- var list:Array = [ btn["upState"], btn["downState"], btn["overState"], btn["hitTestState"],btn];
- if(btn.hasOwnProperty("selectState")) list.push(btn["selectState"]);
- for each(var disp:DisplayObject in list)
- {
- var tf:TextField = disp as TextField;
- if(tf == null){
- var cont:DisplayObjectContainer = disp as DisplayObjectContainer;
- if(cont) tf = findChild(cont, TextField) as TextField;
- }
- if(tf){
- if(isHtml){
- tf.htmlText = text;
- }else{
- tf.text = text;
- }
- prevText = tf.text;
- }
- }
-
- //
- return text || prevText;
- }
-
- /**
- * ???????????
- * @param src
- * @return
- */
- static public function copy_instance(src:DisplayObject):DisplayObject{
- if(src == null) return null;
- var disp:DisplayObject = (new (src as Object).constructor ) as DisplayObject;
- if(disp is MovieClip){
- (disp as MovieClip).gotoAndStop( (src as MovieClip).currentFrame );
- }
- return disp;
- }
-
- /**
- * ?????????
- */
- static public function removeChildrenByType(node:DisplayObjectContainer, tClass:Class=null):void{
-
- if(tClass == null) tClass = DisplayObject;
- var i:int = 0;
- while(i < node.numChildren)
- {
- var disp:DisplayObject = node.getChildAt(i);
- if(disp is tClass) node.removeChildAt(i);
- else i++;
- }
- }
-
- /**
- * ?????????
- * @param node ???????
- * @param tClass ???????, ??? null, ???????
- * @return ??????, ??? null
- */
- static public function findChildren(node:DisplayObjectContainer, tClass:Class=null):Array{
- var arr:Array = new Array;
- if(tClass == null) tClass = DisplayObject;
- for(var i:int=0; i<node.numChildren; i++){
- var disp:DisplayObject = node.getChildAt(i);
- if(disp is tClass) arr.push(disp);
- }
- return (arr.length>0)? arr: null;
- }
-
- /**
- * ? target ?????, ??????????, ???????
- */
- static public function moveFront(target:DisplayObject):void{
- if(!target || !target.parent) return;
- target.parent.setChildIndex(target, target.parent.numChildren-1);
- }
-
- // ???????????
- static public function disableMouseEvent(parent:DisplayObjectContainer, ...nameList):void{
- for each(var name:String in nameList){
- var disp:DisplayObject = parent.getChildByName(name);
- if(disp is InteractiveObject) (disp as InteractiveObject).mouseEnabled = false;
- if(disp is DisplayObjectContainer) (disp as DisplayObjectContainer).mouseChildren = false;
- }
- }
-
- /**
- * ??disp?vars????????
- * @param disp ????sprite
- * @param rect ???????
- * @param odds ???????
- * @param color ????
- * @param alpha ??alpha?
- */
- public static function drawHorizLines(disp:Sprite,rect:Rectangle,odds:Array,color:uint=0x000000,thickness:Number=1,alpha:Number=1):void
- {
- disp.graphics.lineStyle(thickness,color,alpha);
- for(var i:int=1;i<(odds.length-1);i++){
- var arg:int = rect.height*odds[i];
- disp.graphics.moveTo((rect.x),(rect.y+i*arg));
- disp.graphics.lineTo((rect.x+rect.width),(rect.y+i*arg))
- }
- }
-
- /**
- * ??disp?vars???????
- * @param disp ????sprite
- * @param rect ???????
- * @param odds ???????
- * @param color ????
- * @param alpha ??alpha?
- */
- public static function drawVertLines(disp:Sprite,rect:Rectangle,odds:Array,color:uint=0x000000,thickness:Number=1,alpha:Number=1):void
- {
- disp.graphics.lineStyle(thickness,color,alpha);
- for(var i:int=1;i<(odds.length-1);i++){
- var arg:int = rect.width*odds[i];
- disp.graphics.moveTo((rect.x+i*arg),(rect.y));
- disp.graphics.lineTo((rect.x+i*arg),(rect.y+rect.height))
- }
- }
-
- //??????
- public static function setTextTag(txt:String,tag:String):String
- {
- return "<" + tag + ">" + txt + "</" + tag + ">";
- }
- //??????
- public static function setTextClass(txt:String,cla:String):String
- {
- return "<span class='" + cla + "'>" + txt + "</span>";
- }
-
- /**
- * ???????????
- * @param disp
- * @param spacing ??
- * @param hvArrange ?????0????1???
- */
- public static function arrangeDisp(disp:DisplayObjectContainer,spacing:int=0,hvArrange:int=1):void
- {
- var xyArg:String = hvArrange?"y":"x";
- var whArg:String = hvArrange?"height":"width";
- var tmpXY:int = 0;
- for(var i:int;i<disp.numChildren;i++){
- var dp:DisplayObject = disp.getChildAt(i);
- dp[xyArg] = tmpXY;
- tmpXY = tmpXY + dp[whArg] + spacing;
- }
- }
-
- /**
- * ????????????,????????????
- * @param disp
- * @param perColNum ???x?,???0???,??
- * @param colWidth ?????,???0??item???????,???==??
- * @param rowHeight ??????,???0??item?????????,???==??
- */
- public static function arrangeDispRC(disp:DisplayObjectContainer,perColNum:int=1,colWidth:int=0,rowHeight:int=0,spacing:int=0):void
- {
- var tmpY:int=0,tmpX:int=0;
- for(var i:int=0;i<disp.numChildren;i++){
- var dp:DisplayObject = disp.getChildAt(i);
- if(i && !(i%perColNum) && perColNum!=0){
- tmpY = tmpY + spacing + (rowHeight?rowHeight:dp.height);
- tmpX = 0;
- }
- dp.x = tmpX;
- tmpX = tmpX + (colWidth || dp.width) + spacing;
- dp.y = tmpY;
- }
- }
-
- /**
- * ??????????????
- * @param disp ??????
- * @param width ???????
- * @param odds ??
- */
- public static function arrangDispByOdds(disp:DisplayObjectContainer,width:int,odds:Array,isWidth:Boolean=false):void
- {
- var tmpx:int=0;
- for(var i:int=0;i<disp.numChildren;i++){
- var dp:DisplayObject = disp.getChildAt(i);
- if(isWidth) dp.width = Math.round(odds[i]*width);
- dp.x = tmpx;
- tmpx += Math.round(odds[i]*width);
- }
- }
-
- /**
- * ????????
- * DropShadowFilter factory method, used in many of the components.
- * @param dist The distance of the shadow.
- * @param knockout Whether or not to create a knocked out shadow.
- */
- public static function getShadow(dist:Number, blur:Number=5, color:uint=0x000000):DropShadowFilter
- {
- return new DropShadowFilter(dist, 45, color, 1, blur, blur, .3, 1);
- }
-
- // ?????????
- static private var _sprit:Sprite;
- static public function setNextFrameCallback(fn:Function, ...args):void{
- if(!_sprit) _sprit = new Sprite;
- _sprit.addEventListener(Event.ENTER_FRAME, onEnterFrame);
- function onEnterFrame(event:Event):void{
- _sprit.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
- fn.apply(null, args);
- }
- }
-
- /**
- * ?????????arr?????
- * @param arr ????
- * @param disp ??????
- * @param prefix ?????????
- */
- static public function batParseArrTodisp(arr:Array,disp:DisplayObjectContainer,prefix:String="txt_arr",isHtml:Boolean=false):void
- {
- var txt:TextField;
- for(var i:int=0;i<arr.length;i++){
- if(!disp.getChildByName(prefix+i)) continue;
- if(disp.getChildByName(prefix+i) is TextField){
- txt = (disp.getChildByName(prefix+i) as TextField);
- if(!isHtml) txt.text = String(arr[i]);
- else txt.htmlText = String(arr[i]);
- }else if(disp[prefix+i] is MovieClip){
- (disp[prefix+i] as MovieClip).gotoAndStop(int(arr[i]));
- }
- }
- }
-
- /**
- * ?????????data???????
- * @param data ????
- * @param disp ??????
- */
- static public function batParseObjTodisp(data:Object,disp:DisplayObject,isHtml:Boolean=false):void
- {
- var txt:TextField;
- var desc:XMLList = describeType( disp )["variable"];
- // ?? obj ????? prop
- for each(var prop:XML in desc)
- {
- var propName:String = prop.@name; // ???
- var propType:String = prop.@type; // ????
- if(!data.hasOwnProperty(propName)) continue;
- if(disp[propName] is TextField){
- txt = (disp[propName] as TextField);
- if(!isHtml) txt.text = String(data[propName]);
- else txt.htmlText = String(data[propName]);
- }else if(disp[propName] is MovieClip){
- (disp[propName] as MovieClip).gotoAndStop(int(data[propName]));
- }
- }
- }
-
- // ?????????????????????????
- /**
- * ????????????????
- * @param datas ????
- * @return
- */
- public static function oddsByDatas(datas:Object):Array
- {
- var sum:int = 0,i:int=0,odds:Array = [];
- // ???????
- for each(var s:String in datas){
- sum += (s.length==0)?1:s.length;
- }
- // ?????????????????
- for each(s in datas){
- odds.push(((s.length==0)?1:s.length)/sum);
- }
- return odds;
- }
-
- /**
- * ???????????????refStr
- * ?str = "123455"; str = delstr(str,"345");
- * ??str? 125;
- * @param srcStr
- * @param refStr
- */
- public static function delStr(srcStr:String,refStr:String):String
- {
- var s1:String = srcStr.substring(0,srcStr.indexOf(refStr));
- var s2:String = srcStr.substring((s1.length+refStr.length),srcStr.length);
- return s1+s2;
- }
-
- /**
- * ???
- * @param sp ??????
- */
- public static function drawArrow(dr:int=0,dis:int=11):Shape
- {
- var shap:Shape = new Shape();
- shap.graphics.beginFill(0x666666);
- if(dr==0){ //???
- shap.graphics.moveTo(0,0);
- shap.graphics.lineTo(dis,0);
- shap.graphics.lineTo(dis/2,dis);
- }
- else if(dr==1){ //???
- shap.graphics.moveTo(dis/2,0);
- shap.graphics.lineTo(0,dis);
- shap.graphics.lineTo(dis,dis);
- }
- else if(dr==2){ //???
- shap.graphics.moveTo(dis,0);
- shap.graphics.lineTo(dis,dis);
- shap.graphics.lineTo(0,dis/2);
- }
- else if(dr==3){ //???
- shap.graphics.moveTo(0,0);
- shap.graphics.lineTo(0,dis);
- shap.graphics.lineTo(dis,dis/2);
- }
- shap.graphics.endFill();
- return shap;
- }
-
- /**
- * ?? newObj ?? existObj, ????????, ??? existObj ?? newObj ???, ??????
- */
- static public function replace(newObj:DisplayObjectContainer, existObj:DisplayObject):void{
-
- existObj.parent.addChild(newObj);
- existObj.parent.swapChildren(newObj, existObj);
-
- var x:int = existObj.x;
- var y:int = existObj.y;
- var w:int = existObj.width;
- var h:int = existObj.height;
-
- newObj.addChild(existObj);
- existObj.x = 0;
- existObj.y = 0;
-
- newObj.x = x;
- newObj.y = y;
- newObj.width = w;
- newObj.height = h;
- newObj.name = existObj.name;
- }
- }
- }