[SOLVED] How can I get the coordinates x and y of rendered objects?

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
span4ev
Regular
Posts: 105
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

[SOLVED] How can I get the coordinates x and y of rendered objects?

#1 Post by span4ev »

I placed the objects through a loop. Now I need to get a list of tuples with the x and y coordinates of each object. Perhaps there is some method like object.get_pos() or something like that?

I thought that if the objects are already drawn on the screen, then they are assigned coordinates that are stored in memory and probably these coordinates can be accessed.

This is necessary to place hints next to buttons (and other miscellaneous elements), and not in one specific place and not to set specific coordinates that need to be calculated manually. I always avoid specific positioning and always use only dynamic positioning for many objects.

I remember that in Pygame I somehow accessed the coordinates of objects, and could also get the current mouse coordinates via get_pos() and mouse.get_pos().
But mouse coordinates don't help me much. Something like object.get_pos() would help me
Last edited by span4ev on Sat Oct 15, 2022 5:05 am, edited 1 time in total.

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: How can I get the coordinates x and y of rendered objects?

#2 Post by Alex »


User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: How can I get the coordinates x and y of rendered objects?

#3 Post by _ticlock_ »

span4ev wrote: Sun Jul 24, 2022 10:22 am
EDIT:
renpy.get_image_bounds or renpy.get_placement might be useful. Not sure if that is what you are looking for.

span4ev
Regular
Posts: 105
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: How can I get the coordinates x and y of rendered objects?

#4 Post by span4ev »

_ticlock_ wrote: Sun Jul 24, 2022 3:43 pm
span4ev wrote: Sun Jul 24, 2022 10:22 am
EDIT:
renpy.get_image_bounds or renpy.get_placement might be useful. Not sure if that is what you are looking for.
I spent the whole evening yesterday and did not understand how I can use it. I have several elements (buttons). How can I now get a list of tuples of each of them?

Code: Select all

 
init python:
    def get_xy(tag):
        if renpy.get_image_bounds(tag) != None:
            return (int(renpy.get_image_bounds(tag)[0]), int(renpy.get_image_bounds(tag)[1]))
        else:
            return (0,0)

   vbox:

        for btn in range(len(drop_menu_btns_names_list)):
            $ path = 'images/menu/' + drop_menu_btns_names_list[btn] + '.png'
            $ img = Transform(path, size=(drop_menu_btn_icon_size_couple), fit="contain")
            frame:
                style_prefix 'statsMenu_'

                imagebutton:

                    idle img
                    hovered tt.Action(drop_menu_btns_names_dict_translate[drop_menu_btns_names_list[btn]])
                    action drop_menu_btns_actions_list[btn]
                    
                    
                    
                    

User avatar
m_from_space
Miko-Class Veteran
Posts: 978
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: How can I get the coordinates x and y of rendered objects?

#5 Post by m_from_space »

Every now and then this question pops up. And everytime I have to ask myself: How do people not know where their objects are in the first place? Since they obviously placed them (even when using a loop).

So why don't you know where your images are? And why need the coordinates in the first place? (Just curious!)

span4ev
Regular
Posts: 105
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: How can I get the coordinates x and y of rendered objects?

#6 Post by span4ev »

Alex wrote: Sun Jul 24, 2022 2:35 pm Try Nearrect in Ren'Py 8 - https://www.renpy.org/doc/html/screens.html#nearrect
Sounds good, but I couldn't apply it to my example

Code: Select all

    default tt = Tooltip("")

    vbox:

        for btn in range(len(drop_menu_btns_names_list)):
            $ path = 'images/menu/' + drop_menu_btns_names_list[btn] + '.png'
            $ img = Transform(path, size=(drop_menu_btn_icon_size_couple), fit="contain")
            frame:
                style_prefix 'statsMenu_'

                imagebutton:

                    idle img
                    hovered [tt.Action(drop_menu_btns_names_dict_translate[drop_menu_btns_names_list[btn]]), CaptureFocus("toltips")]
                    action [drop_menu_btns_actions_list[btn]]


    showif tt.value and GetFocusRect("toltips"):

        dismiss action ClearFocus("toltips")

        nearrect:
            focus "toltips"

            frame:
                style_prefix 'statsMenuTolltip_'
                text tt.value




span4ev
Regular
Posts: 105
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: How can I get the coordinates x and y of rendered objects?

#7 Post by span4ev »

m_from_space wrote: Mon Jul 25, 2022 6:48 am Every now and then this question pops up. And everytime I have to ask myself: How do people not know where their objects are in the first place? Since they obviously placed them (even when using a loop).

So why don't you know where your images are? And why need the coordinates in the first place? (Just curious!)
I apologize for the primitive question that is often asked. I am a beginner and it is difficult for me to understand Renp syntax.
The matter is that I know coordinates of only one parent element (container). Inside it there are child elements, the coordinates for which are set dynamically by Renp itself.
I don't place a lot of objects at exact coordinates and I think it's bad practice.
If I need to add or remove something, then I will have to manually calculate the coordinates, something like

x=123.5
y=77.9

Therefore, I set the exact positioning to the main frame (parent), and inside it there are many buttons (children), which I add through the loop. I wrote an example loop.
I have a list with image names for buttons and a list with actions for buttons.
Thus, when changing the number of buttons, I only need to manipulate lists. I saved myself from searching for specific coordinates

button A
x=123.5
y=77.9

button B
x = 141.8
y=95.2

Therefore, I do not know their coordinates. But I need to place tooltips next to these buttons. I can do it through
get_mouse_pos()
but then the location of the tooltip will be inaccurate and depend on where the mouse cursor was at the moment.
Therefore, I need to get the coordinates of objects that I do not know, add x, y offset values ​​\u200b\u200bto them, and display tooltips next to the buttons. I am sure that these coordinates should be stored in memory as a list of tuples.

Do you mean coordinates based on

parent location + width and height + padding?

span4ev
Regular
Posts: 105
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: How can I get the coordinates x and y of rendered objects?

#8 Post by span4ev »

m_from_space wrote: Mon Jul 25, 2022 6:48 am Every now and then this question pops up. And everytime I have to ask myself: How do people not know where their objects are in the first place? Since they obviously placed them (even when using a loop).

So why don't you know where your images are? And why need the coordinates in the first place? (Just curious!)
In that case I could use

1. Location of parent x,y
2. Parent padding
3. The length of the list of objects
4. x,y parent + padding = x,y first child (start coordinate)
5. x,y of first child + padding between children + x,y of second child
...
I create a list with these coordinates, add an offset for tooltips.

Your words make sense. In this case, we all really know the coordinates of objects. You're right. But I do not think that this is justified if there is a method that returns the already prepared coordinates of the drawn objects.
It depends on what will be easier and faster: calculate the coordinates yourself or use the built-in methods.

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: How can I get the coordinates x and y of rendered objects?

#9 Post by _ticlock_ »

span4ev wrote: Mon Jul 25, 2022 7:09 am
To avoid confusion, are you actually trying to make tooltips and position them next to the displayable?

Check the tooltip example using Nearrect from renpy documentation tooltip_example2

User avatar
m_from_space
Miko-Class Veteran
Posts: 978
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: How can I get the coordinates x and y of rendered objects?

#10 Post by m_from_space »

span4ev wrote: Mon Jul 25, 2022 7:03 amI apologize for the primitive question that is often asked. I am a beginner and it is difficult for me to understand Renp syntax.
Sorry, it wasn't my intention to sound arrogant. I'm happy to help and don't mind if you're a beginner. I was just wondering about this specific problem. Well, maybe Renpy needs this kind of reliable function, since many people ask for it here and there.

And yes, I was talking about calculations. In most cases the user interfaces are probably not that dynamic that you don't know position and sizes, even when using lists and loops. But who knows. :)

span4ev
Regular
Posts: 105
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: How can I get the coordinates x and y of rendered objects?

#11 Post by span4ev »

m_from_space wrote: Mon Jul 25, 2022 1:54 pm
span4ev wrote: Mon Jul 25, 2022 7:03 amI apologize for the primitive question that is often asked. I am a beginner and it is difficult for me to understand Renp syntax.
Sorry, it wasn't my intention to sound arrogant. I'm happy to help and don't mind if you're a beginner. I was just wondering about this specific problem. Well, maybe Renpy needs this kind of reliable function, since many people ask for it here and there.

And yes, I was talking about calculations. In most cases the user interfaces are probably not that dynamic that you don't know position and sizes, even when using lists and loops. But who knows. :)
I didn't think you were arrogant or anything like that. And the line you quoted was not sarcasm or irony. I perfectly understand what primitive questions are that all beginners ask, because I met this in other areas I think you asked a very relevant question, which, by the way, prompted me to write a function. Yes, I solved this problem using a function and now I get a tooltip exactly opposite each button. I can add and remove buttons, but tooltips will still remain in front of them.
But I would still like to know if there is any method for obtaining coordinates or not.

Here is the code itself. Here I did not specify any specific coordinates. The arrangement of the children is based on the loop and the padding values ​​between them. The parent also does not have specific coordinates. By default, he took the coordinates 0, 0 for x and y.
In my case, I did not specify any coordinates.

Code: Select all

    vbox:

        for btn in range(len(statMenu_btns_names_list)):
            $ path = 'images/menu/' + statMenu_btns_names_list[btn] + '.png'
            $ img = Transform(path, size=(statMenu_btn_icon_sizes), fit="contain")
            frame:
                style_prefix 'statsMenu_'

                imagebutton:
                    idle img
                    hovered [tt.Action(statMenu_btns_names_dict_translate[statMenu_btns_names_list[btn]]), SetScreenVariable('btn_index', btn)]
                    action [statMenu_btns_actions_list[btn]]


User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: How can I get the coordinates x and y of rendered objects?

#12 Post by _ticlock_ »

I think, in the renpy screen language you are not supposed to get coordinates of the displayables. Instead, you can use style properties for positioning and other screen language tools.
span4ev wrote: Mon Jul 25, 2022 2:05 pm Yes, I solved this problem using a function and now I get a tooltip exactly opposite each button. I can add and remove buttons, but tooltips will still remain in front of them.
I think you can easily achieve the same with tooltips and nearrect:

Code: Select all

    vbox:

        for btn in range(len(statMenu_btns_names_list)):
            $ path = 'images/menu/' + statMenu_btns_names_list[btn] + '.png'
            $ img = Transform(path, size=(statMenu_btn_icon_sizes), fit="contain")
            frame:
                style_prefix 'statsMenu_'

                imagebutton:
                    idle img
                    tooltip statMenu_btns_names_dict_translate[statMenu_btns_names_list[btn]]
                    action [statMenu_btns_actions_list[btn]]
                    
    $ tooltip = GetTooltip()

    if tooltip:
        nearrect:
            focus "tooltip"

            frame:
                xpos 1.0 yoffset -50 # The height of the button
                text tooltip

Alternatively, you can make a slightly different design of the screen and use the benefits of loops and if statements:

Code: Select all

    def btn_index = -1
    vbox:

        for btn in range(len(statMenu_btns_names_list)):
            $ path = 'images/menu/' + statMenu_btns_names_list[btn] + '.png'
            $ img = Transform(path, size=(statMenu_btn_icon_sizes), fit="contain")
            hbox:
                #### imagebutton ###
                frame:
                    style_prefix 'statsMenu_'

                    imagebutton:
                        idle img
                        hovered SetScreenVariable('btn_index', btn)
                        unhovered SetScreenVariable('btn_index', -1)
                        action [statMenu_btns_actions_list[btn]]
                ### Tooltip ###
                if btn == btn_index:
                    frame:
                        text statMenu_btns_names_dict_translate[statMenu_btns_names_list[btn]
Basically, renpy screen language is supposed to help you to avoid dealing with coordinates. I believe, there are some reasons why you can't directly access coordinates inside the screen language.
However, in some more complex applications, you may consider using python functions or even creating Creator-Defined Displayables.

span4ev
Regular
Posts: 105
Joined: Fri Jul 08, 2022 9:29 pm
itch: rpymc
Contact:

Re: How can I get the coordinates x and y of rendered objects?

#13 Post by span4ev »

_ticlock_ wrote: Mon Jul 25, 2022 4:29 pm I think, in the renpy screen language you are not supposed to get coordinates of the displayables. Instead, you can use style properties for positioning and other screen language tools.

Basically, renpy screen language is supposed to help you to avoid dealing with coordinates. I believe, there are some reasons why you can't directly access coordinates inside the screen language.
However, in some more complex applications, you may consider using python functions or even creating Creator-Defined Displayables.
Sorry for the late reply, I just read your message.
I tried your first example and it works. It looks elegant and simple, and uses Renp's built-in functions that I didn't know about. When y = button height / 2, the result is exactly the same and the function for finding the coordinates is completely unnecessary.
Thank you for showing me how to correctly and beautifully display hints without unnecessary calculations of coordinates.
I tried the second example, and if I give it the height of the button in ypos, I get the same result.
And all this without having to calculate the exact coordinates of each clue.

Thank you very much for your help. I don't even know which of the two options to leave. Just in case, I'll throw you the function code that I had to use earlier (which I don't need now)

Code: Select all

init:
    $ statMenu_all_btn_size             = 28
    $ statMenu_all_btn_frame_padding    = 10
    $ statMenu_frame_x                  = 0 
    $ statMenu_frame_y                  = statMenu_all_btn_size + statMenu_all_btn_frame_padding * 2
    $ statMenu_btn_width                = statMenu_all_btn_size             
    $ statMenu_btn_height               = statMenu_all_btn_size             
    $ statMenu_frame_padding_x          = statMenu_all_btn_frame_padding    
    $ statMenu_frame_padding_y          = statMenu_all_btn_frame_padding  

    $ statsMenuTooltip_frame_y_size     = 20  
    $ statMenu_tooltip_offset_x         = 30
    $ statMenu_tooltip_pos_x            = statMenu_frame_x + statMenu_frame_padding_x + statMenu_btn_width + statMenu_tooltip_offset_x
    $ statMenu_tooltip_start_pos_y      = statMenu_frame_y + statMenu_frame_padding_y + statMenu_btn_height / 2 - statsMenuTooltip_frame_y_size / 2
    $ statMenu_tooltip_offset_y         = statMenu_btn_height + statMenu_frame_padding_y * 2

init python:
    def get_pos_list_for_tooltip(x, y, l, yo):
    
        coors   = []
    
        for i in range(len(l)):
            t = (x, int(y))
            coors.append(t)
            y += yo
        return coors


    vbox:
        for btn in range(len(statMenu_btns_names_list)):
                imagebutton:
                    hovered [tt.Action(statMenu_btns_names_dict_translate[statMenu_btns_names_list[btn]]), SetScreenVariable('btn_index', btn)]

    showif tt.value:

        $ pos_list = get_pos_list_for_tooltip(statMenu_tooltip_pos_x, statMenu_tooltip_start_pos_y, statMenu_btns_names_list, statMenu_tooltip_offset_y)

        frame:
            pos pos_list[btn_index] 
            text tt.value

Post Reply

Who is online

Users browsing this forum: No registered users