52 lines
1.5 KiB
Python
52 lines
1.5 KiB
Python
import pygame
|
|
import math
|
|
from colors import *
|
|
|
|
class Knob:
|
|
|
|
def __init__(self, group, name, radius, position):
|
|
self.group = group
|
|
self.name = name
|
|
self.color = color_primary
|
|
self.radius = radius
|
|
self.position = position
|
|
self.value = 0
|
|
self.focused = False
|
|
|
|
def set_value(self, value):
|
|
self.value = value
|
|
|
|
def set_color(self, color):
|
|
self.color = color
|
|
|
|
def set_focused(self, focused):
|
|
if focused:
|
|
self.focused = True
|
|
self.color = color_primary_light
|
|
self.group.deactivate_knobs_except(self)
|
|
else:
|
|
self.focused = False
|
|
self.color = color_primary
|
|
|
|
def get_position(self):
|
|
return self.position
|
|
|
|
def __eq__(self, other):
|
|
return self.position == other.get_position()
|
|
|
|
def get_pointer_position(self):
|
|
angle = (self.value * 0.8 * 2 + 0.7) * math.pi
|
|
x = (self.radius - 5) * math.cos(angle) + self.position[0]
|
|
y = (self.radius - 5) * math.sin(angle) + self.position[1]
|
|
return(x, y)
|
|
|
|
def display(self, screen):
|
|
m1, m2, m3 = pygame.mouse.get_pressed(3)
|
|
if m1:
|
|
pos = pygame.mouse.get_pos()
|
|
if math.sqrt(math.pow(pos[0]-self.position[0],2) + math.pow(pos[1]-self.position[1],2)) < self.radius:
|
|
self.set_focused(True)
|
|
|
|
pygame.draw.circle(screen, self.color, self.position, self.radius)
|
|
pygame.draw.line(screen, "black", self.position, self.get_pointer_position(), 4)
|
|
|