forked from MyreMylar/pygame_gui_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_test.py
184 lines (157 loc) · 8.61 KB
/
text_test.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
import pygame
import pygame_gui
from pygame_gui.ui_manager import UIManager
from pygame_gui.elements.ui_text_box import UITextBox
from pygame_gui.core import IncrementalThreadedResourceLoader, ObjectID
from pygame_gui import UI_TEXT_BOX_LINK_CLICKED, UI_TEXT_EFFECT_FINISHED
"""
Font load time taken: 0.911 seconds.
Time taken 1st window: 1.509 seconds.
Time taken 2nd window: 0.181 seconds.
"""
def create_large_text_box():
return UITextBox(
'<font face=Montserrat color=regular_text><font color=#E784A2 size=4.5>'
'<br><b><u><effect id=spin_me>Lorem</effect></u><br><br><br>'
'ipsum dolor sit amet</b></font>,'
' <b><a href="test"><effect id=spin_me>consectetur</effect></a></b> '
'adipiscing elit. in a flibb de dib do '
'rub a la clob slip the perry tin fo glorp yip dorp'
'skorp si pork flum de dum be dung, slob be robble glurp destination flum kin '
'slum. Ram slim gordo, fem '
'tulip squirrel slippers save socks certainly.<br>'
'Vestibulum in <i>commodo me</i> tellus in nisi finibus a sodales.<br>Vestibulum '
'<font size=2>hendrerit mi <i>sed nulla</i> scelerisque</font>, posuere ullamcorper '
'sem pulvinar.'
'Nulla at pulvinar a odio, a dictum dolor.<br>Maecenas at <font size=6><b>tellus a'
'tortor. a<br>'
'In <i><effect id=spin_me>bibendum</effect></i> orci et velit</b> gravida lacinia.'
'<br><br>'
'In hac a habitasse to platea dictumst.<br>'
'<font color=#4CD656 size=4>Vivamus I interdum mollis lacus nec porttitor.<br>Morbi '
'accumsan, lectus at '
'tincidunt to dictum, neque <font color=#879AF6>erat tristique erat</font>, '
'sed a tempus for <b>nunc</b> dolor in nibh.<br>'
'Suspendisse in viverra dui <i>fringilla dolor laoreet</i>, '
'sit amet on pharetra a ante '
'sollicitudin.</font></font>'
'<br><br>'
'<b><effect id=spin_me>consectetur</effect></b> adipiscing elit. in a<br>'
'Vestibulum in <i>commodo me</i> tellus in nisi finibus a sodales.<br>'
'Vestibulum <font size=2>hendrerit mi <i>sed nulla</i> scelerisque</font>,'
' posuere ullamcorper '
'sem pulvinar. '
'Nulla at pulvinar a odio, a dictum dolor.<br>'
'Maepenas at <font size=6><b>tellus a tortor. a<br>'
'In <i>bibendum</i> orci et velit</b> gravida lacinia.<br><br>'
'In hac a habitasse to platea dictumst.<br>'
'<font color=#4CD656 size=4>Vivamus I interdum mollis lacus nec porttitor.<br>Morbi '
'accumsan, lectus at'
'tincidunt to dictum, neque <font color=#879AF6>erat tristique erat</font>, '
'sed a <effect id=spin_me>tempus</effect> for <b><effect id=spin_me>nunc</effect>'
'</b> dolor in nibh.<br>'
'Suspendisse in viverra dui <i>fringilla dolor laoreet</i>, sit amet on'
' pharetra a ante '
'sollicitudin.</font></font>',
pygame.Rect(10, 10, 500, 580),
manager=ui_manager,
object_id='#text_box_1')
pygame.init()
pygame.display.set_caption("Text test")
screen_size = (800, 600)
screen = pygame.display.set_mode(screen_size) # FULLSCREEN
background_surface = pygame.Surface(screen_size)
background_surface.fill(pygame.Color("#000000"))
loader = IncrementalThreadedResourceLoader()
clock = pygame.time.Clock()
ui_manager = UIManager(screen_size, 'data/themes/theme_1.json', resource_loader=loader)
ui_manager.add_font_paths("Montserrat",
"data/fonts/Montserrat-Regular.ttf",
"data/fonts/Montserrat-Bold.ttf",
"data/fonts/Montserrat-Italic.ttf",
"data/fonts/Montserrat-BoldItalic.ttf")
load_time_1 = clock.tick()
ui_manager.preload_fonts([{'name': 'Montserrat', 'html_size': 4.5, 'style': 'bold'},
{'name': 'Montserrat', 'html_size': 4.5, 'style': 'regular'},
{'name': 'Montserrat', 'html_size': 2, 'style': 'regular'},
{'name': 'Montserrat', 'html_size': 2, 'style': 'italic'},
{'name': 'Montserrat', 'html_size': 6, 'style': 'bold'},
{'name': 'Montserrat', 'html_size': 6, 'style': 'regular'},
{'name': 'Montserrat', 'html_size': 6, 'style': 'bold_italic'},
{'name': 'Montserrat', 'html_size': 4, 'style': 'bold'},
{'name': 'Montserrat', 'html_size': 4, 'style': 'regular'},
{'name': 'Montserrat', 'html_size': 4, 'style': 'italic'},
{'name': 'fira_code', 'html_size': 2, 'style': 'regular'},
{'name': 'fira_code', 'html_size': 2, 'style': 'bold'},
{'name': 'fira_code', 'html_size': 2, 'style': 'bold_italic'}
])
loader.start()
finished_loading = False
while not finished_loading:
finished_loading, progress = loader.update()
load_time_2 = clock.tick()
print('Font load time taken:', load_time_2/1000.0, 'seconds.')
time_1 = clock.tick()
html_text_line = create_large_text_box()
time_2 = clock.tick()
htm_text_block_2 = UITextBox('<font face=fira_code size=2 color=#000000><b>Hey, What the heck! </b>'
'<br><br>'
'<body bgcolor=#A0A050>This is</body> some <a href="test">text</a> '
'in a different box,'
' hooray for variety - '
'if you want then you should put a ring upon it. '
'<body bgcolor=#990000>What if we do a really long word?</body> '
'<b><i>derp FALALALALALALALXALALALXALALALALAAPaaaaarp gosh'
'</b></i></font>',
pygame.Rect((520, 10), (250, 200)),
manager=ui_manager,
object_id=ObjectID(class_id="@white_text_box",
object_id="#text_box_2"))
htm_text_block_2.set_active_effect(pygame_gui.TEXT_EFFECT_TYPING_APPEAR)
time_3 = clock.tick()
print('Time taken 1st window:', time_2/1000.0, 'seconds.')
print('Time taken 2nd window:', time_3/1000.0, 'seconds.')
ui_manager.print_unused_fonts()
running = True
while running:
time_delta = clock.tick(60)/1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_f:
htm_text_block_2.set_active_effect(pygame_gui.TEXT_EFFECT_FADE_OUT)
if event.key == pygame.K_g:
htm_text_block_2.set_active_effect(pygame_gui.TEXT_EFFECT_FADE_IN)
if event.key == pygame.K_h:
htm_text_block_2.set_active_effect(pygame_gui.TEXT_EFFECT_TYPING_APPEAR)
if event.key == pygame.K_k:
html_text_line.kill()
if event.key == pygame.K_b:
html_text_line = create_large_text_box()
if event.type == pygame.KEYDOWN and event.key == pygame.K_x:
html_text_line.set_active_effect(pygame_gui.TEXT_EFFECT_TILT,
effect_tag='spin_me')
if event.type == pygame.KEYDOWN and event.key == pygame.K_c:
html_text_line.set_active_effect(pygame_gui.TEXT_EFFECT_EXPAND_CONTRACT,
params={'max_scale': 5.0},
effect_tag='spin_me')
if event.type == pygame.KEYDOWN and event.key == pygame.K_v:
html_text_line.set_active_effect(pygame_gui.TEXT_EFFECT_BOUNCE,
params={'bounce_max_height': 50,
'time_to_complete_bounce': 0.8},
effect_tag='spin_me')
if event.type == UI_TEXT_BOX_LINK_CLICKED:
if event.ui_element is htm_text_block_2:
if event.link_target == 'test':
print('clicked test link')
else:
print('clicked link in text block 1')
if event.type == UI_TEXT_EFFECT_FINISHED:
print("finished text effect: " + event.effect)
ui_manager.process_events(event)
ui_manager.update(time_delta)
screen.blit(background_surface, (0, 0))
ui_manager.draw_ui(screen)
pygame.display.update()