2 * This class constructs and positions the menu buttons and assigns the handlers defined in MenuButtons.
6 constructor(pauseControl, playerViewControl, chat)
8 this.menuButton = Engine.GetGUIObjectByName("menuButton");
9 this.menuButton.onPress = this.toggle.bind(this);
10 registerHotkeyChangeHandler(this.rebuild.bind(this));
13 this.lastTick = undefined;
15 this.menuButtonPanel = Engine.GetGUIObjectByName("menuButtonPanel");
16 let menuButtons = this.menuButtonPanel.children;
17 this.margin = menuButtons[0].size.top;
18 this.buttonHeight = menuButtons[0].size.bottom;
20 let handlerNames = this.getHandlerNames();
21 if (handlerNames.length > menuButtons.length)
23 "There are " + handlerNames.length + " menu buttons defined, " +
24 "but only " + menuButtons.length + " objects!");
26 this.buttons = handlerNames.map((handlerName, i) => {
27 let handler = new MenuButtons.prototype[handlerName](menuButtons[i], pauseControl, playerViewControl, chat);
28 this.initButton(handler, menuButtons[i], i);
32 this.endPosition = this.margin + this.buttonHeight * (1 + handlerNames.length);
33 let size = this.menuButtonPanel.size;
34 size.top = -this.endPosition;
36 this.menuButtonPanel.size = size;
41 this.menuButton.tooltip = sprintf(translate("Press %(hotkey)s to toggle this menu."), {
42 "hotkey": colorizeHotkey("%(hotkey)s", this.menuButton.hotkey),
47 * This function may be overwritten to change the button order.
51 return Object.keys(MenuButtons.prototype);
56 this.isOpen = !this.isOpen;
57 this.startAnimation();
63 this.startAnimation();
66 initButton(handler, button, i)
68 button.onPress = () => {
73 let size = button.size;
74 size.top = this.buttonHeight * (i + 1) + this.margin;
75 size.bottom = this.buttonHeight * (i + 2);
78 button.hidden = false;
83 this.lastTick = Date.now();
84 this.menuButtonPanel.onTick = this.onTick.bind(this);
92 let tickLength = Date.now() - this.lastTick;
93 this.lastTick = Date.now();
98 -this.menuButtonPanel.size.bottom :
99 +this.menuButtonPanel.size.top);
104 delete this.menuButtonPanel.onTick;
108 let offset = Math.min(this.Speed * tickLength, maxOffset) * (this.isOpen ? +1 : -1);
109 let size = this.menuButtonPanel.size;
111 size.bottom += offset;
112 this.menuButtonPanel.size = size;
117 * Number of pixels per millisecond to move.
119 Menu.prototype.Speed = 1.2;