很好用的工具类

package com.pgworld.util {
    import com.yahoo.astra.fl.containers.VBoxPane;

    import flash.display.Bitmap;
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.filters.BitmapFilter;
    import flash.filters.BitmapFilterQuality;
    import flash.filters.ColorMatrixFilter;
    import flash.filters.DropShadowFilter;
    import flash.filters.GlowFilter;
    import flash.geom.ColorTransform;
    import flash.geom.Matrix;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;

    /**
     * @author gan
     */
    public class SpriteUtil {
        public static function clearSprite(v : DisplayObjectContainer) : void {
            if (v == null)
                return;
            var obj : DisplayObject;
            while (v.numChildren > 0) {
                obj = v.getChildAt(0);
                v.removeChild(obj);
                obj = null;
            }
        }

        public static function getTextFilter() : BitmapFilter {
            return new GlowFilter(0x000000, 1, 2, 2, 10, 1, false, false);
        }

        public static function getDropShadowFilter(color : Number = 0x000000, angle : Number = 45, alpha : Number = 0.8, blurX : Number = 3, blurY : Number = 3, distance : Number = 8, strength : Number = 0.65, inner : Boolean = false, knockout : Boolean = false) : Array {
            var quality : Number = BitmapFilterQuality.HIGH;
            var myFilters : Array = new Array();
            myFilters.push(new DropShadowFilter(distance, angle, color, alpha, blurX, blurY, strength, quality, inner, knockout));
            return  myFilters
        }

        public static  function glowFilter(t : TextField) : void {
            var filter : GlowFilter = new GlowFilter(0xf2fc89, .5, 1.1, 1.1, 10, 1, false, false);
            var filters : Array = t.filters;
            filters.push(filter);
            t.filters = filters;
        }

        public static  function getXButton13(_x : int, _y : int, name : String) : XButton13 {
            var xbutton13 : XButton13 = new XButton13();
            xbutton13.x = _x;
            xbutton13.y = _y;
            xbutton13.label = name;
            return xbutton13;
        }

        public static function proceesChild(container : DisplayObjectContainer) : void {
            if (container == null) {
                return;
            }
            var length : uint = container.numChildren;
            for (var i : int = 0;i < length;i++) {
                if (container.getChildAt(i) is DisplayObjectContainer) {
                    var displayObjectContainer : DisplayObjectContainer = container.getChildAt(i) as DisplayObjectContainer;
                    proceesChild(displayObjectContainer);
                    displayObjectContainer = null;
                } else if (container.getChildAt(i) is Bitmap) {
                    var bm : Bitmap = container.getChildAt(i) as Bitmap;
                    if (bm.bitmapData) {
                        bm.bitmapData.dispose();
                        bm = null;
                    }
                } else if (container.getChildAt(i) is DisplayObject) {
                    var displayObject : DisplayObject = container.getChildAt(i) as DisplayObject;
                    displayObject = null;
                }
            }
            SpriteUtil.clearSprite(container);
        }

        public static function remove(displayObj : DisplayObject) : void {
            if (displayObj == null)
                return;
            var parent : DisplayObjectContainer = displayObj.parent;
            if (parent == null)
                return;
            // if(!parent.contains(displayObj))return;
            // 注释by water 查找会找自己和子集,没必要
            displayObj.parent.removeChild(displayObj);
            displayObj = null;
        }

        public static function goFront(displayObj : DisplayObject) : void {
            var container : DisplayObjectContainer = displayObj.parent;
            if (container == null) {
                return;
            }
            var topPosition : uint = container.numChildren - 1;
            container.setChildIndex(displayObj, topPosition);
        }

        public static function goBack(displayObj : DisplayObject) : void {
            var container : DisplayObjectContainer = displayObj.parent;
            if (container == null) {
                return;
            }
            var bottomPosition : uint = 0;
            container.setChildIndex(displayObj, bottomPosition);
        }

        public static function randRange(min : Number, max : Number) : Number {
            var randomNum : Number = Math.floor(Math.random() * (max - min + 1)) + min;
            return randomNum;
        }

        public static var contentText : TextField;

        public static function getScrollPane(_x : int, _y : int, _w : int, _h : int) : VBoxPane {
            var vboxPane : VBoxPane = vBoxPane(_x, _y, _w, _h);
            //
            // var format : TextFormat = new TextFormat();
            // format.font = "宋体";
            // format.size = 12;
            // format.color = 0xffffff;
            // format.align = TextFormatAlign.LEFT;
            //
            // contentText = new TextField();
            // contentText.setTextFormat(format);
            // contentText.width = vboxPane.width-20;
            // contentText.multiline = true;
            // contentText.wordWrap = true;
            // contentText.height = 3000;
            // contentText.htmlText = _name;
            // contentText.height = contentText.bottomScrollV * 16;
            // contentText.selectable = false;
            //
            // vboxPane.addChild(contentText);
            return vboxPane;
        }

        public static function getTextField(_w : int) : TextField {
            var format : TextFormat = new TextFormat();
            format.font = "宋体";
            format.size = 12;
            format.color = 0xffffff;
            format.align = TextFormatAlign.LEFT;
            format.leading = 10;

            var contentText : TextField = new TextField();
            contentText.setTextFormat(format);
            contentText.width = _w;
            contentText.multiline = true;
            contentText.wordWrap = true;
            contentText.height = 3000;

            // contentText.height = contentText.bottomScrollV * 16;
            contentText.selectable = false;
            return contentText;
        }

        private static function vBoxPane(_x : int, _y : int, _w : int, _h : int) : VBoxPane {
            var vboxPane : VBoxPane = new VBoxPane;
            SpriteUtil.setScrollPaneStyle(vboxPane);
            vboxPane.setStyle("upSkin", new Sprite);
            vboxPane.move(_x, _y);

            vboxPane.width = _w;
            vboxPane.height = _h;

            vboxPane.horizontalScrollPolicy = "off";
            vboxPane.paddingBottom = 10;
            vboxPane.paddingTop = 0;

            return vboxPane;
        }

        public static function convertColor(displayObj : DisplayObject, r : Array = null, g : Array = null, b : Array = null, a : Number = 0) : void {
            r = r == null ? [1, 0, 0] : r;
            g = g == null ? [0, 1, 0] : g;
            b = b == null ? [0, 0, 1] : b;
            // r:Array=[1,0,0],g:Array=[0,1,0],b:Array=[0,0,1]
            var matrix : Array = new Array();
            matrix = matrix.concat([r[0], r[1], r[2], 0, -100]);
            matrix = matrix.concat([g[0], g[1], g[2], 0, -100]);
            matrix = matrix.concat([b[0], b[1], b[2], 0, -100]);
            matrix = matrix.concat([0, 0, 0, 1 + a, 0]);
            var filter : BitmapFilter = new ColorMatrixFilter(matrix);
            displayObj.filters = new Array(filter);
        }

        public static function clearBitMap2(_displayObject : DisplayObject) : void {
            if (_displayObject == null) return;
            var bitmap : Bitmap = _displayObject as Bitmap;
            if (bitmap != null) {
                if (bitmap.bitmapData != null) {
                    bitmap.bitmapData.dispose();
                    bitmap.bitmapData = null;
                }
                if (bitmap.parent != null) {
                    bitmap.parent.removeChild(bitmap);
                }
                bitmap = null;
            }
        }

        public static function clearBitMap(_displayObject : DisplayObject) : void {
            var bitmap : Bitmap = _displayObject as Bitmap;
            if (bitmap != null) {
                if (bitmap.bitmapData != null) {
                    bitmap.bitmapData.dispose();
                    bitmap.bitmapData = null;
                }
                // if(bitmap.parent != null) {
                // bitmap.parent.removeChild(bitmap);
                // }
                bitmap = null;
            }
        }

        public static function getSpread(w : Number, h : Number, color : Array, alpha : Array, ratios : Array, gradientType : String = "linear", spreadMethod : String = "pad") : Sprite {
            var myShap : Shape = new Shape();
            var sp : Sprite = new Sprite();
            var type : String = gradientType;
            var matrix : Matrix = new Matrix();
            var boxWidth : Number = w;
            var boxHeight : Number = h;
            var boxRotation : Number = Math.PI / 2;
            matrix.createGradientBox(boxWidth, boxHeight, boxRotation, 0, 0);
            myShap.graphics.beginGradientFill(type, color, alpha, ratios, matrix, spreadMethod);
            myShap.graphics.drawRect(0, 0, w, h);
            myShap.graphics.endFill();
            sp.addChild(myShap);
            return sp;
        }

        public static function getName(s : String) : String {
            switch(s) {
                case "zb":
                    return "装备:(Z)";
                case "cw":
                    return "宠物:(P)";
                case "bb":
                    return "背包:(B)";
                case "jn":
                    return "技能:(S)";
                case "ln":
                    return "琅嬛:(L)";
                case "dw":
                    return "组队:(T)";
                case "hy":
                    return "好友:(F)";
                case "bh":
                    return "帮派:(G)";
                case "rw":
                    return "任务:(Q)";
                default:
                    return "";
            }
        }

        // 移除
        public static function removeAllChild(contaner : DisplayObjectContainer) : void {
            var num : int = contaner.numChildren;
            if (num > 0) {
                for (var i : int = num; i > 0; i--) {
                    contaner.removeChildAt(i - 1);
                    num--;
                }
            }
        }

        /*
         * 获得一个矩形,可以用于TAB的选中状态 added by yong
         */
        public static function getRect(wid : int, hei : int) : Sprite {
            var sp : Sprite = new Sprite();
            sp.graphics.lineStyle(1, 0XCABB19);
            sp.graphics.drawRect(0, 0, wid, hei);
            return sp;
        }

        // 滚动条
        public static function setScrollPaneStyle(scrollPane : *) : void {
            scrollPane.setStyle("upArrowUpSkin", XScrollArrowUp_upSkin);
            scrollPane.setStyle("upArrowOverSkin", XScrollArrowUp_overSkin);
            scrollPane.setStyle("upArrowDownSkin", XScrollArrowUp_downSkin);
            scrollPane.setStyle("upArrowDisabledSkin", XScrollArrowUp_disabledSkin);
            scrollPane.setStyle("downArrowDisabledSkin", XScrollArrowDown_disabledSkin);
            scrollPane.setStyle("downArrowUpSkin", XScrollArrowDown_upSkin);
            scrollPane.setStyle("downArrowOverSkin", XScrollArrowDown_overSkin);
            scrollPane.setStyle("downArrowDownSkin", XScrollArrowDown_downSkin);
            scrollPane.setStyle("trackUpSkin", XScrollTrack_skin);
            scrollPane.setStyle("trackOverSkin", XScrollTrack_skin);
            scrollPane.setStyle("trackDownSkin", XScrollTrack_skin);
            scrollPane.setStyle("trackDisabledSkin", XScrollTrack_skin);
            scrollPane.setStyle("thumbUpSkin", XScrollThumb_upSkin);
            scrollPane.setStyle("thumbOverSkin", XScrollThumb_overSkin);
            scrollPane.setStyle("thumbIcon", XScrollBar_thumbIcon);
            scrollPane.setStyle("thumbDownSkin", XScrollThumb_downSkin);
            scrollPane.setStyle("thumbDisabledSkin", XScrollThumb_upSkin);
        }

        // cellRebderer的style
        public static function setRedererPaneStyle(renderStyle : *) : void {
            renderStyle.setRendererStyle("upSkin", ListCellRenderer_upSkin);
            renderStyle.setRendererStyle("selectedUpSkin", ListCellRenderer_selectedUpSkin);
            renderStyle.setRendererStyle("downSkin", ListCellRenderer_downSkin);
            renderStyle.setRendererStyle("selectedDownSkin", ListCellRenderer_selectedDownSkin);
            renderStyle.setRendererStyle("overSkin", ListCellRenderer_overSkin);
            renderStyle.setRendererStyle("selectedOverSkin", ListCellRenderer_selectedOverSkin);
            renderStyle.setRendererStyle("disabledSkin", ListCellRenderer_disabledSkin);
            renderStyle.setRendererStyle("selectedDisabledSkin", ListCellRenderer_selectedDisabledSkin);
        }

        /*
         * 变灰色
         */
        public static function setGray(disobject : DisplayObject) : void {
            // _activeEquipId_long = 0;
            var redLuminance : Number = 0.212671;
            var greenLuminance : Number = 0.715160;
            var blueLuminance : Number = 0.072169;
            var grayscaleFilter : ColorMatrixFilter = new ColorMatrixFilter(new Array(redLuminance, greenLuminance, blueLuminance, 0, 0, redLuminance, greenLuminance, blueLuminance, 0, 0, redLuminance, greenLuminance, blueLuminance, 0, 0, 0, 0, 0, 1, 0));
            var filters : Array = new Array();
            filters.push(grayscaleFilter);

            if (disobject != null) {
                disobject.filters = filters;
            }
        }

        /*
         * 恢复有色
         */
        public static function setColor(disobject : DisplayObject) : void {
            if (disobject != null) {
                disobject.filters = null;
            }
        }

        // 方式为终止时间(到某年某月某时某分)
        public static function getFullTimeString(playerSeconds : int) : String {
            var date : Date = new Date();
            date.setTime((playerSeconds + 1000000000) * 1000);
            // (playerSeconds+L1000000000)*1000
            var formatter : DateFormat = new DateFormat("Y-m-d H:i:s");

            return formatter.format(date);
        }

        public static function getTimeString(playerSeconds : int) : String {
            var playerMinutes : int = playerSeconds / 60 ;
            // 计算分钟

            var hour : int = playerMinutes / 60 ;
            // 计算剩余小时
            var minutes : int = playerMinutes - hour * 60 ;
            // 计算剩余分钟
            // var seconds:int=playerSeconds-(hour*60*60+minutes*60)  // 计算剩余秒
            var str : String = "";
            if (hour > 0) {
                str = hour + "小时";
            }
            if (minutes > 0) {
                str += minutes + "分钟";
            }
            // if(seconds>0){
            // str += seconds+"秒"
            // }
            return str;
        }

        /**
         * 改变高亮状态
         * @param highlight 是否高亮
         */
        public static function setHighlight(displayobject : DisplayObject, highlight : Boolean) : void {
            var tran : ColorTransform;
            if ( highlight ) {
                tran = new ColorTransform(1, 1, 1, 1, 50, 50, 50, 0);
                displayobject.transform.colorTransform = tran;
            } else {
                tran = new ColorTransform(1, 1, 1, 1, 0, 0, 0, 0);
                displayobject.transform.colorTransform = tran;
                tran = null;
            }
        }
    }
}

 

posted @ 2013-04-15 19:08  tinytiny  阅读(204)  评论(0编辑  收藏  举报