MessageDialog

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

Bases: pygamelib.gfx.ui.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 constants.ALIGN_RIGHT, constants.ALIGN_LEFT or constants.ALIGN_CENTER. 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: int = None, title: str = None, config: pygamelib.gfx.ui.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 (int) – The alignment to apply to the data parameter. Please use the constants.ALIGN_* constants. The default value is constants.ALIGN_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=constants.BOLD),
        base.Text('----', core.Color(0,125,255), style=constants.BOLD),
        '',
    ],
    20,
    5,
    True,
    constants.ALIGN_CENTER,
)
msg.add_line('This is aligned on the right', constants.ALGIN_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
show() Show the dialog and execute the event loop.
add_line(data[, alignment]) Add a line to the message dialog.
render_to_buffer(buffer, row, column, …) Render the object into a display buffer (not a screen buffer).

Attributes

config Get and set the config object (UiConfig).
user_input Facility to store and retrieve the user input.
add_line(data, alignment=30000011) → 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 (constants.ALIGN_RIGHT | constants.ALIGN_LEFT | constants.ALIGN_CENTER) – 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.ALGIN_CENTER,
)
config

Get and set the config object (UiConfig).

height

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 into a display buffer (not a screen 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')
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.