Source code for tessella.constraints
import pygame
[docs]
class WidgetConstraints:
"""
Describes the layout constraints passed down from a parent widget to \
its child during the layout pass, bounding the position and the \
minimum/maximum size a widget is allowed to take.
Attributes:
position_x (int): The X coordinate available to the widget, in pixels.
position_y (int): The Y coordinate available to the widget, in pixels.
min_width (int): The minimum width the widget is allowed to take, in pixels.
min_height (int): The minimum height the widget is allowed to take, in pixels.
max_width (int): The maximum width the widget is allowed to take, in pixels.
max_height (int): The maximum height the widget is allowed to take, in pixels.
rect (pygame.Rect): The rect described by `position_x`, `position_y`, \
`max_width` and `max_height`.
"""
def __init__(
self,
position_x: int = 0,
position_y: int = 0,
min_width: int = 0,
min_height: int = 0,
max_width: int = 0,
max_height: int = 0
):
"""
Initializes a new WidgetConstraints object.
Args:
position_x (int): The X coordinate available to the widget, in \
pixels. Defaults to 0.
position_y (int): The Y coordinate available to the widget, in \
pixels. Defaults to 0.
min_width (int): The minimum width the widget is allowed to \
take, in pixels. Defaults to 0.
min_height (int): The minimum height the widget is allowed to \
take, in pixels. Defaults to 0.
max_width (int): The maximum width the widget is allowed to \
take, in pixels. Defaults to 0.
max_height (int): The maximum height the widget is allowed to \
take, in pixels. Defaults to 0.
"""
self.position_x: int = position_x
self.position_y: int = position_y
self.min_width: int = min_width
self.min_height: int = min_height
self.max_width: int = max_width
self.max_height: int = max_height
self.rect: pygame.Rect = pygame.Rect(
self.position_x,
self.position_y,
self.max_width,
self.max_height
)
[docs]
@classmethod
def from_rect(cls, rect: pygame.Rect):
"""
A constructor for this class which derives the constraints from a \
`pygame.Rect`, using its position as the origin and its size as \
the maximum bounds, with no minimum size.
Args:
rect (pygame.Rect): The rect to derive the constraints from.
Returns:
WidgetConstraints: A new WidgetConstraints instance.
"""
return cls(
position_x=rect.x,
position_y=rect.y,
min_width = 0,
min_height = 0,
max_width = rect.width,
max_height = rect.height
)
[docs]
def copy(self) -> 'WidgetConstraints':
"""
Creates a copy of this instance.
Returns:
WidgetConstraints: A new WidgetConstraints instance with the \
same attribute values as this one.
"""
return WidgetConstraints(
position_x=self.position_x,
position_y=self.position_y,
min_width=self.min_width,
min_height=self.min_height,
max_width=self.max_width,
max_height=self.max_height
)
def __str__(self) -> str:
"""
Returns:
str: A human-readable representation of this instance's \
attribute values.
"""
return str({
"pos_x": self.position_x,
"pos_y": self.position_y,
"min_width": self.min_width,
"min_height": self.min_height,
"max_width": self.max_width,
"max_height": self.max_height
})