BlueGit

blue-star speeltuin

Files

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

1-baseMenu.js

good luck
timfa wsadhelios · pushed by timfa · 4 years ago
f1aab4e6 History
1-baseMenu.js 2.4 KiB Download Raw
1class BaseMenu extends BaseMenuButton
2{
3 children = []; // {label button}
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.push({ label: label, button: childButton });
41 childButton.parent = this;
42 childButton.moveOnParentOpen();
43 childButton.moveOnParentClose();
44 if (childButton instanceof BaseMenu)
45 {
46 childButton.closeOnParentClose();
47 }
48 }
49
50 getChildButton (label)
51 {
52 return this.children.filter(e => e.label == label)[0].button;
53 }
54
55 moveSelfOnOpen (once)
56 {
57 this.isOpened.add((a, b) => this.moveToActiveState(a, b), this, once);
58 }
59
60 moveSelfOnClose (once)
61 {
62 this.isClosed.add((a, b) => this.moveToInactiveState(a, b), this, once);
63 }
64
65 onClick (btn)
66 {
67 this.prevState == MenuStates.open ?
68 this.open() :
69 this.close();
70 }
71
72 open (callback, context)
73 {
74 if (this.state == MenuStates.open)
75 {
76 return;
77 }
78 this.prevState = MenuStates.closed;
79 this.state = MenuStates.moving;
80 this.isOpened.dispatch(this.tOpen, () => { this.notifyDone(MenuStates.open); callback && callback.apply(context); });
81 }
82
83 close (callback, context)
84 {
85 console.log(this, callback);
86 if (this.state == MenuStates.closed || this.state == MenuStates.moving && this.prevState == MenuStates.open)
87 {
88 return;
89 }
90 this.prevState = MenuStates.open;
91 this.state = MenuStates.moving;
92 this.isClosed.dispatch(this.tClose, () => { this.notifyDone(MenuStates.closed); callback && callback.apply(context); });
93 }
94
95 notifyDone (state)
96 {
97 this.state = state;
98 }
99
100
101}