added mouse movement
This commit is contained in:
parent
778c373809
commit
f23572d4d2
@ -30,6 +30,29 @@ class Dial(pygame.Surface):
|
||||
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)
|
||||
final_speed = movement_speed * speed_coeff
|
||||
return final_speed
|
||||
|
||||
class Surface(pygame.Surface):
|
||||
def __init__(self, window_size):
|
||||
super().__init__(window_size, pygame.SRCALPHA)
|
||||
@ -41,8 +64,13 @@ class Surface(pygame.Surface):
|
||||
self.dial = Dial(dial_size)
|
||||
self.dial.rect.center = (window_size[0] / 2, window_size[1] / 2)
|
||||
|
||||
self.mouse_handler = MouseHandler()
|
||||
|
||||
|
||||
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
|
||||
@ -50,6 +78,15 @@ class Surface(pygame.Surface):
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_ESCAPE:
|
||||
self.quit = True
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
self.mouse_handler.active = True
|
||||
self.mouse_handler.prev_mouse_pos = self.mouse_handler.mouse_pos
|
||||
print('yug')
|
||||
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)
|
||||
self.dial.update()
|
||||
|
Loading…
Reference in New Issue
Block a user