a knob can get into (un)focused state

This commit is contained in:
SallarShayegan
2025-02-24 23:23:53 +01:00
parent 9febfbf792
commit f363e303e5

28
knob.py
View File

@ -1,14 +1,18 @@
import pygame
import math
from colors import *
class Knob:
def __init__(self, screen, color, radius, position):
self.screen = screen
self.color = color
def __init__(self, gui, name, radius, position):
self.gui = gui
self.name = name
self.screen = gui.screen
self.color = color_primary
self.radius = radius
self.position = position
self.value = 0
self.focused = False
def set_value(self, value):
self.value = value
@ -16,9 +20,21 @@ class Knob:
def set_color(self, color):
self.color = color
def set_focused(self, focused):
if focused:
self.focused = True
self.color = color_primary_light
self.gui.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]
@ -26,6 +42,12 @@ class Knob:
return(x, y)
def display(self):
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(self.screen, self.color, self.position, self.radius)
pygame.draw.line(self.screen, "black", self.position, self.get_pointer_position(), 4)