jsfehler / renpy-encyclopaedia

A Ren'Py plugin for creating an Encyclopaedia/Glossary/Bestiary or similar system.
https://renpy-encyclopaedia.readthedocs.io/
MIT License
58 stars 6 forks source link

Add hover tooltip (description) on EncEntry-word #8

Open Richd4y opened 1 year ago

Richd4y commented 1 year ago

Implemented separately here by @valery-iwanofu: https://github.com/valery-iwanofu/renpy-word-description

Add optional attributes hint_desc and hint_note to EncEntry class?

image image image

Richd4y commented 1 year ago

We can add additional information directly to the line with the link (and show it in the frame on hover), for example:

def set_encentry_from_text_anchor(value: str) -> None:
    """Parse a Ren'Py text anchor to open an EncEntry.

    This function is added to Ren'Py's config.hyperlink_handlers with the key
    "set_entry"

    Args:
        value: A string in the format of "Encyclopaedia->EncEntry->hint_description->hint_note"
    """
    enc_name, entry_name, entry_hint_description, entry_hint_note = value.split('->')

    enc: 'Encyclopaedia' = getattr(store, enc_name)
    entry: 'EncEntry' = getattr(store, entry_name)

    enc.active = entry

    # Concole test event "show tooltip on hover" 
    print(f"Name: {entry}")
    print(f"Desc: {entry_hint_description}")
    print(f"Note: {entry_hint_note}")

    # Open the Encyclopaedia, the screen will open the active entry for us.
    ShowMenu(enc.list_screen, enc=enc)()

A better way, which I wrote about at the beginning of the topic, is to specify information during the creation of EncEntry objects.

label setup_enc:

    python:
        library_enc = Encyclopaedia(
            name="Wanderer in the Library",
            show_locked_buttons=True,  # show unseen (???) buttons in codex list or no
            sorting_mode=3,
        )

        # Register a callback function.
        library_enc.on('entry_unlocked')(notify_entry_unlocked)

        # Use EncEntryTemplate to set reasonable defaults and reduce duplication
        LibraryEntry = EncEntryTemplate(parent=library_enc, locked=True)

        about_custom = LibraryEntry(
                    name=_('Custom'),
                    subject=_('Other'),
                    text=[
                        "Cusotm info done succesfully",
                    ],
                    hint_description=["desc1", "desc2", "desc3"],
                    hint_note="This is a short one-string note."
                )

And get it later from the object in hyperlink_ren.py with set_encentry_from_text_anchor:

def set_encentry_from_text_anchor(value: str) -> None:
    """Parse a Ren'Py text anchor to open an EncEntry.

    This function is added to Ren'Py's config.hyperlink_handlers with the key
    "set_entry"

    Args:
        value: A string in the format of "Encyclopaedia->EncEntry"
    """
    enc_name, entry_name = value.split('->')

    enc: 'Encyclopaedia' = getattr(store, enc_name)
    entry: 'EncEntry' = getattr(store, entry_name)

    # Example logic (not sure if it's the right thing to do)
    hint_description: 'EncEntry' = getattr(store, entry_hint_description)
    hint_note: 'EncEntry' = getattr(store, entry_hint_note)

    enc.active = entry

    # Concole test event "show tooltip on hover" 
    print(f"Name: {entry}")
    print(f"Desc: {hint_description}")
    print(f"Note: {hint_note}")

    # Open the Encyclopaedia, the screen will open the active entry for us.
    ShowMenu(enc.list_screen, enc=enc)()
jsfehler commented 1 year ago

I can make the hyperlink more flexible by letting you specify the screen that the hyperlink will call. You could then create a separate view for when hyperlinks are clicked. I don't think adding hint attributes is worth it. If you need hints that sounds like a separate system.

Richd4y commented 1 year ago

Considering tooltips as part of the codex/glossary system to be able to give the player the bare minimum of knowledge (short description) without having to open the glossary screen where the full (long) description will be given.

Example image

Solved for me with subclass:

class MyEntry(EncEntry):
    def __init__(self, *args, hint_description: list|str, hint_note: list|str, **kwargs):
        super().__init__(*args, **kwargs)
        self.hint_description = hint_description
        self.hint_note = hint_note

And a hyperlink modified in a custom tag:

screen glossary_word(entry):
    $ focus_rect = GetFocusRect('glossary_tooltip')
    if focus_rect:
        fixed:
            xfit True
            yfit True
            use glossary_word_frame(entry, focus_rect)

screen glossary_word_frame(entry, focus_rect):
    frame:
        xmaximum 600

        # __FitChild will place frame at the screen so that it doesn't go off the screen
        at renpy.curry(__FitChild)(focus_rect=focus_rect)

        style_prefix 'glossary'

        has vbox
        text entry.name style_suffix 'title_text'
        vbox:
            for line in entry.hint_description:
                text line style_suffix 'description_text'

            text entry.hint_note

init python:
    class __EncentryGlossaryShow:
        def __init__(self, enc, entry):
            self.enc = enc
            self.entry = entry

        def __call__(self):
            enc = self.enc
            enc.active = self.entry
            return ShowMenu(enc.list_screen, enc=enc)()

    def encentry_glossary_tag(tag, argument, contents):
        # modified version of hyperlink_ren.set_encentry_from_text_anchor
        enc_name, entry_name = argument.split('->')

        enc: 'Encyclopaedia' = getattr(store, enc_name)
        entry: 'EncEntry' = getattr(store, entry_name)

        open_glossary_action = __EncentryGlossaryShow(enc, entry)

        button = TextButton(
            contents, 
            text_tokenized=True,
            hovered=[CaptureFocus('glossary_tooltip'), ShowTransient('glossary_word', None, entry)],
            unhovered=[ClearFocus('glossary_tooltip'), Hide('glossary_word')],
            action=__EncentryGlossaryShow(enc, entry),
            style='glossary_button'
        )

        return [
            (renpy.TEXT_DISPLAYABLE, button)
        ]

    config.custom_text_tags["glossary"] = encentry_glossary_tag