-
Notifications
You must be signed in to change notification settings - Fork 3
/
gluqlo.py
321 lines (266 loc) · 10.5 KB
/
gluqlo.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
from __future__ import print_function
# yet incomplete translation of: https://github.com/alexanderk23/gluqlo
from datetime import datetime
import math
import os
import sys
import pygame
try:
_dir = os.path.dirname(os.path.abspath(__file__))
except: ## __file__ doesn't exists in py2exe context
_dir = os.path.dirname(os.path.abspath(sys.executable))
FONT_COLOR = 0xb7, 0xb7, 0xb7
BACKGROUND_COLOR = 0x0f, 0x0f, 0x0f
SQRT2 = math.sqrt(2)
TARGET_FPS = 60
DEBUG = False
ANIMATE = True
DURATION = 200
def fill_surface(surf, coords4, r, color):
rpsqrt = int(r/SQRT2)
_x, _y, _w, _h = coords4
_w = int(_w//2) -1
_h = int(_h//2) -1
x0 = _x + _w
y0 = _y + _h
_w -= r
_h -= r
if (_w<=0) or (_h<=0):
return
sy = y0 - _h
ey = y0 + _h
sx = x0 - _w
ex = x0 + _w
surf.lock()
try:
for i in range(int(sy), int(ey)):
for j in range(int(sx-r), int(ex+r+1)):
surf.set_at((j,i), color)
d = -r
x2m1 = -1
y = r
for x in range(rpsqrt+1):
x2m1 += 2
d += x2m1
if d>=0:
y-=1
d-=y*2
for i in range(int(sx-y),int(ex+y)):
surf.set_at((i, int(sy-x)), color)
surf.set_at((i, int(ey+x)), color)
for i in range(int(sx-x),int(ex+x)):
surf.set_at((i, int(sy-y)), color)
surf.set_at((i, int(ey+y)), color)
finally:
surf.unlock()
class Gluqlo:
def show_globals(self):
print('TARGET_FPS: %d' % TARGET_FPS)
print('DEBUG: %r' % DEBUG)
print('DURATION: %d' % DURATION)
def check_unsupported_option(self, option):
if option in sys.argv:
print("unsupported option:", option)
sys.exit(-1)
def check_args(self):
attrs = pygame.DOUBLEBUF
if '/s' in sys.argv:
attrs = attrs|pygame.FULLSCREEN
if '/f' in sys.argv:
attrs = attrs & (~pygame.FULLSCREEN)
self.check_unsupported_option('/c')
self.check_unsupported_option('/p')
return attrs
def pygame_init(self, attrs):
pygame.init()
try:
pygame.display.init()
except:
# required for xp..
os.environ['SDL_VIDEODRIVER']='windib'
pygame.display.init()
try:
self.screen = pygame.display.set_mode((0,0), flags=pygame.HWSURFACE|attrs)
except:
self.screen = pygame.display.set_mode((0,0), attrs)
self.width, self.height = self.screen.get_width(), self.screen.get_height()
self.clock = pygame.time.Clock()
font_filename = os.path.join(_dir, 'gluqlo.ttf')
self.font_time = pygame.font.Font(font_filename, int(self.height / 1.68))
def __init__(self):
attrs = self.check_args()
self.show_globals()
self.pygame_init(attrs)
# config
self.past_h = self.past_m = 99
self._24hs = True
self.leadingZero = True
# calculate
self.rectsize = self.height * 0.6
self.spacing = self.width * 0.031
self.radius = self.height * 0.05714
self.jitter_width = self.jitter_height = 1
# if display_scale_factor != 1:
# self.jitter_width = (screen->w - width) * 0.5;
# self.jitter_height = (screen->h - height) * 0.5;
hourBackground_x = 0.5 * (self.width - (0.031 * self.width) - (1.2 * self.height)) + self.jitter_width
hourBackground_y = 0.2 * self.height + self.jitter_height
hourBackground_w = hourBackground_h = self.rectsize
self.hourBackground = list(map(int, [hourBackground_x, hourBackground_y, hourBackground_w, hourBackground_h]))
self.minBackground = list(map(int,
[self.hourBackground[0]+(self.spacing+(0.6*self.height)), self.hourBackground[1], self.rectsize, self.rectsize ]))
# animation parameters
self.start = self.end = None
# animation frame counter and max
self.cnt = self.maxcnt = 0
def blit_digits(self, surf, bg_rect, spc, digits, color):
adjust_x = 2.5*spc if (digits[0]=='1') else 0
center_x = int(bg_rect[0] + bg_rect[2]/2 - adjust_x)
if len(digits)>1:
minx, maxx, miny, maxy, advance = self.font_time.metrics(digits[0])[0]
s1 = self.font_time.render(digits[0], True, color)
x = center_x-maxx+minx-spc-(spc if adjust_x else 0)
y = bg_rect[1]+int((bg_rect[3]-s1.get_height())/2)
surf.blit(s1, (int(x),int(y)))
# // second digit
minx, maxx, miny, maxy, advance = self.font_time.metrics(digits[1])[0]
s1 = self.font_time.render(digits[1], True, color)
y = bg_rect[1]+int((bg_rect[3]-s1.get_height())/2)
x = center_x+ int(spc/2)
surf.blit(s1, (int(x),int(y)))
else:
s1 = self.font_time.render(digits[0], True, color)
x = center_x - int(s1.get_width()/2)
y = bg_rect[1] + int((bg_rect[3]-s1.get_height())/2)
surf.blit(s1, (x,y))
def render_digits(self, surf, bg_rect, bg, digits, prevdigits, maxsteps, steps):
spc = surf.get_height() * 0.0125
rect = bg_rect[:]
rect[-1] = rect[-1]//2
rect = list(map(int, rect))
# blit digits upper half
surf.set_clip(rect)
surf.blit(bg, rect)
self.blit_digits(surf, bg_rect, spc, digits, FONT_COLOR)
surf.set_clip(None)
# translate step-info into scale & color constants
halfsteps = maxsteps/2
upperhalf = (steps+1)<=halfsteps
if upperhalf:
P = lambda x: x - (x*1.0*steps)/(halfsteps-1)
else:
P = lambda x: x*(((1.0*steps)-halfsteps+1)/halfsteps)
scale = P(1.0)
c = P(0xb7)
color = int(c), int(c), int(c)
# // create surface to scale from filled background surface
# full digits blit
bgcopy = bg.convert()
rect[0] = rect[1] = 0
rect[2] = bgcopy.get_width()
rect[3] = bgcopy.get_height()
self.blit_digits(bgcopy, rect, spc, prevdigits if upperhalf else digits, color)
# // scale and blend it to dest
ww, hh = bgcopy.get_width(), bgcopy.get_height()
scaled = pygame.transform.smoothscale(bgcopy, (ww, int(hh*scale)))
scaled_h = scaled.get_height()
scaled_h2 = int(scaled_h/2)
rect = [0, 0 if upperhalf else scaled_h2, scaled.get_width(), scaled_h2]
dst_y = (bg_rect[3]-scaled_h) if upperhalf else bg_rect[3]
dstrect = list(map(int, [bg_rect[0], bg_rect[1]+(dst_y/2), rect[2], rect[3]]))
surf.set_clip(dstrect)
surf.blit(scaled, dstrect, area=rect)
surf.set_clip(None)
# # // draw divider
# rect2 = list(map(int, [bg_rect[0], bg_rect[1]+(bg_rect[3]-(surf.get_height() * 0.005))/2, bg_rect[2], surf.get_height() * 0.005]))
# fill_surface(surf, rect2, self.radius, BACKGROUND_COLOR)
# rect2[1] += rect2[3]
# rect2[3] = 1
# fill_surface(surf, rect2, self.radius, (0x1a,0x1a,0x1a, 0xff))
def render_clock(self, bg, maxsteps, steps, hour, minute):
if hour!=self.past_h:
h = hour if self._24hs else (((hour+11)%12)+1)
F = '%02d' if self.leadingZero else '%d'
buffer = F%h
buffer2 = F%self.past_h
self.render_digits(self.screen, self.hourBackground, bg, buffer, buffer2, maxsteps, steps)
if minute!=self.past_m:
buffer = '%02d'%minute
buffer2 = '%02d'%self.past_m
self.render_digits(self.screen, self.minBackground, bg, buffer, buffer2, maxsteps, steps)
pygame.display.flip()
if steps == maxsteps-1:
self.past_h = hour
self.past_m = minute
def render_animate(self, bg, a, b):
current = pygame.time.get_ticks()
frame = 99
if self.start is None:
if b!=self.past_m:
# start animation
self.start = current
self.end = current+DURATION
if not(self.start is None):
if current>self.end:
# current = self.end # unnecessary
self.end = self.start = None
else:
frame = (99.0 * (current-self.start)) / DURATION # (self.end-self.start)
common_frame = frame==99
if not common_frame:
self.cnt+=1
else:
self.maxcnt = self.cnt if self.cnt>self.maxcnt else self.maxcnt
self.cnt = 0
#print('frame:' if common_frame else "*** %d"%frame, "---", self.start, current, self.end, "---", b, self.past_m)
self.render_clock(bg, 100, frame, a, b)
def main(self):
global ANIMATE
done = False
self.screen.fill((0,0,0))
bg_rect = 0, 0, self.rectsize, self.rectsize
bg = pygame.Surface((int(self.rectsize), int(self.rectsize)), pygame.HWSURFACE|pygame.SRCALPHA)
fill_surface(bg, bg_rect, self.radius, BACKGROUND_COLOR)
self.clock.tick(TARGET_FPS)
while not done:
self.clock.tick(TARGET_FPS)
dt = datetime.now()
a, b = (dt.minute, dt.second) if DEBUG else (dt.hour, dt.minute)
if ANIMATE:
self.render_animate(bg, a, b)
else:
self.render_clock(bg, 20, 19, a, b)
for event in pygame.event.get():
done = event.type in (pygame.KEYDOWN, pygame.K_ESCAPE, pygame.QUIT)
def get_bool(name, default):
QUOTES = '"', "'"
if name in os.environ:
value = os.environ.get(name, str(default)).lower()
if value[0] in QUOTES:
value = value[1:]
if value[-1] in QUOTES:
value = value[:-1]
return value in ('y', 'yes', 's', 'si','true', 't', '1')
return default
def get_int(name, default=None):
if name in os.environ:
value = os.environ.get(name, str(default))
try:
return int(value)
except:
print("Cannot interpret:", value, "as integer!")
return default
if __name__=="__main__":
_TARGET_FPS, _FPS = 'TARGET_FPS', 'FPS'
_DEBUG = 'DEBUG'
_DURATION = 'DURATION'
_ANIMATE = 'ANIMATE'
TARGET_FPS = get_int(_TARGET_FPS, get_int(_FPS, default=TARGET_FPS))
DURATION = get_int(_DURATION, default=DURATION)
DEBUG = get_bool(_DEBUG, DEBUG)
ANIMATE = get_bool(_ANIMATE, ANIMATE)
ss = Gluqlo()
try:
ss.main()
finally:
if DEBUG: print("total: ", ss.maxcnt)