From 958eec2aa0b30e21d568062eafc0de4ff9a4c4d3 Mon Sep 17 00:00:00 2001 From: SallarShayegan Date: Tue, 25 Feb 2025 19:59:56 +0100 Subject: [PATCH] outsourced the fx_mode buttons to a new class and some refactoring --- lozenge_button.py | 38 ++++++++++++++++++++++++++++++++++++++ main.py | 40 +++++++++++++++++++++------------------- 2 files changed, 59 insertions(+), 19 deletions(-) create mode 100644 lozenge_button.py diff --git a/lozenge_button.py b/lozenge_button.py new file mode 100644 index 0000000..2a2a94c --- /dev/null +++ b/lozenge_button.py @@ -0,0 +1,38 @@ +import pygame +import math +from colors import * +from fonts import * + +class LozengeButton: + + def __init__(self, gui, name, position, radius_x, radius_y): + self.gui = gui + self.screen = gui.screen + self.name = name + self.x = position[0] + self.y = position[1] + self.radius_x = radius_x + self.radius_y = radius_y + self.focused = False + + 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.x, 2) + math.pow(pos[1] - self.y, 2)) < self.radius_y: + self.focused = True + self.gui.set_fx_mode(self.name) + + if self.focused: + w = 0 + fx_mode_label = font_helvetica16.render(self.name, False, color_primary) + self.screen.blit(fx_mode_label, (self.x + 20, self.y - 8)) + else: + w = 2 + fx_mode_label = font_helvetica16.render(str(self.name)[0:1], False, color_primary) + self.screen.blit(fx_mode_label, (self.x - 4, self.y - 7)) + pygame.draw.polygon(self.screen, color_primary, + [(self.x - self.radius_x, self.y), + (self.x, self.y - self.radius_y), + (self.x + self.radius_x, self.y), + (self.x, self.y + self.radius_y)], w) \ No newline at end of file diff --git a/main.py b/main.py index 807d2c6..0da7758 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ import pygame from knob import * from colors import * from fonts import * +from lozenge_button import LozengeButton class GuiMain: @@ -12,7 +13,7 @@ class GuiMain: self.screenH = 325 self.screen = pygame.display.set_mode((self.screenW, self.screenH)) - self.fx_modes = [ + fx_mode_labels = [ 'Strings', 'Beat', 'L-Loop', @@ -31,6 +32,18 @@ class GuiMain: 'Gain' ] + # Create and position the fx_mode buttons + self.fx_mode_buttons = [] + + for i in range(len(fx_mode_labels)): + x = self.screenW * 2.5 / 19 + y = i * self.screenH / 7.3 + 35 + r_x = self.screenW * 0.35 / 19 + r_y = (self.screenH-20) / 16 + self.fx_mode_buttons.append(LozengeButton(self, fx_mode_labels[i], (x,y), r_x, r_y)) + + self.fx_mode_buttons[0].focused = True + # Create and position the knobs self.knobs_radius = self.screenW / 25 knobs_spacing = self.knobs_radius * 2 @@ -87,17 +100,15 @@ class GuiMain: 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 draw_lozi(self, x, y, color, outlined = False): - rX = self.screenW * 0.35 / 19 - rY = (self.screenH-20) / 16 - w = 2 if outlined else 0 - pygame.draw.polygon(self.screen, color, [(x-rX, y), (x, y-rY), (x+rX, y), (x, y+rY)], w) 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 + def run(self): while self.running: # poll for events @@ -115,19 +126,10 @@ class GuiMain: if knob.focused: self.display_label(knob) knob.display() + for button in self.fx_mode_buttons: + button.display() + pygame.draw.rect(self.screen, color_primary_dark, ((10, 10), (self.screenW * 1.5 / 19, self.screenH - 18))) - fx_mode = 1 - for i in range(6): - x = self.screenW * 2.5 / 19 - y = i * self.screenH / 7.3 + 35 - if i == fx_mode: - self.draw_lozi(x, y, color_primary) - fx_mode_label = font_helvetica16.render(self.fx_modes[i], False, color_primary) - self.screen.blit(fx_mode_label, (x+20, y-8)) - else: - self.draw_lozi(x, y, color_primary, True) - fx_mode_label = font_helvetica16.render(str(self.fx_modes[i])[0:1], False, color_primary) - self.screen.blit(fx_mode_label, (x-4, y-7)) pygame.draw.polygon(self.screen, "black", [(0, self.screenH/2), (self.screenW*3 / 19, self.screenH), (0, self.screenH)], 20) pygame.draw.polygon(self.screen, "white", [(0, self.screenH/2), (self.screenW*3 / 19, self.screenH), (0, self.screenH)])