Move Capture and Sleep screens in module

master
Guillaume Dott 2018-08-27 16:15:37 +02:00
parent d11042531e
commit 9091d7ca75
3 changed files with 105 additions and 83 deletions

View File

@ -1,5 +1,5 @@
import io, os, sys, time, pygame import io, os, sys, time, pygame
import photoboite.buttons import photoboite.screens, photoboite.buttons
try: try:
import picamera import picamera
@ -75,39 +75,6 @@ class Photo:
return self return self
class SleepMode:
def __init__(self, seconds, screen):
self.enabled = True
self.drawn = False
self.seconds = seconds
self.screen = screen
self.clicker = pygame.image.load(os.path.dirname(os.path.realpath(__file__)) + '/../images/clicker.png').convert_alpha()
self.clicker_rect = self.clicker.get_rect()
self.clicker_rect.topleft = ((self.screen.get_width() - self.clicker_rect.width) / 2, (self.screen.get_height() - self.clicker_rect.height) / 2)
def move(self):
self.last_action = time.time()
def event(self):
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP or event.type == pygame.KEYUP:
self.move()
self.enabled = False
def draw(self):
if not self.drawn:
self.screen.fill((255,255,255))
self.screen.blit(self.clicker, self.clicker_rect)
pygame.display.flip()
self.drawn = True
def sleep(self):
if time.time() - self.last_action > self.seconds:
self.enabled = True
self.drawn = False
class Photoboite: class Photoboite:
def __init__(self): def __init__(self):
self.screen = self.screen() self.screen = self.screen()
@ -115,22 +82,10 @@ class Photoboite:
pygame.mouse.set_visible(False) pygame.mouse.set_visible(False)
self.buttons = []
button_size = int(self.screen.get_height() / 4)
topleft = (self.screen.get_width() - button_size) / 2, self.screen.get_height() - button_size - 10
self.capture_button = photoboite.buttons.CaptureButton(button_size, topleft, self)
self.buttons.append(self.capture_button)
self.buttons.append(photoboite.buttons.PowerButton(pygame.Rect(self.screen.get_width() - 50, 0, 50, 50)))
self.buttons.append(photoboite.buttons.InfoButton(pygame.Rect(0, 0, 50, 50), self))
camera_rect = pygame.Rect(0, 0, 800, 480)
self.camera = Camera(camera_rect)
self.clock = pygame.time.Clock() self.clock = pygame.time.Clock()
self.sleep = SleepMode(30, self.screen) self.sleep = photoboite.screens.Sleep(30, self)
self.capture = photoboite.screens.Capture(self, self.sleep)
def screen(self): def screen(self):
@ -157,42 +112,16 @@ class Photoboite:
return pygame.display.set_mode() return pygame.display.set_mode()
def draw(self):
self.clock.tick(60)
self.camera.draw_on(self.screen)
for button in self.buttons:
button.draw_on(self.screen)
def event(self):
for event in pygame.event.get():
self.sleep.move()
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.unicode == 'q':
self.quit()
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()
def run(self): def run(self):
pressed = False
while True: while True:
if self.sleep.enabled: if self.sleep.enabled:
self.sleep.event() self.sleep.run()
self.sleep.draw()
self.clock.tick(60)
else: else:
self.event() self.capture.run()
self.draw()
self.sleep.sleep() self.clock.tick(60)
pygame.display.flip()
def quit(self): def quit(self):
self.camera.close() self.capture.quit()
sys.exit() sys.exit()

View File

@ -60,7 +60,7 @@ class InfoButton(Button):
texts = [ texts = [
"DATE: " + self.date(), "DATE: " + self.date(),
"COUNT: " + str(self.photoboite.capture_button.count), "COUNT: " + str(self.photoboite.capture.capture_button.count),
"FPS: " + str(self.photoboite.clock.get_fps()), "FPS: " + str(self.photoboite.clock.get_fps()),
"DISK USAGE:" + self.disk_usage() "DISK USAGE:" + self.disk_usage()
] ]
@ -122,7 +122,7 @@ class CaptureButton(Button):
self.capture_mode = False self.capture_mode = False
self.count = 0 self.count = 0
self.background_rect = pygame.Rect(0, self.photoboite.screen.get_height() - 80, self.photoboite.screen.get_width(), 80) self.background_rect = pygame.Rect(0, self.photoboite.screen().get_height() - 80, self.photoboite.screen().get_width(), 80)
self.background = pygame.Surface((self.background_rect.width, self.background_rect.height)) self.background = pygame.Surface((self.background_rect.width, self.background_rect.height))
self.background.fill((0, 0, 0)) self.background.fill((0, 0, 0))
self.background.set_alpha(150) self.background.set_alpha(150)
@ -174,8 +174,8 @@ class CaptureButton(Button):
pygame.time.wait(600) pygame.time.wait(600)
for photo in capture.photos: for photo in capture.photos:
self.photoboite.screen.blit(photo.capture, (81, 0)) self.photoboite.screen().blit(photo.capture, (81, 0))
self.photoboite.screen.blit(self.photo_film, (0, 0)) self.photoboite.screen().blit(self.photo_film, (0, 0))
pygame.display.flip() pygame.display.flip()
pygame.time.wait(800) pygame.time.wait(800)
self.photoboite.event() self.photoboite.event()

View File

@ -0,0 +1,93 @@
import os, time, sys, pygame
import photoboite
class Capture:
def __init__(self, main, sleep):
self.main = main
self.sleep = sleep
self.buttons = []
button_size = int(self.main.screen.get_height() / 4)
topleft = (self.main.screen.get_width() - button_size) / 2, self.main.screen.get_height() - button_size - 10
self.capture_button = photoboite.buttons.CaptureButton(button_size, topleft, self)
self.buttons.append(self.capture_button)
self.buttons.append(photoboite.buttons.PowerButton(pygame.Rect(self.main.screen.get_width() - 50, 0, 50, 50)))
self.buttons.append(photoboite.buttons.InfoButton(pygame.Rect(0, 0, 50, 50), self))
camera_rect = pygame.Rect(0, 0, 800, 480)
self.camera = photoboite.Camera(camera_rect)
def draw(self):
self.camera.draw_on(self.main.screen)
for button in self.buttons:
button.draw_on(self.main.screen)
def event(self):
for event in pygame.event.get():
self.sleep.move()
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.unicode == 'q':
self.main.quit()
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()
def screen(self):
return self.main.screen
def quit(self):
self.camera.close()
def run(self):
self.event()
self.draw()
self.sleep.sleep()
pygame.display.flip()
class Sleep:
def __init__(self, seconds, main):
self.enabled = True
self.drawn = False
self.seconds = seconds
self.main = main
self.clicker = pygame.image.load(os.path.dirname(os.path.realpath(__file__)) + '/../../images/clicker.png').convert_alpha()
self.clicker_rect = self.clicker.get_rect()
self.clicker_rect.topleft = ((self.main.screen.get_width() - self.clicker_rect.width) / 2, (self.main.screen.get_height() - self.clicker_rect.height) / 2)
def move(self):
self.last_action = time.time()
def event(self):
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.unicode == 'q':
self.main.quit()
elif event.type == pygame.MOUSEBUTTONUP or event.type == pygame.KEYUP:
self.move()
self.enabled = False
def draw(self):
if not self.drawn:
self.main.screen.fill((255,255,255))
self.main.screen.blit(self.clicker, self.clicker_rect)
pygame.display.flip()
self.drawn = True
def run(self):
self.event()
self.draw()
def sleep(self):
if time.time() - self.last_action > self.seconds:
self.enabled = True
self.drawn = False