GridSelectorο
- class pygamelib.gfx.ui.GridSelector(choices: list = None, width: int = None, height: int = None, minimum_width: int = None, minimum_height: int = None, maximum_width: int = 5, maximum_height: int = 10, config: UiConfig | None = None)ο
Bases:
WidgetThe GridSelector is a widget that present a list of elements as a grid to the user.
It also provides the API to draw and manage the cursor and to retrieve the selected element.
Warning
In the first version of that widget, only the characters that have a length of 1 are supported. This excludes some UTF8 characters and most of the emojis.
- __init__(choices: list = None, width: int = None, height: int = None, minimum_width: int = None, minimum_height: int = None, maximum_width: int = 5, maximum_height: int = 10, config: UiConfig | None = None) Noneο
- Parameters:
choices (list) β A list of choices to present to the user. The elements of the list needs to be str or
Sprixel.minimum_width (int) β The minimum width of the GridSelector.
minimum_height (int) β The minimum height of the GridSelector.
maximum_width (int) β The maximum width of the GridSelector.
maximum_height (int) β The maximum height of the GridSelector.
config (
UiConfig) β The configuration object.
Example:
choices = ["@","#","$","%","&","*","[","]"] grid_selector = GridSelector( choices, maximum_width=30, maximum_height=10, config=conf ) screen.place(grid_selector, 10, 10) screen.update()
Methods
__init__([choices,Β width,Β height,Β ...])Returns the currently selected sprixel.
Move the selection cursor one row down.
Move the selection cursor one column to the left.
Move the selection cursor one column to the right.
Move the selection cursor one row up.
Returns the number of items per page.
nb_pages()Returns the number of pages.
Change the current page to the one immediately down (current_page + 1).
page_up()Change the current page to the one immediately up (current_page - 1).
render_to_buffer(buffer,Β row,Β column,Β ...)Render the object from the display buffer to the frame buffer.
Attributes
Get and set the list of choices, it has to be a list of
Sprixelor str.Get and set the currently selected item's index (the current choice), it needs to be an int.
Get and set the current page of the grid selector, it needs to be an int.
- attach(observer)ο
Attach an observer to this instance. It means that until it is detached, it will be notified every time that a notification is issued (usually on changes).
An object cannot add itself to the list of observers (to avoid infinite recursions).
- Parameters:
observer (
PglBaseObject) β An observer to attach to this object.- Returns:
True or False depending on the success of the operation.
- Return type:
bool
Example:
myboard = Board() screen = Game.instance().screen # screen will be notified of all changes in myboard myboard.attach(screen)
- property bg_color: Colorο
This property get/set the background color of the widget.
When the color is changed the pygamelib.gfx.ui.Widget.bg_color:changed event is sent to the observers.
- property children: Set[Widget]ο
This read only property property returns the list of children widgets.
- property current_choice: intο
Get and set the currently selected itemβs index (the current choice), it needs to be an int. Use
current_sprixel()to get the actual current item.
- property current_page: intο
Get and set the current page of the grid selector, it needs to be an int.
- cursor_down() Noneο
Move the selection cursor one row down.
- cursor_left() Noneο
Move the selection cursor one column to the left.
- cursor_right() Noneο
Move the selection cursor one column to the right.
- cursor_up() Noneο
Move the selection cursor one row up.
- detach(observer)ο
Detach an observer from this instance. If observer is not in the list this returns False.
- Parameters:
observer (
PglBaseObject) β An observer to detach from this object.- Returns:
True or False depending on the success of the operation.
- Return type:
bool
Example:
# screen will no longer be notified of the changes in myboard. myboard.detach(screen)
- property focus: boolο
This property get/set the focus property. It is a boolean.
At the moment it is mostly an informational property, to tell the programmer and potentially the Widget user (i.e: the class inheriting from Widget) about its own state.
- handle_notification(subject, attribute=None, value=None)ο
A virtual method that needs to be implemented by the observer. By default it does nothing but each observer needs to implement it if something needs to be done when notified.
This method always receive the notifying object as first parameter. The 2 other parameters are optional and can be None.
You can use the attribute and value as you see fit. You are free to consider attribute as an event and value as the eventβs value.
- Parameters:
subject (
PglBaseObject) β The object that has changed.attribute (str) β The attribute that has changed, it is usually a βFQDN styleβ string. This can be None.
value (Any) β The new value of the attribute. This can be None.
- property height: intο
This property get/set the height of the widget. This property respects the boundaries set by the maximum_height and minimum_height properties.
When the height is changed the pygamelib.gfx.ui.Widget.resizeEvent:height event is sent to the observers.
- items_per_page() intο
Returns the number of items per page.
- property layout: Layout | Noneο
This property get/set the layout of the widget. You can then add sub widgets to the layout.
This must be a
Layoutor a class that inherits from it.When the layout is changed the pygamelib.gfx.ui.Widget.layout:changed event is sent to the observers.
- property maximum_height: intο
This property get/set the maximum height of the widget. This property is used when changing the size constraints and the height property.
- property maximum_width: intο
This property get/set the maximum width of the widget. This property is used when changing the size constraints and the width property.
- property minimum_height: intο
This property get/set the minimum height of the widget. This property is used when changing the size constraints and the height property.
- property minimum_width: intο
This property get/set the minimum width of the widget. This property is used when changing the size constraints and the width property.
- nb_pages() intο
Returns the number of pages.
- notify(modifier: PglBaseObject | None = None, attribute: str | None = None, value: Any | None = None) Noneο
Notify all the observers that a change occurred.
- Parameters:
modifier (
PglBaseObject) β An optional parameter that identify the modifier object to exclude it from the notified objects.attribute (str) β An optional parameter that identify the attribute that has changed.
value (Any) β An optional parameter that identify the new value of the attribute.
Example:
# This example is silly, you would usually notify other objects from inside # an object that changes a value that's important for the observers. color = Color(255,200,125) color.attach(some_text_object) color.notify()
- page_down() Noneο
Change the current page to the one immediately down (current_page + 1).
- page_up() Noneο
Change the current page to the one immediately up (current_page - 1).
- post_processing(attribute)ο
This virtual method allows for subclasses to perform their own operations when a specific attribute is changed
- Parameters:
attribute β The attribute that has changed. This is a string containing
the name of the changed attribute :type attribute: str
- 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.
- property screen_column: intο
A property to get/set the screen column.
- Parameters:
value (int) β the screen column
- Return type:
int
- property screen_row: intο
A property to get/set the screen row.
- Parameters:
value (int) β the screen row
- Return type:
int
- property size_constraint: SizeConstraintο
This property get/set the size constraints of the widget. Changing the size constraints immediately resize the widget.
- store_screen_position(row: int, column: int) boolο
Store the screen position of the object.
This method is automatically called by Screen.place().
- Parameters:
row (int) β The row (or y) coordinate.
column (int) β The column (or x) coordinate.
Example:
an_object.store_screen_coordinate(3,8)
- property width: intο
This property get/set the width of the widget. This property respects the boundaries set by the maximum_width and minimum_width properties.
When the width is changed the pygamelib.gfx.ui.Widget.resizeEvent:width event is sent to the observers.
- property x: intο
This property get/set the x position of the widget on screen. Since a Widget is a
PglBaseObjectthis is an alias for the screen_column property.
- property y: intο
This property get/set the y position of the widget on screen. Since a Widget is a
PglBaseObjectthis is an alias for the screen_row property.