created a knobs module to outsource the grouping of the knobs
This commit is contained in:
13
knob.py
13
knob.py
@ -4,10 +4,9 @@ from colors import *
|
||||
|
||||
class Knob:
|
||||
|
||||
def __init__(self, gui, name, radius, position):
|
||||
self.gui = gui
|
||||
def __init__(self, group, name, radius, position):
|
||||
self.group = group
|
||||
self.name = name
|
||||
self.screen = gui.screen
|
||||
self.color = color_primary
|
||||
self.radius = radius
|
||||
self.position = position
|
||||
@ -24,7 +23,7 @@ class Knob:
|
||||
if focused:
|
||||
self.focused = True
|
||||
self.color = color_primary_light
|
||||
self.gui.deactivate_knobs_except(self)
|
||||
self.group.deactivate_knobs_except(self)
|
||||
else:
|
||||
self.focused = False
|
||||
self.color = color_primary
|
||||
@ -41,13 +40,13 @@ class Knob:
|
||||
y = (self.radius - 5) * math.sin(angle) + self.position[1]
|
||||
return(x, y)
|
||||
|
||||
def display(self):
|
||||
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(self.screen, self.color, self.position, self.radius)
|
||||
pygame.draw.line(self.screen, "black", self.position, self.get_pointer_position(), 4)
|
||||
pygame.draw.circle(screen, self.color, self.position, self.radius)
|
||||
pygame.draw.line(screen, "black", self.position, self.get_pointer_position(), 4)
|
||||
|
||||
83
knobs.py
Normal file
83
knobs.py
Normal file
@ -0,0 +1,83 @@
|
||||
import pygame
|
||||
from colors import *
|
||||
from fonts import *
|
||||
from knob import *
|
||||
|
||||
class Knobs:
|
||||
|
||||
def __init__(self, gui):
|
||||
|
||||
self.gui = gui
|
||||
|
||||
knob_names = [
|
||||
'Delay Time',
|
||||
'Delay Vol',
|
||||
'Reverb Wet',
|
||||
'Reverb Size',
|
||||
'Filter Freq',
|
||||
'Distortion',
|
||||
'Detune',
|
||||
'Gain'
|
||||
]
|
||||
|
||||
# Create and position the knobs
|
||||
self.knobs_radius = self.gui.screenW / 25
|
||||
knobs_spacing = self.knobs_radius * 2
|
||||
knobs_x = (self.gui.screenW - (self.knobs_radius + knobs_spacing) * 2) / 2
|
||||
x = knobs_x
|
||||
knobs_y = (self.gui.screenH - (self.knobs_radius + knobs_spacing) * 3) / 2
|
||||
y = knobs_y
|
||||
self.knobs = []
|
||||
for i in range(8):
|
||||
self.knobs.append(Knob(self, knob_names[i], self.knobs_radius, (x, y)))
|
||||
y += self.knobs_radius + knobs_spacing
|
||||
if i == 3 or i == 6:
|
||||
x += self.knobs_radius + knobs_spacing
|
||||
y = knobs_y
|
||||
|
||||
def display_label(self, knob):
|
||||
label_fx_name = font_helvetica.render(knob.name, False, color_primary_light)
|
||||
label_fx_value = font_helvetica.render('[ ' + str(knob.value) + '% ]',
|
||||
False, color_primary_light)
|
||||
|
||||
label_x = self.gui.screenW - label_fx_name.get_width() - 20
|
||||
label_y = (self.gui.screenH - label_fx_name.get_height() - label_fx_value.get_height()) / 2
|
||||
self.gui.screen.blit(label_fx_name, (label_x, label_y))
|
||||
self.gui.screen.blit(label_fx_value, (label_x, self.gui.screenH / 2))
|
||||
pygame.draw.line(self.gui.screen, color_primary_dark, (label_x - 10, label_y), (label_x - 10, self.gui.screenH - label_y), 2)
|
||||
|
||||
line_points = []
|
||||
line_points.append(knob.get_position())
|
||||
|
||||
if (knob == self.knobs[0] or knob == self.knobs[1] or
|
||||
knob == self.knobs[4] or knob == self.knobs[5]):
|
||||
x = knob.get_position()[0] + self.knobs_radius * 1.5
|
||||
y = knob.get_position()[1] + self.knobs_radius * 1.5
|
||||
line_points.append((x, y))
|
||||
elif (knob == self.knobs[2] or knob == self.knobs[3] or knob == self.knobs[6]):
|
||||
x = knob.get_position()[0] + self.knobs_radius * 1.5
|
||||
y = knob.get_position()[1] - self.knobs_radius * 1.5
|
||||
line_points.append((x, y))
|
||||
elif (knob == self.knobs[7]):
|
||||
x = knob.get_position()[0] - self.knobs_radius * 1.5
|
||||
y = knob.get_position()[1] + self.knobs_radius * 1.5
|
||||
line_points.append((x, y))
|
||||
|
||||
if (knob == self.knobs[0] or knob == self.knobs[3]):
|
||||
line_points.append((self.knobs[0].get_position()[0] + self.knobs_radius * 1.5, self.gui.screenH/2))
|
||||
elif (knob == self.knobs[4] or knob == self.knobs[6] or knob == self.knobs[7]):
|
||||
line_points.append((self.knobs[4].get_position()[0] + self.knobs_radius * 1.5, self.gui.screenH/2))
|
||||
|
||||
line_points.append((label_x - 10, self.gui.screenH/2))
|
||||
|
||||
for i in range(len(line_points)-1):
|
||||
pygame.draw.line(self.gui.screen, color_primary_dark, line_points[i], line_points[i+1], 2)
|
||||
|
||||
def deactivate_knobs_except(self, knob):
|
||||
for k in self.knobs:
|
||||
if k != knob: k.set_focused(False)
|
||||
|
||||
def display(self):
|
||||
for knob in self.knobs:
|
||||
if knob.focused: self.display_label(knob)
|
||||
knob.display(self.gui.screen)
|
||||
85
main.py
85
main.py
@ -1,7 +1,7 @@
|
||||
import pygame
|
||||
from knob import *
|
||||
from colors import *
|
||||
from fonts import *
|
||||
from knobs import Knobs
|
||||
from lozenge_button import LozengeButton
|
||||
|
||||
class GuiMain:
|
||||
@ -9,8 +9,8 @@ class GuiMain:
|
||||
def __init__(self):
|
||||
# pygame setup
|
||||
pygame.init()
|
||||
#self.screen = pygame.display.set_mode((800, 480))
|
||||
self.screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
|
||||
self.screen = pygame.display.set_mode((800, 480))
|
||||
#self.screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
|
||||
self.screenH = self.screen.get_height()
|
||||
self.screenW = self.screen.get_width()
|
||||
|
||||
@ -22,16 +22,6 @@ class GuiMain:
|
||||
'Jack',
|
||||
'Athmo'
|
||||
]
|
||||
knob_names = [
|
||||
'Delay Time',
|
||||
'Delay Vol',
|
||||
'Reverb Wet',
|
||||
'Reverb Size',
|
||||
'Filter Freq',
|
||||
'Distortion',
|
||||
'Detune',
|
||||
'Gain'
|
||||
]
|
||||
|
||||
# Create and position the fx_mode buttons
|
||||
self.fx_mode_buttons = []
|
||||
@ -45,67 +35,12 @@ class GuiMain:
|
||||
|
||||
self.fx_mode_buttons[0].focused = True
|
||||
|
||||
# Create and position the knobs
|
||||
self.knobs_radius = self.screenW / 25
|
||||
knobs_spacing = self.knobs_radius * 2
|
||||
knobs_x = (self.screenW - (self.knobs_radius + knobs_spacing) * 2) / 2
|
||||
x = knobs_x
|
||||
knobs_y = (self.screenH - (self.knobs_radius + knobs_spacing) * 3) / 2
|
||||
y = knobs_y
|
||||
self.knobs = []
|
||||
for i in range(8):
|
||||
self.knobs.append(Knob(self, knob_names[i], self.knobs_radius, (x, y)))
|
||||
y += self.knobs_radius + knobs_spacing
|
||||
if i == 3 or i == 6:
|
||||
x += self.knobs_radius + knobs_spacing
|
||||
y = knobs_y
|
||||
self.knobs = Knobs(self)
|
||||
|
||||
self.clock = pygame.time.Clock()
|
||||
self.running = True
|
||||
self.run()
|
||||
|
||||
def display_label(self, knob):
|
||||
fx_label = font_helvetica.render(knob.name, False, color_primary_light)
|
||||
fx_value = font_helvetica.render('[ ' + str(knob.value) + '% ]',
|
||||
False, color_primary_light)
|
||||
|
||||
label_x = self.screenW - fx_label.get_width() - 20
|
||||
label_y = (self.screenH - fx_label.get_height() - fx_value.get_height()) / 2
|
||||
self.screen.blit(fx_label, (label_x, label_y))
|
||||
self.screen.blit(fx_value, (label_x, self.screenH / 2))
|
||||
pygame.draw.line(self.screen, color_primary_dark, (label_x - 10, label_y), (label_x - 10, self.screenH - label_y), 2)
|
||||
|
||||
line_points = []
|
||||
line_points.append(knob.get_position())
|
||||
|
||||
if (knob == self.knobs[0] or knob == self.knobs[1] or
|
||||
knob == self.knobs[4] or knob == self.knobs[5]):
|
||||
x = knob.get_position()[0] + self.knobs_radius * 1.5
|
||||
y = knob.get_position()[1] + self.knobs_radius * 1.5
|
||||
line_points.append((x, y))
|
||||
elif (knob == self.knobs[2] or knob == self.knobs[3] or knob == self.knobs[6]):
|
||||
x = knob.get_position()[0] + self.knobs_radius * 1.5
|
||||
y = knob.get_position()[1] - self.knobs_radius * 1.5
|
||||
line_points.append((x, y))
|
||||
elif (knob == self.knobs[7]):
|
||||
x = knob.get_position()[0] - self.knobs_radius * 1.5
|
||||
y = knob.get_position()[1] + self.knobs_radius * 1.5
|
||||
line_points.append((x, y))
|
||||
|
||||
if (knob == self.knobs[0] or knob == self.knobs[3]):
|
||||
line_points.append((self.knobs[0].get_position()[0] + self.knobs_radius * 1.5, self.screenH/2))
|
||||
elif (knob == self.knobs[4] or knob == self.knobs[6] or knob == self.knobs[7]):
|
||||
line_points.append((self.knobs[4].get_position()[0] + self.knobs_radius * 1.5, self.screenH/2))
|
||||
|
||||
line_points.append((label_x - 10, self.screenH/2))
|
||||
|
||||
for i in range(len(line_points)-1):
|
||||
pygame.draw.line(self.screen, color_primary_dark, line_points[i], line_points[i+1], 2)
|
||||
|
||||
def deactivate_knobs_except(self, knob):
|
||||
for k in self.knobs:
|
||||
if k != knob: k.set_focused(False)
|
||||
|
||||
def set_fx_mode(self, mode):
|
||||
for b in self.fx_mode_buttons:
|
||||
if b.name != mode: b.focused = False
|
||||
@ -123,9 +58,7 @@ class GuiMain:
|
||||
|
||||
# RENDER YOUR GAME HERE
|
||||
|
||||
for knob in self.knobs:
|
||||
if knob.focused: self.display_label(knob)
|
||||
knob.display()
|
||||
self.knobs.display()
|
||||
|
||||
for button in self.fx_mode_buttons:
|
||||
button.display()
|
||||
@ -140,14 +73,6 @@ class GuiMain:
|
||||
pygame.draw.rect(self.screen, color_primary,
|
||||
((20, self.fx_mode_buttons[0].y-20),(40, 40)))
|
||||
|
||||
"""
|
||||
pygame.draw.polygon(self.screen, color_primary_dark, [
|
||||
(12, 12),
|
||||
(self.screenW * 1.5 / 19 + 12, 12),
|
||||
(self.screenW * 1.5 / 19 + 12, self.fx_mode_buttons[5].y - 30),
|
||||
(12, self.screenH/2 - 30)], 2)
|
||||
"""
|
||||
|
||||
pygame.draw.polygon(self.screen, "white", [(0, self.screenH/2), (self.screenW*3 / 19, self.screenH), (0, self.screenH)])
|
||||
# flip() the display to put your work on screen
|
||||
pygame.display.flip()
|
||||
|
||||
Reference in New Issue
Block a user