-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitle.py
159 lines (141 loc) · 5.29 KB
/
title.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
from logging import Handler, getLevelName
class CustomHandler(Handler):
"""
This is a custom logging handler, which you can put a function in to run every log
"""
def __init__(self, func=None):
"""
Initialize the handler, with an optional function. Without a function, this is pretty useless.
"""
Handler.__init__(self)
if func is None:
func = lambda: None
self.func = func
def emit(self, record):
msg = self.format(record)
self.func(msg)
def __repr__(self):
level = getLevelName(self.level)
return '<CustomHandler (%s)>' % level
from threading import Thread
import pygame, os
import multiprocessing as MP
class TitleScreen:
def __init__(self, width, height, amount):
self.STATUS = [0, amount]
self.size = (width, height)
self.txt = ''
self.t = None
self.t2 = None
self.joining = False
def set_txt(self, txt):
self.txt = txt
self.updateQ()
def update(self):
self.STATUS[0] += 1
self.updateQ()
def whileloading(self, border, loadingtxtColour, Q, abortEvent):
# Create the loading bar surface
pygame.init()
window = pygame.display.set_mode((700, 300), pygame.NOFRAME)
window.fill((255, 255, 255))
title = pygame.image.load('images/FoxIcon.png')
pygame.display.set_icon(title)
pygame.display.set_caption('Loading Blaze Sudios...')
window.blit(title, ((window.get_width()-title.get_width())/2, (window.get_height()-title.get_height())/2))
pygame.display.update()
bar = pygame.Surface(self.size)
bar.fill(0)
bar.set_colorkey(0)
font = pygame.font.SysFont('Arial', 20)
prev_screen = window.copy()
running = True
clock = pygame.time.Clock()
dots = '.'
dotcounter = 0
while running:
x, y = (window.get_width() - 600) // 2, (window.get_height() - 50) // 2
# Handle the events
for event in pygame.event.get():
# If the user clicks the close button, exit the loop
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
running = False
abortEvent.set()
break
try:
gottenq = Q.get_nowait()
self.STATUS = gottenq[0]
self.txt = gottenq[1]
except:
pass # Timeout - nothing was sent
dotcounter += 1
if dotcounter > 60 / 3:
if dots == '.': dots = '..'
elif dots == '..': dots = '...'
elif dots == '...': dots = '.'
dotcounter = 0
# Clear the window
window.fill((255, 255, 255))
window.blit(prev_screen, (0, 0))
# Draw the loading bar border
pygame.draw.rect(window, 0, (x, y, bar.get_width(), bar.get_height()))
# Draw the loading bar fill
try: perc = 100/self.STATUS[1] * self.STATUS[0]
except ZeroDivisionError: perc = 0
perc = (perc * 100) // 100
pygame.draw.rect(bar, (10, 255, 50), (border, border, (bar.get_width() - 2 * border) / 100 * perc, bar.get_height() - 2 * border))
window.blit(font.render(self.txt.format(str(self.STATUS[0]), str(self.STATUS[1]), str(perc), dots), 1, loadingtxtColour), (0, 0))
# Blit the loading bar surface onto the window
window.blit(bar, (x, y))
# Update the display
pygame.display.flip()
clock.tick(60)
if self.STATUS[0] >= self.STATUS[1]:
running = False
break
self.txt = ''
pygame.display.quit()
def wait_for_aborted(self):
while not self.joining:
if self.EV.is_set():
os._exit(1)
def updateQ(self):
self.Q.put((self.STATUS, self.txt))
def __call__(self, border_width, loadingtxt='Loading{3} {2}% ({0} / {1})', loadingtxtColour=(0, 0, 0)):
if self.txt == '': # If something changed the text before it got time to initialise
self.txt = loadingtxt
self.Q = MP.Queue()
self.EV = MP.Event()
self.t = MP.Process(
target=self.whileloading, args=(
border_width, loadingtxtColour, self.Q, self.EV),
daemon=True
)
self.t.start()
self.joining = False
self.t2 = Thread(target=self.wait_for_aborted, daemon=True)
self.t2.start()
def join(self):
self.t.join()
self.joining = True
self.t2.join()
def wrapdemo(Q, EV, started, demoname):
import demos
def newprint(*msg, sep=' ', end='\n'):
Q.put([0, sep.join(msg)+end])
def newinp(msg):
EV.clear()
Q.put([1, msg])
while not EV.is_set():
pass # Don't block the process
return Q.get()
func = getattr(demos, demoname)
func.__globals__['print'] = newprint
func.__globals__['input'] = newinp
started.set()
try:
func()
except Exception as e:
Q.put([2, e])
while True:
pass # Breakpoint here to catch error