BlueGit

blue-star speeltuin

Files

speeltuin / src / objects / menus / 1-baseMenu.js

1-baseMenu.js

1-baseMenu.js 2.2 KiB Download Raw
1class 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((cont) => this.open(null, cont), this);
24 }
25
26 closeOnParentClose ()
27 {
28 this.parent.isClosed.add((cont) => this.close(null, cont), 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 (once)
51 {
52 this.isOpened.add((a, b) => this.moveToActiveState(a, b), this, once);
53 }
54
55 moveSelfOnClose (once)
56 {
57 this.isClosed.add((a, b) => this.moveToInactiveState(a, b), this, once);
58 }
59
60 onClick (btn)
61 {
62 this.prevState == MenuStates.open ?
63 this.open() :
64 this.close();
65 }
66
67 open (callback, context)
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 && callback.apply(context); });
76 }
77
78 close (callback, context)
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 && callback.apply(context); });
87 }
88
89 notifyDone (state)
90 {
91 this.state = state;
92 }
93
94
95}