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;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值