Add countdown to capture photos
parent
a5524e7fe7
commit
b958325a24
|
@ -35,41 +35,72 @@ class Button:
|
||||||
print('click')
|
print('click')
|
||||||
|
|
||||||
class CaptureButton(Button):
|
class CaptureButton(Button):
|
||||||
|
countdown = 3
|
||||||
|
|
||||||
def __init__(self, size, topleft, photoboite):
|
def __init__(self, size, topleft, photoboite):
|
||||||
super(CaptureButton, self).__init__(size, topleft)
|
super(CaptureButton, self).__init__(size, topleft)
|
||||||
self.photoboite = photoboite
|
self.photoboite = photoboite
|
||||||
self.elapsed = 0
|
self.elapsed = 0
|
||||||
self.capture = False
|
self.capture_mode = False
|
||||||
|
self.count = 0
|
||||||
|
|
||||||
def draw_on(self, screen):
|
def draw_on(self, screen):
|
||||||
super(CaptureButton, self).draw_on(screen)
|
super(CaptureButton, self).draw_on(screen)
|
||||||
if self.capture:
|
if self.capture_mode:
|
||||||
text = str(self.elapsed + 1)
|
if self.elapsed >= CaptureButton.countdown:
|
||||||
|
text = "CLIC!"
|
||||||
|
else:
|
||||||
|
text = str(CaptureButton.countdown - self.elapsed)
|
||||||
size = self.photoboite.font.size(text)
|
size = self.photoboite.font.size(text)
|
||||||
ren = self.photoboite.font.render(text, 0, (255, 255, 0))
|
ren = self.photoboite.font.render(text, True, (0, 0, 0))
|
||||||
screen.blit(ren, (400, 400))
|
topleft = (self.rect.topleft[0] + self.rect.width / 2 - size[0] / 2, self.rect.topleft[1] + self.rect.height / 2 - size[1] / 2)
|
||||||
|
screen.blit(ren, topleft)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
pygame.event.set_blocked((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
|
pygame.event.set_blocked((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
|
||||||
self.capture = True
|
self.capture_mode = True
|
||||||
|
|
||||||
|
capture = Capture(self.count)
|
||||||
|
self.count += 1
|
||||||
|
|
||||||
for i in range(4):
|
for i in range(4):
|
||||||
wait = True
|
wait = True
|
||||||
start = time.time()
|
start = time.time()
|
||||||
while wait:
|
while wait:
|
||||||
self.elapsed = int(time.time() - start)
|
self.elapsed = int(time.time() - start)
|
||||||
if self.elapsed >= 3:
|
if self.elapsed >= CaptureButton.countdown:
|
||||||
wait = False
|
wait = False
|
||||||
else:
|
|
||||||
photoboite.event()
|
self.photoboite.event()
|
||||||
photoboite.draw()
|
self.photoboite.draw()
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
print('Capture')
|
capture.take()
|
||||||
pygame.time.wait(1000)
|
pygame.time.wait(1000)
|
||||||
pygame.event.set_allowed((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
|
pygame.event.set_allowed((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
|
||||||
self.capture = False
|
self.capture_mode = False
|
||||||
|
|
||||||
|
|
||||||
|
class Capture:
|
||||||
|
def __init__(self, uid):
|
||||||
|
self.uid = uid
|
||||||
|
self.photos = []
|
||||||
|
|
||||||
|
def take(self):
|
||||||
|
self.photos.append(Photo(self.uid, len(self.photos)).take())
|
||||||
|
|
||||||
|
|
||||||
|
class Photo:
|
||||||
|
def __init__(self, cid, uid):
|
||||||
|
self.cid = cid
|
||||||
|
self.uid = uid
|
||||||
|
|
||||||
|
def take(self):
|
||||||
|
self.name = "%s-%04d-%04d" % (time.strftime('%Y%m%d-%H%M%S'), self.cid, self.uid)
|
||||||
|
print("Capture " + self.name)
|
||||||
|
|
||||||
|
return self
|
||||||
|
|
||||||
class Photoboite:
|
class Photoboite:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.screen = self.screen()
|
self.screen = self.screen()
|
||||||
|
@ -85,7 +116,7 @@ class Photoboite:
|
||||||
|
|
||||||
self.camera = pygame.Rect(20, 20, self.screen.get_width() - self.screen.get_width() / 4 - 60, self.screen.get_height() - 40)
|
self.camera = pygame.Rect(20, 20, self.screen.get_width() - self.screen.get_width() / 4 - 60, self.screen.get_height() - 40)
|
||||||
|
|
||||||
self.font = pygame.font.Font(None, 80)
|
self.font = pygame.font.Font(pygame.font.match_font('calibri'), 180)
|
||||||
|
|
||||||
|
|
||||||
def screen(self):
|
def screen(self):
|
||||||
|
@ -140,5 +171,4 @@ class Photoboite:
|
||||||
|
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
|
|
||||||
photoboite = Photoboite()
|
Photoboite().run()
|
||||||
photoboite.run()
|
|
||||||
|
|
Loading…
Reference in New Issue