-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTextBox.py
170 lines (150 loc) · 8.68 KB
/
TextBox.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
import pygame
from pygame.locals import *
import ImageData
import DisplayInfo
import pygame.time
import GlobalData
def loadTextImages(xNum, yNum, alpha = None):
GlobalData.textureManager.loadTexture("text", "images/textbox/box.PNG", alpha)
for y in range(0,xNum):
for x in range(0,yNum):
GlobalData.textureManager.spriteRects["text"].append(pygame.rect.Rect(x*24,y*24,24,24))
class TextBox:
def __init__(self, x, y, text = ""):
self.font = pygame.font.Font(None, 24)
self.x = x
self.y = y
self.maxWidth = GlobalData.display.getScreenWidth() - 96
self.maxLength = GlobalData.display.getScreenHeight() - 96
self.width = 48
self.height = 48
self.text = text
def battleShow(self):
final_lines = ["Attack", "WTC", "HTC", "Item", "Run"]
self.width = 120
self.xCount = self.width/24
if self.width%24 != 0:
self.xCount += 1
self.yCount = len(final_lines)
if len(final_lines) == 1:
self.yCount +=1
for x in range(self.xCount):
for y in range(self.yCount):
if x == 0:
if y == 0:
#print "topleft"
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][1])
elif y == len(final_lines)-1:
#print "botleft"
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][7])
else:
#print "midleft"
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][4])
elif x == self.xCount - 1 :
if y == 0:
#print "topright"
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][3])
elif y == len(final_lines)-1:
#print "botright"
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][9])
else:
#print "midright"
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][6])
else:
if y == 0:
#print "topmid"
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][2])
elif y == len(final_lines)-1:
#print "botmid"
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][8])
else:
#print "center"
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][5])
self.yPlus = 0
for x in final_lines:
GlobalData.display.getScreen().blit(self.font.render(x, 0, (255,255,255)), (self.x + 6, self.y + self.yPlus + 4))
self.yPlus += 24
def draw(self):
final_lines = []
requested_lines = self.text
while self.font.size(requested_lines)[0]> self.width - 24:
if self.width < self.maxWidth:
self.width += 24
else:
words = requested_lines.split(' ')
# if any of our words are too long to fit, return.
for word in words:
if self.font.size(word)[0] >= self.width - 24:
print "The word " + word + " is too long to fit in the rect passed."
# Start a new line
accumulated_line = ""
for word in words:
test_line = accumulated_line + word + " "
# Build the line while the words fit.
if self.font.size(test_line)[0] < self.width - 24:
accumulated_line = test_line
else:
final_lines.append(accumulated_line)
accumulated_line = word + " "
final_lines.append(accumulated_line)
break
if self.width < self.maxWidth:
final_lines.append(requested_lines)
self.xCount = self.width/24
if self.width%24 != 0:
self.xCount += 1
self.yCount = len(final_lines)
if len(final_lines) == 1:
self.yCount += 1
for x in range(self.xCount):
for y in range(self.yCount):
if x == 0:
if y == 0:
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][1])
elif y == len(final_lines):
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][7])
else:
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][4])
elif x == self.xCount - 1 :
if y == 0:
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][3])
elif y == len(final_lines):
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][9])
else:
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][6])
else:
if y == 0:
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][2])
elif y == len(final_lines):
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][8])
else:
GlobalData.display.getScreen().blit(GlobalData.textureManager.textures["text"][0], (self.x + x*24, self.y + y*24), GlobalData.textureManager.spriteRects["text"][5])
self.yPlus = 0
for x in final_lines:
GlobalData.display.getScreen().blit(self.font.render(x, 0, (255,255,255)), (self.x + 6, self.y + self.yPlus + 4))
self.yPlus += 24
#pygame.display.flip()
def setText(self, text):
if self.font.size(text)[0] > self.width:
self.width = self.font.size(text)[0]
if self.font.size(text)[1] > self.height:
self.height = self.font.size(text)[1]
if self.font.size(self.title)[0] > self.width:
self.width = self.font.size(self.title)[0]
if self.font.size(self.title)[1] > self.height:
self.height = self.font.size(self.title)[1]
self.text = text
class QuickBox:
def __init__(self, x, y, text):
self.box = TextBox(x, y, text)
self.open = True
while self.open:
self.box.draw()
pygame.display.flip()
for e in pygame.event.get():
if e.type == QUIT:
GlobalData.quitFlag = 1
return
elif e.type == KEYDOWN:
if e.key == K_RETURN:
self.open = False