pygstormgames/display.py
2025-02-23 13:26:14 -05:00

161 lines
5.3 KiB
Python

"""Display management module for PygStormGames.
Handles text display, navigation, and information presentation including:
- Text display with navigation
- Instructions display
- Credits display
- Donation link handling
"""
import os
import webbrowser
import pyglet
from pyglet.window import key
import pyperclip
import wx
class Display:
"""Handles display and text navigation systems."""
def __init__(self, gameTitle):
"""Initialize display system.
Args:
gameTitle (str): Title of the game
"""
self.window = pyglet.window.Window(800, 600, caption=gameTitle)
self.currentText = []
self.currentIndex = 0
self.gameTitle = gameTitle
def display_text(self, text, speech):
"""Display and navigate text with speech output.
Args:
text (list): List of text lines to display
speech (Speech): Speech system for audio output
"""
# Store original text with blank lines for copying
self.originalText = text.copy()
# Create navigation text by filtering out blank lines
self.navText = [line for line in text if line.strip()]
# Add instructions at start
instructions = ("Press space to read the whole text. Use up and down arrows to navigate "
"the text line by line. Press c to copy the current line to the clipboard "
"or t to copy the entire text. Press enter or escape when you are done reading.")
self.navText.insert(0, instructions)
# Add end marker
self.navText.append("End of text.")
self.currentIndex = 0
speech.speak(self.navText[self.currentIndex])
@self.window.event
def on_key_press(symbol, modifiers):
if symbol in (key.ESCAPE, key.RETURN):
self.window.remove_handler('on_key_press', on_key_press)
return
if symbol in (key.DOWN, key.S) and self.currentIndex < len(self.navText) - 1:
self.currentIndex += 1
speech.speak(self.navText[self.currentIndex])
if symbol in (key.UP, key.W) and self.currentIndex > 0:
self.currentIndex -= 1
speech.speak(self.navText[self.currentIndex])
if symbol == key.SPACE:
speech.speak('\n'.join(self.originalText[1:-1]))
if symbol == key.C:
try:
pyperclip.copy(self.navText[self.currentIndex])
speech.speak("Copied " + self.navText[self.currentIndex] + " to the clipboard.")
except:
speech.speak("Failed to copy the text to the clipboard.")
if symbol == key.T:
try:
pyperclip.copy(''.join(self.originalText[2:-1]))
speech.speak("Copied entire message to the clipboard.")
except:
speech.speak("Failed to copy the text to the clipboard.")
def instructions(self, speech):
"""Display game instructions from file.
Args:
speech (Speech): Speech system for audio output
"""
try:
with open('files/instructions.txt', 'r') as f:
info = f.readlines()
except:
info = ["Instructions file is missing."]
self.display_text(info, speech)
def credits(self, speech):
"""Display game credits from file.
Args:
speech (Speech): Speech system for audio output
"""
try:
with open('files/credits.txt', 'r') as f:
info = f.readlines()
# Add the header
info.insert(0, f"{self.gameTitle}: brought to you by Storm Dragon")
except:
info = ["Credits file is missing."]
self.display_text(info, speech)
def messagebox(self, text):
"""Display a simple message box with text.
Shows a message that can be repeated until the user chooses to continue.
Args:
text (str): Message to display
"""
text = text + "\nPress any key to repeat or enter to continue."
while True:
# Speak the text
self.game.speech.speak(text)
# Wait for any key
key = self.game.wait()
# Exit if enter/escape pressed
if key in (pyglet.window.key.RETURN, pyglet.window.key.ESCAPE):
break
def get_input(self, prompt="Enter text:", defaultText=""):
"""Display a dialog box for text input.
Args:
prompt (str): Prompt text to display
defaultText (str): Initial text in input box
Returns:
str: User input text, or None if cancelled
"""
app = wx.App(False)
dialog = wx.TextEntryDialog(None, prompt, "Input", defaultText)
dialog.SetValue(defaultText)
if dialog.ShowModal() == wx.ID_OK:
userInput = dialog.GetValue()
else:
userInput = None
dialog.Destroy()
return userInput
def donate(self, speech):
"""Open the donation webpage."""
speech.speak("Opening donation page.")
webbrowser.open('https://ko-fi.com/stormux')