Source code for libretro.api.input.mouse
"""
Mouse input types, buttons, and axis IDs.
Corresponds to the ``RETRO_DEVICE_ID_MOUSE_*`` constants in ``libretro.h``.
"""
from dataclasses import dataclass
from enum import IntEnum
from typing import Literal, overload
from .device import InputDeviceState
RETRO_DEVICE_ID_MOUSE_X = 0
RETRO_DEVICE_ID_MOUSE_Y = 1
RETRO_DEVICE_ID_MOUSE_LEFT = 2
RETRO_DEVICE_ID_MOUSE_RIGHT = 3
RETRO_DEVICE_ID_MOUSE_WHEELUP = 4
RETRO_DEVICE_ID_MOUSE_WHEELDOWN = 5
RETRO_DEVICE_ID_MOUSE_MIDDLE = 6
RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP = 7
RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN = 8
RETRO_DEVICE_ID_MOUSE_BUTTON_4 = 9
RETRO_DEVICE_ID_MOUSE_BUTTON_5 = 10
[docs]
class DeviceIdMouse(IntEnum):
"""
Input IDs for the mouse device.
Corresponds to the ``RETRO_DEVICE_ID_MOUSE_*`` constants in ``libretro.h``.
>>> from libretro.api.input import DeviceIdMouse
>>> DeviceIdMouse.LEFT
<DeviceIdMouse.LEFT: 2>
"""
X = RETRO_DEVICE_ID_MOUSE_X
Y = RETRO_DEVICE_ID_MOUSE_Y
LEFT = RETRO_DEVICE_ID_MOUSE_LEFT
RIGHT = RETRO_DEVICE_ID_MOUSE_RIGHT
WHEELUP = RETRO_DEVICE_ID_MOUSE_WHEELUP
WHEELDOWN = RETRO_DEVICE_ID_MOUSE_WHEELDOWN
MIDDLE = RETRO_DEVICE_ID_MOUSE_MIDDLE
HORIZ_WHEELUP = RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELUP
HORIZ_WHEELDOWN = RETRO_DEVICE_ID_MOUSE_HORIZ_WHEELDOWN
BUTTON_4 = RETRO_DEVICE_ID_MOUSE_BUTTON_4
BUTTON_5 = RETRO_DEVICE_ID_MOUSE_BUTTON_5
type DeviceIdMouseButton = Literal[
DeviceIdMouse.LEFT,
DeviceIdMouse.RIGHT,
DeviceIdMouse.MIDDLE,
DeviceIdMouse.BUTTON_4,
DeviceIdMouse.BUTTON_5,
]
type DeviceIdMouseWheel = Literal[
DeviceIdMouse.WHEELUP,
DeviceIdMouse.WHEELDOWN,
DeviceIdMouse.HORIZ_WHEELUP,
DeviceIdMouse.HORIZ_WHEELDOWN,
]
type DeviceIdMouseAxis = Literal[
DeviceIdMouse.X,
DeviceIdMouse.Y,
]
[docs]
@dataclass(frozen=True, slots=True)
class MouseState(InputDeviceState):
"""
Snapshot of mouse state.
>>> from libretro.api.input import MouseState
>>> state = MouseState()
>>> state.left
False
"""
x: int = 0
y: int = 0
left: bool = False
right: bool = False
wheel_up: bool = False
wheel_down: bool = False
middle: bool = False
horizontal_wheel_up: bool = False
horizontal_wheel_down: bool = False
button4: bool = False
button5: bool = False
@overload
def __getitem__(self, item: DeviceIdMouseAxis) -> int: ...
@overload
def __getitem__(self, item: DeviceIdMouseButton | DeviceIdMouseWheel) -> bool: ...
@overload
def __getitem__(self, item: int) -> bool | int: ...
[docs]
def __getitem__(self, item: DeviceIdMouse | int) -> bool | int:
"""
Get the state of a specific mouse input by its ID.
For example:
>>> from libretro.api.input import MouseState, DeviceIdMouse
>>> state = MouseState(x=123, left=True)
>>> state[DeviceIdMouse.X]
123
>>> state[DeviceIdMouse.LEFT]
True
:param item: The ID of the input to query, as a :class:`DeviceIdMouse` or integer.
:return: The state of the specified input,
as an :class:`int` for axes or :class:`bool` for buttons.
Note that wheel inputs are treated as buttons that are "pressed"
when the wheel is moved in the corresponding direction.
:raises KeyError: If ``item`` isn't a valid input ID.
:raises IndexError: If ``item`` is an integer but not a valid input ID
"""
match item:
case DeviceIdMouse.X:
return self.x
case DeviceIdMouse.Y:
return self.y
case DeviceIdMouse.LEFT:
return self.left
case DeviceIdMouse.RIGHT:
return self.right
case DeviceIdMouse.WHEELUP:
return self.wheel_up
case DeviceIdMouse.WHEELDOWN:
return self.wheel_down
case DeviceIdMouse.MIDDLE:
return self.middle
case DeviceIdMouse.HORIZ_WHEELUP:
return self.horizontal_wheel_up
case DeviceIdMouse.HORIZ_WHEELDOWN:
return self.horizontal_wheel_down
case DeviceIdMouse.BUTTON_4:
return self.button4
case DeviceIdMouse.BUTTON_5:
return self.button5
case int():
raise IndexError(f"Index {item!r} is not a valid DeviceIdMouse")
case _:
raise KeyError(f"Expected an int or DeviceIdMouse, got {item!r}")
__all__ = ["DeviceIdMouse", "MouseState"]