class BaseMenu extends BaseMenuButton { children = []; // {label button} state = 0; prevState = null; isOpened = null; isClosed = null; tOpen = 0; tClose = 0; constructor (sx, sy, ex, ey, sprite, layer, tOpen = 800, tClose = 1000) { super(sx, sy, ex, ey, sprite, layer); this.tOpen = tOpen; this.tClose = tClose; } openOnParentOpen () { this.parent.isOpened.add((cont) => this.open(null, cont), this); } closeOnParentClose () { this.parent.isClosed.add((cont) => this.close(null, cont), this); } addedToScene () { this.isOpened = new Signal(); this.isClosed = new Signal(); // reminder to implement this in subclasses to instantiate child buttons etc } addChildButton (label, childButton) { this.children.push({ label: label, button: childButton }); childButton.parent = this; childButton.moveOnParentOpen(); childButton.moveOnParentClose(); if (childButton instanceof BaseMenu) { childButton.closeOnParentClose(); } } getChildButton (label) { return this.children.filter(e => e.label == label)[0].button; } moveSelfOnOpen (once) { this.isOpened.add((a, b) => this.moveToActiveState(a, b), this, once); } moveSelfOnClose (once) { this.isClosed.add((a, b) => this.moveToInactiveState(a, b), this, once); } onClick (btn) { this.prevState == MenuStates.open ? this.open() : this.close(); } open (callback, context) { if (this.state == MenuStates.open) { return; } this.prevState = MenuStates.closed; this.state = MenuStates.moving; this.isOpened.dispatch(this.tOpen, () => { this.notifyDone(MenuStates.open); callback && callback.apply(context); }); } close (callback, context) { console.log(this, callback); if (this.state == MenuStates.closed || this.state == MenuStates.moving && this.prevState == MenuStates.open) { return; } this.prevState = MenuStates.open; this.state = MenuStates.moving; this.isClosed.dispatch(this.tClose, () => { this.notifyDone(MenuStates.closed); callback && callback.apply(context); }); } notifyDone (state) { this.state = state; } }