custom Displayable style_prefix

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:

custom Displayable style_prefix

#1 Post by WladekProd »

How to make your own custom button "textbutton" so that style_prefix "navigation" styles are applied to it ?

Code: Select all

init python:
    import math
    class SmoothHover(renpy.Displayable):
        def __init__(self, text, action, text_colors, outline_colors, **kwargs):
            super(SmoothHover, self).__init__(**kwargs)
            self.hover = False
            self.action = action

            self.text_colors = text_colors
            self.outline_colors = outline_colors
            self.current_text_color = text_colors[0]
            self.current_outline_color = outline_colors[0]

            self.child = TextButton(text, clicked=action)
            
            self.width = 0
            self.height = 0

        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

        def event(self, ev, x, y, st):
            if 0 <= x <= self.width and 0 <= y <= self.height:
                self.hover = True
                current_text_color = self.text_colors[1]
            else:
                self.hover = False
                current_text_color = self.text_colors[0]
            
            if self.current_text_color != current_text_color:
                self.current_text_color = self.lerp_color(self.current_text_color, current_text_color, 0.09)
                renpy.timeout(0)
                renpy.redraw(self, 0)
            return self.child.event(ev, x, y, st)

        def visit(self):
            return [ self.child ]
        
        @staticmethod
        def hex_to_rgb(hex_color):
            return tuple(int(hex_color[i:i+2], 16) for i in (1, 3, 5))
        
        @staticmethod
        def rgb_to_hex(rgb):
            return '#{:02x}{:02x}{:02x}'.format(*rgb)
        
        @staticmethod
        def colors_equal(color1, color2):
            return color1[0] != color2[0] and color1[1] != color2[1] and color1[2] != color2[2]

        @staticmethod
        def lerp_color(color1, color2, t):
            color1 = SmoothHover.hex_to_rgb(color1)
            color2 = SmoothHover.hex_to_rgb(color2)
            r = math.floor(color1[0] + (color2[0] - color1[0]) * t)
            g = math.floor(color1[1] + (color2[1] - color1[1]) * t)
            b = math.floor(color1[2] + (color2[2] - color1[2]) * t)
            return SmoothHover.rgb_to_hex((r, g, b))
in the screen navigation() block: I added a button but the styles were not applied:

Code: Select all

add SmoothHover("Options", ShowMenu("preferences"), ["#ffffff", "#ff0000"], ["#ffffff", "#ff0000"])
Image

jeffster
Veteran
Posts: 409
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: custom Displayable style_prefix

#2 Post by jeffster »

WladekProd wrote: Sun Apr 07, 2024 5:24 am How to make your own custom button "textbutton" so that style_prefix "navigation" styles are applied to it ?

Code: Select all

    class SmoothHover(renpy.Displayable):
        def __init__(self, text, action, text_colors, outline_colors, **kwargs):
in the screen navigation() block: I added a button but the styles were not applied:

Code: Select all

add SmoothHover("Options", ShowMenu("preferences"), ["#ffffff", "#ff0000"], ["#ffffff", "#ff0000"])
The __init__ function of "SmoothHover" has a list of optional keyword parameters: **kwargs.
They are passed to the parent constructor.
It might work if you try to add parameter style="navigation_button" or something:

Code: Select all

add SmoothHover("Options", ShowMenu("preferences"), ["#ffffff", "#ff0000"], ["#ffffff", "#ff0000"], style="navigation_button")
Or try

Code: Select all

add SmoothHover("Options", ShowMenu("preferences"), ["#ffffff", "#ff0000"], ["#ffffff", "#ff0000"]):
    style "navigation_button"
Or maybe just inherit from renpy.Button or something, instead of renpy.Displayable?

PS. In the source code,
https://github.com/renpy/renpy/blob/641 ... or.py#L890

Button is init'ed with style='button':

Code: Select all

class Button(renpy.display.layout.Window):
    # ...
    def __init__(self, child=None, style='button', clicked=None,
                 hovered=None, unhovered=None, action=None, role=None,
                 time_policy=None, keymap={}, alternate=None,
                 selected=None, sensitive=None, keysym=None, alternate_keysym=None,
                 **properties):

        # ...
        super(Button, self).__init__(child, style=style, **properties)

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

Re: custom Displayable style_prefix

#3 Post by WladekProd »

jeffster wrote: Sun Apr 07, 2024 8:35 am The __init__ function of "SmoothHover" has a list of optional keyword parameters: **kwargs.
They are passed to the parent constructor.
It might work if you try to add parameter style="navigation_button" or something:

Code: Select all

add SmoothHover("Options", ShowMenu("preferences"), ["#ffffff", "#ff0000"], ["#ffffff", "#ff0000"], style="navigation_button")
Or try

Code: Select all

add SmoothHover("Options", ShowMenu("preferences"), ["#ffffff", "#ff0000"], ["#ffffff", "#ff0000"]):
    style "navigation_button"
Or maybe just inherit from renpy.Button or something, instead of renpy.Displayable?

PS. In the source code,
https://github.com/renpy/renpy/blob/641 ... or.py#L890

Button is init'ed with style='button':

Code: Select all

class Button(renpy.display.layout.Window):
    # ...
    def __init__(self, child=None, style='button', clicked=None,
                 hovered=None, unhovered=None, action=None, role=None,
                 time_policy=None, keymap={}, alternate=None,
                 selected=None, sensitive=None, keysym=None, alternate_keysym=None,
                 **properties):

        # ...
        super(Button, self).__init__(child, style=style, **properties)
Yes, the style parameter works, but I want to make the prefix automatic.

Code: Select all

screen navigation():
    vbox:
        style_prefix "navigation"
        	yalign 0.5
        	spacing gui.navigation_spacing
        	
        	textbutton "Options" action ShowMenu("preferences")
        	add SmoothHover("Custom Options", ShowMenu("preferences"), ["#ffffff", "#ff0000"], ["#ffffff", "#ff0000"], style='navigation_button', text_style='navigation_button_text')
Is it possible to get the value from - style_prefix "navigation" ?
I want to make a button like "textbutton" so that it pulls styles from the parent style_prefix.

jeffster
Veteran
Posts: 409
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: custom Displayable style_prefix

#4 Post by jeffster »

WladekProd wrote: Sun Apr 07, 2024 10:48 am Is it possible to get the value from - style_prefix "navigation" ?
I want to make a button like "textbutton" so that it pulls styles from the parent style_prefix.
It works for standard displayables, like Button. It might be possible to employ the same mechanism here.
What if we try

Code: Select all

        super(SmoothHover, self).__init__(style='button', **kwargs)
?

Otherwise you can see how it's done in Ren'Py source and do likewise.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot]