Animated application icon

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
Andredron
Miko-Class Veteran
Posts: 722
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Animated application icon

#1 Post by Andredron »

https://boosty.to/7dots/posts/242cd9cb- ... 9e5dd5b1b4

Image

set_icon.rpy

Code: Select all

## HOW TO USE:

# set static icon (only works with files)
# $ set_icon(filename)

# return the default icon
# $ set_icon()

# set animated icon (works only with files)
# parameters as in normal Animation - filename[, pause]
# $ set_ani_icon(“gui/icon/1.png”, .5, “gui/icon/2.png”, 1, “gui/icon/3.png”, .5, “gui/icon/2.png”, 1)

# you can show the icon large on the screen, if you want to
# show expression AniIcon()

## IT'S BETTER NOT TO CHANGE ANYTHING FURTHER
init python:
    # change the current icon (only works with files)
    def set_icon(filename=None):
        import copy

        if not filename:
            filename = config.window_icon

            if “icon_screen” in config.always_shown_screens:
                config.always_shown_screens.remove(“icon_screen”)

            persistent.window_icon_args = [ config.window_icon ]

        temp = copy.copy(config.window_icon)

        config.window_icon = filename
        renpy.game.interface.set_icon()
        config.window_icon = temp

    # class for animated icon (works only with files)
    class IconAnimation(renpy.display.core.Displayable):
        def __init__(self, *args, **properties):
            properties.setdefault('style', 'animation')
            self.anim_timebase = properties.pop('anim_timebase', True)

            super(IconAnimation, self).__init__(**properties)

            files = [ ]
            images = [ ]
            delays = [ ]

            for i, arg in enumerate(args):

                if i % 2 == 0:
                    files.append(arg)
                    images.append(renpy.easy.displayable(arg))
                else:
                    delays.append(arg)

            if len(images) > len(delays):
                delays.append(365.25 * 86400.0) # year is enough

            self.files = files
            self.images = images
            self.delays = delays

        def render(self, width, height, st, at):

            if self.anim_timebase:
                orig_t = at
            else:
                orig_t = st

            t = orig_t % sum(self.delays)

            for image, file, delay in zip(self.images, self.files, self.delays):
                if t < delay:
                    if not renpy.game.less_updates:
                        renpy.display.render.redraw(self, delay - t)

                    im = renpy.display.render.render(image, width, height, t, at)
                    width, height = im.get_size()
                    rv = renpy.display.render.render(width, height)
                    rv.blit(im, (0, 0))

                    set_icon(file)

                    return rv

                else:
                    t = t - delay

        def visit(self):
            return self.images

    # to store the parameters of the animated icon
    if persistent.window_icon_args is None:
        persistent.window_icon_args = [ config.window_icon ]

    # set animated icon
    def set_ani_icon(*args):
        persistent.window_icon_args = args
        if not “icon_screen” in config.always_shown_screens:
            config.always_shown_screens.append(“icon_screen”)

    # to display on the screen at the actual size
    def AniIcon():
        return Animation(*persistent.window_icon_args)

# screen to display the icon - while it is shown (but not visible), the icon changes
screen icon_screen:
    add IconAnimation(*persistent.window_icon_args) xoffset -11111

Translated with DeepL.com (free version)


script.rpy

Code: Select all

label start:

    menu:
        “Set animated icon.”:

            # set animated icon
            $ set_ani_icon(“gui/icon/1.png”, .5, “gui/icon/2.png”, 1, “gui/icon/3.png”, .5, “gui/icon/2.png”, 1)

            # let's show a close-up, just for fun
            show expression AniIcon() as icon:
                align(.5, .65)
            with dissolve

            “Done. Check the icon in the upper left corner.”

        “Set the default icon.”:
            show expression config.window_icon as icon:
                align(.5, .65)
            with dissolve

            $ set_icon()

            “Done. Check the icon in the upper left corner.”

        “Done.”
            return

    jump start

    return

Translated with DeepL.com (free version)

Post Reply

Who is online

Users browsing this forum: DownAbbeyRoad