the circle buttons have been refactored in a more object oriented way

This commit is contained in:
SallarShayegan
2025-03-02 22:40:13 +01:00
parent 6f0e1e3e5e
commit 692da4b3f2
2 changed files with 61 additions and 23 deletions

View File

@ -4,18 +4,62 @@ import math
class CircleButton:
def __init__(self, gui, x, y, radius):
self.screen = gui.screen
self.x = x
self.y = y
def __init__(self, gui, position, radius):
self.surface = gui.screen
self.position = position
self.radius = radius
self.color = color_primary
def check_click(self, pos, action):
if math.sqrt(math.pow(pos[0] - self.x, 2) +
math.pow(pos[1] - self.y, 2)) < self.radius:
action()
def check_click(self, pos):
if math.sqrt(math.pow(pos[0] - self.position[0], 2) +
math.pow(pos[1] - self.position[1], 2)) < self.radius:
self.on_click()
def on_click(self):
pass
def draw(self):
pass
def display(self):
pygame.draw.circle(self.screen, self.color,
(self.x, self.y), self.radius)
pygame.draw.circle(self.surface, self.color, self.position, 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