Skip to content

Instantly share code, notes, and snippets.

@methanoliver
Last active August 28, 2022 18:58
Show Gist options
  • Save methanoliver/bbb026b2c6daeb9b7aae508314e30cfd to your computer and use it in GitHub Desktop.
Save methanoliver/bbb026b2c6daeb9b7aae508314e30cfd to your computer and use it in GitHub Desktop.
How to preview text cps in preferences.
# I wanted to have a slow cps preview window in the preferences screen,
# for UX, so that the user could visually adjust how fast the text is
# to be displayed. The canonical way people have been doing this sort of
# thing is having a separate screen, which would pop up when this preview
# is needed, and be hidden and then reshown to restart the slow cps text.
#
# But in the default gui the preferences screen is inside a viewport,
# which I think is a good idea, and would rather keep. If I have the
# preview window as a separate screen, viewport will slide around
# underneath it. I really wanted to avoid that separate screen, because
# there's no sensible way to position a screen relative to elements laid
# out on another screen, either, unless I try to exploit focus and tooltip
# support mechanics, which will have its own side effects.
#
# So once I got the separate screen solution working as I wanted,
# I set out to get rid of it.
#
# Getting the slow text to restart proved fruitless by every high level
# method I could think of: Slow text is effectively an animated displayable
# that reveals parts of itself according to the animation timebase,
# which, in this case, counts from when the the screen was shown.
# Even if you replace the widget with a completely new one, it won't
# restart: the replacement widget thinks it should have already been
# done displaying by the time it shows up.
#
# Solution was to build a displayable derived from Text which pokes
# into the innards of Text and gets it to loop, which is presented here.
#
# Usage: In the appropriate place of your preferences screen, do
#
# add LoopingSlowText("This is your example", loop_time=3.0)
#
# where "loop_time" is how often the text will loop. Every new loop
# will be displayed with the value of preferences.slow_cps at the time
# it starts, which is most likely what you want.
init -10 python:
class LoopingSlowText(Text):
loop_time = 0
def __init__(self, text, loop_time=2.0, scope=None, substitute=None, replaces=None, mask=None, **properties):
# Because dividing by zero is not nice.
self.loop_time = float(loop_time if loop_time > 0 else 0.00001)
return super(LoopingSlowText, self).__init__(text, slow=True, scope=scope, substitute=substitute, slow_done=None,
replaces=replaces, mask=mask, **properties)
def call_slow_done(self, st):
# This is called by Text when the text is done displaying, but before it becomes locked.
# If we let it finish -- which can happen if the loop_time is longer than it takes
# for the text to display completely -- it becomes frozen, and won't call the render
# method again.
# So we use that moment to trigger the text to redraw from the beginning.
renpy.display.render.redraw(self, 0)
return
def render(self, width, height, st, at):
# We fool the animation timebase, by dividing time by our loop time
# and keeping the remainder, so that every time the loop time would be
# reached, animation goes back in time.
st = st % self.loop_time
at = at % self.loop_time
return super(LoopingSlowText, self).render(width, height, st, at)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment