Render button with transform

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
User avatar
WladekProd
Newbie
Posts: 5
Joined: Fri Apr 05, 2024 3:48 pm
Contact:

Render button with transform

#1 Post by WladekProd »

Tell me how to render a button using Transform and alignment?

I tried several options, in the first one I don’t understand where to connect my Transform, but the text aligns perfectly:

Code: Select all

        def render(self, width, height, st, at):
            if self.style.time_policy:
                st, self.time_policy_data = self.style.time_policy(st, self.time_policy_data, self.style)
            rv = super(Btn, self).render(width, height, st, at)
            if self.clicked:
                rect = self.style.focus_rect
                if rect is not None:
                    fx, fy, fw, fh = rect
                else:
                    fx = self.style.left_margin
                    fy = self.style.top_margin
                    fw = rv.width - self.style.right_margin
                    fh = rv.height - self.style.bottom_margin
                mask = self.style.focus_mask
                if mask is True:
                    mask = rv
                elif mask is not None:
                    try:
                        mask = renpy.display.render.render(mask, rv.width, rv.height, st, at)
                    except Exception:
                        if callable(mask):
                            mask = mask
                        else:
                            raise Exception("Focus_mask must be None, True, a displayable, or a callable.")
                if mask is not None:
                    fmx = 0
                    fmy = 0
                else:
                    fmx = None
                    fmy = None
                rv.add_focus(self, None,
                            fx, fy, fw, fh,
                            fmx, fmy, mask)
            return rv
In the second, Transform is applied but the text alignment is lost:

Code: Select all

        def render(self, width, height, st, at):
            t = Transform(child=self.child, matrixcolor=TintMatrix(self.current_text_color))
            child_render = renpy.render(t, width, height, st, at)
            self.width, self.height = child_render.get_size()
            render = renpy.Render(self.width, self.height)
            render.blit(child_render, (0, 0))
            return render
Is it possible to specify your own transform in rv in order to preserve the text alignment?

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2411
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Render button with transform

#2 Post by Ocelot »

I am not completely sure how the first piece ofcode works, but you can wrap child in transform you need:

Code: Select all

# at the end of init
self.child = Transform(child=self.child, matrixcolor=TintMatrix(self.current_text_color))
If you want to constantly chnage text color and make it reflect on text, then replace transform as neeed:

Code: Select all

# in render code
if self.current_text_color != self.old_text_color:
    self.child = Transform(child=self.child.child, matrixcolor=TintMatrix(self.current_text_color))
    self.old_text_color = self.current_text_color
< < insert Rick Cook quote here > >

User avatar
WladekProd
Newbie
Posts: 5
Joined: Fri Apr 05, 2024 3:48 pm
Contact:

Re: Render button with transform

#3 Post by WladekProd »

Ocelot wrote: Tue Apr 09, 2024 3:04 am I am not completely sure how the first piece ofcode works, but you can wrap child in transform you need:

Code: Select all

# at the end of init
self.child = Transform(child=self.child, matrixcolor=TintMatrix(self.current_text_color))
If you want to constantly chnage text color and make it reflect on text, then replace transform as neeed:

Code: Select all

# in render code
if self.current_text_color != self.old_text_color:
    self.child = Transform(child=self.child.child, matrixcolor=TintMatrix(self.current_text_color))
    self.old_text_color = self.current_text_color
Thanks a lot!
It works :shock:

Post Reply

Who is online

Users browsing this forum: Bing [Bot], DewyNebula, Google [Bot], OffDeez399