[Solved]Tooltip box that moves with mouse?

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
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

[Solved]Tooltip box that moves with mouse?

#1 Post by Imperf3kt »

I've added a tooltip to a recent project and its working nicely, but I'd like to try and place the text in a vbox or something that only shows while a button is hovered, and gets its positioning based on where the mouse currently is.

I looked into renpy.get_mouse_pos(), but I don't know how I could use this to achieve what I want, if I even can.

I can make the screen appear easily enough by just using "if tooltip", but how do I make it follow the mouse or even just change position based on the mouse? I don't know how to use a tuple as x/y positioning. Is this even possible?

Sorry about not supplying further info, the project isn't my own, so I'm not sure how much information about it I can freely share.
Last edited by Imperf3kt on Tue Jan 29, 2019 8:13 am, edited 1 time in total.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: Tooltip box that moves with mouse?

#2 Post by Kia »

I would use a timer that get's the position of the mouse in short intervals and stores it in variables that are used for positioning the tooltip frame, from the top of my head:

Code: Select all

timer .02 repeat True action [SetScreenVariable("ttxp", get_mouse[0]), SetScreenVariable("ttyp", get_mouse[1])]

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Tooltip box that moves with mouse?

#3 Post by Imperf3kt »

Kia wrote: Fri Jan 25, 2019 1:15 pm I would use a timer that get's the position of the mouse in short intervals and stores it in variables that are used for positioning the tooltip frame, from the top of my head:

Code: Select all

timer .02 repeat True action [SetScreenVariable("ttxp", get_mouse[0]), SetScreenVariable("ttyp", get_mouse[1])]
Thanks, but I'm not sure I am using it correctly.

I placed the code in a screen:

Code: Select all

screen mopos():
    timer .02 repeat True action [SetScreenVariable("ttxp", get_mouse[0]), SetScreenVariable("ttyp", get_mouse[1])]
    
    vbox:
        xpos [ttxp]
        ypos [ttyp]
    
        text "[tooltip]"
        
And gave an imagebutton an action to show the screen when hovered: ToggleScreen("mopos")

Code: Select all

screen ret():
    
    fixed:
        xpos 270
        ypos 882

        imagebutton:
            idle "images/Buttons & Graphics/Text Buttons/Return_Select_Off.png"
            hover "images/Buttons & Graphics/Text Buttons/Return_Select_On.png"
            action Return(), ToggleScreen("mopos")
            tooltip "Return to the previous menu"
            
But nothing happened.

So I put the code in my existing tooltip screen:

Code: Select all

screen ttp():
    $ tooltip = GetTooltip()
    timer .02 repeat True action [SetScreenVariable("ttxp", get_mouse[0]), SetScreenVariable("ttyp", get_mouse[1])]
    fixed:
#        xmaximum 1000
#        xpos 300
#        ypos 1030
        xpos [ttxp]
        ypos [ttyp]
        
        if tooltip:
            text "[tooltip]"
            
and I get an error "get_mouse" is not defined.
If I define get_mouse, I am told "module has no attribute __get_mouse__"
If I use renpy.get_mouse, I am told the same thing as if I defined get_mouse.

I'm really confused here and honestly don't know what I'm doing.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: Tooltip box that moves with mouse?

#4 Post by Kia »

oh, the code I've gave you was from the top of my head, the things you need to add are:
two screen variables, and you need to replace the "get_mouse" with the function you get the mouse position from.
something like this should work, though, I haven't had chance to test it, there might be mistakes you'll have to track down and fix.

Code: Select all

screen mopos():
    default = ttxp
    default = ttyp
    timer .02 repeat True action [SetScreenVariable("ttxp", renpy.get_mouse_pos()[0]), SetScreenVariable("ttyp",renpy.get_mouse_pos()[1])]
    
    vbox:
        xpos [ttxp]
        ypos [ttyp]
    
        text "[tooltip]"

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Tooltip box that moves with mouse?

#5 Post by Imperf3kt »

I give up. Thanks for trying
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: Tooltip box that moves with mouse?

#6 Post by Kia »

this one works, I tested it and it follows the mouse around, all you have to do is showing it on hover, hiding it on unhover and passing the text to it.

Code: Select all

screen tt:
    default ttx = 0
    default tty = 0
    
    timer .02 repeat True action [SetScreenVariable("ttx", renpy.get_mouse_pos()[0]), SetScreenVariable("tty",renpy.get_mouse_pos()[1])]
    frame:
        offset(10,10) anchor(0.0,0.0)
        xpos ttx
        ypos tty
        text "codex"

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Tooltip box that moves with mouse?

#7 Post by Imperf3kt »

Oh cool thanks.
I spent so many hours yesterday trying so many different things that I thought I'd never work it out, so I really appreciate the help.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: Tooltip box that moves with mouse?

#8 Post by Kia »

from my experience, it's always something small that we miss and cause frustration. ^_^

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Tooltip box that moves with mouse?

#9 Post by Imperf3kt »

Edit: Fixed it, turns out the repeat was not necessary unless I want the screen to follow the mouse, which I do, but isn't necessary for this purpose.
Edit 2: Not as useful as I thought, it appears to cause unintended behaviour. Back to the drawing board.

Original post:
That almost works perfectly, but I don't seem to be able to get the screen to go away without clicking. Any idea what I might have missed?

Code: Select all

screen ret():
    
    fixed:
        xpos 270
        ypos 882

        imagebutton:
            idle "images/Buttons & Graphics/Text Buttons/Return_Select_Off.png"
            hover "images/Buttons & Graphics/Text Buttons/Return_Select_On.png"
            action Return()
            hovered Show("tt", transition=dissolve)
            unhovered Hide("tt", transition=dissolve)
            tooltip "Return to the previous menu"

screen tt:
    default ttx = 0
    default tty = 0
    $ tooltip = GetTooltip()
    
    timer .02 repeat True action [SetScreenVariable("ttx", renpy.get_mouse_pos()[0]), SetScreenVariable("tty",renpy.get_mouse_pos()[1])]
    if tooltip:
        frame:
            offset(10,10) anchor(0.0,0.0)
            xpos ttx
            ypos tty
            text "[tooltip]"
I tried several things and sometimes the screen would show on a lower layer, other times it wouldn't appear until you clicked on a button.
I'll put this aside for now and go have some breakfast before I throw my mouse at something xD

I added a Return("None") to the timer action, but that makes the game jump to the start label lol
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

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

Re: Tooltip box that moves with mouse?

#10 Post by Alex »

Have you seen this thread - viewtopic.php?f=51&t=47205#p472810 ?

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Tooltip box that moves with mouse?

#11 Post by Imperf3kt »

Alex wrote: Sun Jan 27, 2019 5:42 pm Have you seen this thread - viewtopic.php?f=51&t=47205#p472810 ?
Thanks for the link. Will try it soon.
Last edited by Imperf3kt on Tue Jan 29, 2019 7:28 am, edited 1 time in total.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: Tooltip box that moves with mouse?

#12 Post by Imperf3kt »

Alex wrote: Sun Jan 27, 2019 5:42 pm Have you seen this thread - viewtopic.php?f=51&t=47205#p472810 ?
Thats great!
Thank you so much for the link, and a huge thanks to Human Bolt Diary for coming up with it.

Edit: Or it does, until you try to put it in a screens.rpy

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/screens.rpy", line 88, in script
    init -1500 python:
  File "game/screens.rpy", line 90, in <module>
    class MouseTooltip(Tooltip, renpy.Displayable):
NameError: name 'Tooltip' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/screens.rpy", line 88, in script
    init -1500 python:
  File "C:\Program Files (x86)\renpy\renpy\ast.py", line 881, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\Program Files (x86)\renpy\renpy\python.py", line 1913, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/screens.rpy", line 90, in <module>
    class MouseTooltip(Tooltip, renpy.Displayable):
NameError: name 'Tooltip' is not defined

Windows-8-6.2.9200
Ren'Py 7.1.3.1092
 
Tue Jan 29 22:37:35 2019
Placing it in a different file, it works fine.

I do have one problem though, I can't work out how to put the text within a box so it doesn't flow off the screen...

I got it, found a feature of Ren'Py and decided to just put it in and see if it works, and it did!
For example:

Code: Select all

hovered mtt.Action(VBox(Text("Continue\nLine break test"), xmaximum=50))

#or

hovered mtt.Action(Frame(Text("Continue\nLine break test"), xmaximum=250, ymaximum=250))

#Or even

hovered mtt.Action(Fixed(Image("images/bg3.jpg"), Text("Begin a new game"), xmaximum=250, ymaximum=250))
With a bit more fiddling, I'll have this how I want soon(™).
Thanks again for the massive help.

Any idea how to bring it in front of other displayables?
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]