-
Notifications
You must be signed in to change notification settings - Fork 0
/
PopupMessage.py
75 lines (59 loc) · 2.26 KB
/
PopupMessage.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
'''
Created on Jul 16, 2012
@author: jami
'''
import Utils
import os
import pygame
class PopupMessage(object):
'''
classdocs
'''
DEFAULT_DURATION = 600
icon = None
title = ""
body = ""
duration = 0
font = None
surface = None
def __init__(self, title, body, duration, width, icon = None, font = None):
'''
Constructor
'''
self.title = title
self.body = body
self.icon = icon
self.duration = duration
self.font = font
if not self.font:
self.font = pygame.font.Font(os.path.join("assets", "promethean.ttf"), 20)
titleSurface = self.font.render(self.title, 1, (204, 204, 204))
if self.icon:
self.body = Utils.parse(body, width - self.icon.get_width(), self.font)
else:
self.body = Utils.parse(body, width, self.font)
bodySurfaces = []
maxWidth = titleSurface.get_width()
for str in self.body:
bodySurfaces.append(self.font.render(str, 1, (204, 255, 204)))
if bodySurfaces[len(bodySurfaces) - 1].get_width() > maxWidth:
maxWidth = bodySurfaces[len(bodySurfaces) - 1].get_width()
y = 0
textSurface = pygame.Surface((maxWidth, titleSurface.get_height() + 2 + (bodySurfaces[0].get_height() + 2) * len(bodySurfaces) ))
textSurface.set_colorkey((0,0,0))
textSurface.blit(titleSurface, (0,y))
y += titleSurface.get_height() + 2
for surf in bodySurfaces:
textSurface.blit(surf, (0, y))
y += surf.get_height() + 2
if self.icon:
self.surface = pygame.Surface((self.icon.get_width() + textSurface.get_width(), self.icon.get_height() + textSurface.get_height()))
self.surface.set_colorkey((0,0,0))
self.surface.blit(self.icon, (0,0))
self.surface.blit(textSurface, (self.icon.get_width(),0))
else:
self.surface = textSurface
def update(self, parent = None):
self.duration -= 1
if parent and self.duration <= 0:
if self in parent: parent.remove(self)