MessageDialog

class pygamelib.gfx.ui.MessageDialog(data: list = None, width: int = 20, height: int = None, adaptive_height: bool = True, alignment: Alignment = None, title: str = None, config: UiConfig = None)

Bases: Dialog

The message dialog is a popup that can display multiple lines of text.

It supports formatted text (base.Text), python strings, pygamelib.gfx.core.Sprixel, core.Sprite and more generally anything that can be rendered on screen (i.e: posess a render_to_buffer(self, buffer , row, column, buffer_height, buffer_width) method).

Each line can be aligned separately using one of the Alignment constants. Please see add_line().

It also implements the show() virtual method of Dialog. This method is blocking and has its own event loop. It does not return anything.

ESC or ENTER close the dialog.

For the moment, the full message dialog needs to be displayed on screen. There is no pagination, but it is going to be implemented in a future release.

As all dialogs it also has a user_input property that reflects the user input. It is not used here however.

Like all dialogs, it is automatically destroyed on exit of the show() method. It is also deleted from the screen buffer.

Todo

Implements pagination.

__init__(data: list = None, width: int = 20, height: int = None, adaptive_height: bool = True, alignment: Alignment = None, title: str = None, config: UiConfig = None) None
Parameters:
  • data (list) – A list of data to display inside the MessageDialog. Elements in the list can contain various data types like base.Text, python strings, pygamelib.gfx.core.Sprixel, core.Sprite

  • width (int) – The width of the message dialog widget (in number of screen cells).

  • height (int) – The height of the message dialog widget (in number of screen cells).

  • adaptive_height (bool) – If True, the dialog height will be automatically adapted to match the content size.

  • alignment (Alignment) – The alignment to apply to the data parameter. Please use the Alignment constants. The default value is pygamelib.constants.Alignment.LEFT

  • title (str) – The short title of the dialog. Only used when the dialog is not borderless.

  • config (UiConfig) – The configuration object.

Example:

msg = MessageDialog(
    [
        base.Text('HELP', core.Color(0,125,255), style=TextStyle.BOLD),
        base.Text('----', core.Color(0,125,255), style=TextStyle.BOLD),
        '',
    ],
    20,
    5,
    True,
    Alignment.CENTER,
)
msg.add_line('This is aligned on the right', Alignment.RIGHT)
msg.add_line('This is aligned on the left')
screen.place(msg, 10, 10)
msg.show()

Methods

__init__([data, width, height, ...])

param data:

A list of data to display inside the MessageDialog. Elements in

add_line(data[, alignment])

Add a line to the message dialog.

render_to_buffer(buffer, row, column, ...)

Render the object from the display buffer to the frame buffer.

show()

Show the dialog and execute the event loop.

Attributes

config

Get and set the config object (UiConfig).

height

Get and set the height of the message dialog, it has to be an int.

title

Get and set the title of the dialog, it has to be a str.

user_input

Facility to store and retrieve the user input.

add_line(data, alignment: Alignment = Alignment.LEFT) None

Add a line to the message dialog.

The line can be any type of data that can be rendered on screen. This means that any object that expose a render_to_buffer(self, buffer, row, column, buffer_height, buffer_width) method can be added as a “line”. Python strings are also obviously accepted.

Here is a non-exhaustive list of supported types:

Parameters:
  • data (various) – The data to add to the message dialog.

  • alignment (Alignment) – The alignment of the line to add.

Example:

msg.add_line(
    base.Text(
        'This is centered and very red',
        core.Color(255,0,0),
    ),
    constants.Alignment.CENTER,
)
property config

Get and set the config object (UiConfig).

property height: int

Get and set the height of the message dialog, it has to be an int.

render_to_buffer(buffer, row, column, buffer_height, buffer_width) 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.

show() None

Show the dialog and execute the event loop. Until this method returns, all keyboards event are processed by the local event loop. This is also true if called from the main event loop.

This event loop returns the key pressed .

Example:

key_pressed = msg.show()
if key_pressed.name = 'KEY_ENTER':
    // do something
else:
    print('Good bye')
property title: str

Get and set the title of the dialog, it has to be a str.

property user_input

Facility to store and retrieve the user input.