@@ -167,6 +167,34 @@ def anchor_rect_ip(self, rectangle: pygame.Rect, position: Point) -> None:
167167 rectangle .__setattr__ (self .target_attribute , position )
168168
169169
170+ class CustomAnchor (Anchor ):
171+ """
172+ A custom variant of Anchor, where a supplied offset is used to determine where in
173+ the rectangle the position is based.
174+ """
175+
176+ def __init__ (self , relative_position : Point ) -> None :
177+ """
178+ Creates a custom anchor point, defining a relative point for the location of
179+ the position of a rectangle.
180+
181+ :param relative_position: (0, 0) is top left, and (1, 1) is bottom right.
182+ """
183+ self ._relative_position = pygame .Vector2 (relative_position )
184+
185+ def anchor_rect (self , rectangle : RectLike , position : Point ) -> pygame .Rect :
186+ rect = pygame .Rect (rectangle )
187+ pivot : pygame .Vector2 = self ._relative_position .elementwise () * rect .size
188+ offset = pygame .Vector2 (rect .topleft ) - pivot
189+ rect .topleft = offset
190+ return rect
191+
192+ def anchor_rect_ip (self , rectangle : pygame .Rect , position : Point ) -> None :
193+ pivot : pygame .Vector2 = self ._relative_position .elementwise () * rectangle .size
194+ offset = pygame .Vector2 (rectangle .topleft ) - pivot
195+ rectangle .topleft = offset
196+
197+
170198# TODO Add CustomAnchor, which uses an FRect like SurfaceSector to determine relative
171199# position. Maybe Vector2 instead? We'll see.
172200
0 commit comments