pygamelib
v1.3.x

Contents (API reference):

  • actuators
    • Actuator
    • Behavioral
    • PathActuator
    • PatrolActuator
    • PathFinder
    • RandomActuator
    • UnidirectionalActuator
  • assets
    • graphics
      • Blocks
      • BoxDrawings
      • GeometricShapes
      • MiscTechnicals
      • Models
    • Fonts
      • 8bits
        • How to use?
        • What does it look like?
      • figlet-caligraphy
        • How to use?
        • What does it look like?
        • More
      • figlet-doom
        • How to use?
        • What does it look like?
        • More
      • figlet-graffiti
        • How to use?
        • What does it look like?
        • More
      • figlet-mirror
        • How to use?
        • What does it look like?
        • More
      • figlet-pepper
        • How to use?
        • What does it look like?
        • More
      • figlet-poison
        • How to use?
        • What does it look like?
        • More
      • figlet-puffy
        • How to use?
        • What does it look like?
        • More
      • figlet-rounded
        • How to use?
        • What does it look like?
        • More
      • figlet-stampatello
        • How to use?
        • What does it look like?
        • More
      • figlet-univers
        • How to use?
        • What does it look like?
        • More
      • figlet-wavy
        • How to use?
        • What does it look like?
        • More
  • base
    • Console
    • Math
    • PglBaseObject
    • PglException
    • PglInvalidLevelException
    • PglInvalidTypeException
    • PglInventoryException
    • PglObjectIsNotMovableException
    • PglOutOfBoardBoundException
    • Text
    • Vector2D
    • Deprecated objects
      • HacException
      • HacInvalidLevelException
      • HacInvalidTypeException
      • HacObjectIsNotMovableException
      • HacOutOfBoardBoundException
  • board_items
    • Actionable
    • ActionableTile
    • BoardComplexItem
    • BoardItemComplexComponent
    • BoardItem
    • BoardItemVoid
    • Camera
    • Character
    • ComplexDoor
    • ComplexNPC
    • ComplexPlayer
    • ComplexTreasure
    • ComplexWall
    • Door
    • GenericActionableStructure
    • GenericStructureComplexComponent
    • GenericStructure
    • Immovable
    • Movable
    • NPC
    • Player
    • Projectile
    • TextItem
    • Tile
    • Treasure
    • Wall
  • constants
  • engine
    • Board
    • Game
    • Inventory
    • Screen
  • gfx
    • core
      • Animation
      • Font
      • SpriteCollection
      • Sprite
      • Sprixel
      • Color
    • ui
      • Box
      • ColorPickerDialog
      • ColorPicker
      • Dialog
      • FileDialog
      • GridSelectorDialog
      • GridSelector
      • LineInputDialog
      • Menu
      • MenuAction
      • MenuBar
      • MessageDialog
      • MultiLineInputDialog
      • ProgressBar
      • ProgressDialog
      • UiConfig
    • particles
      • CircleEmitter
      • ColorParticle
      • ColorPartitionParticle
      • EmitterProperties
      • ParticleEmitter
      • ParticlePool
      • Particle
      • ParticleSprixel
      • PartitionParticle
      • RandomColorParticle
      • RandomColorPartitionParticle
  • Credits
    • Development Lead
    • Contributors
  • Release notes
    • 1.3.0 (2022-10-07)
      • ⬆️ Main updates
      • ⚠️ Breaking changes
      • 🔧 Other changes
    • 1.2.3 (2020-09-01)
    • 1.2.2 (2020-09-01)
    • 1.2.0 (2020-08-29)
    • 1.1.1 (2020-07-18)
    • 1.1.0 (2020-06-12)
    • 1.0.1 (2020-05-17)
    • 1.0.0 (2020-03-20)
    • 2019.5
    • pre-2019.5
pygamelib
  • Docs »
  • gfx »
  • ui »
  • MenuBar
  • Edit on GitHub

MenuBar¶

class pygamelib.gfx.ui.MenuBar(entries: list = None, spacing: int = 2, config: pygamelib.gfx.ui.UiConfig = None)¶

Bases: object

The MenuBar widget is exactly that: an horizontal bar that can hold Menu or MenuAction 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: pygamelib.gfx.ui.UiConfig = None) → None¶

The constructor takes the following parameters.

Parameters:
  • entries (list) – A list of MenuAction or Menu 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.
current_entry() 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_next() Select the next element in the menubar.
select_previous() Select the previous element in the menubar.

Attributes

config Get / set the config of the MenuBar, it needs to be a UiConfig.
current_index Get / set the currently selected menu entry, it needs to be an int.
entries Get / set the entries of the MenuBar, it needs to be a list of MenuAction or Menu objects.
spacing 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 a Menu. 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 and MenuAction.

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.

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 a MenuAction object.

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.

entries¶

Get / set the entries of the MenuBar, it needs to be a list of MenuAction or Menu 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()
spacing¶

Get / set the spacing between menu entries, it needs to be an int.

Next Previous

© Copyright 2019-2022, Arnaud Dupuis Revision 683af6b9.

Built with Sphinx using a theme provided by Read the Docs.