Skip to content

Commit

Permalink
Expand dual angle operators and separate from_any
Browse files Browse the repository at this point in the history
Same logic as for position, other code shouldn't have to worry which
operators have first class support, and there are less mental hoops to
jump through when we look back from years in the future.
  • Loading branch information
mal committed Jan 29, 2024
1 parent 09490b7 commit 8d82878
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
4 changes: 2 additions & 2 deletions renpy/atl.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import renpy
from renpy.pyanalysis import Analysis, NOT_CONST, GLOBAL_CONST
from renpy.types import DualAngle, position
from renpy.types import DualAngle, dualangle, position


def compiling(loc):
Expand Down Expand Up @@ -1448,7 +1448,7 @@ def execute(self, trans, st, state, events):
if anchorangles is not None:
startangle, endangle = anchorangles[:2]

anchorangle = interpolate(complete, startangle, endangle, DualAngle.from_any)
anchorangle = interpolate(complete, startangle, endangle, dualangle)
trans.state.anchorangle = anchorangle

if anchorradii is not None:
Expand Down
4 changes: 2 additions & 2 deletions renpy/display/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import renpy
from renpy.display.layout import Container
from renpy.display.accelerator import RenderTransform
from renpy.types import DualAngle, absolute, any_object, bool_or_none, float_or_none, matrix, mesh, pixels, position
from renpy.types import DualAngle, absolute, any_object, bool_or_none, dualangle, float_or_none, matrix, mesh, position

class Camera(renpy.object.Object):
"""
Expand Down Expand Up @@ -1277,7 +1277,7 @@ def add_gl_property(name):
"alignaround" : (float, float),
"align" : (position, position), # documented as (float, float)
"anchor" : (position, position),
"anchorangle" : DualAngle.from_any,
"anchorangle" : dualangle,
"anchoraround" : (position, position),
"anchorradius" : position,
"angle" : float,
Expand Down
38 changes: 25 additions & 13 deletions renpy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,35 +248,35 @@ def simplify(self):


class DualAngle(object):
@classmethod
def from_any(cls, other):
if isinstance(other, cls):
return other
elif type(other) is float:
return cls(other, other)
raise TypeError("Cannot convert {} to DualAngle".format(type(other)))

def __init__(self, absolute, relative): # for tests, convert to PY2 after
def __init__(self, absolute, relative):
self.absolute = absolute
self.relative = relative

def __add__(self, other):
if isinstance(other, DualAngle):
return DualAngle(self.absolute + other.absolute, self.relative + other.relative)
return DualAngle(self.absolute + other.absolute,
self.relative + other.relative)

return NotImplemented

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

return NotImplemented

__rmul__ = __mul__

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

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

return NotImplemented


def any_object(x):
Expand All @@ -289,6 +289,18 @@ def bool_or_none(x):
return bool(x)


def dualangle(x):
xt = type(x)

if xt is DualAngle:
return x

if xt is float:
return DualAngle(x, x)

raise TypeError("Cannot convert {} to DualAngle".format(type(xt)))


def float_or_none(x):
if x is None:
return x
Expand Down

0 comments on commit 8d82878

Please sign in to comment.