-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogs.py
201 lines (153 loc) · 6.42 KB
/
dialogs.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import pygame as PG
#----------------------------------------------------------------------
def popUp(msg='text', title=None, w=250,background=(255, 200, 175),
font='Times', fsize=12, fcolor=(0, 0, 0)):
"""pops up a box with text on it
returns the surface w/ the dialog"""
borderWidth = 4
cornerRadius = 10
lineColor = (11, 11, 101)
#text inside
f = PG.font.SysFont(font, fsize)
if title:
titleLines = breakupText(title, f, w, borderWidth, cornerRadius)
listTitleSurfs = renderTextToSurf(titleLines, f, fcolor, 1)
#break up the title and text into lines to render
lines = breakupText(msg, f, w, borderWidth , cornerRadius)
#put textSurfaces with the rendered text in the created list
listLinesSurfs = renderTextToSurf(lines, f, fcolor)
#calc their combined height
height = 0
for surface in listLinesSurfs:
height += surface.get_rect().h
#print 'before h', height
if title:
#beforeTitleHeight = height
for surface in listTitleSurfs:
height += surface.get_rect().h
#print 'after h', height
#afterTitleHeight = height
#create a surf to blit texts too, to put borders onto
surfaceLines = PG.surface.Surface((w, height))
surfaceLines.set_colorkey((123, 123, 123))
surfaceLines.fill((123, 123, 123))
#move all the renderedTexts to the larger surface
#n = 0
#height of blitting
drawingH = 0
if title:
for surface in listTitleSurfs:
surfaceLines.blit(surface, (0, drawingH))
drawingH += surface.get_rect().h
for surface in listLinesSurfs:
surfaceLines.blit(surface, (0, drawingH))
#n += 1
drawingH += surface.get_rect().h
#now we have a surface with all the text on it: surfaceLines
borderedSurface = drawBorders(drawingH, cornerRadius, w,
background, lineColor, borderWidth)
borderedSurface.blit(surfaceLines, (cornerRadius + borderWidth, borderWidth))
#print 'returning a surface', borderedSurface
return borderedSurface
#----------------------------------------------------------------------
def breakupText(rawText, font, w, borderWidth, cornerRadius):
"""breaks up text into lines that are smaller that width"""
f = font
splitText = rawText.split('\n')
#for each line in rawText
allLines = []
for text in splitText:
#get width of surface with text on it
width = f.size(text)[0]
#print 'width:', width
#if width is too wide, try breaking down the lines:
lines = []
if width >= w - 20:
#break down the sentence by word
wordList = text.split(' ')
widthNeeded = w - (borderWidth * 2)
#while textwidth is wider than width
while f.size(' '.join(wordList))[0] >= widthNeeded:
wordCount = 0
linesize = 0
currentLine = []
while f.size(' '.join(currentLine))[0] < widthNeeded:
#try to add another word, test first if itd fit
listed = list(wordList[wordCount])
if f.size(' '.join(currentLine + listed))[0] < widthNeeded:
currentLine.append(wordList[wordCount])
wordCount += 1
else:
break
lines.append(currentLine)
for word in currentLine:
wordList.remove(word)
#print 'leftover words:', wordList
lines.append(wordList)
else:
lines.append(text)
allLines.append(lines)
return allLines
#----------------------------------------------------------------------
def drawBorders(drawingH, cornerRadius, w, background, lineColor, borderWidth):
"""draws the borders of a window"""
h = drawingH + cornerRadius
surf = PG.surface.Surface((w, h))
surf.fill(background)
surf.set_colorkey((123, 123, 123))
#corners of the surface
tlcorner = (0, 0)
trcorner = (w - 1, 0)
blcorner = (0, h - 1)
brcorner = (w - 1, h - 1)
points = (tlcorner, trcorner, brcorner, blcorner)
size = (25, 25)
#set up rects with corners from above
rects = []
for pos in points:
rect = PG.Rect(pos, size)
rect.center = pos
rects.append(rect)
#draw each circle in the corners
for rect in rects:
PG.draw.circle(surf, lineColor , rect.center, cornerRadius)
#draw lines to each corner
PG.draw.lines(surf, lineColor, 1, points, borderWidth)
return surf
#----------------------------------------------------------------------
def renderTextToSurf(lines, font, fcolor, underline=0):
"""renders a list of lines of text, and returns a list of them"""
listLinesSurfs = []
font.set_underline(underline)
for line in lines:
printable = ' '.join(line)
surfaceText = font.render(printable, 0, fcolor)
listLinesSurfs.append(surfaceText)
return listLinesSurfs
#----------------------------------------------------------------------
def splitLines(text, width):
"""splits lines according to width"""
lines = []
if width >= w - 20:
#break down the sentence by word
wordList = msg.split(' ')
widthNeeded = w - (borderWidth * 2)
#while msgwidth is wider than width
while f.size(' '.join(wordList))[0] >= widthNeeded:
wordCount = 0
linesize = 0
currentLine = []
while f.size(' '.join(currentLine))[0] < widthNeeded:
#try to add another word, test first if itd fit
listed = list(wordList[wordCount])
if f.size(' '.join(currentLine + listed))[0] < widthNeeded:
currentLine.append(wordList[wordCount])
wordCount += 1
else:
break
lines.append(currentLine)
for word in currentLine:
wordList.remove(word)
#print 'leftover words:', wordList
lines.append(wordList)
return lines