class BaseMenu extends BaseMenuButton { children = {}; 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(() => this.open(), this); } closeOnParentClose () { this.parent.isClosed.add(() => this.close(), 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[label] = childButton; childButton.parent = this; childButton.moveOnParentOpen(); childButton.moveOnParentClose(); if (childButton instanceof BaseMenu) { childButton.closeOnParentClose(); } } moveSelfOnOpen () { this.isOpened.add((a, b) => this.moveToActiveState(a, b), this); } moveSelfOnClose () { this.isClosed.add((a, b) => this.moveToInactiveState(a, b), this); } onClick (btn) { this.prevState == MenuStates.open ? this.open() : this.close(); } open (callback) { if (this.state == MenuStates.open) { return; } this.prevState = MenuStates.closed; this.state = MenuStates.moving; this.isOpened.dispatch(this.tOpen, () => this.notifyDone(MenuStates.open, callback)); } close (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)); } /** * gets called when children complete their movement. * Callback is the one received from open(callback), and gets called only once per state change */ notifyDone (state, callback) { if (this.state != state) { this.state = state; callback && callback.apply(this); } } }