Files
MatchMaster/assets/Script/PopupBackdropSource.ts
T

46 lines
2.0 KiB
TypeScript

import SharedPopupBackdrop, { PopupBackdropSource } from './SharedPopupBackdrop';
const { ccclass } = cc._decorator;
/** Attach only to a full-screen dim mask, never to an activity's artwork.
* Keep the original node and input components so prefab event bindings still work. */
@ccclass
export default class PopupBackdropSourceComponent extends cc.Component {
private manager: SharedPopupBackdrop = null;
private source: PopupBackdropSource = null;
private renderers: Array<{ component: cc.RenderComponent; enabled: boolean }> = [];
private originalOpacity = 255;
onLoad(): void {
this.originalOpacity = this.node.opacity;
this.renderers = this.node.getComponents(cc.RenderComponent).map(component => ({ component, enabled: component.enabled }));
const visual = new cc.Node('ManagedBackdropVisual');
visual.active = false;
this.node.addChild(visual);
visual.addComponent(cc.Graphics);
this.source = { node: this.node, visual, active: false };
}
onEnable(): void {
let canvas = this.node.parent;
while (canvas && canvas.name !== 'Canvas') canvas = canvas.parent;
if (!canvas) return;
this.manager = SharedPopupBackdrop.get(canvas);
this.renderers.forEach(item => item.component.enabled = false);
this.source.active = true;
this.manager.register(this.source);
}
onDisable(): void {
if (this.source) { this.source.active = false; this.source.visual.active = false; }
this.node.opacity = this.originalOpacity;
this.renderers.forEach(item => { if (cc.isValid(item.component, true)) item.component.enabled = item.enabled; });
if (this.manager && cc.isValid(this.manager, true)) this.manager.refresh();
}
onDestroy(): void {
if (this.manager && cc.isValid(this.manager, true)) this.manager.unregister(this.source);
if (this.source && cc.isValid(this.source.visual, true)) this.source.visual.destroy();
this.manager = null; this.source = null;
}
}