Skip to content

Commit 0a6bdcb

Browse files
Merge pull request #43 from BetterBuiltFool/custom_anchor
Custom anchor
2 parents 4877946 + a77a914 commit 0a6bdcb

1 file changed

Lines changed: 23 additions & 33 deletions

File tree

src/pyrite/types/enums.py

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -136,52 +136,42 @@ def _(cls, index: int) -> Layer:
136136

137137
class Anchor:
138138
"""
139-
Determines where on a surface the position is based.
139+
Defines, relative to a rectangle, what spot is considered the position.
140140
"""
141141

142-
def __init__(self, target_attribute: str) -> None:
143-
self.target_attribute = target_attribute
144-
145-
def anchor_rect(self, rectangle: RectLike, position: Point) -> pygame.Rect:
142+
def __init__(self, relative_position: Point) -> None:
146143
"""
147-
Supplies a new rectangle, with the size of the original, but the location of
148-
the rectangle based on the given position, and the anchor point's assigned
149-
attribute.
144+
Creates an anchor point, defining a relative point for the location of
145+
the position of a rectangle.
150146
151-
:param rectangle: A rect-like that will be moved to the position
152-
:param position: A position to move the rectangle to.
153-
:return: The new rectangle, in the position location.
147+
:param relative_position: (0, 0) is top left, and (1, 1) is bottom right.
154148
"""
149+
self._relative_position = pygame.Vector2(relative_position)
150+
151+
def anchor_rect(self, rectangle: RectLike, position: Point) -> pygame.Rect:
155152
rect = pygame.Rect(rectangle)
156-
rect.__setattr__(self.target_attribute, position)
153+
pivot: pygame.Vector2 = self._relative_position.elementwise() * rect.size
154+
offset = pygame.Vector2(rect.topleft) - pivot
155+
rect.topleft = offset
157156
return rect
158157

159158
def anchor_rect_ip(self, rectangle: pygame.Rect, position: Point) -> None:
160-
"""
161-
Moves the passed rectangle to the position, with that position being based on
162-
the anchorpoint.
163-
164-
:param rectangle: A rectangle that will have its position modified.
165-
:param position: A position to move the rectangle to.
166-
"""
167-
rectangle.__setattr__(self.target_attribute, position)
168-
169-
170-
# TODO Add CustomAnchor, which uses an FRect like SurfaceSector to determine relative
171-
# position. Maybe Vector2 instead? We'll see.
159+
pivot: pygame.Vector2 = self._relative_position.elementwise() * rectangle.size
160+
offset = pygame.Vector2(rectangle.topleft) - pivot
161+
rectangle.topleft = offset
172162

173163

174164
class AnchorPoint:
175165
"""
176166
An enum for modifying the position of a rectangle for renderables.
177167
"""
178168

179-
TOPLEFT = Anchor("topleft")
180-
MIDTOP = Anchor("midtop")
181-
TOPRIGHT = Anchor("topright")
182-
MIDLEFT = Anchor("midleft")
183-
CENTER = Anchor("center")
184-
MIDRIGHT = Anchor("midright")
185-
BOTTOMLEFT = Anchor("bottomleft")
186-
MIDBOTTOM = Anchor("midbottom")
187-
BOTTOMRIGHT = Anchor("bottomright")
169+
TOPLEFT = Anchor((0, 0))
170+
MIDTOP = Anchor((0.5, 0))
171+
TOPRIGHT = Anchor((1, 0))
172+
MIDLEFT = Anchor((0, 0.5))
173+
CENTER = Anchor((0.5, 0.5))
174+
MIDRIGHT = Anchor((1, 0.5))
175+
BOTTOMLEFT = Anchor((0, 1))
176+
MIDBOTTOM = Anchor((0.5, 1))
177+
BOTTOMRIGHT = Anchor((1, 1))

0 commit comments

Comments
 (0)