2017-09-28 23:16:08 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
import os, sys, time, pygame
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
class Button:
|
|
|
|
def __init__(self, size, topleft):
|
|
|
|
self.up_image = pygame.image.load('button-up.png').convert_alpha()
|
|
|
|
self.up_image = pygame.transform.smoothscale(self.up_image, (size, size))
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
self.down_image = pygame.image.load('button-down.png').convert_alpha()
|
|
|
|
self.down_image = pygame.transform.smoothscale(self.down_image, (size, size))
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
self.rect = self.up_image.get_rect()
|
|
|
|
self.rect.topleft = topleft
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
self.pressed = False
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def draw_on(self, screen):
|
|
|
|
if self.pressed:
|
|
|
|
screen.blit(self.down_image, self.rect)
|
|
|
|
else:
|
|
|
|
screen.blit(self.up_image, self.rect)
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def is_pressed(self, pos):
|
|
|
|
return self.rect.collidepoint(pos)
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def press(self):
|
|
|
|
self.pressed = True
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def unpress(self):
|
|
|
|
self.pressed = False
|
2017-09-28 13:56:32 +02:00
|
|
|
self.run()
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def run(self):
|
|
|
|
print('click')
|
2017-09-21 11:38:03 +02:00
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
class CaptureButton(Button):
|
2017-09-28 16:39:22 +02:00
|
|
|
countdown = 3
|
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
def __init__(self, size, topleft, photoboite):
|
|
|
|
super(CaptureButton, self).__init__(size, topleft)
|
|
|
|
self.photoboite = photoboite
|
|
|
|
self.elapsed = 0
|
2017-09-28 16:39:22 +02:00
|
|
|
self.capture_mode = False
|
|
|
|
self.count = 0
|
2017-09-28 13:56:32 +02:00
|
|
|
|
|
|
|
def draw_on(self, screen):
|
|
|
|
super(CaptureButton, self).draw_on(screen)
|
2017-09-28 16:39:22 +02:00
|
|
|
if self.capture_mode:
|
|
|
|
if self.elapsed >= CaptureButton.countdown:
|
|
|
|
text = "CLIC!"
|
|
|
|
else:
|
|
|
|
text = str(CaptureButton.countdown - self.elapsed)
|
2017-09-28 13:56:32 +02:00
|
|
|
size = self.photoboite.font.size(text)
|
2017-09-28 16:39:22 +02:00
|
|
|
ren = self.photoboite.font.render(text, True, (0, 0, 0))
|
|
|
|
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)
|
2017-09-21 14:31:45 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def run(self):
|
2017-09-28 13:56:32 +02:00
|
|
|
pygame.event.set_blocked((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
|
2017-09-28 16:39:22 +02:00
|
|
|
self.capture_mode = True
|
|
|
|
|
|
|
|
capture = Capture(self.count)
|
|
|
|
self.count += 1
|
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
for i in range(4):
|
|
|
|
wait = True
|
|
|
|
start = time.time()
|
|
|
|
while wait:
|
|
|
|
self.elapsed = int(time.time() - start)
|
2017-09-28 16:39:22 +02:00
|
|
|
if self.elapsed >= CaptureButton.countdown:
|
2017-09-28 13:56:32 +02:00
|
|
|
wait = False
|
2017-09-28 16:39:22 +02:00
|
|
|
|
|
|
|
self.photoboite.event()
|
|
|
|
self.photoboite.draw()
|
2017-09-28 13:56:32 +02:00
|
|
|
pygame.display.flip()
|
|
|
|
|
2017-09-28 16:39:22 +02:00
|
|
|
capture.take()
|
2017-09-28 13:56:32 +02:00
|
|
|
pygame.time.wait(1000)
|
|
|
|
pygame.event.set_allowed((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
|
2017-09-28 16:39:22 +02:00
|
|
|
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)
|
2017-09-21 14:31:45 +02:00
|
|
|
|
2017-09-28 16:39:22 +02:00
|
|
|
return self
|
2017-09-21 14:31:45 +02:00
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
class Photoboite:
|
2017-09-27 00:04:44 +02:00
|
|
|
def __init__(self):
|
2017-09-28 09:36:11 +02:00
|
|
|
self.screen = self.screen()
|
|
|
|
pygame.font.init()
|
2017-09-21 14:31:45 +02:00
|
|
|
|
2017-09-28 23:16:38 +02:00
|
|
|
pygame.mouse.set_visible(False)
|
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
self.background = 255, 255, 255
|
|
|
|
self.buttons = []
|
|
|
|
|
|
|
|
button_size = int(self.screen.get_width() / 4)
|
|
|
|
topleft = self.screen.get_width() - button_size - 20, self.screen.get_height() / 4 - button_size / 2
|
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
self.buttons.append(CaptureButton(button_size, topleft, self))
|
2017-09-27 00:04:44 +02:00
|
|
|
|
|
|
|
self.camera = pygame.Rect(20, 20, self.screen.get_width() - self.screen.get_width() / 4 - 60, self.screen.get_height() - 40)
|
|
|
|
|
2017-09-28 16:39:22 +02:00
|
|
|
self.font = pygame.font.Font(pygame.font.match_font('calibri'), 180)
|
2017-09-27 00:04:44 +02:00
|
|
|
|
|
|
|
|
2017-09-28 09:36:11 +02:00
|
|
|
def screen(self):
|
|
|
|
if os.getenv('DISPLAY'):
|
|
|
|
pygame.display.init()
|
|
|
|
return pygame.display.set_mode((1280, 768))
|
|
|
|
else:
|
|
|
|
drivers = ['directfb', 'fbcon', 'svgalib']
|
|
|
|
found = False
|
|
|
|
for driver in drivers:
|
|
|
|
if not os.getenv('SDL_VIDEODRIVER'):
|
|
|
|
os.putenv('SDL_VIDEODRIVER', driver)
|
|
|
|
try:
|
|
|
|
pygame.display.init()
|
|
|
|
except pygame.error as e:
|
|
|
|
print('Driver: {0} failed.'.format(driver))
|
|
|
|
continue
|
|
|
|
found = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if not found:
|
|
|
|
raise Exception('No suitable video driver found!')
|
|
|
|
|
|
|
|
return pygame.display.set_mode()
|
|
|
|
|
2017-09-28 13:56:32 +02:00
|
|
|
def draw(self):
|
|
|
|
self.screen.fill(self.background)
|
|
|
|
|
|
|
|
self.screen.fill((0,0,0), rect=self.camera)
|
|
|
|
|
|
|
|
for button in self.buttons:
|
|
|
|
button.draw_on(self.screen)
|
|
|
|
|
|
|
|
def event(self):
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.unicode == 'q':
|
|
|
|
sys.exit()
|
|
|
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
|
|
|
for button in self.buttons:
|
|
|
|
if button.is_pressed(event.pos):
|
|
|
|
button.press()
|
|
|
|
elif event.type == pygame.MOUSEBUTTONUP:
|
|
|
|
for button in self.buttons:
|
|
|
|
if button.is_pressed(event.pos):
|
|
|
|
button.unpress()
|
2017-09-28 09:36:11 +02:00
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
def run(self):
|
|
|
|
pressed = False
|
|
|
|
while True:
|
2017-09-28 13:56:32 +02:00
|
|
|
self.event()
|
|
|
|
self.draw()
|
|
|
|
|
2017-09-27 00:04:44 +02:00
|
|
|
pygame.display.flip()
|
|
|
|
|
2017-09-28 16:39:22 +02:00
|
|
|
Photoboite().run()
|