the circle buttons have been refactored in a more object oriented way
This commit is contained in:
@ -4,18 +4,62 @@ import math
|
|||||||
|
|
||||||
class CircleButton:
|
class CircleButton:
|
||||||
|
|
||||||
def __init__(self, gui, x, y, radius):
|
def __init__(self, gui, position, radius):
|
||||||
self.screen = gui.screen
|
self.surface = gui.screen
|
||||||
self.x = x
|
self.position = position
|
||||||
self.y = y
|
|
||||||
self.radius = radius
|
self.radius = radius
|
||||||
self.color = color_primary
|
self.color = color_primary
|
||||||
|
|
||||||
def check_click(self, pos, action):
|
def check_click(self, pos):
|
||||||
if math.sqrt(math.pow(pos[0] - self.x, 2) +
|
if math.sqrt(math.pow(pos[0] - self.position[0], 2) +
|
||||||
math.pow(pos[1] - self.y, 2)) < self.radius:
|
math.pow(pos[1] - self.position[1], 2)) < self.radius:
|
||||||
action()
|
self.on_click()
|
||||||
|
|
||||||
|
def on_click(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
pass
|
||||||
|
|
||||||
def display(self):
|
def display(self):
|
||||||
pygame.draw.circle(self.screen, self.color,
|
pygame.draw.circle(self.surface, self.color, self.position, self.radius)
|
||||||
(self.x, self.y), self.radius)
|
self.draw()
|
||||||
|
|
||||||
|
class BeatButton(CircleButton):
|
||||||
|
|
||||||
|
def __init__(self, gui, position, radius):
|
||||||
|
self.playing = False
|
||||||
|
super().__init__(gui, position, radius)
|
||||||
|
|
||||||
|
def on_click(self):
|
||||||
|
self.playing = not self.playing
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
x, y = self.position
|
||||||
|
pygame.draw.polygon(self.surface, "black", [
|
||||||
|
(x-self.radius/2.5, y-self.radius/1.5),
|
||||||
|
(x+self.radius/1.5, y),
|
||||||
|
(x-self.radius/2.5, y+self.radius/1.5)
|
||||||
|
], 2 if self.playing else 0)
|
||||||
|
|
||||||
|
class LoopButton(CircleButton):
|
||||||
|
def __init__(self, gui, position, radius):
|
||||||
|
self.state = False
|
||||||
|
self.counter = 0
|
||||||
|
self.frame_threshold = 30
|
||||||
|
self.recording = False
|
||||||
|
super().__init__(gui, position, radius)
|
||||||
|
|
||||||
|
def on_click(self):
|
||||||
|
self.recording = not self.recording
|
||||||
|
self.state = not self.state
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
pygame.draw.circle(self.surface, "black", self.position, self.radius/1.5)
|
||||||
|
pygame.draw.circle(self.surface, self.color, self.position, self.radius/2,
|
||||||
|
2 if not self.state else 0)
|
||||||
|
if self.recording:
|
||||||
|
if self.counter == self.frame_threshold:
|
||||||
|
self.state = not self.state
|
||||||
|
self.counter = 0
|
||||||
|
self.counter += 1
|
||||||
20
main.py
20
main.py
@ -4,8 +4,7 @@ from fonts import *
|
|||||||
from knobs import Knobs
|
from knobs import Knobs
|
||||||
from lozenge_button import LozengeButton
|
from lozenge_button import LozengeButton
|
||||||
from binary_button import BinaryButton
|
from binary_button import BinaryButton
|
||||||
from circle_button import CircleButton
|
from circle_button import *
|
||||||
from functools import partial
|
|
||||||
|
|
||||||
class GuiMain:
|
class GuiMain:
|
||||||
|
|
||||||
@ -54,9 +53,9 @@ class GuiMain:
|
|||||||
(20, self.fx_mode_buttons[0].y-20),
|
(20, self.fx_mode_buttons[0].y-20),
|
||||||
(40, 40), 'M',
|
(40, 40), 'M',
|
||||||
False)
|
False)
|
||||||
self.beat_button = CircleButton(self, 40, self.fx_mode_buttons[1].y, 20)
|
self.beat_button = BeatButton(self, (40, self.fx_mode_buttons[1].y), 20)
|
||||||
self.rloop_button = CircleButton(self, 40, self.fx_mode_buttons[2].y, 20)
|
self.rloop_button = LoopButton(self, (40, self.fx_mode_buttons[2].y), 20)
|
||||||
self.lloop_button = CircleButton(self, 40, self.fx_mode_buttons[3].y, 20)
|
self.lloop_button = LoopButton(self, (40, self.fx_mode_buttons[3].y), 20)
|
||||||
|
|
||||||
self.rec_button = BinaryButton(
|
self.rec_button = BinaryButton(
|
||||||
self.screen,
|
self.screen,
|
||||||
@ -70,11 +69,6 @@ class GuiMain:
|
|||||||
self.running = True
|
self.running = True
|
||||||
self.run()
|
self.run()
|
||||||
|
|
||||||
def change_color(self, component):
|
|
||||||
if component.color == color_primary:
|
|
||||||
component.color = color_primary_dark
|
|
||||||
else: component.color = color_primary
|
|
||||||
|
|
||||||
def set_fx_mode(self, mode):
|
def set_fx_mode(self, mode):
|
||||||
self.knobs[self.fx_mode].opacity = 0
|
self.knobs[self.fx_mode].opacity = 0
|
||||||
self.fx_mode = self.fx_mode_labels.index(mode)
|
self.fx_mode = self.fx_mode_labels.index(mode)
|
||||||
@ -132,9 +126,9 @@ class GuiMain:
|
|||||||
if event.button == 1:
|
if event.button == 1:
|
||||||
self.mute_button.check_click(pygame.mouse.get_pos())
|
self.mute_button.check_click(pygame.mouse.get_pos())
|
||||||
self.rec_button.check_click(pygame.mouse.get_pos())
|
self.rec_button.check_click(pygame.mouse.get_pos())
|
||||||
self.beat_button.check_click(pygame.mouse.get_pos(), partial(self.change_color, self.beat_button))
|
self.beat_button.check_click(pygame.mouse.get_pos())
|
||||||
self.lloop_button.check_click(pygame.mouse.get_pos(), partial(self.change_color, self.lloop_button))
|
self.lloop_button.check_click(pygame.mouse.get_pos())
|
||||||
self.rloop_button.check_click(pygame.mouse.get_pos(), partial(self.change_color, self.rloop_button))
|
self.rloop_button.check_click(pygame.mouse.get_pos())
|
||||||
|
|
||||||
pygame.quit()
|
pygame.quit()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user