1import pygame
2from pygame import display
3
4class Notification_Handler:
5
6 notifications = []
7
8 def __init__(self, game):
9 self.game = game
10
11 def send(self, title, message):
12 print("[" + title + "] " + message)
13 self.notifications.append({"title": title, "message": message, "time": 300})
14
15 def render(self):
16 height = 0
17 last_end_y = 22
18
19 for index, notification in enumerate(self.notifications):
20 display_text = []
21 current = ""
22 message = notification["message"].split(" ")
23 for index, word in enumerate(message):
24 if current != "":
25 current += " " + word
26 else:
27 current += word
28 if index < len(message) - 1:
29 if len(current + message[index + 1]) > 30:
30 display_text.append(current)
31 current = ""
32
33 display_text.append(current)
34
35 height = 32 + (15 * len(display_text))
36 pygame.draw.rect(self.game.main_surface, self.game.color_handler.get_color_rgb("notification.border"), ((self.game.main_surface.get_width() - 250, last_end_y), (230, height)), False, 10)
37 pygame.draw.rect(self.game.main_surface, self.game.color_handler.get_color_rgb("notification.fill"), ((self.game.main_surface.get_width() - 246, last_end_y + 4), (222, height - 8)), False, 10)
38
39 title_surface = self.game.font_handler.get_font("default_18", bold=True).render(notification["title"], True, self.game.color_handler.get_color_rgb("notification.text"))
40 self.game.main_surface.blit(title_surface, (self.game.main_surface.get_width() - 241, last_end_y + 2))
41
42 for i, notif in enumerate(display_text):
43 title_surface = self.game.font_handler.get_font("default_15").render(notif, True, self.game.color_handler.get_color_rgb("notification.text"))
44 self.game.main_surface.blit(title_surface, (self.game.main_surface.get_width() - 241, last_end_y + (i * 14) + 20))
45
46 last_end_y = height + 20 + last_end_y
47
48 def update(self):
49 new_notif = self.notifications.copy()
50 for index, notification in enumerate(self.notifications):
51 self.notifications[index]["time"] -= 1
52 if self.notifications[index]["time"] <= 0:
53 new_notif.pop(new_notif.index(self.notifications[index]))
54 self.notifications = new_notif