Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type consolidation and improvements #5319

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
186 changes: 16 additions & 170 deletions renpy/atl.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

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


def compiling(loc):
file, number = loc # @ReservedAssignment
Expand Down Expand Up @@ -65,161 +67,6 @@ def instant(t):
return 1.0



class position(object):
"""
A combination of relative and absolute coordinates.
"""
__slots__ = ('absolute', 'relative')

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
return self

@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 simplify(self):
"""
Tries to represent this position as an int, float, or absolute, if
possible.
"""

if self.relative == 0.0:
if self.absolute == int(self.absolute):
return int(self.absolute)
else:
return renpy.display.core.absolute(self.absolute)
elif self.absolute == 0:
return float(self.relative)
else:
return self

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 NotImplemented

__radd__ = __add__

def __sub__(self, other):
return self + -other

def __rsub__(self, other):
return other + -self

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

__rmul__ = __mul__

def __truediv__(self, other):
if isinstance(other, (int, float)):
return self * (1/other)
return NotImplemented

__div__ = __truediv__ # PY2

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

def __neg__(self):
return -1 * self

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


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

@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 __add__(self, other):
if isinstance(other, DualAngle):
return DualAngle(self.absolute + other.absolute, self.relative + other.relative)
return NotImplemented

def __sub__(self, other):
return self + -other

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

__rmul__ = __mul__

def __neg__(self):
return -1 * self


def position_or_none(x):
if x is None:
return None
return position.from_any(x)


def any_object(x):
return x


def bool_or_none(x):
if x is None:
return x
return bool(x)


def float_or_none(x):
if x is None:
return x
return float(x)


def matrix(x):
if x is None:
return None
elif callable(x):
return x
else:
return renpy.display.matrix.Matrix(x)


def mesh(x):
if isinstance(x, (renpy.gl2.gl2mesh2.Mesh2, renpy.gl2.gl2mesh3.Mesh3, tuple)):
return x

return bool(x)


# A dictionary giving property names and the corresponding type or
# function. This is massively added to by renpy.display.transform.
PROPERTIES = { }
Expand Down Expand Up @@ -259,11 +106,10 @@ def interpolate(t, a, b, typ):
if a is None:
a = 0

if typ in (position_or_none, position):
if typ is position:
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
a = position(a)
b = position(b)
else:
typ = type(b)

Expand All @@ -281,8 +127,8 @@ def interpolate_spline(t, spline, typ):
if spline[0] is None:
return spline[-1]

if renpy.config.mixed_position and typ in (position_or_none, position):
spline = [position_or_none(i) for i in spline]
if typ is position and renpy.config.mixed_position:
spline = [i if i is None else position(i) for i in spline]

lenspline = len(spline)

Expand Down Expand Up @@ -333,8 +179,8 @@ def interpolate_spline(t, spline, typ):

if rv is None:
return None
else:
return type(spline[-1])(rv)

return type(spline[-1])(rv)


def get_catmull_rom_value(t, p_1, p0, p1, p2):
Expand Down Expand Up @@ -1510,8 +1356,8 @@ def execute(self, trans, st, state, events):
angles = (startangle, endangle)
anchorradii = (startanchorradius, endanchorradius)
anchorangles = (
DualAngle(startanchorangle_absolute, startanchorangle_relative),
DualAngle(endanchorangle_absolute, endanchorangle_relative),
dualangle(startanchorangle_absolute, startanchorangle_relative),
dualangle(endanchorangle_absolute, endanchorangle_relative),
)

else:
Expand Down Expand Up @@ -1547,8 +1393,8 @@ def execute(self, trans, st, state, events):
start_relative -= 360

anchorangles = (
DualAngle(start_absolute, start_relative),
DualAngle(end_absolute, end_relative),
dualangle(start_absolute, start_relative),
dualangle(end_absolute, end_relative),
)

if has_anchorradius:
Expand Down Expand Up @@ -1596,17 +1442,17 @@ def execute(self, trans, st, state, events):

if radii is not None:
startradius, endradius = radii
trans.state.radius = interpolate(complete, startradius, endradius, position_or_none)
trans.state.radius = interpolate(complete, startradius, endradius, position)

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:
startradius, endradius = anchorradii
trans.state.anchorradius = interpolate(complete, startradius, endradius, position_or_none)
trans.state.anchorradius = interpolate(complete, startradius, endradius, position)



Expand Down
2 changes: 1 addition & 1 deletion renpy/common/00action_other.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ init -1500 python:
elif self.amount == "page":
amount = delta * adjustment.page
else:
amount = absolute.compute_raw(delta*self.amount, adjustment.range)
amount = absolute.compute(delta * self.amount, adjustment.range)

if self.delay == 0.0:
adjustment.change(adjustment.value + amount)
Expand Down
2 changes: 1 addition & 1 deletion renpy/common/00splines.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ init -1500 python:
self.initialized = None

def init_values(self, sizes):
to_abs_ = absolute.compute_raw
to_abs_ = absolute.compute

def coord_(c):

Expand Down
14 changes: 14 additions & 0 deletions renpy/compat/pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ def make_datetime(cls, *args, **kwargs):

return cls.__new__(cls, *args, **kwargs)

class make_dualangle:
def __new__(cls):
return renpy.display.types.dualangle(0, 0)

class make_position:
def __new__(cls):
return renpy.display.types.position(0, 0)

class Unpickler(pickle.Unpickler):
date = functools.partial(make_datetime, datetime.date)
time = functools.partial(make_datetime, datetime.time)
Expand All @@ -90,6 +98,12 @@ def find_class(self, module, name):
elif name == "datetime":
return self.datetime

if module == "renpy.atl":
if name == "position":
return make_position
elif name == "DualAngle":
return make_dualangle

return super().find_class(module, name)

def load(f):
Expand Down
12 changes: 6 additions & 6 deletions renpy/display/accelerator.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import renpy
import math
from renpy.display.matrix cimport Matrix
from renpy.display.render cimport Render, Matrix2D, render
from renpy.display.core import absolute
from renpy.display.types import absolute

from sdl2 cimport *
from pygame_sdl2 cimport *
Expand Down Expand Up @@ -119,7 +119,7 @@ def relative(n, base, limit):
while a float is interpreted as a fraction of the limit).
"""

return min(int(absolute.compute_raw(n, base)), limit)
return min(int(absolute.compute(n, base)), limit)

cdef class RenderTransform:
"""
Expand Down Expand Up @@ -403,11 +403,11 @@ cdef class RenderTransform:
ysize = state.ysize

if xsize is not None:
xsize = absolute.compute_raw(xsize, self.widtho if renpy.config.relative_transform_size else 1 )
xsize = absolute.compute(xsize, self.widtho if renpy.config.relative_transform_size else 1)
self.widtho = xsize

if ysize is not None:
ysize = absolute.compute_raw(ysize, self.heighto if renpy.config.relative_transform_size else 1)
ysize = absolute.compute(ysize, self.heighto if renpy.config.relative_transform_size else 1)
self.heighto = ysize

self.cr = render(child, self.widtho, self.heighto, st - self.transform.child_st_base, at)
Expand Down Expand Up @@ -762,7 +762,7 @@ cdef class RenderTransform:
manchory = height / 2.0

else:
manchorx, manchory = map(absolute.compute_raw, state.matrixanchor, (width, height))
manchorx, manchory = map(absolute.compute, state.matrixanchor, (width, height))

m = Matrix.offset(-manchorx, -manchory, 0.0)

Expand Down Expand Up @@ -837,7 +837,7 @@ cdef class RenderTransform:
manchory = self.height / 2.0

else:
manchorx, manchory = map(absolute.compute_raw, state.matrixanchor, (self.width, self.height))
manchorx, manchory = map(absolute.compute, state.matrixanchor, (self.width, self.height))

m = Matrix.offset(-manchorx, -manchory, 0.0)
m = mt * m
Expand Down