async2v.components.pygame.mouse module

Pygame based mouse input

For most use cases, the EventBasedMouseHandler is sufficient. It emits two kinds of mouse events, MouseMovement and MouseEvent. Those events include a MouseRegion where the event occurred. There is always at least one region, screen, which covers the whole screen. Additional regions can be returned from the draw method of displays. This is especially required for the Button from the gui module to handle click events.

When a new mouse event is received from pygame, the list of regions is searched in reverse order. The first region that contains the location of the mouse event is then passed with the emitted MouseMovement or MouseEvent event.

The root region, screen, is always the last resort if no other region matched before.

Some builtin display components (e.g. OpenCvDisplay or OpenCvMultiDisplay) also provide regions other than the root region.

The EventBasedMouseHandler needs no configuration, it just needs to be passed to the MainWindow:

class Launcher(ApplicationLauncher):

    def __init__(self):
        super().__init__()
        self.add_configurator(MainWindow.configurator())

    def register_application_components(self, args, app: Application):
        main_window_config = MainWindow.configurator().config_from_args(args)
        mouse_handler = EventBasedMouseHandler()
        displays = [
            OpenCvDebugDisplay(),
        ]
        main_window = MainWindow(displays, config=main_window_config, mouse_handler=mouse_handler)
        app.register(main_window)

For special use cases, the abstract MouseHandler can be implemented and used instead. Note that in this case you need to implement region handling yourself (if you require it).

Summary

Classes:

EventBasedMouseHandler Event based mouse handler
MouseButton
MouseEvent
MouseEventType
MouseHandler Abstract mouse handler base class
MouseMovement Mouse movement event
MouseRegion Rectangular area on the screen capturing mouse events

Reference

class MouseButton

Bases: enum.Enum

LEFT = 1
MIDDLE = 2
RIGHT = 3
WHEEL_UP = 4
WHEEL_DOWN = 5
BUTTON_6 = 6
BUTTON_7 = 7
BUTTON_8 = 8
BUTTON_9 = 9
class MouseEventType

Bases: enum.Enum

UP = 1

Mouse up

DOWN = 2

Mouse down

ENTER = 3

Mouse enters region

LEAVE = 4

Mouse leaves region

class MouseRegion(name, rect, original_size)

Bases: object

Rectangular area on the screen capturing mouse events

name = None
Type:str
rect = None
Type:pygame.Rect

Location of the mouse region on pygame surface

original_size = None
Type:Tuple[int, int]

Reference dimensions (width, height) to calculate mouse positions to (e.g. in coordinates of drawn OpenCV image)

class MouseMovement(region, position, movement, buttons)

Bases: object

Mouse movement event

region = None
Type:MouseRegion

Region this movement was registered on

position = None
Type:Tuple[int, int]

Position in pixels on pygame surface

movement = None
Type:Tuple[int, int]

Movement in pixels on pygame surface

buttons = None
Type:Dict[MouseButton, bool]

State of all mouse buttons (True for pressed buttons)

relative_position

Position in pixels within region

Return type:Tuple[int, int]
normalized_position

Position within region, normalized to coordinates between 0 and 1

Return type:Tuple[float, float]
restored_position

Position within region, normalized to the dimensions given in original_size

Return type:Tuple[int, int]
class MouseEvent(region, position, event_type, button=None)

Bases: object

region = None
Type:MouseRegion

Region this movement was registered on

position = None
Type:Tuple[int, int]

Position in pixels on pygame surface

event_type = None
Type:MouseEventType
button = None
Type:MouseButton

Button that was pressed or released if event_type is UP or DOWN, None otherwise

relative_position

Position in pixels within region

Return type:Tuple[int, int]
normalized_position

Position within region, normalized to coordinates between 0 and 1

Return type:Tuple[float, float]
restored_position

Position within region, normalized to the dimensions given in original_size

Return type:Tuple[int, int]
class MouseHandler

Bases: async2v.components.base.SubComponent

Abstract mouse handler base class

Override this class to implement advanced mouse handling. For most use cases, EventBasedMouseHandler should be sufficient.

push_regions(regions)

This method is used by the framework to push the current regions to the handler. You should not need to call this method from your production code. You need to override this method when implementing the MouseHandler.

push_button_down(position, button)

This method is used by the framework to push a MOUSEBUTTONDOWN event to the handler. You should not need to call this method from your production code. You need to override this method when implementing the MouseHandler.

push_button_up(position, button)

This method is used by the framework to push a MOUSEBUTTONUP event to the handler. You should not need to call this method from your production code. You need to override this method when implementing the MouseHandler.

push_movement(position, rel, buttons)

This method is used by the framework to push a MOUSEMOTION event to the handler. You should not need to call this method from your production code. You need to override this method when implementing the MouseHandler.

class EventBasedMouseHandler

Bases: async2v.components.pygame.mouse.MouseHandler

Event based mouse handler

This mouse handler emits events containing MouseEvent payload on the event key MOUSE_EVENT and events containing MouseMovement payload on the event key MOUSE_MOVEMENT.

Supports the MouseRegion concept explained above.

MOUSE_EVENT = 'async2v.mouse.event'
Type:str
MOUSE_MOVEMENT = 'async2v.mouse.movement'
Type:str