pi-thermostat/thermostat/surfaces/button_pad.py

73 lines
2.4 KiB
Python

import math
import pygame
import os
DIAL_SCALING = 0.9
QUIT_BUTTON_SIZE = 50
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
class MouseHandler:
def __init__(self):
self.prev_mouse_pos = pygame.mouse.get_pos()
self.mouse_pos = pygame.mouse.get_pos()
self.active = False
self.click = False
def update(self):
self.prev_mouse_pos = self.mouse_pos
self.mouse_pos = pygame.mouse.get_pos()
self.click = False
class QuitButton(pygame.Surface):
def __init__(self):
super().__init__((QUIT_BUTTON_SIZE, QUIT_BUTTON_SIZE), pygame.SRCALPHA)
self.rect = self.get_rect()
pygame.draw.rect(self, (255, 0, 0), self.rect, 3)
pygame.draw.line(self, (255, 0, 0), (0, 0), (QUIT_BUTTON_SIZE, QUIT_BUTTON_SIZE), 3)
pygame.draw.line(self, (255, 0, 0), (0, QUIT_BUTTON_SIZE), (QUIT_BUTTON_SIZE, 0), 3)
class Surface(pygame.Surface):
def __init__(self, window_size):
super().__init__(window_size, pygame.SRCALPHA)
self.running = True
self.quit = False
self.next_surface = ''
self.mouse_frames = 0
dial_size = int(min(window_size[0], window_size[1]) * 0.9)
self.quit_button = QuitButton()
self.mouse_handler = MouseHandler()
self.font = pygame.font.Font(SCRIPT_DIR + '/resources/tuffy.ttf', 128)
def update(self):
self.fill((32, 32, 32))
self.mouse_handler.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
self.quit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
self.quit = True
if event.type == pygame.MOUSEBUTTONDOWN:
self.mouse_handler.active = True
self.mouse_handler.update()
self.mouse_handler.prev_mouse_pos = self.mouse_handler.mouse_pos
self.mouse_handler.click = True
if event.type == pygame.MOUSEBUTTONUP:
self.mouse_handler.active = False
if self.mouse_handler.click:
if self.mouse_handler.mouse_pos[0] <= QUIT_BUTTON_SIZE and \
self.mouse_handler.mouse_pos[1] <= QUIT_BUTTON_SIZE:
self.running = False
self.quit = True
self.blit(self.quit_button, self.quit_button.rect)