How to properly compose custom Displayables using built-in ones?

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.
Post Reply
Message
Author
DsyD
Newbie
Posts: 6
Joined: Tue Jun 23, 2020 7:31 am
Contact:

How to properly compose custom Displayables using built-in ones?

#1 Post by DsyD » Wed Feb 09, 2022 8:36 pm

I want to use a single type of displayable consisting of multiple built-in displayables many times in my game. An example would be a vbox containing an imagebutton above some text.

I tried creating a simple one using just an ImageButton and some text:

Code: Select all

init python:

    class MyButton(renpy.Displayable):

        def __init__(self, idle_img, hover_img, ttext='', clicked=None, **kwargs):
            super(MyButton, self).__init__(**kwargs)
            self._imagebutton = ImageButton(idle_img, hover_img, clicked=clicked or NullAction(), **kwargs)
            self._text = Text(ttext, False, xalign=0.5, yalign=1.0, **kwargs)

        def render(self, width, height, st, at):
            btn = self._imagebutton.render(width, height, st, at)
            txt = self._text.render(width, height, st, at)
            render = renpy.Render(128, 128)
            render.blit(txt, (0, 128))
            render.blit(btn, (0, 0))
            renpy.redraw(self, 0) # Required to get ImageButton to change on hover?
            return render

        def visit(self):
            return [self._imagebutton, self._text]

screen testimgbtn:
    add MyButton("images/icon_idle.png", "images/icon_hover.png", 'Hello', clicked=Jump("sc002"), focus_mask=True)

label sc001:
	show screen testimgbtn
	
label sc002:
	"You jumped!"
This shows the MyButton and hovering over the ImageButton works, but clicking the ImageButton does nothing. What am I missing here?

Post Reply

Who is online

Users browsing this forum: Bing [Bot]