async2v.components.pygame.gui module¶
Very simple and basic gui elements.
Gui elements have a fixed height and a minimal width. They can only be aligned in vertical stacked groups. Afterwards, all elements are drawn with the same width, which is the width of the widest element.
Summary¶
Classes:
Button |
Button to be part of a Menu |
GuiElement |
Abstract base class for gui elements |
Label |
Label to be part of a Menu |
Menu |
Container for gui elements |
Functions:
render_hud_text |
Draw text on the screen |
Reference¶
-
class
Menu
(elements, position=(0, 0), bgcolor=(0, 0, 0, 128), padding=4)¶ Bases:
object
Container for gui elements
Handles positioning and drawing of a vertical list of gui elements, such as
Label
orButton
. For buttons in the menu to be functional, mouse events need to be passed from the display drawing the menu.Example:
class MyDisplay(OpenCvDisplay): def __init__(self, source): super().__init__(source) self.mouse_event: Buffer[MouseEvent] = Buffer(EventBasedMouseHandler.MOUSE_EVENT) self.menu = Menu([ Label('Menu'), Button('Do something', self.handler), ], position=(1, 0)) def handler(self): # do something def draw(self, surface: pygame.Surface) -> List[MouseRegion]: regions = super().draw(surface) self.menu.handle_mouse_events(self.mouse_event.values) regions += self.menu.draw(surface) return regions
Parameters: - elements (
List
[GuiElement
]) – Gui elements to render. Will be arranged vertically from top to bottom. - position (
Tuple
[float
,float
]) – Relative position within the display from (0, 0) to (1, 1). (0.5, 0.5) for center. - bgcolor – Background color as RGB or RGBA
- padding (
float
) – Padding, defaults to 4.
-
draw
(surface)¶ Draw the menu on the given pygame surface.
Return type: List
[MouseRegion
]
-
handle_mouse_events
(events)¶ This method needs to be called for received mouse events to enable interactive elements like buttons.
- elements (
-
class
Label
(text, font=None, size=20, align=0.5, fgcolor=(255, 255, 255), bgcolor=None)¶ Bases:
async2v.components.pygame.gui.GuiElement
Label to be part of a
Menu
Parameters: - text (
str
) – Label text (can be multi-line) - font (
Optional
[Font
]) – Defaults to Bedstead if not set - size (
int
) – Font size - align (
float
) – Horizontal text position from 0 (left) to 1 (right) - fgcolor (
Tuple
) – Foreground color as RGB or RGBA - bgcolor (
Optional
[Tuple
]) – Background color as RGB or RGBA
- text (
-
class
Button
(text, action, font=None, size=20, align=0.5, fgcolor=(255, 255, 255), bgcolor=(64, 64, 64, 128), hlbgcolor=(128, 128, 128, 224))¶ Bases:
async2v.components.pygame.gui.Label
Button to be part of a
Menu
Parameters: - text (
str
) – Label text (can be multi-line) - action (
Callable
) – Click handler - font (
Optional
[Font
]) – Defaults to Bedstead if not set - size (
int
) – Font size - align (
float
) – Horizontal text position from 0 (left) to 1 (right) - fgcolor (
Tuple
) – Foreground color as RGB or RGBA - bgcolor (
Tuple
) – Background color as RGB or RGBA - hlbgcolor (
Tuple
) – Background color as RGB or RGBA when highlighted
- text (
-
render_hud_text
(surface, text, font=None, size=20, fgcolor=(255, 255, 255), bgcolor=None, position=(0, 0))¶ Draw text on the screen
This is a shorthand for drawing a
Menu
with exactly oneLabel
element.Parameters: - surface (
Surface
) – Surface to draw text on - text (
str
) – Hud text (can be multi-line) - font (
Optional
[Font
]) – Defaults to Bedstead if not set - size (
int
) – Font size - fgcolor – Foreground color as RGB or RGBA
- bgcolor – Background color as RGB or RGBA
- position (
Tuple
[float
,float
]) – Relative position within the display from (0, 0) to (1, 1). (0.5, 0.5) for center.
- surface (