Re: Imagebutton behavior when clicked
Posted: Sun Dec 19, 2021 2:54 pm
Asceai wrote: ↑Thu Jun 12, 2014 7:29 pmI did it!
This was irritating only because I forgot to implement a crucial part of the UDD interface:
This class (and associated transform) will perform Actions when events occur, then pass on the event to its child.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)
The way we then use this is with something like:
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.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)
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)