Making tooltips disappear when the mouse is not hovering over the word

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
BeepBoop
Newbie
Posts: 6
Joined: Wed Jul 26, 2023 1:26 pm
Contact:

Making tooltips disappear when the mouse is not hovering over the word

#1 Post by BeepBoop »

Hi!

I'm trying to make some text tooltips for my game where the player hovers their mouse over a highlighted word and they get a little tooltip that explains the lore.

I did manage to find a code on these forums that had what I was looking for, except there's one problem: the tooltip doesn't stay up for very long due to the timer. I want it to remain up so long as the player has their mouse hovering over the word, only disappearing when the mouse is moved elsewhere. So far, I've tried removing the timer, but the tooltip stays up once the player hovers their mouse over it. Any ideas?

Here's the code if you don't want to read the original thread:

Code: Select all

define e = Character("Eileen")

init python:

    lexicon = {
        ('Visual Novel', 'VN')  : """
            A visual novel (bijuaru noberu) is an interactive game genre, 
            which originated in Japan in the early 1990s, featuring mostly 
            static graphics, most often using anime-style art or occasionally 
            live-action stills.
            As the name might suggest, they resemble mixed-media novels.""",
        ('Ren\'Py', 'Renpy') : """
            The Ren'Py Visual Novel Engine is a free software engine which 
            facilitates the creation of visual novels, a form of 
            computer-mediated storytelling. Ren'Py is a portmanteau of ren'ai, 
            the Japanese word for 'romantic love' and Python, 
            the programming language that Ren'Py runs on. """
    }

    def hyperlink_lexicon( str_to_test ):

        for keys in lexicon:
        
            if isinstance(keys, basestring):
                keys = [keys]
        
            for phrase in keys:

                # preceded by a space
                str_to_test = str_to_test.replace(
                    " {0}".format(phrase),
                    " {{a=lexicon:{phrase}}}{phrase}{{/a}}".format(
                        phrase = phrase ) )
                
                # followed by a space
                str_to_test = str_to_test.replace(
                    "{0} ".format(phrase),
                    "{{a=lexicon:{phrase}}}{phrase}{{/a}} ".format(
                        phrase = phrase ) )
        
        return str_to_test

    config.say_menu_text_filter = hyperlink_lexicon


    def hyperlink_styler(*args):

        return style.hyperlink_text

    def hyperlink_hovered(*args):
        
        if not args[0]:
            # Ren'Py 7+ recent nightly only, see below
            renpy.hide_screen("lexicon_popup")
        
        elif args[0][:8] == "lexicon:":

            renpy.show_screen( "lexicon_popup", 
                               args[0][8:], 
                               renpy.get_mouse_pos() )
            
            renpy.restart_interaction()
        
        return

    def hyperlink_clicked(*args):

        if args[0] and args[0][:8] != 'lexicon:':

            # adapted from common/00defaults.rpy
            if args[0].startswith("http:") or args[0].startswith("https:"):
                try:
                    import webbrowser
                    webbrowser.open(args[0])
                except:
                    renpy.notify("Failed to open browser")

            elif args[0].startswith("jump:"):
                renpy.jump( args[0][5:] )

            else:
                renpy.call_in_new_context(args[0][args[0].index(':')+1:])

    
    style.default.hyperlink_functions = ( hyperlink_styler, 
                                          hyperlink_clicked, 
                                          hyperlink_hovered )


screen lexicon_popup(phrase=None, pos=(100,100)):

    if phrase:

        python:
            # get description
            d = [ lexicon[k] for k in lexicon if phrase in k ]
            description = d[0] if len(d) else "No description found."
            description = " ".join( [ k for k in description.split()
                                      if k not in [" ", "\t"] ] )
            
            # move the ypos up by a bit
            pos = ( pos[0], pos[1] - 25 )

            # reformat phrase
            p = [ k for k in lexicon if phrase in k ]
            primary_phrase = p[0][0] if len(p) else phrase
            if primary_phrase != phrase:
                phrase = "{0} ({1})".format(phrase, primary_phrase)

        frame:
            anchor (0.5, 1.0)
            pos pos
            xsize 340
            background Solid("#A9B")
            vbox:
                text "[phrase]" size 18
                text "[description]" size 14

    # Hacky workaround as hyperlink_hovered does not seem to nicely hide this
    # --- Fixed in Ren'Py 7.0 nightlies of May 23rd onwards apparently

    timer 0.5 action Hide("lexicon_popup")

label start:

    e "Start"
    e "Oh look, this VN is made with Ren'Py"
    e "Test phrases that should not make link... AVNX aRenpyx"
    e "Test phrases that should not make link... miniVN{#no-lex} {#no-lex}Renpy-esque"
    e "Mix and match VN with {a=call:label_2}call{/a} {a=jump:label_2}jump{/a} {a=https:www.renpy.org}browser{/a}"
    e "End"

label label_2:
    e "Hiding in different label"
    return

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

Re: Making tooltips disappear when the mouse is not hovering over the word

#2 Post by Ocelot »

Basically, drop the timer from your screen and instead hide screen when hyperlink hovered function receives None as an argument.

https://www.renpy.org/doc/html/style_pr ... _functions
< < insert Rick Cook quote here > >

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

Re: Making tooltips disappear when the mouse is not hovering over the word

#3 Post by Alex »

BeepBoop wrote: Wed Jul 26, 2023 1:53 pm ...So far, I've tried removing the timer, but the tooltip stays up once the player hovers their mouse over it. Any ideas?...
Try

Code: Select all

    def hyperlink_hovered(*args):
        
        if not args[0]:
            # Ren'Py 7+ recent nightly only, see below
            renpy.hide_screen("lexicon_popup")
            renpy.restart_interaction() # <---
        
        elif args[0][:8] == "lexicon:":

BeepBoop
Newbie
Posts: 6
Joined: Wed Jul 26, 2023 1:26 pm
Contact:

Re: Making tooltips disappear when the mouse is not hovering over the word

#4 Post by BeepBoop »

Ocelot wrote: Wed Jul 26, 2023 2:03 pm Basically, drop the timer from your screen and instead hide screen when hyperlink hovered function receives None as an argument.

https://www.renpy.org/doc/html/style_pr ... _functions
Is that not already coded here? (New to Python)

Code: Select all

    def hyperlink_hovered(*args):
        
        if not args[0]:
            # Ren'Py 7+ recent nightly only, see below
            renpy.hide_screen("lexicon_popup")
        
        elif args[0][:8] == "lexicon:":

            renpy.show_screen( "lexicon_popup", 
                               args[0][8:], 
                               renpy.get_mouse_pos() )
            
            renpy.restart_interaction()
        
        return
Alex wrote: Wed Jul 26, 2023 2:04 pm
BeepBoop wrote: Wed Jul 26, 2023 1:53 pm ...So far, I've tried removing the timer, but the tooltip stays up once the player hovers their mouse over it. Any ideas?...
Try

Code: Select all

    def hyperlink_hovered(*args):
        
        if not args[0]:
            # Ren'Py 7+ recent nightly only, see below
            renpy.hide_screen("lexicon_popup")
            renpy.restart_interaction() # <---
        
        elif args[0][:8] == "lexicon:":
It's giving me this error.

Code: Select all

[code]
I'm sorry, but errors were detected in your script. Please correct the
errors listed below, and try again.


File "game/script.rpy", line 163: 'def' is not a keyword argument or valid child of the screen statement.
    def hyperlink_hovered(*args):
       ^

Ren'Py Version: Ren'Py 8.1.1.23060707
Wed Jul 26 11:13:45 2023
[/code]

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

Re: Making tooltips disappear when the mouse is not hovering over the word

#5 Post by Alex »

BeepBoop wrote: Wed Jul 26, 2023 2:16 pm ...It's giving me this error. ...
You did something wrong with your code - all you have to do is just add one line (marked in my post) to the code you've posted in first post.

BeepBoop
Newbie
Posts: 6
Joined: Wed Jul 26, 2023 1:26 pm
Contact:

Re: Making tooltips disappear when the mouse is not hovering over the word

#6 Post by BeepBoop »

Alex wrote: Wed Jul 26, 2023 3:43 pm
BeepBoop wrote: Wed Jul 26, 2023 2:16 pm ...It's giving me this error. ...
You did something wrong with your code - all you have to do is just add one line (marked in my post) to the code you've posted in first post.
Fixed it and now it works!

Thank you!

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]