From 4e2916c3f0a97adca55ae2aca44f6e6c1979f3e7 Mon Sep 17 00:00:00 2001 From: Tom Rothamel Date: Fri, 27 Dec 2013 23:19:35 -0500 Subject: [PATCH] Implement style property retrieval with inheritance. --- renpy/styleaccel.pyx | 38 +++++++++++++++++++++++++++++++++----- unittests/teststyles.py | 6 +++++- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/renpy/styleaccel.pyx b/renpy/styleaccel.pyx index aaee2d9e7..0b46764f8 100644 --- a/renpy/styleaccel.pyx +++ b/renpy/styleaccel.pyx @@ -223,19 +223,47 @@ cdef class StyleCore: Retrieves the property at `index` from this style or its parents. """ + cdef PyObject *o + + # The current style object we're looking at. + cdef StyleCore s + + # The style object we'll backtrack to when s has no down-parent. + cdef StyleCore left + index += self.offset if not self.built: build_style(self) - cdef PyObject *o + s = self + left = None + + while True: + + # If we have the style, return it. + if s.cache != NULL: + o = s.cache[index] + if o != NULL: + return o + + # If there is no left-parent, and we have one, store it. + if left is None and s.left_parent is not None: + left = s.left_parent + + s = s.down_parent + + # If no down-parent, try left. + if s is None: + s = left + left = None + + # If no down-parent or left-parent, default to None. + if s is None: + return None - o = self.cache[index] - if o == NULL: - return None - return o from renpy.styleclass import Style diff --git a/unittests/teststyles.py b/unittests/teststyles.py index 583cf4699..ca5ae8acd 100644 --- a/unittests/teststyles.py +++ b/unittests/teststyles.py @@ -65,7 +65,11 @@ class TestStyles(unittest.TestCase): build_styles() - + s = sm.prefs_default["foo"] + + assert s.bold == "default_foo" + assert s.italic == "prefs_default" + assert s.size == "default"