Skip to content

Commit

Permalink
pos: Add explicit operator methods to position
Browse files Browse the repository at this point in the history
This avoids other code needing to know or care about which operators are
implemented directly and thus more performant and which are amalgams of
other operators with additional overhead.
  • Loading branch information
mal committed Jan 29, 2024
1 parent aeb4586 commit eae1b6d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion renpy/atl.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def interpolate(t, a, b, typ):
if renpy.config.mixed_position:
a = position.from_any(a)
b = position.from_any(b)
return (1-t)*a + t*b # same result, faster execution
return a + t * (b - a)
else:
typ = type(b)

Expand Down
30 changes: 21 additions & 9 deletions renpy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,38 +171,50 @@ def __new__(cls, absolute=0, relative=None):

def __add__(self, other):
if isinstance(other, position):
return position(self.absolute + other.absolute, self.relative + other.relative)
# elif isinstance(other, (int, float)):
# return self + position.from_any(other)
return position(self.absolute + other.absolute,
self.relative + other.relative)

return NotImplemented

__radd__ = __add__

def __mul__(self, other):
if isinstance(other, (int, float)):
return position(self.absolute * other, self.relative * other)
return position(self.absolute * other,
self.relative * other)

return NotImplemented

__rmul__ = __mul__

def __neg__(self):
return -1 * self
return position(-self.absolute, -self.relative)

def __pos__(self):
return position(renpy.types.absolute(self.absolute), float(self.relative))
return self

def __repr__(self):
return "position(absolute={}, relative={})".format(self.absolute, self.relative)

def __rsub__(self, other):
return other + -self
if isinstance(other, position):
return position(other.absolute - self.absolute,
other.relative - self.relative)

return NotImplemented

def __sub__(self, other):
return self + -other
if isinstance(other, position):
return position(self.absolute - other.absolute,
self.relative - other.relative)

return NotImplemented

def __truediv__(self, other):
if isinstance(other, (int, float)):
return self * (1/other)
return position(self.absolute / other,
self.relative / other)

return NotImplemented

__div__ = __truediv__ # PY2
Expand Down

0 comments on commit eae1b6d

Please sign in to comment.