-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMultiLineText.py
104 lines (84 loc) · 3.44 KB
/
MultiLineText.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import pygame.font
from pygame import Surface
class MultiLineText:
def __init__(self, x, y, w, h, color="white", size=24, font=pygame.font.get_default_font(), text="", save_len=-1, save_index_start=None):
self.x, self.y, self.w, self.h = x, y, w, h
self.size = size
self.color = color
self.text_lines = []
if len(text) != 0:
self.text_lines.append(text)
self.font = pygame.font.Font(font, self.size)
self.save_len = save_len
self.save_index = save_index_start
self.margin = 10
def change_text(self, text: str):
if "\n" in text:
text_lines = text.split("\n")
else:
text_lines = [text]
self.text_lines = text_lines
def add_text(self, text: str):
self.text_lines.append(text)
def add_all_text(self, texts: list[str]):
for text in texts:
self.add_text(text)
def add_last(self, text: str):
index_new_last = len(self.text_lines[-1])
self.text_lines[-1] += text
return index_new_last
def remove_last(self):
if len(self.text_lines[-1]) > self.save_len:
self.text_lines[-1] = self.text_lines[-1][:-1]
def insert_last(self, text, index):
self.text_lines[-1] = self.text_lines[-1][:index] + text + self.text_lines[-1][index:]
def remove_last_index(self, start, end):
if start > self.save_index:
self.text_lines[-1] = self.text_lines[-1].replace(self.get_from_last_index(start, end), "")
def get_from_last_index(self, start, end):
return self.text_lines[-1][start:end]
def new_line(self):
self.text_lines.append("")
def get_last(self):
return self.text_lines[-1]
def refresh(self, screen: Surface):
self._render_multi_line(screen)
def clear(self):
self.text_lines = []
def __create_text_surface(self, text: str):
return self.font.render(text, True, self.color)
def _render_multi_line(self, screen: Surface):
surfaces = self._generate_surface_all_text()
for i, s in enumerate(surfaces):
self._render_surface(s, screen, i)
def _render_surface(self, surface: Surface, screen: Surface, i):
rect = surface.get_rect()
height_text, width_text = rect.height, rect.width
screen.blit(surface, (self.x, self.y + (height_text + self.margin) * i))
def _generate_surface_all_text(self) -> list[Surface]:
surfaces = []
for i in self.text_lines:
surfaces += self.__generate_line_surfaces(i)
return surfaces
def __generate_line_surfaces(self, line: str) -> list[Surface]:
surfaces = []
now_surface = None
now_text = ""
while len(line) != 0:
if now_surface is None:
now_surface = self.__create_text_surface(line[0])
now_text = line[0]
line = line[1:]
else:
sim_w = self.font.size(line[0])[0]
if now_surface.get_rect().width + sim_w > self.w:
surfaces.append(now_surface)
now_text = ""
now_surface = None
else:
now_text += line[0]
now_surface = self.__create_text_surface(now_text)
line = line[1:]
if now_text != "" and now_surface is not None:
surfaces.append(now_surface)
return surfaces