BlueGit

blue-star speeltuin

Files

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

1-baseMenu.js

fixed mess & made more mess
timfa Nick Hiert · pushed by timfa · 4 years ago
d2b34663 History
1-baseMenu.js 2.3 KiB Download Raw
1class BaseMenu
2{
3 children = {};
4
5 owner = null;
6
7 state = 0;
8 prevState = 1;
9
10 isOpened = null;
11 isClosed = null;
12
13 tOpen = 0;
14 tClose = 0;
15
16 constructor (owner, tOpen = 800, tClose = 1000)
17 {
18 this.owner = owner;
19 this.tOpen = tOpen;
20 this.tClose = tClose;
21 this.init();
22 }
23
24 openOnParentOpen ()
25 {
26 this.owner.parent.isOpened.add(() => this.open(), this);
27 }
28
29 closeOnParentClose ()
30 {
31 this.owner.parent.isClosed.add(() => this.close(), this);
32 }
33
34 init ()
35 {
36 this.isOpened = new Signal();
37 this.isClosed = new Signal();
38 }
39
40 addChildItem (label, childItem)
41 {
42 this.children[label] = childItem;
43 childItem.parent = this;
44 childItem.moveOnParentOpen();
45 childItem.moveOnParentClose();
46 if (childItem.menu && childItem.menu instanceof BaseMenu)
47 {
48 childItem.menu.closeOnParentClose();
49 }
50 }
51
52 moveSelfOnOpen ()
53 {
54 this.isOpened.add((a, b) => this.owner.moveToActiveState(a, b), this);
55 }
56
57 moveSelfOnClose ()
58 {
59 this.isClosed.add((a, b) => this.owner.moveToInactiveState(a, b), this);
60 }
61
62 onClick (btn)
63 {
64 this.prevState == MenuStates.open ?
65 this.open() :
66 this.close();
67 }
68
69 open (callback)
70 {
71 if (this.state == MenuStates.open)
72 {
73 return;
74 }
75 this.prevState = MenuStates.closed;
76 this.state = MenuStates.moving;
77 this.isOpened.dispatch(this.tOpen, () => this.notifyDone(MenuStates.open, callback));
78 }
79
80 close (callback)
81 {
82 if (this.state == MenuStates.closed || (this.state == MenuStates.moving && this.prevState == MenuStates.open))
83 {
84 return;
85 }
86 this.prevState = MenuStates.open;
87 this.state = MenuStates.moving;
88 this.isClosed.dispatch(this.tClose, () => this.notifyDone(MenuStates.closed, callback));
89 }
90
91 /**
92 * gets called when children complete their movement.
93 * Callback is the one received from open(callback), and gets called only once per state change
94 */
95 notifyDone (state, callback)
96 {
97 if (this.state != state)
98 {
99 this.state = state;
100 callback && callback.apply(this);
101 }
102 }
103
104
105}