This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
forked from thomas-/stationclock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphics.py
98 lines (83 loc) · 3.73 KB
/
graphics.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
import pygame
import time
from pygame.sprite import Sprite
import settings
from utils import send_event, calculate_dot_positions
pygame.font.init()
class ProgressBar(Sprite):
def __init__(self, length=1, artist='', title=''):
Sprite.__init__(self)
self.track_start = time.time()
self.track_end = time.time() + length
self.track_length = length
self.track_progress = 0
self.image = pygame.Surface((settings.RESOLUTION[0], settings.NOW_PLAYING_HEIGHT))
self.rect = self.image.get_rect()
self.metadata = pygame.font.Font(
settings.PROGRESS_FONT,
settings.PROGRESS_FONT_SIZE
).render('%s - %s' % (artist, title), True, pygame.Color('white'))
self.rect.bottom = settings.RESOLUTION[1]
self.update()
def update(self):
# background
pygame.draw.rect(self.image, (30, 0, 100),
pygame.Rect(0, 0, settings.RESOLUTION[0], settings.NOW_PLAYING_HEIGHT))
# progress bar
self.progress = (time.time() - self.track_start) / float(self.track_length)
if self.progress < 1:
pygame.draw.rect(self.image, (100, 0, 255),
pygame.Rect(0, 0, settings.RESOLUTION[0]*self.progress, settings.NOW_PLAYING_HEIGHT))
else:
self.kill()
send_event(event='song_finished')
# track info
self.image.blit(self.metadata, (settings.PADDING,
settings.NOW_PLAYING_HEIGHT - self.metadata.get_height() - settings.PADDING))
# remaining time
r = self.track_end - time.time()
m, s = divmod(r, 60)
remaining = pygame.font.Font(
settings.PROGRESS_FONT,
settings.PROGRESS_FONT_SIZE
).render('%d:%02d' % (m, s), True, pygame.Color('white'))
self.image.blit(remaining, (settings.RESOLUTION[0]-remaining.get_width()*1.2,
settings.NOW_PLAYING_HEIGHT - remaining.get_height() - settings.PADDING))
def get_font(self):
return pygame.font.Font(settings.PROGRESS_FONT, settings.PROGRESS_FONT_SIZE)
class Announcement(Sprite):
def __init__(self, value='', color='white', textcolor='black'):
Sprite.__init__(self)
self.image = pygame.Surface((settings.RESOLUTION[0], settings.NOW_PLAYING_HEIGHT))
self.image.fill(pygame.Color(color))
self.rect = self.image.get_rect()
self.text = pygame.font.Font(
settings.PROGRESS_FONT,
settings.PROGRESS_FONT_SIZE
).render('%s' % (value), True, pygame.Color(textcolor))
self.image.blit(self.text, (settings.RESOLUTION[0]/2 - self.text.get_width()/2,
settings.NOW_PLAYING_HEIGHT - self.text.get_height() - settings.PADDING))
self.rect.bottom = settings.RESOLUTION[1]
send_event(event='position', now_playing=True)
def kill(self):
Sprite.kill(self)
class Dots(Sprite):
def __init__(self, value='', color='white', textcolor='black'):
Sprite.__init__(self)
self.image = pygame.Surface(settings.RESOLUTION)
self.markers = calculate_dot_positions(outer=True)
self.pos = calculate_dot_positions()
self.rect = self.image.get_rect()
self.blank()
def blank(self):
self.image.fill(pygame.Color('black'))
for x in xrange(12):
pygame.draw.circle(self.image, (200,0,0), self.markers[x], 6)
def update(self):
s = self.get_seconds()
if s == 0:
self.blank()
#for x in xrange(self.get_seconds()+1):
pygame.draw.circle(self.image, (255,0,0), self.pos[s], 6)
def get_seconds(self):
return time.localtime()[5]