-
Notifications
You must be signed in to change notification settings - Fork 127
PopupMenu and MenuBar
VisUI provides PopupMenu and MenuBar widgets. Currently menus cannot have submenus.
PopupMenu is standard standard menu that can be displayed anywhere on stage. Any widget can be added to it however it provides two methods for adding standard menu items and menu separators:
menu.addItem (MenuItem item)
menu.addSeparator ()
To display or remove menu call:
menu.displayMenu (Stage stage, float x, float y)
menu.remove ()
displayMenu() makes sure that menu stays inside stage, alternative way is to add menu like any other scene2d.ui widget.
Removed menu can be reused later. Method is called remove and not hide because the menu is actually completely removed from stage and not only hidden.
Menus can also be removed automatically if user clicked outside it, that feature is disabled by default. To enable it call:
menu.setAutoRemove (true)
Alternatively you can use PopupMenu (boolean autoRemove) constructor.
Even if auto remove is enabled menu won't be removed if user clicked inside menu, that means clicking on menu items doesn't remove menu automatically. If you want to remove menu on menu item click you have to do that from menu item listener.
MenuItem is a component that is displayed inside PopupMenu. It provides various constructors, that allows to construct MenuItem with text only, or with text and a icon. There are also constructors that takes ChangeListener along with text and icon. Best icon size is 22x22px.
Listener is called when button has been clicked.
Additionally you can set shortcut that will be displayed next to menu text, shortcut text is gray and is right aligned (see screenshot above). setShortcut() method can take int value from LibGDX Keys class or custom text. This methods returns current MenuItem for the purpose of chaining method together. Please note that shortcuts are only decoration, listener won't be called if user will press key that you set for shortcut.
MenuBar is a bar with menus, usually displayed on top of the stage. Constructor takes Stage.
Recommended way to add menu is:
root.add(menuBar.getTable()).fillX().expandX().row();
//or (if you have more than one column root in table)
root.add(menuBar.getTable()).fillX().expandX().colspan(x).row();
To add menu to MenuBar call:
menuBar.addMenu(Menu menu)
The only difference between Menu and PopupMenu is that Menu constructor takes title, that will be used on MenuBar. You can add MenuItems and separators same way you added them to PopupMenu.
See README for VisUI introduction.