In renpy write script, the script highlight the text we hovering with mouse (2s) and can…
- User
In renpy write script, the script highlight the text we hovering with mouse (2s) and can see description of this word. Provide full code
- Assistant
In renpy write script, the script highlight the text we hovering with mouse (2s) and can see description of this word. Provide full code
Creating a hover effect for individual words in Ren'Py involves breaking the text into interactive elements (such as buttons) and managing hover states with timing logic. Below is a complete Ren'Py script that demonstrates how to highlight a word when the mouse hovers over it for 2 seconds and display a description of that word.
Create a new Ren'Py project and replace the contents of script.rpy with the following code. Additionally, ensure that you have the necessary style definitions in your options.rpy or within the same script.rpy as needed.
# script.rpy
init python:
import renpy.exports as renpy
# Define a list of words with their descriptions
WORDS = [
{"text": "Ren'Py", "description": "A visual novel engine that helps you create visual novels easily."},
{"text": "hover", "description": "An action where the mouse pointer rests over a particular element."},
{"text": "highlight", "description": "To make something stand out visually."},
{"text": "description", "description": "Detailed information about something."},
]
# Create a dictionary to hold state for each word
word_states = {}
for index, word in enumerate(WORDS):
word_states[index] = {
"hover": False,
"timer": None,
"highlighted": False,
}
# Function to handle hover start
def on_hover_start(index, show_description_callback):
def callback():
word_states[index]["highlighted"] = True
show_description_callback(WORDS[index]["description"])
# Start a timer for 2 seconds
word_states[index]["timer"] = renpy.call_in_new_context(callback, delay=2.0)
# Function to handle hover end
def on_hover_end(index, hide_description_callback):
if word_states[index]["timer"] is not None:
renpy.cancel_context(word_states[index]["timer"])
word_states[index]["timer"] = None
if word_states[index]["highlighted"]:
word_states[index]["highlighted"] = False
hide_description_callback()
screen interactive_text:
# Background box for the description
if persistent.hover_description:
text "[persistent.hover_description]" pos (10, 300) size 20 xalign 0.0
# Display the interactive words
fixed:
ypos 100
xmargin 50
ymargin 100
# Iterate through the words and create buttons
for i, word in enumerate(WORDS):
# Add space between words except for the last word
if i != 0:
text " "
# Word button
if word_states[i]["highlighted"]:
# Highlighted style
style_prefix = "hovered_"
else:
style_prefix = "default_"
textbutton word["text"] action NullAction() style_prefix=""
# Overlay the hover behavior
at ListGridLayout(cols=100)
# Alternatively, use a more flexible layout
vbox:
ypos 100
xpadding 50
ypadding 100
spacing 0
hbox:
spacing 10
for i, word in enumerate(WORDS):
textbutton word["text"] action NullAction()
hovered SetVariable("current_hover", i)
unhovered SetVariable("current_hover", -1)
# Apply the appropriate style
style_prefix [ "hovered_" if word_states[i]["highlighted"] else "default_" ]
label start:
# Initialize a variable to track current hover
$ current_hover = -1
# Show the interactive text screen
show screen interactive_text
# Python loop to monitor hover
python:
def show_description(desc):
persistent.hover_description = desc
def hide_description():
persistent.hover_description = None
while True:
if current_hover != -1:
state = word_states[current_hover]
if not state["hover"]:
state["hover"] = True
on_hover_start(current_hover, show_description)
else:
for index, state in word_states.items():
if state["hover"]:
state["hover"] = False
on_hover_end(index, hide_description)
renpy.pause(0.1)
"Hover over the highlighted words to see their descriptions after 2 seconds."
"For example, try hovering over the word 'Ren'Py'."
return
# Define styles for default and hovered words
init style:
style default_hovered_textbutton:
# Default word style
text_align 0
xminimum 1
yminimum 1
padding (0, 0)
background None
foreground "#FFFFFF"
hovered_foreground "#FFFF00"
style hovered_hovered_textbutton:
# Highlighted word style
text_align 0
xminimum 1
yminimum 1
padding (0, 0)
background None
foreground "#FF0000"
hovered_foreground "#FF8800"
# Prevent the description from persisting between sessions
init python:
if not hasattr(persistent, 'hover_description'):
persistent.hover_description = None
Word Definitions: The WORDS list contains dictionaries with each word and its corresponding description.
State Management: The word_states dictionary keeps track of each word’s hover state, whether it is highlighted, and any active timer.
Hover Handling Functions:
on_hover_start: Initiates a 2-second timer when the mouse starts hovering over a word. After 2 seconds, it highlights the word and displays its description.on_hover_end: Cancels the timer if the mouse moves away before 2 seconds and removes the highlight and description if they were set.Interactive Text Screen:
interactive_text screen displays each word as a textbutton. The appearance changes based on whether the word is highlighted.hovered event), it sets the current_hover variable to the word's index. When the hover ends (unhovered event), it resets current_hover to -1.Main Label:
start label initializes the current_hover variable and displays the interactive_text screen.on_hover_start; if it ends, it calls on_hover_end.Styles:
default_hovered_textbutton: The default style for words.hovered_hovered_textbutton: The style applied when a word is highlighted after being hovered for 2 seconds.Description Display:
pos (10, 300)), but you can adjust the positioning as needed.Adding More Words: Extend the WORDS list with additional words and descriptions.
WORDS = [
{"text": "Ren'Py", "description": "A visual novel engine that helps you create visual novels easily."},
{"text": "hover", "description": "An action where the mouse pointer rests over a particular element."},
{"text": "highlight", "description": "To make something stand out visually."},
{"text": "description", "description": "Detailed information about something."},
# Add more words here
]
Styling: Adjust the styles in the init style block to change the appearance of highlighted and default words. For example, you can change colors, fonts, or add backgrounds.
Description Positioning: Modify the pos (10, 300) in the description text to reposition where the description appears on the screen.
Performance: This script uses a simple loop with renpy.pause(0.1) to check the hover state. For larger texts or more complex interactions, consider optimizing the hover detection logic.
Scalability: For very long texts, splitting the text into individual buttons might be cumbersome. In such cases, consider implementing more advanced text parsing and rendering techniques.
Ren'Py Versions: Ensure you are using a recent version of Ren'Py that supports the features used in this script.
Create a New Project: Open Ren'Py and create a new project.
Replace Script: Replace the contents of script.rpy with the above code.
Run the Project: Launch the project to see the interactive text in action. Hover over the specified words to see them highlight and display their descriptions after 2 seconds.
Feel free to customize and expand upon this script to fit the needs of your visual novel!