-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui_elements.py
89 lines (68 loc) · 3.77 KB
/
ui_elements.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import pygame
# ═══════════════════════════════════════════════════════════════════════════ #
# ═════ TEXT WRAP FUNCTION ═════════════════════════════════════════════════ #
def text_wrap(screen, text, position, font, color, max_width):
words = text.split(" ")
lines = []
current_line = words[0]
for word in words[1:]:
if font.size(current_line + " " + word)[0] < max_width:
current_line += " " + word
else:
lines.append(current_line)
current_line = word
lines.append(current_line)
for i, line in enumerate(lines):
text_surface = font.render(line, True, color)
text_rect = text_surface.get_rect(topleft=(position[0], position[1] + i * font.get_linesize()))
screen.blit(text_surface, text_rect)
# ═══════════════════════════════════════════════════════════════════════════ #
# ═══ POP-UP WINDOW ═════════════════════════════════════════════════════════ #
def draw_popup(screen, message):
popup_width, popup_height = 300, 150
popup_surface = pygame.Surface((popup_width, popup_height))
popup_surface.fill((13, 17, 23))
popup_rect = popup_surface.get_rect(center=(screen.get_width() // 2, screen.get_height() // 2))
# Draw border
border_thickness = 5
border_color = (255, 255, 255)
border_rect = pygame.Rect(
popup_rect.left - border_thickness,
popup_rect.top - border_thickness,
popup_width + 2 * border_thickness,
popup_height + 2 * border_thickness
)
pygame.draw.rect(screen, border_color, border_rect)
screen.blit(popup_surface, popup_rect)
# Draw message
font = pygame.font.Font("assets/arial.ttf", 24)
text_surface = font.render(message, True, (255, 255, 255))
text_rect = text_surface.get_rect(center=(popup_rect.centerx, popup_rect.centery - 20))
screen.blit(text_surface, text_rect)
# Draw button
button_rect = pygame.Rect(popup_rect.centerx - 50, popup_rect.centery + 20, 100, 40)
pygame.draw.rect(screen, (255, 255, 255), button_rect)
button_text = font.render("Continue", True, (0, 0, 0))
button_text_rect = button_text.get_rect(center=button_rect.center)
screen.blit(button_text, button_text_rect)
return button_rect
# ═══════════════════════════════════════════════════════════════════════════ #
# ═══ SHIMMER & HOVER CHECK ═════════════════════════════════════════════════ #
""" Check if mouse cursor is hovering over image """
def is_hovered(rect):
mouse_pos = pygame.mouse.get_pos()
return rect.collidepoint(mouse_pos)
""" Shimmer effect """
def draw_shimmer(surface, rect, progress):
# Create a translucent white surface
shimmer_surface = pygame.Surface((rect.width, rect.height), pygame.SRCALPHA)
shimmer_surface.fill((255, 255, 255, 0)) # Fully transparent
# Define shimmer width and calculate position
shimmer_width = int(rect.width * 0.1)
x_position = int(rect.width * progress) - shimmer_width
# Draw the shimmer
if x_position < rect.width:
shimmer_rect = pygame.Rect(x_position, 0, shimmer_width, rect.height)
shimmer_surface.fill((255, 255, 255, 128), shimmer_rect)
# Draw the shimmer surface onto the main surface
surface.blit(shimmer_surface, rect.topleft)