-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgets.py
127 lines (104 loc) · 3.97 KB
/
widgets.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
#!/usr/bin/env python3
import pygame
from datetime import datetime, timedelta
from constants import *
class Ticker:
LENGTH = 9
def __init__(self, surface, font_size=0, offset_y=0):
self.surface = surface
self.offset_y = offset_y
self.preferred_font_size = font_size
self.__update_font()
def on_resize(self, new_surface):
self.surface = new_surface
self.__update_font()
def render(self, ticker_text, color):
self.__render_text(ALL_ON_CHAR * self.LENGTH, COLOR_TEXT_OFF)
self.__render_text(ticker_text, color)
def __render_text(self, text, color):
text = self.font.render(text, True, color)
x, y = self.surface.get_size()
text_xy = (x // 2 - text.get_width() // 2,
y // 2 - text.get_height() // 2 + self.offset_y)
self.surface.blit(text, text_xy)
def __update_font(self):
if self.preferred_font_size > 0:
font_size = self.preferred_font_size
else:
font_size = 8
while True:
x, y = self.surface.get_size()
font = self.__get_font(font_size + 2)
font_x, font_y = font.size(ALL_ON_CHAR * self.LENGTH)
if font_x <= (x - 20) and font_y <= (y - 10):
font_size += 2
else:
break
self.font = self.__get_font(font_size)
pass
def __get_font(self, size):
try:
font = pygame.font.Font(DEFAULT_FONT, size)
return font
except:
print(f"ERROR: font {DEFAULT_FONT} not found! Exiting...")
sys.exit(-1)
class WallClock:
DEFAULT_TEXT = ALL_ON_CHAR * 2 + ":" + ALL_ON_CHAR * 2
def __init__(self, surface, font_size):
self.surface = surface
self.font_size = font_size
self.__update_font()
def on_resize(self, new_surface):
self.surface = new_surface
self.__update_font()
def render(self, color):
self.__render_text(self.DEFAULT_TEXT, COLOR_TEXT_OFF)
now = datetime.now()
if (now.microsecond < (500000)) == 0:
separator = ":"
else:
separator = " "
ticker_text = now.strftime("%I" + separator + "%M")
self.__render_text(ticker_text, color)
def __render_text(self, text, color):
text = self.font.render(text, True, color)
x, y = self.surface.get_size()
text_xy = (x - text.get_width() - 12,
y - text.get_height() - 12)
self.surface.blit(text, text_xy)
def __update_font(self):
self.font = self.__get_font(self.font_size)
pass
def __get_font(self, size):
return pygame.font.Font(DEFAULT_FONT, size)
class StopWatch:
DEFAULT_TEXT = ALL_ON_CHAR + ":" + ALL_ON_CHAR * 2 + ":" + ALL_ON_CHAR * 2
def __init__(self, surface, font_size):
self.surface = surface
self.font_size = font_size
self.__update_font()
self.start_time = datetime.now()
def on_resize(self, new_surface):
self.surface = new_surface
self.__update_font()
def render(self, color):
self.__render_text(self.DEFAULT_TEXT, COLOR_TEXT_OFF)
time_diff = datetime.now() - self.start_time
# time_diff += timedelta(hours=9, minutes=59, seconds=50)
seconds = time_diff.seconds % 60
minutes = (time_diff.seconds // 60) % 60
hours = (time_diff.seconds // 60 // 60) % 10
ticker_text = f"{hours:01}:{minutes:02}:{seconds:02}"
self.__render_text(ticker_text, color)
def __render_text(self, text, color):
text = self.font.render(text, True, color)
x, y = self.surface.get_size()
text_xy = (12,
y - text.get_height() - 12)
self.surface.blit(text, text_xy)
def __update_font(self):
self.font = self.__get_font(self.font_size)
pass
def __get_font(self, size):
return pygame.font.Font(DEFAULT_FONT, size)