288156a3-6b99-42a5-bfb4-05a42916f834.js 6.8 KB
"use strict";
cc._RF.push(module, '28815aja5lCpb+0BaQpFvg0', 'CCViewBase');
// script/common/classbase/CCViewBase.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CCPureSubview = exports.CCPureView = exports.CCSubview = exports.CCView = void 0;
const simba_eventkit_1 = require("simba-eventkit");
const ViewBase_1 = require("./ViewBase");
class CCViewBase extends cc.Component {
    constructor() {
        super(...arguments);
        this._emitter = new simba_eventkit_1.Emitter;
        this._showed = false;
        this._isHidden = false;
        this.onShow = this._emitter.createEvent();
        this.onHide = this._emitter.createEvent();
    }
    get isHidden() { return this._isHidden || !this.node.active; }
    show() {
        if (!this.node.parent || !this.isHidden)
            return;
        if (this.isHidden) {
            this.node.active = true;
            this._isHidden = false;
            this.node.opacity = 255;
            if (this._posX !== undefined)
                this.node.x = this._posX;
            this.onShow.emit();
        }
    }
    hide() {
        if (!this.node.parent || this.isHidden)
            return;
        if (!this.isHidden) {
            this._isHidden = true;
            this._posX = this.node.x;
            this.node.opacity = 0;
            this.node.x = 10000;
            this.onHide.emit();
        }
    }
}
class CCView extends CCViewBase {
    constructor() {
        super(...arguments);
        this._type = "View";
        this.onClose = this._emitter.createEvent();
    }
    open(parent) {
        this.node.parent = parent.node;
    }
    close(destroy) {
        if (!this.node.parent)
            return;
        let com = this.node.getComponent('AlterAnim');
        let yes = com ? this.node.getComponent('AlterAnim').enabled : false;
        if (destroy) {
            if (yes) {
                com.hideUIAnim({
                    callback: () => {
                        this.node.destroy();
                    },
                });
            }
            else {
                this.node.destroy();
            }
        }
        else {
            if (yes) {
                com.hideUIAnim({
                    callback: () => {
                        this.node.removeFromParent(false);
                    },
                });
            }
            else {
                this.node.removeFromParent(false);
            }
        }
        this.onClose.emit(destroy);
    }
    /**
     * @param node
     * @param gray 是否置灰 true:置灰 false:正常
     * @param childrenIsGray node传node时孩子节点是否置灰(将递归所有节点)
     */
    setGray(node, gray = true, childrenIsGray) {
        let target = node;
        let str = gray ? cc.Material.BUILTIN_NAME.GRAY_SPRITE : cc.Material.BUILTIN_NAME.SPRITE;
        var material = cc.Material.getBuiltinMaterial(str + "");
        if (node instanceof cc.Node) {
            target = node.getComponent(cc.Sprite);
            target && target.setMaterial(0, material);
            target = node.getComponent(cc.Label);
            target && target.setMaterial(0, material);
            childrenIsGray && node.children.forEach(subNode => {
                this.setGray(subNode, gray, childrenIsGray);
            }, this);
        }
        else {
            target && target.setMaterial(0, material);
        }
    }
}
exports.CCView = CCView;
class CCSubview extends CCViewBase {
    constructor() {
        super();
        this._type = "Subview";
        ViewBase_1.OnSubviewCreated.emit(this);
    }
    onLoad() {
        this.name = this.node.name;
    }
    attachParent(parent) {
        this.node.parent = parent.node;
    }
}
exports.CCSubview = CCSubview;
class PureViewBase {
    constructor() {
        this._prepareUpdate = false;
        this._propBinder = {};
    }
    setProps(props) {
        this._tempProps = props;
        if (!this._props) {
            this._setProps();
        }
        else {
            if (!this._prepareUpdate) {
                this._prepareUpdate = true;
                this.scheduleOnce(() => {
                    this._setProps();
                });
            }
        }
    }
    _setProps() {
        this._prepareUpdate = false;
        this._prevProps = this._props;
        this._props = Object.assign({}, this._tempProps);
        delete this._tempProps;
        this.onPropsReceive();
        if (!this._prevProps) {
            this.onPropsLoad(this._props);
        }
        else {
            let keys = new Set([...Object.keys(this._prevProps), ...Object.keys(this._props)]);
            for (let key of keys) {
                if (this._props[key] !== this._prevProps[key]) {
                    this.onPropChange(key);
                }
            }
        }
    }
    updateProps(props) {
        if (!this._props)
            throw new Error("setProps first.");
        let fullProps = this._tempProps ? Object.assign(Object.assign({}, this._tempProps), props) : Object.assign(Object.assign({}, this._props), props);
        this.setProps(fullProps);
    }
    bindProp(key, target, prop) {
        if (!this._propBinder[key])
            this._propBinder[key] = [];
        this._propBinder[key].push({ target, prop });
    }
    onPropsReceive() {
    }
    onPropsLoad(props) {
        for (let key in props) {
            if (this._propBinder[key]) {
                for (const { target, prop } of this._propBinder[key]) {
                    if (typeof target === "function") {
                        target(this._props[key]);
                    }
                    else {
                        target[prop] = this._props[key];
                    }
                }
            }
        }
    }
    onPropChange(key) {
        if (this._propBinder[key]) {
            for (const { target, prop } of this._propBinder[key]) {
                if (typeof target === "function") {
                    target(this._props[key]);
                }
                else {
                    target[prop] = this._props[key];
                }
            }
        }
    }
}
class CCPureView extends CCView {
    constructor() {
        super();
        this._propBinder = {};
    }
    onLoad() { }
}
exports.CCPureView = CCPureView;
class CCPureSubview extends CCSubview {
    constructor() {
        super();
        this._propBinder = {};
    }
}
exports.CCPureSubview = CCPureSubview;
function applyMixins(derivedCtor, constructors) {
    constructors.forEach((baseCtor) => {
        Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
            if (name !== "constructor") {
                Object.defineProperty(derivedCtor.prototype, name, Object.getOwnPropertyDescriptor(baseCtor.prototype, name));
            }
        });
    });
}
applyMixins(CCPureView, [PureViewBase]);
applyMixins(CCPureSubview, [PureViewBase]);

cc._RF.pop();