| 1 | class BaseMenu extends BaseMenuButton |
| 2 | { |
| 3 | children = {}; |
| 4 | |
| 5 | state = 0; |
| 6 | prevState = null; |
| 7 | |
| 8 | isOpened = null; |
| 9 | isClosed = null; |
| 10 | |
| 11 | tOpen = 0; |
| 12 | tClose = 0; |
| 13 | |
| 14 | constructor (sx, sy, ex, ey, sprite, layer, tOpen = 800, tClose = 1000) |
| 15 | { |
| 16 | super(sx, sy, ex, ey, sprite, layer); |
| 17 | this.tOpen = tOpen; |
| 18 | this.tClose = tClose; |
| 19 | } |
| 20 | |
| 21 | openOnParentOpen () |
| 22 | { |
| 23 | this.parent.isOpened.add(() => this.open(), this); |
| 24 | } |
| 25 | |
| 26 | closeOnParentClose () |
| 27 | { |
| 28 | this.parent.isClosed.add(() => this.close(), this); |
| 29 | } |
| 30 | |
| 31 | addedToScene () |
| 32 | { |
| 33 | this.isOpened = new Signal(); |
| 34 | this.isClosed = new Signal(); |
| 35 | // reminder to implement this in subclasses to instantiate child buttons etc |
| 36 | } |
| 37 | |
| 38 | addChildButton (label, childButton) |
| 39 | { |
| 40 | this.children[label] = childButton; |
| 41 | childButton.parent = this; |
| 42 | childButton.moveOnParentOpen(); |
| 43 | childButton.moveOnParentClose(); |
| 44 | if (childButton instanceof BaseMenu) |
| 45 | { |
| 46 | childButton.closeOnParentClose(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | moveSelfOnOpen () |
| 51 | { |
| 52 | this.isOpened.add((a, b) => this.moveToActiveState(a, b), this); |
| 53 | } |
| 54 | |
| 55 | moveSelfOnClose () |
| 56 | { |
| 57 | this.isClosed.add((a, b) => this.moveToInactiveState(a, b), this); |
| 58 | } |
| 59 | |
| 60 | onClick (btn) |
| 61 | { |
| 62 | this.prevState == MenuStates.open ? |
| 63 | this.open() : |
| 64 | this.close(); |
| 65 | } |
| 66 | |
| 67 | open (callback) |
| 68 | { |
| 69 | if (this.state == MenuStates.open) |
| 70 | { |
| 71 | return; |
| 72 | } |
| 73 | this.prevState = MenuStates.closed; |
| 74 | this.state = MenuStates.moving; |
| 75 | this.isOpened.dispatch(this.tOpen, () => this.notifyDone(MenuStates.open, callback)); |
| 76 | } |
| 77 | |
| 78 | close (callback) |
| 79 | { |
| 80 | if (this.state == MenuStates.closed || this.state == MenuStates.moving && this.prevState == MenuStates.open) |
| 81 | { |
| 82 | return; |
| 83 | } |
| 84 | this.prevState = MenuStates.open; |
| 85 | this.state = MenuStates.moving; |
| 86 | this.isClosed.dispatch(this.tClose, () => this.notifyDone(MenuStates.closed, callback)); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * gets called when children complete their movement. |
| 91 | * Callback is the one received from open(callback), and gets called only once per state change |
| 92 | */ |
| 93 | notifyDone (state, callback) |
| 94 | { |
| 95 | if (this.state != state) |
| 96 | { |
| 97 | this.state = state; |
| 98 | callback && callback.apply(this); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
| 103 | } |