Files
SantoscopeUI/knob.py

61 lines
1.9 KiB
Python
Raw Normal View History

2025-02-23 16:17:11 +01:00
import pygame
2025-02-23 17:05:46 +01:00
import math
from .colors import *
2025-02-23 16:17:11 +01:00
class Knob:
def __init__(self, group, name, radius, position, value = 0):
self.group = group
2025-02-24 23:23:53 +01:00
self.name = name
self.color = color_primary
self.radius = radius
self.position = position
self.value = value
2025-02-24 23:23:53 +01:00
self.focused = False
2025-03-02 20:16:40 +01:00
self.fade_count_down = 150
self.vanish_label_path = False
self.label_path = []
2025-02-23 17:05:46 +01:00
def set_value(self, value):
self.value = value
def set_color(self, color):
self.color = color
2025-02-24 23:23:53 +01:00
def set_focused(self, focused):
if focused:
self.focused = True
self.color = color_primary_light
else:
self.focused = False
self.color = color_primary
self.fade_count_down = 150
if not self.vanish_label_path: self.label_path = []
2025-02-24 23:23:53 +01:00
def get_position(self):
return self.position
2025-02-24 23:23:53 +01:00
def __eq__(self, other):
if other is None: return False
return self.name == other.name
2025-02-24 23:23:53 +01:00
2025-02-23 17:05:46 +01:00
def get_pointer_position(self):
2025-02-23 21:11:49 +01:00
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]
2025-02-23 17:05:46 +01:00
return(x, y)
2025-02-23 16:17:11 +01:00
2025-03-02 20:16:40 +01:00
def display(self, surface):
2025-02-24 23:23:53 +01:00
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.group.set_focused(self)
if self.focused: self.fade_count_down -= 1
2025-02-24 23:23:53 +01:00
if self.fade_count_down == 0:
self.vanish_label_path = True
self.set_focused(False)
2025-03-02 20:16:40 +01:00
pygame.draw.circle(surface, self.color + (self.group.opacity,), self.position, self.radius)
pygame.draw.line(surface, "black", self.position, self.get_pointer_position(), 4)
2025-02-23 16:17:11 +01:00