[Solved] How to get the screen x & y position of a displayable?

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: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

[Solved] How to get the screen x & y position of a displayable?

#1 Post by henvu50 »

edit: Thanks to Alex & Philat. Scroll down to alex's post for the solution. Solution: renpy.focus_coordinates() # returns (x, y, w, h) of focused displayable https://www.renpy.org/doc/html/other.ht ... oordinates

I'm very close. I'm trying to get the xpos & ypos of the textbutton.

Here is my code.
(This simple code took 2 hours to figure out because there is no example of its usage in the documentation.)

Code: Select all

screen test():
   textbutton 'test':
        id 'test123'
        xpos 818
        ypos 238
        hovered Function(getButtonProperties, i_id='test123')

init python:
 def getButtonProperties(i_id):
  # this works, it returns 'button' when printed to the console
  disp = renpy.get_displayable(screen=None, id=i_id, layer=None)
  disp_properties = renpy.get_placement(disp)
  print disp_properties.pos #returns the values of (0.0, 0.5) instead of (818,238)
Instead of returning the values of 818, 238, the renpy.get_placement function returns the values of (0.0, 0.5) which is not what I want.

How do I get the actual xpos & ypos (818,238) position of the textbutton above? I'm so close.
Last edited by henvu50 on Fri Aug 27, 2021 2:58 pm, edited 1 time in total.

strayerror
Regular
Posts: 159
Joined: Fri Jan 04, 2019 3:44 pm
Contact:

Re: How to get the screen x & y position of a displayable?

#2 Post by strayerror »

I'd guess that the xpos, ypos values you want actually belong to a Transform to which test123 is a child, explaining the values you see.

It might be better to try and explain what broader problem you're trying to solve with this information, as at the point you find yourself pulling displayables out of a screen with get_displayable it becomes ever more likely that it's not the best solution to the high level problem you're trying to solve. :(

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

Re: How to get the screen x & y position of a displayable?

#3 Post by Ocelot »

Generally it is not a good idea to touch screen internals directly. Unless you are doing something like adding your own statements or messing with parsers in some other ways, and you know very well how screens work internaly.

For example, there is no such thing as textbutton natively. AFAIR it is actually a Button, containing Transform, containing Text. And everything could be wrapped in own Transform in some cases. textbutton passes some style properties to Button, some to Transform, some to Text: to make it work naturally and seamlessly from user point of view. That also means that you will have time to learn how everything is implemented if you want to go beyond what user is supposed to do.

As I always say, if you are trying to make decisions based on UI internal data, you are doing it wrong. Code should control interface, not the other way around.
< < insert Rick Cook quote here > >

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How to get the screen x & y position of a displayable?

#4 Post by henvu50 »

Currently my tooltip is in a fixed location. I wanted to create an improved tooltip that shows up near the displayable it's hovering over, dynamically.

For now, I do this manually. I place a tooltip like text displayable near the button manually. The problem with this, is if I change the design of the layout, I have to go and change all screen position values all over again.

Code: Select all

# static placement of a tooltip-like text
screen menu_A():

	default isButtonBHovered = False

	textbutton 'B":
		hovered SetVariable('isButtonBHovered', True)

	if isButtonBHovered:
		frame background None xpos 384 ypos 198:
     			text 'press key o'


I experimented with doing it via tooltip, but I have to put screen position data inside the tooltip, then filter it out later. But I'd still have to manually edit these values later if the position changes. So this idea is out.

Code: Select all

screen test():
    textbutton 'Attack':
        tooltip '({xpos 813} {ypos 382}) Attack your enemy.)


Ideally, it would be great if GetToolTip returned the screen position of the displayable it's hovering over.

Code: Select all


screen tooltip_universal():
    
    the_tooltip = GetToolTip()
    
    # the tooltip text of the hovered button
    the_tooltip.text 
    
    # xpos of the hovered button
    the_tooltip.xpos
    
    # ypos of the hovered button
    the_tooltip.ypos
    
    # from here, you'd be able to dynamically show helpful tooltips near the button to the user, dynamically.
    if tooltip is not None:
        text the_tooltip.text :
            xpos the_tooltip.xpos
            ypos the_tooltip.ypos
I was trying to simulate the above pseudo code by running a function that gets the button's screen position when hovered, with the code I originally posted.

Sure, Renpytom could add this as a feature in the future, but why burden him with it if I could access the screen position of a displayable myself?

The code in the original post is very close to returning the screen pos of the button. Then I can easily show a tooltip dynamically to the user.

lunesc
Newbie
Posts: 7
Joined: Fri Mar 19, 2021 12:23 am
Contact:

Re: How to get the screen x & y position of a displayable?

#5 Post by lunesc »

I have also wondered how to do this, and for the same reason. In general, it seems reasonable to want to position a displayable relative to a button. If there is already a built-in way of achieving this, I would also like to know about it. If not, it would be awesome to be able to get the position to avoid having to update all magic number positions of tooltips or whatever else.

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

Re: How to get the screen x & y position of a displayable?

#6 Post by Ocelot »

I never used it, but did you try renpy.focus_coordinates? It seems like it is something that would be helpful in this case.

https://www.renpy.org/doc/html/other.ht ... oordinates
< < insert Rick Cook quote here > >

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

Re: How to get the screen x & y position of a displayable?

#7 Post by Alex »

henvu50 wrote: Thu Aug 26, 2021 1:53 pm ...
Try this sample, looks like it's what you are looking for - viewtopic.php?f=8&t=62714&sid=e35f5bc43 ... 91#p546039

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: How to get the screen x & y position of a displayable?

#8 Post by henvu50 »

Alex wrote: Fri Aug 27, 2021 1:59 pm
henvu50 wrote: Thu Aug 26, 2021 1:53 pm ...
Try this sample, looks like it's what you are looking for - viewtopic.php?f=8&t=62714&sid=e35f5bc43 ... 91#p546039
That's it!

Damn. I swear, I looked so hard to search for something like that. 90% of the time I find what I'm looking for, but that 10% stumps me sometimes.

Thank you! :)

Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], bloodzy