Add CaptureButton

master
Guillaume Dott 2017-09-28 13:56:32 +02:00
parent c8207afa5f
commit a5524e7fe7
1 changed files with 58 additions and 34 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
import os, sys, pygame import os, sys, time, pygame
class Button: class Button:
def __init__(self, size, topleft): def __init__(self, size, topleft):
@ -26,25 +26,51 @@ class Button:
def press(self): def press(self):
self.pressed = True self.pressed = True
self.run()
def unpress(self): def unpress(self):
self.pressed = False self.pressed = False
self.run()
def run(self): def run(self):
print('click') print('click')
class IncButton(Button): class CaptureButton(Button):
def run(self): def __init__(self, size, topleft, photoboite):
Photoboite.i += 1 super(CaptureButton, self).__init__(size, topleft)
self.photoboite = photoboite
self.elapsed = 0
self.capture = False
def draw_on(self, screen):
super(CaptureButton, self).draw_on(screen)
if self.capture:
text = str(self.elapsed + 1)
size = self.photoboite.font.size(text)
ren = self.photoboite.font.render(text, 0, (255, 255, 0))
screen.blit(ren, (400, 400))
class DecButton(Button):
def run(self): def run(self):
Photoboite.i -= 1 pygame.event.set_blocked((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
self.capture = True
for i in range(4):
wait = True
start = time.time()
while wait:
self.elapsed = int(time.time() - start)
if self.elapsed >= 3:
wait = False
else:
photoboite.event()
photoboite.draw()
pygame.display.flip()
print('Capture')
pygame.time.wait(1000)
pygame.event.set_allowed((pygame.MOUSEBUTTONDOWN, pygame.MOUSEBUTTONUP))
self.capture = False
class Photoboite: class Photoboite:
i = 0
def __init__(self): def __init__(self):
self.screen = self.screen() self.screen = self.screen()
pygame.font.init() pygame.font.init()
@ -55,8 +81,7 @@ class Photoboite:
button_size = int(self.screen.get_width() / 4) button_size = int(self.screen.get_width() / 4)
topleft = self.screen.get_width() - button_size - 20, self.screen.get_height() / 4 - button_size / 2 topleft = self.screen.get_width() - button_size - 20, self.screen.get_height() / 4 - button_size / 2
self.buttons.append(IncButton(button_size, topleft)) self.buttons.append(CaptureButton(button_size, topleft, self))
self.buttons.append(DecButton(50, (70, 400)))
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)
@ -86,34 +111,33 @@ class Photoboite:
return pygame.display.set_mode() return pygame.display.set_mode()
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()
def run(self): def run(self):
pressed = False pressed = False
while True: while True:
for event in pygame.event.get(): self.event()
if event.type == pygame.QUIT or event.type == pygame.KEYDOWN and event.unicode == 'q': self.draw()
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()
self.screen.fill(self.background)
self.screen.fill((0,0,0), rect=self.camera)
text = str(Photoboite.i)
size = self.font.size(text)
ren = self.font.render(text, 0, (255, 255, 0))
self.screen.blit(ren, (40, 40))
for button in self.buttons:
button.draw_on(self.screen)
pygame.display.flip() pygame.display.flip()
photoboite = Photoboite() photoboite = Photoboite()