Can someone provide an example of how focus_mask callable works?

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
henvu50
Veteran
Posts: 322
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Can someone provide an example of how focus_mask callable works?

#1 Post by henvu50 » Tue Aug 23, 2022 11:20 am

https://www.renpy.org/doc/html/style_pr ... focus_mask

Code: Select all

callable
If a non-displayable callable (like a function, method, or object with a __call__ method) is given, the function is called with two arguments, the x and y offset from the top-left corner of the displayable.

If the function returns a callable when called with two arguments, that callable is called with four arguments - the x and y offsets, and the width and height of the displayable.

If the function returns true, the displayable is focused.
Is there a code example of what and how the above works? It talks about the theory, but there's no example.

Is the following how it works? The following code is just guesswork.

Code: Select all

init python:
    def some_function(x, y):
        print(x)
        print(y)
        
imagebutton:
    focus_mask callable some_function
Is that how it works? What is it supposed to do actually?

I need to know the coordinates of where an image is in a transparent image. So let's say you have a 1920x1080 transparent image, but inside it is a 300x300 pixel of book. I want to know where the book is TOP, LEFT wise, and the books width & height.

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

Re: Can someone provide an example of how focus_mask callable works?

#2 Post by Ocelot » Tue Aug 23, 2022 11:41 am

focus_mask is simple: you get two coordinates (or coordinates and width and height) and you should return if displayable should be focused (able to be clicked/receives hover events/etc)
Example 1: make top 20 pixels of the button not active:

Code: Select all

init python:
    def cut_top_20(x, y):
        return y > 20

# . . .
imagebutton auto "imgbtn1_%s" focus_mask cut_top_20
Example 2: create 10 pixel borders at the button, manually set size:

Code: Select all

init python:
    class Bevel:
        def __init__(self, wd, hg):
            self.width = wd
            self.height = hg

        def __call__(self, x, y):
            return x > 10 and x < (self.width - 10) and y > 10 and y < (self.height - 10)

# . . .
imagebutton auto "imgbtn2_%s" focus_mask Bevel(300, 100) # assuming image 300x100 px
Example 3: ellipsis inscribed in button is focusable, get button size from RenPy:

Code: Select all

init python:
    def circle_focus(x, y):
        def real_circle_focus(x, y, w, h):
            w = w / 2.
            h = h / 2.
            x = (float(x) / w) - 1.
            y = (float(y) / h) - 1
            return x * x + y * y < 1.
        return real_circle_focus

# . . .
imagebutton auto "imgbtn2_%s" focus_mask circle_focus
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot]