cooldown = 0.25 # seconds
def __init__(self, x, y, text, font, size, text_color=Color.BLACK, background=Color.WHITE, border_color=Color.BLACK,
- border_width=1, border_radius=0, padding=10, func=None):
+ border_width=1, border_radius=0, padding=10, darken=True, func=None):
super().__init__(x, y, text, font, size, text_color, background, border_color, border_width, border_radius,
padding)
self.is_hover = False
self.func = func
- self.hover_color = Color.darken(self.background_color, 20)
- self.pressed_color = Color.darken(self.background_color, 40)
+ if darken:
+ self.hover_color = Color.darken(self.background_color, 20)
+ self.pressed_color = Color.darken(self.background_color, 40)
+ else:
+ self.hover_color = Color.lighten(self.background_color, 20)
+ self.pressed_color = Color.lighten(self.background_color, 40)
self.last_click = 0
self.still_pressed = False
def get_child_index(self, child):
return self.children.index(child)
+ def center(self, x=None, y=None):
+ if x is None:
+ x = self.x
+ if y is None:
+ y = self.y
+ self.set_position(x - (self.width - 2 * self.padding) // 2, y - (self.height - 2 * self.padding) // 2)
+
def set_position(self, x, y):
self.x = x
self.y = y
child_y = self.y
for i, child in enumerate(self.children):
child_position_x = self.x + (
- self.width - 2 * self.padding - child.get_width()) // 2 if self.centered else self.x
+ self.get_width() - 2 * self.padding - child.get_width()) // 2 if self.centered else self.x
child_position_y = child_y
if hasattr(child, 'padding'):
child_position_x += child.padding
child.set_position(child_position_x, child_position_y)
child_y += child.get_height() + self.separation_distance
+ self.rect.set_size(self.get_width(), self.get_height())
def update(self):
for child in self.children:
self.update_children_position()
def get_width(self):
- return self.width
+ if self.get_child_count() == 0:
+ return 0
+ return max([child.get_width() for child in self.children]) + self.padding * 2
def get_height(self):
- return self.height
+ return sum([child.get_height() for child in self.children]) + self.padding * 2 + (
+ self.get_child_count() - 1) * self.separation_distance
def move(self, dx, dy):
self.x += dx
def get_child_index(self, child):
return self.children.index(child)
+ def center(self, x=None, y=None):
+ if x is None:
+ x = self.x
+ if y is None:
+ y = self.y
+ self.set_position(x - (self.width - 2 * self.padding) // 2, y - (self.height - 2 * self.padding) // 2)
+
def set_position(self, x, y):
self.x = x
self.y = y
for i, child in enumerate(self.children):
child_position_x = child_x
child_position_y = self.y + (
- self.height - 2 * self.padding - child.get_height()) // 2 if self.centered else self.x
+ self.get_height() - 2 * self.padding - child.get_height()) // 2 if self.centered else self.x
if hasattr(child, 'padding'):
child_position_x += child.padding
child.set_position(child_position_x, child_position_y)
child_x += child.get_width() + self.separation_distance
+ self.rect.set_size(self.get_width(), self.get_height())
def update(self):
for child in self.children:
if hasattr(child, 'update'):
child.update()
+ self.update_children_position()
def get_width(self):
- return self.width
+ return sum([child.get_width() for child in self.children]) + self.padding * 2 + (
+ self.get_child_count() - 1) * self.separation_distance
def get_height(self):
- return self.height
+ if self.get_child_count() == 0:
+ return 0
+ return max([child.get_height() for child in self.children]) + self.padding * 2
def move(self, dx, dy):
self.x += dx
return self.children.index(child)
return -1
+ def center(self, x=None, y=None):
+ if x is None:
+ x = self.x
+ if y is None:
+ y = self.y
+ self.set_position(x - (self.width - 2 * self.grid.padding) // 2, y - (self.height - 2 * self.grid.padding) // 2)
+ self.grid.center(x, y)
+
def set_position(self, x, y):
self.x = x
self.y = y
self.grid.set_position(x, y)
def update(self):
- for row in self.children:
- for child in row:
- if hasattr(child, 'update'):
- child.update()
+ self.grid.update()
def get_width(self):
- return self.width
+ return sum([child.get_width() for child in self.children]) + self.grid.padding * 2 + (
+ self.get_child_count() - 1) * self.grid.separation_distance
def get_height(self):
- return self.height
+ return sum([child.get_height() for child in self.children]) + self.grid.padding * 2 + (
+ self.get_child_count() - 1) * self.grid.separation_distance
def move(self, dx, dy):
self.x += dx
def get_height(self):
return self.height
+ def center(self, x=None, y=None):
+ if x is None:
+ x = self.x
+ if y is None:
+ y = self.y
+ self.set_position(x - self.width / 2, y - self.height / 2)
+
def set_position(self, x, y):
self.x = x
self.y = y
clock = pygame.time.Clock()
delta = 0
-a = Slider(100, 100, 100, 20, 0, 100, 50, Color.LIGHT_GRAY, Color.DARK_GRAY, 4, 5, 10)
+a = Slider(100, 100, 100, 20, 0, 100, 50, Color.LIGHT_GRAY, Color.DARK_GRAY, 1, 1, 0, 15, False)
b = Text(100, 100, "Slider", "Arial", 20, Color.BLACK)
-c = VBoxContainer(100, 100, Color.LIGHT_GRAY, Color.DARK_GRAY, 4, 5, 10, 0, True, [b, a])
-
+d = Button(100, 100, "Button", "arial", 15, Color.BLACK, Color.LIGHT_GRAY, Color.DARK_GRAY, 4, 5, 10, False,
+ lambda: print("Button clicked!"))
+e = Image(100, 100, "image.PNG", Color.BLUE, 1)
+e.resize(100, 100)
+c = GridContainer(10, 10, 2, 2, Color.LIGHT_GRAY, Color.DARK_GRAY, 1, 5, 10, 20, [[a, b], [d, e]])
+c.center(x=640 / 2, y=480 / 2)
+
+g = Label(100, 100, "Label", "Arial", 20, Color.BLACK, Color.LIGHT_GRAY, 1, 5, 10, 20)
+h = Text(100, 100, "Text", "Arial", 20, Color.BLACK)
+i = Rectangle(100, 100, 100, 100, Color.LIGHT_GRAY, Color.DARK_GRAY, 1, 5)
+f = VBoxContainer(30, 30, Color.LIGHT_GRAY, Color.DARK_GRAY, 1, 5, 10, 20, True, [g, h, i])
+
+j = Button(100, 100, "Button1", "arial", 15, Color.RED, Color.BLACK, Color.BLACK, 4, 5, 10, True,
+ lambda: print("Button1 clicked!"))
+k = Button(100, 100, "Button2", "arial", 15, Color.BLACK, Color.LIGHT_GRAY, Color.DARK_GRAY, 4, 5, 10, False,
+ lambda: print("Button2 clicked!"))
+l = Button(100, 100, "Button3", "arial", 15, Color.BLACK, Color.LIGHT_GRAY, Color.DARK_GRAY, 4, 5, 10, False,
+ lambda: print("Button3 clicked!"))
+m = Button(100, 100, "Button4", "arial", 15, Color.BLACK, Color.LIGHT_GRAY, Color.DARK_GRAY, 4, 5, 10, False,
+ lambda: print("Button4 clicked!"))
+n = HBoxContainer(30, 400, Color.LIGHT_GRAY, Color.DARK_GRAY, 1, 5, 10, 20, True, [j, k, l, m])
+n.center(x=640 / 2)
is_running = True
while is_running:
for event in pygame.event.get():
c.update()
c.draw(screen)
+ f.move(delta * 5, 0)
+ f.update()
+ f.draw(screen)
+
+ n.update()
+ n.draw(screen)
+
pygame.display.update()
- delta = clock.tick(60) / 1000 # Seconds since last frame
+ delta = clock.tick(120) / 1000 # Seconds since last frame
pygame.quit()
def collidepoint(self, point):
return self.x <= point[0] <= self.x + self.width and self.y <= point[1] <= self.y + self.height
+ def center(self, x=None, y=None):
+ if x is None:
+ x = self.x
+ if y is None:
+ y = self.y
+ self.set_position(x - self.width / 2, y - self.height / 2)
+
def draw(self, screen):
pygame.draw.rect(screen, self.background_color, (self.x, self.y, self.width, self.height),
border_radius=self.border_radius)
slider_thickness = 4
def __init__(self, x, y, width, height, min_val=0, max_val=100, default=0, background_color=Color.WHITE,
- slider_color=Color.BLACK, border_color=Color.BLACK, border_width=1, border_radius=0, padding=15):
+ slider_color=Color.BLACK, border_color=Color.BLACK, border_width=1, border_radius=0, padding=15,
+ darken=True):
self.x = x
self.y = y
self.background_color = background_color
self.is_pressed = False
self.is_clicked = False
- self.hover_color = Color.darken(self.background_color, 20)
- self.pressed_color = Color.darken(self.background_color, 40)
+ if darken:
+ self.hover_color = Color.darken(self.background_color, 20)
+ self.pressed_color = Color.darken(self.background_color, 40)
+ else:
+ self.hover_color = Color.lighten(self.background_color, 20)
+ self.pressed_color = Color.lighten(self.background_color, 40)
self.last_click = 0
self.still_pressed = False
self.text_color = text_color
self.update_text()
+ def center(self, x=None, y=None):
+ if x is None:
+ x = self.x
+ if y is None:
+ y = self.y
+ self.set_position(x - self.text_surface.get_width() / 2, y - self.text_surface.get_height() / 2)
+
def draw(self, screen):
screen.blit(self.text_surface, (self.x, self.y))