Log.js 5.34 KB
var Network = require('Network');
var Common  = require('Common');
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
        _iLogCount:0,
        _oScene:null,
        
    },

    init: function (oScene) {
        this._oScene=oScene;
    },
    // use this for initialization
    onLoad: function () {
        //设置本身节点的宽高透明
        this.node.x=0;
        this.node.y=0;
        this.node.width=Common.SCREEN_WIDTH;
        this.node.height=Common.SCREEN_HEIGHT;
        
        let nodeLogMask=new cc.Node('LogMask');
        nodeLogMask.x=0;
        nodeLogMask.y=0;

        this.node.addChild(nodeLogMask,10);
        
        //设置为全屏遮罩
        // let urlSplash = cc.url.raw('resources/Texture/splash_black.png');
        // let textureFocus = cc.textureCache.addImage(urlSplash);

        let spriteSplash=nodeLogMask.addComponent(cc.Sprite);
        // spriteSplash.spriteFrame = new cc.SpriteFrame(textureFocus);
        //TODO:2.0移除cc.textureCache
        // cc.loader.loadRes("Texture/ui/splash_black",cc.SpriteFrame,function(error,spriteFrame){
        //     spriteSplash.spriteFrame=spriteFrame;
        // });
        //因为纹理2*2所以这里写死了
        nodeLogMask.setScale(Common.SCREEN_WIDTH/2,Common.SCREEN_HEIGHT/2);
        nodeLogMask.opacity=100;
        // nodeLogMask.setLocalZOrder(400);//2.0中所有子节点都会在父节点之后渲染

        let nodeLogList=new cc.Node('LogList');
        nodeLogList.x = 20 - Common.SCREEN_WIDTH/2;
        nodeLogList.y = Common.SCREEN_HEIGHT/2 - 20;
        nodeLogList.setAnchorPoint(0,1);
        this.node.addChild(nodeLogList,10);
        // nodeLogList.setLocalZOrder(402);
        this._nodeLogList = nodeLogList;

        //this.node.color=new cc.Color(0, 0, 0); //黑色
        //this.node.cascadeOpacity=false;
        
        //this.node.parent=this._oScene.node;

        //this.node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(Texture2D.);

        //parentNode.cascadeOpacity=false;
    },

    //statics: {
        
        
        serverD:function(strInfo){
            
        },
        screenD:function(strInfo){
            this.flushLogToScreen(strInfo,'Debug');
        },
        serverI:function(strInfo){

        },
        screenI:function(strInfo){

            this.flushLogToScreen(strInfo,'Info');
        },
        serverW:function(strInfo){

        },
        screenW:function(strInfo){

            this.flushLogToScreen(strInfo,'Warning');
        },
        serverE:function(strInfo){

        },
        screenE:function(strInfo){

            this.flushLogToScreen(strInfo,'Error');
        },

        flushLogToScreen:function(strInfo, strLevel){
            let nodeInfo=new cc.Node('log_info_'+this._iLogCount);
            let lblInfo=nodeInfo.addComponent(cc.Label);
            this._iLogCount++;
            //if _iLogCount>20
            lblInfo.string=strLevel+':'+strInfo;//+(nodeInfo.width/2-Common.SCREEN_WIDTH/2);
            lblInfo.fontSize=18;
            lblInfo.lineHeight=20;
            //nodeInfo.parent=this.node;
            nodeInfo.parent=this._nodeLogList;
            switch(strLevel){
                case 'Debug':
                    nodeInfo.color=new cc.color(51,255,255,255);
                    break;
                case 'Info':
                    nodeInfo.color=new cc.color(51,255,51,255);
                    break;
                case 'Warning':
                    nodeInfo.color=new cc.color(255,255,51,255);
                    break;
                case 'Error':
                    nodeInfo.color=new cc.color(255,51,51,255);
                    break;
                default: 
                    nodeInfo.color=new cc.color(255,255,255,255);

            }
            
            //nodeInfo.color=new cc.color(255,255,255,255);
            nodeInfo.height=20;
            nodeInfo.setAnchorPoint(0,1);
            nodeInfo.y = -(this._iLogCount-1)*20;
            // nodeInfo.setLocalZOrder(401);
            this._nodeLogList.y = this._nodeLogList.y + ((this._iLogCount - parseInt(Common.SCREEN_HEIGHT/20)+1)>0 ? 20 : 0);
        },
        clearLogOnScreen : function () {
            this._nodeLogList.destroy();
            this._iLogCount = 0;
            let nodeLogList=new cc.Node('LogList');
            nodeLogList.x = 20 - Common.SCREEN_WIDTH/2;
            nodeLogList.y = Common.SCREEN_HEIGHT/2 - 20;
            nodeLogList.setAnchorPoint(0,1);
            this.node.addChild(nodeLogList,10);
            // nodeLogList.setLocalZOrder(402);
            this._nodeLogList = nodeLogList;
        },
        flushLogToServer:function(){


        }
        //TODO:需要加一个清除(所有条目全删除)和删除日志(一条一条删除)的函数
    //}
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});