sync pyglet

This commit is contained in:
shenjack 2023-11-13 20:28:44 +08:00
parent b3c52d2a3f
commit 2289d40a14
Signed by: shenjack
GPG Key ID: 7B1134A979775551
2 changed files with 58 additions and 203 deletions

View File

@ -621,7 +621,7 @@ def get_apple_remote(display=None):
def _create_controller(device, display): def _create_controller(device, display):
if not device.transport and device.transport.upper() in ('USB', 'BLUETOOTH'): if not device.transport or not device.transport.upper() in ('USB', 'BLUETOOTH'):
return return
if device.is_joystick() or device.is_gamepad() or device.is_multi_axis(): if device.is_joystick() or device.is_gamepad() or device.is_multi_axis():

View File

@ -25,7 +25,6 @@ from collections.abc import Iterable as _Iterable
from collections.abc import Iterator as _Iterator from collections.abc import Iterator as _Iterator
number = _typing.Union[float, int]
Mat3T = _typing.TypeVar("Mat3T", bound="Mat3") Mat3T = _typing.TypeVar("Mat3T", bound="Mat3")
Mat4T = _typing.TypeVar("Mat4T", bound="Mat4") Mat4T = _typing.TypeVar("Mat4T", bound="Mat4")
@ -39,7 +38,7 @@ class Vec2:
"""A two-dimensional vector represented as an X Y coordinate pair.""" """A two-dimensional vector represented as an X Y coordinate pair."""
def __init__(self, x: number = 0.0, y: number = 0.0) -> None: def __init__(self, x: float = 0.0, y: float = 0.0) -> None:
self.x = x self.x = x
self.y = y self.y = y
@ -74,13 +73,13 @@ class Vec2:
def __sub__(self, other: Vec2) -> Vec2: def __sub__(self, other: Vec2) -> Vec2:
return Vec2(self.x - other.x, self.y - other.y) return Vec2(self.x - other.x, self.y - other.y)
def __mul__(self, scalar: number) -> Vec2: def __mul__(self, scalar: float) -> Vec2:
return Vec2(self.x * scalar, self.y * scalar) return Vec2(self.x * scalar, self.y * scalar)
def __truediv__(self, scalar: number) -> Vec2: def __truediv__(self, scalar: float) -> Vec2:
return Vec2(self.x / scalar, self.y / scalar) return Vec2(self.x / scalar, self.y / scalar)
def __floordiv__(self, scalar: number) -> Vec2: def __floordiv__(self, scalar: float) -> Vec2:
return Vec2(self.x // scalar, self.y // scalar) return Vec2(self.x // scalar, self.y // scalar)
def __abs__(self) -> float: def __abs__(self) -> float:
@ -115,9 +114,6 @@ class Vec2:
The magnitude of the vector. The magnitude of the vector.
`angle` : int or float : `angle` : int or float :
The angle of the vector in radians. The angle of the vector in radians.
:returns: A new vector with the given angle and magnitude.
:rtype: Vec2
""" """
return Vec2(mag * _math.cos(angle), mag * _math.sin(angle)) return Vec2(mag * _math.cos(angle), mag * _math.sin(angle))
@ -128,9 +124,6 @@ class Vec2:
:parameters: :parameters:
`magnitude` : int or float : `magnitude` : int or float :
The magnitude of the new vector. The magnitude of the new vector.
:returns: A new vector with the magnitude.
:rtype: Vec2
""" """
return self.normalize() * magnitude return self.normalize() * magnitude
@ -140,19 +133,13 @@ class Vec2:
:parameters: :parameters:
`heading` : int or float : `heading` : int or float :
The angle of the new vector in radians. The angle of the new vector in radians.
:returns: A new vector with the given heading.
:rtype: Vec2
""" """
mag = self.__abs__() mag = self.__abs__()
return Vec2(mag * _math.cos(heading), mag * _math.sin(heading)) return Vec2(mag * _math.cos(heading), mag * _math.sin(heading))
@property @property
def heading(self) -> float: def heading(self) -> float:
"""The angle of the vector in radians. """The angle of the vector in radians."""
:type: float
"""
return _math.atan2(self.y, self.x) return _math.atan2(self.y, self.x)
@property @property
@ -160,21 +147,11 @@ class Vec2:
"""The magnitude, or length of the vector. The distance between the coordinates and the origin. """The magnitude, or length of the vector. The distance between the coordinates and the origin.
Alias of abs(self). Alias of abs(self).
:type: float
""" """
return self.__abs__() return self.__abs__()
def limit(self, maximum: float) -> Vec2: def limit(self, maximum: float) -> Vec2:
"""Limit the magnitude of the vector to the value used for the max parameter. """Limit the magnitude of the vector to passed maximum value."""
:parameters:
`maximum` : int or float :
The maximum magnitude for the vector.
:returns: Either self or a new vector with the maximum magnitude.
:rtype: Vec2
"""
if self.x ** 2 + self.y ** 2 > maximum * maximum: if self.x ** 2 + self.y ** 2 > maximum * maximum:
return self.from_magnitude(maximum) return self.from_magnitude(maximum)
return self return self
@ -189,9 +166,6 @@ class Vec2:
The amount of interpolation. The amount of interpolation.
Some value between 0.0 (this vector) and 1.0 (other vector). Some value between 0.0 (this vector) and 1.0 (other vector).
0.5 is halfway inbetween. 0.5 is halfway inbetween.
:returns: A new interpolated vector.
:rtype: Vec2
""" """
return Vec2(self.x + (alpha * (other.x - self.x)), return Vec2(self.x + (alpha * (other.x - self.x)),
self.y + (alpha * (other.y - self.y))) self.y + (alpha * (other.y - self.y)))
@ -201,15 +175,7 @@ class Vec2:
return self - normal * 2 * normal.dot(self) return self - normal * 2 * normal.dot(self)
def rotate(self, angle: float) -> Vec2: def rotate(self, angle: float) -> Vec2:
"""Create a new Vector rotated by the angle. The magnitude remains unchanged. """Create a new Vector rotated by the angle. The magnitude remains unchanged."""
:parameters:
`angle` : int or float :
The angle to rotate by
:returns: A new rotated vector of the same magnitude.
:rtype: Vec2
"""
s = _math.sin(angle) s = _math.sin(angle)
c = _math.cos(angle) c = _math.cos(angle)
return Vec2(c * self.x - s * self.y, s * self.x + c * self.y) return Vec2(c * self.x - s * self.y, s * self.x + c * self.y)
@ -219,40 +185,18 @@ class Vec2:
return _math.sqrt(((other.x - self.x) ** 2) + ((other.y - self.y) ** 2)) return _math.sqrt(((other.x - self.x) ** 2) + ((other.y - self.y) ** 2))
def normalize(self) -> Vec2: def normalize(self) -> Vec2:
"""Normalize the vector to have a magnitude of 1. i.e. make it a unit vector. """Normalize the vector to have a magnitude of 1. i.e. make it a unit vector."""
:returns: A unit vector with the same heading.
:rtype: Vec2
"""
d = self.__abs__() d = self.__abs__()
if d: if d:
return Vec2(self.x / d, self.y / d) return Vec2(self.x / d, self.y / d)
return self return self
def clamp(self, min_val: float, max_val: float) -> Vec2: def clamp(self, min_val: float, max_val: float) -> Vec2:
"""Restrict the value of the X and Y components of the vector to be within the given values. """Restrict the value of the X and Y components of the vector to be within the given values."""
:parameters:
`min_val` : int or float :
The minimum value
`max_val` : int or float :
The maximum value
:returns: A new vector with clamped X and Y components.
:rtype: Vec2
"""
return Vec2(clamp(self.x, min_val, max_val), clamp(self.y, min_val, max_val)) return Vec2(clamp(self.x, min_val, max_val), clamp(self.y, min_val, max_val))
def dot(self, other: Vec2) -> float: def dot(self, other: Vec2) -> float:
"""Calculate the dot product of this vector and another 2D vector. """Calculate the dot product of this vector and another 2D vector."""
:parameters:
`other` : Vec2 :
The other vector.
:returns: The dot product of the two vectors.
:rtype: float
"""
return self.x * other.x + self.y * other.y return self.x * other.x + self.y * other.y
def __getattr__(self, attrs: str) -> Vec2 | Vec3 | Vec4: def __getattr__(self, attrs: str) -> Vec2 | Vec3 | Vec4:
@ -274,7 +218,7 @@ class Vec3:
"""A three-dimensional vector represented as X Y Z coordinates.""" """A three-dimensional vector represented as X Y Z coordinates."""
def __init__(self, x: number = 0.0, y: number = 0.0, z: number = 0.0) -> None: def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0) -> None:
self.x = x self.x = x
self.y = y self.y = y
self.z = z self.z = z
@ -321,13 +265,13 @@ class Vec3:
def __sub__(self, other: Vec3) -> Vec3: def __sub__(self, other: Vec3) -> Vec3:
return Vec3(self.x - other.x, self.y - other.y, self.z - other.z) return Vec3(self.x - other.x, self.y - other.y, self.z - other.z)
def __mul__(self, scalar: number) -> Vec3: def __mul__(self, scalar: float) -> Vec3:
return Vec3(self.x * scalar, self.y * scalar, self.z * scalar) return Vec3(self.x * scalar, self.y * scalar, self.z * scalar)
def __truediv__(self, scalar: number) -> Vec3: def __truediv__(self, scalar: float) -> Vec3:
return Vec3(self.x / scalar, self.y / scalar, self.z / scalar) return Vec3(self.x / scalar, self.y / scalar, self.z / scalar)
def __floordiv__(self, scalar: number) -> Vec3: def __floordiv__(self, scalar: float) -> Vec3:
return Vec3(self.x // scalar, self.y // scalar, self.z // scalar) return Vec3(self.x // scalar, self.y // scalar, self.z // scalar)
def __abs__(self) -> float: def __abs__(self) -> float:
@ -355,94 +299,44 @@ class Vec3:
def from_magnitude(self, magnitude: float) -> Vec3: def from_magnitude(self, magnitude: float) -> Vec3:
"""Create a new Vector of the given magnitude by normalizing, """Create a new Vector of the given magnitude by normalizing,
then scaling the vector. The rotation remains unchanged. then scaling the vector. The rotation remains unchanged.
:parameters:
`magnitude` : int or float :
The magnitude of the new vector.
:returns: A new vector with the magnitude.
:rtype: Vec3
""" """
return self.normalize() * magnitude return self.normalize() * magnitude
def limit(self, maximum: float) -> Vec3: def limit(self, maximum: float) -> Vec3:
"""Limit the magnitude of the vector to the value used for the max parameter. """Limit the magnitude of the vector to the passed maximum value."""
:parameters:
`maximum` : int or float :
The maximum magnitude for the vector.
:returns: Either self or a new vector with the maximum magnitude.
:rtype: Vec3
"""
if self.x ** 2 + self.y ** 2 + self.z ** 2 > maximum * maximum * maximum: if self.x ** 2 + self.y ** 2 + self.z ** 2 > maximum * maximum * maximum:
return self.from_magnitude(maximum) return self.from_magnitude(maximum)
return self return self
def cross(self, other: Vec3) -> Vec3: def cross(self, other: Vec3) -> Vec3:
"""Calculate the cross product of this vector and another 3D vector. """Calculate the cross product of this vector and another 3D vector."""
:parameters:
`other` : Vec3 :
The other vector.
:returns: The cross product of the two vectors.
:rtype: float
"""
return Vec3((self.y * other.z) - (self.z * other.y), return Vec3((self.y * other.z) - (self.z * other.y),
(self.z * other.x) - (self.x * other.z), (self.z * other.x) - (self.x * other.z),
(self.x * other.y) - (self.y * other.x)) (self.x * other.y) - (self.y * other.x))
def dot(self, other: Vec3) -> float: def dot(self, other: Vec3) -> float:
"""Calculate the dot product of this vector and another 3D vector. """Calculate the dot product of this vector and another 3D vector."""
:parameters:
`other` : Vec3 :
The other vector.
:returns: The dot product of the two vectors.
:rtype: float
"""
return self.x * other.x + self.y * other.y + self.z * other.z return self.x * other.x + self.y * other.y + self.z * other.z
def lerp(self, other: Vec3, alpha: float) -> Vec3: def lerp(self, other: Vec3, alpha: float) -> Vec3:
"""Create a new Vec3 linearly interpolated between this vector and another Vec3. """Create a new Vec3 linearly interpolated between this vector and another Vec3.
:parameters: The `alpha` parameter dictates the amount of interpolation.
`other` : Vec3 : This should be a value between 0.0 (this vector) and 1.0 (other vector).
The vector to linearly interpolate with. For example; 0.5 is the midway point between both vectors.
`alpha` : float or int :
The amount of interpolation.
Some value between 0.0 (this vector) and 1.0 (other vector).
0.5 is halfway inbetween.
:returns: A new interpolated vector.
:rtype: Vec3
""" """
return Vec3(self.x + (alpha * (other.x - self.x)), return Vec3(self.x + (alpha * (other.x - self.x)),
self.y + (alpha * (other.y - self.y)), self.y + (alpha * (other.y - self.y)),
self.z + (alpha * (other.z - self.z))) self.z + (alpha * (other.z - self.z)))
def distance(self, other: Vec3) -> float: def distance(self, other: Vec3) -> float:
"""Calculate the distance between this vector and another 3D vector. """Get the distance between this vector and another 3D vector."""
:parameters:
`other` : Vec3 :
The other vector
:returns: The distance between the two vectors.
:rtype: float
"""
return _math.sqrt(((other.x - self.x) ** 2) + return _math.sqrt(((other.x - self.x) ** 2) +
((other.y - self.y) ** 2) + ((other.y - self.y) ** 2) +
((other.z - self.z) ** 2)) ((other.z - self.z) ** 2))
def normalize(self) -> Vec3: def normalize(self) -> Vec3:
"""Normalize the vector to have a magnitude of 1. i.e. make it a unit vector. """Normalize the vector to have a magnitude of 1. i.e. make it a unit vector."""
:returns: A unit vector with the same rotation.
:rtype: Vec3
"""
try: try:
d = self.__abs__() d = self.__abs__()
return Vec3(self.x / d, self.y / d, self.z / d) return Vec3(self.x / d, self.y / d, self.z / d)
@ -450,17 +344,7 @@ class Vec3:
return self return self
def clamp(self, min_val: float, max_val: float) -> Vec3: def clamp(self, min_val: float, max_val: float) -> Vec3:
"""Restrict the value of the X, Y and Z components of the vector to be within the given values. """Restrict the value of the X, Y and Z components of the vector to be within the given values."""
:parameters:
`min_val` : int or float :
The minimum value
`max_val` : int or float :
The maximum value
:returns: A new vector with clamped X, Y and Z components.
:rtype: Vec3
"""
return Vec3(clamp(self.x, min_val, max_val), return Vec3(clamp(self.x, min_val, max_val),
clamp(self.y, min_val, max_val), clamp(self.y, min_val, max_val),
clamp(self.z, min_val, max_val)) clamp(self.z, min_val, max_val))
@ -484,7 +368,7 @@ class Vec4:
"""A four-dimensional vector represented as X Y Z W coordinates.""" """A four-dimensional vector represented as X Y Z W coordinates."""
def __init__(self, x: number = 0.0, y: number = 0.0, z: number = 0.0, w: number = 0.0) -> None: def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0, w: float = 0.0) -> None:
self.x = x self.x = x
self.y = y self.y = y
self.z = z self.z = z
@ -523,13 +407,13 @@ class Vec4:
def __sub__(self, other: Vec4) -> Vec4: def __sub__(self, other: Vec4) -> Vec4:
return Vec4(self.x - other.x, self.y - other.y, self.z - other.z, self.w - other.w) return Vec4(self.x - other.x, self.y - other.y, self.z - other.z, self.w - other.w)
def __mul__(self, scalar: number) -> Vec4: def __mul__(self, scalar: float) -> Vec4:
return Vec4(self.x * scalar, self.y * scalar, self.z * scalar, self.w * scalar) return Vec4(self.x * scalar, self.y * scalar, self.z * scalar, self.w * scalar)
def __truediv__(self, scalar: number) -> Vec4: def __truediv__(self, scalar: float) -> Vec4:
return Vec4(self.x / scalar, self.y / scalar, self.z / scalar, self.w / scalar) return Vec4(self.x / scalar, self.y / scalar, self.z / scalar, self.w / scalar)
def __floordiv__(self, scalar: number) -> Vec4: def __floordiv__(self, scalar: float) -> Vec4:
return Vec4(self.x // scalar, self.y // scalar, self.z // scalar, self.w // scalar) return Vec4(self.x // scalar, self.y // scalar, self.z // scalar, self.w // scalar)
def __abs__(self) -> float: def __abs__(self) -> float:
@ -568,16 +452,9 @@ class Vec4:
def lerp(self, other: Vec4, alpha: float) -> Vec4: def lerp(self, other: Vec4, alpha: float) -> Vec4:
"""Create a new Vec4 linearly interpolated between this one and another Vec4. """Create a new Vec4 linearly interpolated between this one and another Vec4.
:parameters: The `alpha` parameter dictates the amount of interpolation.
`other` : Vec4 : This should be a value between 0.0 (this vector) and 1.0 (other vector).
The vector to linearly interpolate with. For example; 0.5 is the midway point between both vectors.
`alpha` : float or int :
The amount of interpolation.
Some value between 0.0 (this vector) and 1.0 (other vector).
0.5 is halfway inbetween.
:returns: A new interpolated vector.
:rtype: Vec4
""" """
return Vec4(self.x + (alpha * (other.x - self.x)), return Vec4(self.x + (alpha * (other.x - self.x)),
self.y + (alpha * (other.y - self.y)), self.y + (alpha * (other.y - self.y)),
@ -621,25 +498,19 @@ class Vec4:
class Mat3(tuple): class Mat3(tuple):
"""A 3x3 Matrix class """A 3x3 Matrix
`Mat3` is an immutable 3x3 Matrix, including most common `Mat3` is an immutable 3x3 Matrix, including most common
operators. Matrix multiplication must be performed using operators.
the "@" operator.
A Matrix can be created with a list or tuple of 12 values.
If no values are provided, an "identity matrix" will be created
(1.0 on the main diagonal). Mat3 objects are immutable, so
all operations return a new Mat3 object.
.. note:: Matrix multiplication is performed using the "@" operator.
""" """
def __new__(cls: type[Mat3T], values: _Iterable[float] = (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)) -> Mat3T: def __new__(cls: type[Mat3T], values: _Iterable[float] = (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)) -> Mat3T:
"""Create a 3x3 Matrix
A Mat3 can be created with a list or tuple of 9 values.
If no values are provided, an "identity matrix" will be created
(1.0 on the main diagonal). Matrix objects are immutable, so
all operations return a new Mat3 object.
:Parameters:
`values` : tuple of float or int
A tuple or list containing 9 floats or ints.
"""
new = super().__new__(cls, values) new = super().__new__(cls, values)
assert len(new) == 9, "A 3x3 Matrix requires 9 values" assert len(new) == 9, "A 3x3 Matrix requires 9 values"
return new return new
@ -720,25 +591,23 @@ class Mat3(tuple):
class Mat4(tuple): class Mat4(tuple):
"""A 4x4 Matrix
`Mat4` is an immutable 4x4 Matrix, which includs most common
operators. This includes class methods for creating orthogonal
and perspective projection matrixes, to be used by OpenGL.
A Matrix can be created with a list or tuple of 16 values.
If no values are provided, an "identity matrix" will be created
(1.0 on the main diagonal). Mat4 objects are immutable, so
all operations return a new Mat4 object.
.. note:: Matrix multiplication is performed using the "@" operator.
"""
def __new__(cls: type[Mat4T], values: _Iterable[float] = (1.0, 0.0, 0.0, 0.0, def __new__(cls: type[Mat4T], values: _Iterable[float] = (1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,)) -> Mat4T: 0.0, 0.0, 0.0, 1.0,)) -> Mat4T:
"""Create a 4x4 Matrix.
`Mat4` is an immutable 4x4 Matrix, which includs most common
operators. This includes class methods for creating orthogonal
and perspective projection matrixes, to be used by OpenGL.
A Matrix can be created with a list or tuple of 16 values.
If no values are provided, an "identity matrix" will be created
(1.0 on the main diagonal). Matrix objects are immutable, so
all operations return a new Mat4 object.
.. note:: Matrix multiplication is performed using the "@" operator.
"""
new = super().__new__(cls, values) new = super().__new__(cls, values)
assert len(new) == 16, "A 4x4 Matrix requires 16 values" assert len(new) == 16, "A 4x4 Matrix requires 16 values"
return new return new
@ -797,24 +666,12 @@ class Mat4(tuple):
@classmethod @classmethod
def from_rotation(cls, angle: float, vector: Vec3) -> Mat4: def from_rotation(cls, angle: float, vector: Vec3) -> Mat4:
"""Create a rotation matrix from an angle and Vec3. """Create a rotation matrix from an angle and Vec3."""
:Parameters:
`angle` : A `float` :
The angle as a float.
`vector` : A `Vec3`, or 3 component tuple of float or int :
Vec3 or tuple with x, y and z translation values
"""
return cls().rotate(angle, vector) return cls().rotate(angle, vector)
@classmethod @classmethod
def from_scale(cls: type[Mat4T], vector: Vec3) -> Mat4T: def from_scale(cls: type[Mat4T], vector: Vec3) -> Mat4T:
"""Create a scale matrix from a Vec3. """Create a scale matrix from a Vec3."""
:Parameters:
`vector` : A `Vec3`, or 3 component tuple of float or int
Vec3 or tuple with x, y and z scale values
"""
return cls((vector[0], 0.0, 0.0, 0.0, return cls((vector[0], 0.0, 0.0, 0.0,
0.0, vector[1], 0.0, 0.0, 0.0, vector[1], 0.0, 0.0,
0.0, 0.0, vector[2], 0.0, 0.0, 0.0, vector[2], 0.0,
@ -822,12 +679,7 @@ class Mat4(tuple):
@classmethod @classmethod
def from_translation(cls: type[Mat4T], vector: Vec3) -> Mat4T: def from_translation(cls: type[Mat4T], vector: Vec3) -> Mat4T:
"""Create a translation matrix from a Vec3. """Create a translation matrix from a Vec3."""
:Parameters:
`vector` : A `Vec3`, or 3 component tuple of float or int
Vec3 or tuple with x, y and z translation values
"""
return cls((1.0, 0.0, 0.0, 0.0, return cls((1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0,
@ -1113,5 +965,8 @@ class Quaternion(tuple):
def __invert__(self) -> Quaternion: def __invert__(self) -> Quaternion:
raise NotImplementedError("Not yet implemented") raise NotImplementedError("Not yet implemented")
def __matmul__(self, other):
raise NotImplementedError("Not yet implemented")
def __repr__(self) -> str: def __repr__(self) -> str:
return f"{self.__class__.__name__}(w={self[0]}, x={self[1]}, y={self[2]}, z={self[3]})" return f"{self.__class__.__name__}(w={self[0]}, x={self[1]}, y={self[2]}, z={self[3]})"