MenuBar
- class pygamelib.gfx.ui.MenuBar(entries: list = None, spacing: int = 2, config: UiConfig = None)
Bases:
object
The MenuBar widget is exactly that: an horizontal bar that can hold
Menu
orMenuAction
objects.Contrary to these 2 classes, MenuBar does not have an activate() method. The reason is that the menubar cannot block rendering with its own event loop as it is supposed to be showned at all times. So the management of interactions are left to the programmer to implement.
A typical implementation would look like this:
Example:
# First create a menubar menubar = MenuBar(config=UiConfig.instance(game=Game.instance())) # Then create a Menu file_menu = Menu( "File", [ MenuAction("Open", open_file), MenuAction("Save", save_file), MenuAction("Save as", save_file_as), MenuAction("Quit", exit_application), ] ) menubar.add_entry( file_menu ) menubar.add_entry( MenuAction("Help", display_help) ) # Place the menubar on screen screen.place(menubar, 0, 0) screen.update() # Then, somewhere in an event loop, manage the inputs for example in the user # update function def user_update(game, inkey, elapsed_time): if inkey == engine.key.DOWN: if menubar.current_entry() is not None: menubar.current_entry().activate() elif inkey == engine.key.LEFT: menubar.select_previous() elif inkey == engine.key.RIGHT: menubar.select_next() elif inkey.name == "KEY_ENTER": if menubar.current_entry() is not None: menubar.current_entry().activate() elif inkey.name == "KEY_ESCAPE": menubar.close()
- __init__(entries: list = None, spacing: int = 2, config: UiConfig = None) → None
The constructor takes the following parameters.
- Parameters:
entries (list) – A list of
MenuAction
orMenu
objects.spacing – The horizontal spacing between entries.
config (
UiConfig
) – The configuration object.
Methods
__init__
([entries, spacing, config])The constructor takes the following parameters.
add_entry
(entry)Add an entry to the menu bar.
close
()Close and unselect menu entries/submenu.
Return the currently selected menu entry.
length
()Returns the total length of the menubar.
render_to_buffer
(buffer, row, column, ...)Render the object from the display buffer to the frame buffer.
Select the next element in the menubar.
Select the previous element in the menubar.
Attributes
Get / set the config of the MenuBar, it needs to be a
UiConfig
.Get / set the currently selected menu entry, it needs to be an int.
Get / set the entries of the MenuBar, it needs to be a list of
MenuAction
orMenu
objects.Get / set the spacing between menu entries, it needs to be an int.
- add_entry(entry)
Add an entry to the menu bar. An entry can be a
MenuAction
or aMenu
. Entries are displayed in the order of there additions from left to right.Important
The config of the entry is overwritten by the config of the MenuBar. That is why it’s not mandatory for
Menu
andMenuAction
.- Parameters:
entry (
MenuAction
|Menu
) – The entry to add.
Example:
menubar.add_entry( Menu('File') ) menubar.add_entry( MenuAction('Exit', quit_application) )
- close()
Close and unselect menu entries/submenu.
Please call that method when the menu bar loses focus.
- property config
Get / set the config of the MenuBar, it needs to be a
UiConfig
.Important
The MenuBar’s config is imposed on the managed items (Menu and MenuAction).
- current_entry()
Return the currently selected menu entry.
It can be either a
Menu
object or aMenuAction
object.
- property current_index
Get / set the currently selected menu entry, it needs to be an int. When setting the current_index, if the previous index was corresponding to a selected entry, said entry is first unselected.
- property entries: list
Get / set the entries of the MenuBar, it needs to be a list of
MenuAction
orMenu
objects.
- length() → int
Returns the total length of the menubar. This is computed everytime the method is called and it includes the spacing.
- render_to_buffer(buffer, row: int, column: int, buffer_height: int, buffer_width: int) → None
Render the object from the display buffer to the frame buffer.
This method is automatically called by
pygamelib.engine.Screen.render()
.- Parameters:
buffer (numpy.array) – A screen buffer to render the item into.
row (int) – The row to render in.
column (int) – The column to render in.
height (int) – The total height of the display buffer.
width (int) – The total width of the display buffer.
- select_next()
Select the next element in the menubar.
Example
if user_input.name == 'KEY_RIGHT': menubar.select_next()
- select_previous()
Select the previous element in the menubar.
Example
if user_input.name == 'KEY_RIGHT': menubar.select_previous()
- property spacing
Get / set the spacing between menu entries, it needs to be an int.