a knob can get into (un)focused state
This commit is contained in:
28
knob.py
28
knob.py
@ -1,14 +1,18 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import math
|
import math
|
||||||
|
from colors import *
|
||||||
|
|
||||||
class Knob:
|
class Knob:
|
||||||
|
|
||||||
def __init__(self, screen, color, radius, position):
|
def __init__(self, gui, name, radius, position):
|
||||||
self.screen = screen
|
self.gui = gui
|
||||||
self.color = color
|
self.name = name
|
||||||
|
self.screen = gui.screen
|
||||||
|
self.color = color_primary
|
||||||
self.radius = radius
|
self.radius = radius
|
||||||
self.position = position
|
self.position = position
|
||||||
self.value = 0
|
self.value = 0
|
||||||
|
self.focused = False
|
||||||
|
|
||||||
def set_value(self, value):
|
def set_value(self, value):
|
||||||
self.value = value
|
self.value = value
|
||||||
@ -16,9 +20,21 @@ class Knob:
|
|||||||
def set_color(self, color):
|
def set_color(self, color):
|
||||||
self.color = 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):
|
def get_position(self):
|
||||||
return self.position
|
return self.position
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.position == other.get_position()
|
||||||
|
|
||||||
def get_pointer_position(self):
|
def get_pointer_position(self):
|
||||||
angle = (self.value * 0.8 * 2 + 0.7) * math.pi
|
angle = (self.value * 0.8 * 2 + 0.7) * math.pi
|
||||||
x = (self.radius - 5) * math.cos(angle) + self.position[0]
|
x = (self.radius - 5) * math.cos(angle) + self.position[0]
|
||||||
@ -26,6 +42,12 @@ class Knob:
|
|||||||
return(x, y)
|
return(x, y)
|
||||||
|
|
||||||
def display(self):
|
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.circle(self.screen, self.color, self.position, self.radius)
|
||||||
pygame.draw.line(self.screen, "black", self.position, self.get_pointer_position(), 4)
|
pygame.draw.line(self.screen, "black", self.position, self.get_pointer_position(), 4)
|
||||||
|
|
||||||
Reference in New Issue
Block a user