From e936b835e1a90ad0ca4e0da022720a2a3c369ecd Mon Sep 17 00:00:00 2001 From: Nicholas Dyer Date: Fri, 18 Aug 2023 20:13:55 -0400 Subject: [PATCH] added simple blank slate to build on --- thermostat/__init__.py | 4 ++ thermostat/__main__.py | 17 ++++++++ thermostat/mainloop.py | 45 +++++++++++++++++++ thermostat/surfaces/__init__.py | 4 ++ thermostat/surfaces/button_pad.py | 72 +++++++++++++++++++++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 thermostat/__init__.py create mode 100644 thermostat/__main__.py create mode 100644 thermostat/mainloop.py create mode 100644 thermostat/surfaces/__init__.py create mode 100644 thermostat/surfaces/button_pad.py diff --git a/thermostat/__init__.py b/thermostat/__init__.py new file mode 100644 index 0000000..fb13450 --- /dev/null +++ b/thermostat/__init__.py @@ -0,0 +1,4 @@ +"""Smart thermostat for local CITRUxx use.""" +__version__ = '0.0.1' + +from . import mainloop \ No newline at end of file diff --git a/thermostat/__main__.py b/thermostat/__main__.py new file mode 100644 index 0000000..dd04b14 --- /dev/null +++ b/thermostat/__main__.py @@ -0,0 +1,17 @@ +""" +Launch script for Pocket Friends. +""" +import sys +import thermostat + +if __name__ == '__main__': + + is_windowed = False + if len(sys.argv) > 0: + for arg in sys.argv: + print(arg) + if arg == '--windowed': + is_windowed = True + + thermostat.mainloop.main(is_windowed=is_windowed) + diff --git a/thermostat/mainloop.py b/thermostat/mainloop.py new file mode 100644 index 0000000..80088d8 --- /dev/null +++ b/thermostat/mainloop.py @@ -0,0 +1,45 @@ +import pygame +import os +import thermostat.surfaces as surfaces + +# Global variables +SCREEN_SIZE = (720, 720) +FPS = 60 +VALID_SURFACES = [ + 'button_pad', + 'screensaver' +] + + +def main(is_windowed=False): + """ + Main scene manager to display the scenes of the application + :param is_windowed: boolean to start the game in windowed mode + """ + pygame.init() + clock = pygame.time.Clock() + if is_windowed: + window = pygame.display.set_mode(SCREEN_SIZE) + else: + window = pygame.display.set_mode(SCREEN_SIZE, pygame.FULLSCREEN) + + # Starts the program with the surface 'dial' as the default + surface = getattr(globals()['surfaces'], 'button_pad').Surface(SCREEN_SIZE) + running = True + while running: + clock.tick(FPS) + surface.update() + + window.blit(surface, surface.get_rect()) + + if not surface.running: + if surface.quit: + break + # Switch the surface to a new surface + next_surface = surface.next_surface + if next_surface not in VALID_SURFACES: + raise Exception('Given surface is not a valid surface!') + surface = getattr(globals()['surfaces'], next_surface).Surface(SCREEN_SIZE) + + pygame.display.flip() + pygame.quit() diff --git a/thermostat/surfaces/__init__.py b/thermostat/surfaces/__init__.py new file mode 100644 index 0000000..06df230 --- /dev/null +++ b/thermostat/surfaces/__init__.py @@ -0,0 +1,4 @@ +"""Smart thermostat for local CITRUxx use.""" +__version__ = '0.0.1' + +from . import button_pad \ No newline at end of file diff --git a/thermostat/surfaces/button_pad.py b/thermostat/surfaces/button_pad.py new file mode 100644 index 0000000..aafe75b --- /dev/null +++ b/thermostat/surfaces/button_pad.py @@ -0,0 +1,72 @@ +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)