-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMenuExpendButton.java
More file actions
35 lines (30 loc) · 1.02 KB
/
MenuExpendButton.java
File metadata and controls
35 lines (30 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* A special button that is used to open or close the menu.
*/
public class MenuExpendButton extends Button {
public static final String IDLE_IMAGE_NAME = "menu-expand-button.png";
public static final String ACTIVE_IMAGE_NAME = "menu-expand-button-active.png";
public MenuExpendButton() {
super(IDLE_IMAGE_NAME, ACTIVE_IMAGE_NAME);
}
@Override
protected void clickAction() {
toggleMenu();
}
/**
* Either opens or closes the menu when pressing the button.
*/
private void toggleMenu() {
setActive(!isActive());
Menu menu = new Menu();
if(isActive()) { // expand menu
setLocation(getX(), getY() - menu.getImage().getHeight());
getWorld().addObject(menu, getWorld().getWidth() / 2, getWorld().getHeight() - menu.getImage().getHeight() / 2 - 2);
menu.buildMenu();
} else { // close menu
menu = getWorld().getObjectsAt(getX(), getY() + menu.getImage().getHeight(), Menu.class).get(0);
setLocation(getX(), getWorld().getHeight() - (getImage().getHeight() / 2 + 4));
menu.removeYourself();
}
}
}