ADD breakpoints on slider for round changes
This commit is contained in:
55
widgets/break_slider.py
Normal file
55
widgets/break_slider.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from widgets.slider import HorizontalSlider
|
||||
import pygame
|
||||
|
||||
class BreakSlider(HorizontalSlider):
|
||||
def __init__(self, screen, x, y, width, height, min_value, max_value):
|
||||
super().__init__(screen, x, y, width, height, min_value, max_value)
|
||||
self.breakpoints = []
|
||||
|
||||
# defaults
|
||||
self.breakpoint_colour = (0, 0, 0)
|
||||
self.breakpoint_line_width = 2
|
||||
|
||||
def add_breakpoint(self, breakpoint):
|
||||
if breakpoint < self.min_value or breakpoint > self.max_value:
|
||||
raise ValueError("Breakpoint must fit between min and max values")
|
||||
|
||||
self.breakpoints.append(breakpoint)
|
||||
|
||||
def set_breakpoints(self, breakpoints):
|
||||
self.breakpoints = breakpoints
|
||||
|
||||
def _draw_breakpoints(self):
|
||||
for breakpoint in self.breakpoints:
|
||||
# create line
|
||||
break_x = self._value_to_knob(breakpoint)
|
||||
|
||||
pygame.draw.line(self.screen,
|
||||
self.breakpoint_colour,
|
||||
(break_x, self.y),
|
||||
(break_x, self.y + self.height),
|
||||
width=self.breakpoint_line_width)
|
||||
|
||||
def draw(self):
|
||||
self._draw_slider()
|
||||
self._draw_breakpoints()
|
||||
self._draw_knob()
|
||||
|
||||
if __name__ == "__main__":
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((800, 600))
|
||||
slider = BreakSlider(screen, 50, 50, 700, 20, 0, 1000)
|
||||
slider.add_breakpoint(100)
|
||||
slider.add_breakpoint(500)
|
||||
slider.add_breakpoint(700)
|
||||
running = True
|
||||
|
||||
while running:
|
||||
screen.fill((0, 0, 0))
|
||||
slider.draw()
|
||||
pygame.display.flip()
|
||||
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
slider.handle_event(event)
|
||||
@@ -54,17 +54,24 @@ class HorizontalSlider:
|
||||
self.knob_x = event.pos[0]
|
||||
self.value = self._knob_to_value(self.knob_x)
|
||||
|
||||
def draw(self):
|
||||
"""
|
||||
Draw the slider on the screen
|
||||
"""
|
||||
def _draw_slider(self):
|
||||
if self.fill:
|
||||
pygame.draw.rect(self.screen, self.fill_colour, (self.x, self.y, self.knob_x, self.height), border_radius=self.rect_radius)
|
||||
pygame.draw.rect(self.screen, self.background_colour, (self.knob_x, self.y, self.width - (self.knob_x - self.x), self.height), border_radius=self.rect_radius)
|
||||
else:
|
||||
pygame.draw.rect(self.screen, self.background_colour, (self.x, self.y, self.width, self.height), border_radius=self.rect_radius)
|
||||
|
||||
def _draw_knob(self):
|
||||
pygame.draw.circle(self.screen, self.knob_colour, (int(self.knob_x), self.y + self.height // 2), self.knob_radius)
|
||||
|
||||
def draw(self):
|
||||
"""
|
||||
Draw the slider on the screen
|
||||
"""
|
||||
self._draw_slider()
|
||||
self._draw_knob()
|
||||
|
||||
|
||||
def set_value(self, value):
|
||||
"""
|
||||
Set the value of the slider
|
||||
@@ -89,6 +96,9 @@ class HorizontalSlider:
|
||||
def set_knob_colour(self, colour):
|
||||
self.knob_colour = colour
|
||||
|
||||
def set_fill_colour(self, colour):
|
||||
self.fill_colour = colour
|
||||
|
||||
if __name__ == "__main__":
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((800, 600))
|
||||
|
||||
Reference in New Issue
Block a user