Skip to content

Commit

Permalink
pos: Eliminate round-trips from position coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
mal committed Jan 29, 2024
1 parent eb05eda commit aeb4586
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions renpy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,29 +142,33 @@ class position(object):

__slots__ = ('absolute', 'relative')

@classmethod
def from_any(cls, other):
if isinstance(other, cls):
return other
elif type(other) is float:
return cls(0, other)
else:
return cls(other, 0)

def __new__(cls, absolute=0, relative=None):
"""
If passed two parameters, takes them as an absolute and a relative.
If passed only one parameter, converts it.
Using __new__ so that passing a position returns it unchanged.
"""

if relative is None:
self = cls.from_any(absolute)
else:
self = object.__new__(cls)
self.absolute = absolute
self.relative = relative
typ = type(absolute)

if typ is cls:
return absolute

if typ is float:
relative = absolute
absolute = 0
else:
relative = 0

self = object.__new__(cls)
self.absolute = absolute
self.relative = relative

return self

from_any = classmethod(__new__)

def __add__(self, other):
if isinstance(other, position):
return position(self.absolute + other.absolute, self.relative + other.relative)
Expand Down

0 comments on commit aeb4586

Please sign in to comment.