101 lines
3.7 KiB
Python
101 lines
3.7 KiB
Python
import math
|
|
import pygame
|
|
import os
|
|
|
|
DIAL_SCALING = 0.9
|
|
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)
|
|
self.rect = self.get_rect()
|
|
self.dial_size = dial_size
|
|
self.center = (self.dial_size / 2, self.dial_size / 2)
|
|
|
|
self.setting = 1
|
|
|
|
def update(self):
|
|
self.fill((0, 0, 0, 0))
|
|
|
|
pygame.draw.circle(self, (0, 255, 0), self.center, self.dial_size / 2, 5)
|
|
pygame.draw.circle(self, (64, 64, 64), self.center, self.dial_size / 2 * 0.95)
|
|
|
|
angle = (1.25 - (1.5 * self.setting)) * math.pi
|
|
x_1 = (math.cos(angle) * self.dial_size / 2 * 0.75) + (self.dial_size / 2)
|
|
y_1 = (-1 * math.sin(angle) * self.dial_size / 2 * 0.75) + (self.dial_size / 2)
|
|
x_2 = (math.cos(angle) * self.dial_size / 2 * 0.99) + (self.dial_size / 2)
|
|
y_2 = (-1 * math.sin(angle) * self.dial_size / 2 * 0.99) + (self.dial_size / 2)
|
|
pygame.draw.line(self, (0, 255, 0), (x_1, y_1), (x_2, y_2), 10)
|
|
|
|
def set_setting(self, new_setting):
|
|
self.setting = max(0, min(new_setting, 1))
|
|
|
|
def move_dial(self, movement_amount):
|
|
self.setting = max(0, min(self.setting + movement_amount, 1))
|
|
|
|
|
|
class MouseHandler:
|
|
def __init__(self):
|
|
self.prev_mouse_pos = pygame.mouse.get_pos()
|
|
self.mouse_pos = pygame.mouse.get_pos()
|
|
self.active = False
|
|
|
|
def update(self):
|
|
self.prev_mouse_pos = self.mouse_pos
|
|
self.mouse_pos = pygame.mouse.get_pos()
|
|
|
|
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])
|
|
movement_speed = math.sqrt((mouse_diff[0] ** 2) + (mouse_diff[1] ** 2))
|
|
mouse_angle = math.atan2(self.mouse_pos[0] - center[0], self.mouse_pos[1] - center[1])
|
|
diff_angle = math.atan2(mouse_diff[0], mouse_diff[1])
|
|
speed_coeff = math.cos((mouse_angle - (0.5 * math.pi)) - diff_angle)
|
|
return movement_speed * speed_coeff
|
|
|
|
|
|
class Surface(pygame.Surface):
|
|
def __init__(self, window_size):
|
|
super().__init__(window_size, pygame.SRCALPHA)
|
|
self.running = True
|
|
self.quit = False
|
|
self.next_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.mouse_handler = MouseHandler()
|
|
self.font = pygame.font.Font(SCRIPT_DIR + '/resources/tuffy.ttf', 128)
|
|
|
|
pygame.mouse.set_visible(False)
|
|
|
|
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.prev_mouse_pos = self.mouse_handler.mouse_pos
|
|
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.blit(self.dial, self.dial.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
|
|
self.blit(text, text_rect)
|
|
|
|
self.dial.update()
|