added a temporary quit button

This commit is contained in:
Nicholas Dyer 2023-02-21 12:31:02 -05:00
parent f1ab4e7be6
commit bbefe51278
No known key found for this signature in database
GPG Key ID: E4E6388793FA2105

View File

@ -3,8 +3,10 @@ import pygame
import os
DIAL_SCALING = 0.9
QUIT_BUTTON_SIZE = 50
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
class Dial(pygame.Surface):
def __init__(self, dial_size):
super().__init__((dial_size, dial_size), pygame.SRCALPHA)
@ -43,10 +45,12 @@ class MouseHandler:
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
def get_circular_speed(self, center):
mouse_diff = (self.prev_mouse_pos[0] - self.mouse_pos[0], self.prev_mouse_pos[1] - self.mouse_pos[1])
@ -57,6 +61,15 @@ class MouseHandler:
return movement_speed * speed_coeff
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)
@ -67,6 +80,7 @@ class Surface(pygame.Surface):
dial_size = int(min(window_size[0], window_size[1]) * 0.9)
self.dial = Dial(dial_size)
self.dial.rect.center = (window_size[0] / 2, window_size[1] / 2)
self.quit_button = QuitButton()
self.mouse_handler = MouseHandler()
self.font = pygame.font.Font(SCRIPT_DIR + '/resources/tuffy.ttf', 128)
@ -89,17 +103,26 @@ class Surface(pygame.Surface):
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.active:
self.dial.move_dial(self.mouse_handler.get_circular_speed(self.dial.center) / -3000.0)
self.dial.moving = True
else:
self.dial.moving = False
if self.mouse_handler.active:
self.dial.move_dial(self.mouse_handler.get_circular_speed(self.dial.center) / -3000.0)
self.dial.moving = True
else:
self.dial.moving = 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.dial, self.dial.rect)
self.blit(self.quit_button, self.quit_button.rect)
text = self.font.render('{0}%'.format(round(self.dial.setting * 100)), True, (255, 255, 255))
text_rect = text.get_rect()
text_rect.center = self.dial.rect.center