Compare commits

..

10 Commits

3 changed files with 54 additions and 16 deletions

View File

@ -9,12 +9,11 @@ PI = 3.14159265
VALID_SURFACES = [ VALID_SURFACES = [
'dial' 'dial'
] ]
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
def main(windowed_mode=False): def main(windowed_mode=False):
""" """
Main looping method. Main scene manager to display the scenes of the application
:param windowed_mode: boolean to start the game in windowed mode :param windowed_mode: boolean to start the game in windowed mode
""" """
pygame.init() pygame.init()

View File

@ -1,8 +1,10 @@
import math import math
import pygame import pygame
import os
DIAL_SCALING = 0.9 DIAL_SCALING = 0.9
QUIT_BUTTON_SIZE = 50
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
class Dial(pygame.Surface): class Dial(pygame.Surface):
@ -11,21 +13,25 @@ class Dial(pygame.Surface):
self.rect = self.get_rect() self.rect = self.get_rect()
self.dial_size = dial_size self.dial_size = dial_size
self.center = (self.dial_size / 2, self.dial_size / 2) self.center = (self.dial_size / 2, self.dial_size / 2)
self.moving = False
self.setting = 1 self.setting = 1
def update(self): def update(self):
self.fill((0, 0, 0, 0)) 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, 255), self.center, self.dial_size / 2, 5)
if self.moving:
pygame.draw.circle(self, (48, 48, 48), self.center, self.dial_size / 2 * 0.95)
else:
pygame.draw.circle(self, (64, 64, 64), self.center, self.dial_size / 2 * 0.95) pygame.draw.circle(self, (64, 64, 64), self.center, self.dial_size / 2 * 0.95)
angle = (1.25 - (1.5 * self.setting)) * math.pi angle = (1.25 - (1.5 * self.setting)) * math.pi
x_1 = (math.cos(angle) * self.dial_size / 2 * 0.8) + (self.dial_size / 2) 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.8) + (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) 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) 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) pygame.draw.line(self, (64, 64, 255), (x_1, y_1), (x_2, y_2), 10)
def set_setting(self, new_setting): def set_setting(self, new_setting):
self.setting = max(0, min(new_setting, 1)) self.setting = max(0, min(new_setting, 1))
@ -33,25 +39,36 @@ class Dial(pygame.Surface):
def move_dial(self, movement_amount): def move_dial(self, movement_amount):
self.setting = max(0, min(self.setting + movement_amount, 1)) self.setting = max(0, min(self.setting + movement_amount, 1))
class MouseHandler: class MouseHandler:
def __init__(self): def __init__(self):
self.prev_mouse_pos = pygame.mouse.get_pos() self.prev_mouse_pos = pygame.mouse.get_pos()
self.mouse_pos = pygame.mouse.get_pos() self.mouse_pos = pygame.mouse.get_pos()
self.active = False self.active = False
self.click = False
def update(self): def update(self):
self.prev_mouse_pos = self.mouse_pos self.prev_mouse_pos = self.mouse_pos
self.mouse_pos = pygame.mouse.get_pos() self.mouse_pos = pygame.mouse.get_pos()
self.click = False
def get_circular_speed(self, center): 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]) 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)) 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]) 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]) mouse_diff_angle = math.atan2(mouse_diff[0], mouse_diff[1])
speed_coeff = math.cos((mouse_angle - (0.5 * math.pi)) - diff_angle) speed_coeff = math.cos((mouse_angle - (0.5 * math.pi)) - mouse_diff_angle)
final_speed = movement_speed * speed_coeff return movement_speed * speed_coeff
return final_speed
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): class Surface(pygame.Surface):
def __init__(self, window_size): def __init__(self, window_size):
@ -63,9 +80,12 @@ class Surface(pygame.Surface):
dial_size = int(min(window_size[0], window_size[1]) * 0.9) dial_size = int(min(window_size[0], window_size[1]) * 0.9)
self.dial = Dial(dial_size) self.dial = Dial(dial_size)
self.dial.rect.center = (window_size[0] / 2, window_size[1] / 2) self.dial.rect.center = (window_size[0] / 2, window_size[1] / 2)
self.quit_button = QuitButton()
self.mouse_handler = MouseHandler() self.mouse_handler = MouseHandler()
self.font = pygame.font.Font(SCRIPT_DIR + '/resources/tuffy.ttf', 128)
pygame.mouse.set_visible(False)
def update(self): def update(self):
self.fill((32, 32, 32)) self.fill((32, 32, 32))
@ -77,16 +97,35 @@ class Surface(pygame.Surface):
self.quit = True self.quit = True
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: if event.key == pygame.K_ESCAPE:
self.running = False
self.quit = True self.quit = True
if event.type == pygame.MOUSEBUTTONDOWN: if event.type == pygame.MOUSEBUTTONDOWN:
self.mouse_handler.active = True self.mouse_handler.active = True
self.mouse_handler.update()
self.mouse_handler.prev_mouse_pos = self.mouse_handler.mouse_pos self.mouse_handler.prev_mouse_pos = self.mouse_handler.mouse_pos
print('yug') self.mouse_handler.click = True
if event.type == pygame.MOUSEBUTTONUP: if event.type == pygame.MOUSEBUTTONUP:
self.mouse_handler.active = False self.mouse_handler.active = False
if self.mouse_handler.active: if self.mouse_handler.active:
self.dial.move_dial(self.mouse_handler.get_circular_speed(self.dial.center) / -3000.0) 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.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
self.blit(text, text_rect)
self.dial.update() self.dial.update()

Binary file not shown.