commit ee2f1cc91319e0d9dc6baca4f9422edd08d127ec
parent 5be4277ff865dd6a6fb95181f75e87e5e02ced58
Author: Erik Letson <hmagellan@hmagellan.com>
Date: Sat, 17 Oct 2020 18:30:30 -0500
Fixed multi-line rendering in scenes
Diffstat:
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/src/scene.py b/src/scene.py
@@ -1,6 +1,6 @@
import pygame, os, json
from . import manager, vgo
-from .constants import SCENE_JSON_PATH, FONT_PATH
+from .constants import SCREEN_WIDTH, SCENE_JSON_PATH, FONT_PATH
############
# scene.py #
@@ -91,6 +91,7 @@ class StillScene(object):
self.displayed_strings = [""] # list of text lines
self.rendered_text_surfaces = [None]
self.line_number = 0
+ self.current_line_starting_index = 0
self.current_font = None # An index of the 'fonts' value
self.current_font_height = 0
self.current_text_voice = None
@@ -132,13 +133,14 @@ class StillScene(object):
self.displayed_strings = [""]
self.rendered_text_surfaces = [None]
self.line_number = 0
+ self.current_line_starting_index = 0
self.displayed_characters.empty()
# Next, load up the next segment
self.script_index += 1
self.current_font = self.fonts[self.script[self.script_index]["line_font"]]
self.current_font_height = self.current_font.get_height()
- self.max_line = int(3200 / self.current_font_height)
+ self.max_line = int((3 * SCREEN_WIDTH) / self.current_font_height)
self.current_text_string = self.script[self.script_index]["line"]
for c in self.script[self.script_index]["characters"]:
nc = vgo.VisibleGameObject(self.manager.game.sheet_manager.loaded_sheets[c["sheet"]], tuple(c["sprite"]))
@@ -150,6 +152,11 @@ class StillScene(object):
Write out the text of a script and render it to
a pygame Font surface.
"""
+ # TODO: Needs to be able to scroll down when too many lines
+ # are written at once. Just delete the 0th line and
+ # drop all lines down 1 in index. Hard part will be
+ # to dynamically determine how many lines is the max
+ # based on font height and screen dimensions.
# If we aren't ready to write a new char yet, don't
if self.text_write_timer < self.text_speed and not self.continue_ready:
self.text_write_timer += 1
@@ -162,23 +169,27 @@ class StillScene(object):
else:
self.text_write_timer = 0
self.current_text_char_index += 1
- self.displayed_strings[self.line_number] = self.current_text_string[self.max_line * self.line_number:self.current_text_char_index]
+ self.displayed_strings[self.line_number] = self.current_text_string[self.current_line_starting_index:self.current_text_char_index]
# Check if we need to add a new line
if len(self.displayed_strings[self.line_number]) > self.max_line:
self.displayed_strings.append("")
self.rendered_text_surfaces.append(None)
self.line_number += 1
+ self.current_line_starting_index = self.current_text_char_index - 1
# Factor in line breaks in the middle of words
ch = len(self.displayed_strings[self.line_number - 1]) - 1
while self.displayed_strings[self.line_number - 1][ch] != " ":
- self.displayed_strings[self.line_number] += self.displayed_strings[self.line_number - 1][ch]
self.displayed_strings[self.line_number - 1] = self.displayed_strings[self.line_number - 1][0:ch]
+ self.current_line_starting_index -= 1
ch -= 1
- # Render the preceding line one last time
- self.rendered_text_surfaces[self.line_number - 1] = self.current_font.render(self.displayed_strings[self.line_number - 1], False, (0, 0, 0)).convert()
+ # Render the preceding line one last time and invert the carryover
+ com2 = self.displayed_strings[self.line_number - 1]
+ while len(com2) > 0 and com2[0] == " ":
+ com2 = com2[1:len(com2)]
+ self.rendered_text_surfaces[self.line_number - 1] = self.current_font.render(com2, False, (0, 0, 0)).convert()
# Compensate for initial white space
compen = self.displayed_strings[self.line_number]