Imagebutton behavior when clicked

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Message
Author
lewishunter
Newbie
Posts: 13
Joined: Sun Aug 01, 2021 8:06 pm
Contact:

Re: Imagebutton behavior when clicked

#16 Post by lewishunter » Sun Dec 19, 2021 2:54 pm

Asceai wrote:
Thu Jun 12, 2014 7:29 pm
I did it!

This was irritating only because I forgot to implement a crucial part of the UDD interface:

Code: Select all

init python:
    def run_action(action):
        try:
            for a in action:
                run_action(a)
        except TypeError, te:
            action()
    class Keymapper(renpy.Displayable):
        def __init__(self, vargs, child, **kwargs):
            super(Keymapper, self).__init__(**kwargs)
            self.child = child
            self.vargs = vargs
        def render(self, width, height, st, at):
            return renpy.render(self.child, width, height, st, at)
        def event(self, ev, x, y, st):
            for keysym, action in self.vargs:
                if renpy.map_event(ev, keysym):
                    run_action(action)
            self.child.event(ev, x, y, st)
        def visit(self):
            return [ self.child ]

    KeymapTransform = renpy.curry(Keymapper)
This class (and associated transform) will perform Actions when events occur, then pass on the event to its child.

The way we then use this is with something like:

Code: Select all

screen main_menu:
    default mouse_clicked = False
    fixed at KeymapTransform([('mousedown_1', SetScreenVariable('mouse_clicked', True)), ('mouseup_1', SetScreenVariable('mouse_clicked', False))]):
        $y = 450
        imagebutton idle "menu/main/start_idle.png" hover ("menu/main/start_clicked.png" if mouse_clicked else "menu/main/start_hover.png") xpos 80 ypos y focus_mask True action Start()
        imagebutton idle "menu/main/load_idle.png" hover ("menu/main/load_clicked.png" if mouse_clicked else "menu/main/load_hover.png") xpos 214 ypos y focus_mask True  action ShowMenu('load')
        imagebutton idle "menu/main/config_idle.png" hover ("menu/main/config_clicked.png" if mouse_clicked else "menu/main/config_hover.png") xpos 342 ypos y focus_mask True action ShowMenu('preferences')
        if persistent.extra_unlocked:
            imagebutton idle "menu/main/extra_idle.png" hover ("menu/main/extra_clicked.png" if mouse_clicked else "menu/main/extra_hover.png") xpos 508 ypos y focus_mask True action Start('extras')
        imagebutton idle "menu/main/exit_idle.png" hover ("menu/main/exit_clicked.png" if mouse_clicked else "menu/main/exit_hover.png") xpos 646 ypos y focus_mask True action Quit(confirm=False)
The above code is only an example- I haven't tested it since I don't have your images etc. but it's along the right lines. Make sure everything in that screen is within the fixed- that way the mouseup/mousedown events won't be missed regardless of where the mouse is.

I think this code is great. I wish this could be somewhat implemented inside Ren'Py. Even if you do not use a pressed button image it still makes the button look selected while the screens are transitioning, which gives it a more polish look.

I think this line should be changed so it will cause less bugs in complicated screens.

Code: Select all

    class Keymapper(renpy.Displayable):
        def __init__(self, vargs, child, **kwargs):
            super(Keymapper, self).__init__(**kwargs)
            self.child = child
            self.vargs = vargs
        def render(self, width, height, st, at):
            return renpy.render(self.child, width, height, st, at)
        def event(self, ev, x, y, st):
            for keysym, action in self.vargs:
                if renpy.map_event(ev, keysym):
                    run_action(action)
            return self.child.event(ev, x, y, st)  ###### return added ######
        def visit(self):
            return [ self.child ]

    KeymapTransform = renpy.curry(Keymapper)

lewishunter
Newbie
Posts: 13
Joined: Sun Aug 01, 2021 8:06 pm
Contact:

Re: Imagebutton behavior when clicked

#17 Post by lewishunter » Thu Jan 06, 2022 2:27 am

The run_action function should be replaced with the renpy.run function because it does the same and the except syntax may not work on Python 3:

Erase this:

Code: Select all

    def run_action(action):
        try:
            for a in action:
                run_action(a)
        except TypeError, te:
            action()
And replace this line in here:

Code: Select all

        def event(self, ev, x, y, st):
            for keysym, action in self.vargs:
                if renpy.map_event(ev, keysym):
                    renpy.run(action)   ##### Line replaced #####

Post Reply

Who is online

Users browsing this forum: Google [Bot]