""" \author Tristan Israël """
[docs]
class MouseWheel():
""" This class codes the mouse wheel movement """
NO_MOVE = 0
UP = 1
DOWN = -1
[docs]
class MouseMove():
"""
This class codes the mouse positing strategy used by the system
In relative positioning, an negative ``x`` value means that the mouse is moving left. A negative ``y`` value
means that the mouse is moving up.
"""
RELATIVE = 0
""" Used in systems with regular mouse. The coordinates are relative from the previous value. """
ABSOLUTE = 1
""" Used in systems with touch screens or touch pads. The coordinates are absolute to the screen and depends from the resolution. """
[docs]
class Mouse():
"""
This class encapsulates information about a mouse or a touch event.
The properties are public in order to make the changes quicker because the events are very numerous and
need to be sent very quickly.
"""
buttons = MouseButton.UNKNOWN
x:int = 0
y:int = 0
move = MouseMove.RELATIVE
wheel = MouseWheel.NO_MOVE
def __init__(self, move:MouseMove=MouseMove.RELATIVE, x:int=0, y:int=0, buttons:int=0, wheel:int=0):
self.x = x
self.y = y
self.move = move
self.buttons = buttons
self.wheel = wheel
[docs]
def equals(self, other) -> bool:
"""
Returns True if the position and the buttons are the same between this instance and another instance.
:param other: Another instance of Mouse
:type other: Mouse
"""
return (other.x == self.x
and other.y == self.y
and other.buttons == self.buttons
and other.wheel == self.wheel)
[docs]
def wheel_equals(self, other) -> bool:
"""
Returns True if the wheel state is the same between this instance and another one.
:param other: Another instance of Mouse
:type other: Mouse
"""
return self.wheel == other.wheel
[docs]
def wheel_moved(self) -> bool:
"""
Returns True if the wheel is moved
"""
return self.wheel != MouseWheel.NO_MOVE
[docs]
def serialize(self) -> bytes:
"""
Serializes this instance's data in order to send it on a serial bus.
"""
return f"{self.move},{self.x},{self.y},{self.buttons},{self.wheel}".encode()
[docs]
@staticmethod
def from_data(data: bytes):
"""
Deserializes the data received from a serial bus in this instance.
"""
if data is None:
return None
if data.count(b',') < 4:
return None
s = data.split(b',')
return Mouse(int(s[0]), int(s[1]), int(s[2]), int(s[3]), int(s[4]))