Cocosd2d-js 实现窗体管理器

根据弹出窗体的不同层级进行管理和关闭,可以知道当前scene都弹出哪些窗体,便于统一化管理

"use strict";
var modalDlg = require("./../../ui/common/ModalDialogLayer.js");

var WindowManager = cc.Class.extend({
    windowList: null,
    baseZorder: 1000000,
    baseTag: 1000000,

    ctor: function() {
        this.windowList = {};
        this.windowList.menus = [];
        this.windowList.layers = [];
    },

    createMenu: function(parent, zorder, menuItems) {
        var menu = new cc.Menu(menuItems);
        parent.addChild(menu, zorder);
        this.windowList.menus.push({parent: parent, menu: menu});
        return menu;
    },

    destroyCurrentMenu: function() {
        var object = this.windowList.menus.pop();
        if (object != undefined) {
            object.parent.removeChild(object.menu);
        }
    },

    createLayer: function (parent, isInteadCur, dlgName) {
        if (isInteadCur === true) {
            this.removeTopLayer();
        }
        var orderNum = this.windowList.layers.length;
        // TODO: 传入继承与modallayer的窗体,灵活使用
        // var addLayer = new modalDlg();
        // parent.addChild(addLayer, this.baseZorder+orderNum*100, this.baseTag+orderNum*100);
        // addLayer.setPosition(0, 0);
        // dlgName.setPosition(0, 0);
        // addLayer.addChild(dlgName, 10000);
        dlgName.setPosition(0, 0);
        if (parent == undefined) {
            parent = cc.director.getRunningScene();
        }
        parent.addChild(dlgName, this.baseZorder+orderNum*100, this.baseTag+orderNum*100);

        this.windowList.layers.push({index: orderNum, father: parent, dlg: dlgName, tag: this.baseTag+orderNum*100, zorder: this.baseZorder+orderNum*100})
    },

    removeLayerByDlg: function (dlg) {
        var found = false;
        for (i=0;i<this.windowList.layers.length;i++) {
            var lastLayer = this.windowList.layers[i];
            if (lastLayer != undefined && lastLayer.dlg === dlg) {
                lastLayer.father.removeChildByTag(lastLayer.tag, true);
                // this.windowList.layers[i] = null;
                this.windowList.layers.splice(i, 1);
                found = true;
            }
        }
        if (found == false) {
            cc.log("warning: window manager cant find dialog when remove " + dlg);
        }
    },

    removeAllLayers: function () {
        for (i=0;i<this.windowList.layers.length;i++) {
            var lastLayer = this.windowList.layers[i];
            if (lastLayer != undefined) {
                lastLayer.father.removeChildByTag(lastLayer.tag, true);
            }
        }
        this.windowList.layers = [];
    },

    removeTopLayer: function () {
        var orderNum = this.windowList.layers.length;
        if (orderNum > 0) {
            var lastLayer = this.windowList.layers.pop();
            if (lastLayer != undefined) {
                lastLayer.father.removeChildByTag(lastLayer.tag, true);
            }
        }
    },

    insteadBaseLayer: function (parent, dlgName) {
        this.removeAllLayers();
        this.createLayer(parent, false, dlgName);
    },

    getCurrentLayers: function () {
        return this.windowList.layers;
    },
});

module.exports = WindowManager;


你可能感兴趣的:(cocos2d,窗口管理)