-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui.py
261 lines (204 loc) · 9.07 KB
/
ui.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# <This file contains the code for the user interface windows.>
# Copyright (C) <2012> <Phong Le and Benjamin Liyanage>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pygame
from pygame.locals import *
pygame.init()
FONTCOLOR = (10, 10, 10)
BACKGROUNDCOLOR = (250,250,250)
BLUE = (0,0,255)
RED =( 255,0,0)
VIOLET = ( 130,0,255)
MOVE="Move"
CANCEL="Cancel"
WAIT="End Turn"
class InfoBox(object):
def __init__(self, title, font, x, y, width, height):
self._font = font
self._title = title
self._title = self._font.render(title, 0, RED)
self._titlePosition = self._title.get_rect()
self._titlePosition.top = 5
self._titlePosition.left = 5
self.surface = pygame.Surface((width, height))
self.surface.convert()
self.surface.fill(BACKGROUNDCOLOR)
self.surface.blit(self._title, self._titlePosition)
self.rect = self.surface.get_rect()
self.rect.top = y
self.rect.left = x
class CharacterInfo(InfoBox):
def __init__(self, character, font, screenHeight):
super(CharacterInfo,self).__init__(character._Name, font, 0, screenHeight-150, 300, 200)
self._toolTips = []
# Print Stats in columns
# First Column
xOffset = 10
yOffset = self._titlePosition.bottom+15
yOffset = self.AddTip("Health: " +str(character.Health())+"/"+str(character.MaxHealth()), "", xOffset, yOffset)+5
yOffset = self.AddTip("Level: " + str(character.Level()), "", xOffset, yOffset) + 5
yOffset = self.AddTip("Power: " + str(character.Power()), "", xOffset, yOffset) + 5
self.AddTip("Speed: " + str(character.Speed()), "", xOffset, yOffset)
# Second Column
xOffset = 125
yOffset = self._titlePosition.bottom+30
yOffset = self.AddTip("Experience: " + str(int(character.Experience())), "", xOffset, yOffset) + 5
yOffset = self.AddTip("Defense: " + str(character.Defense()), "", xOffset, yOffset) + 5
self.AddTip("Movement: " + str(character.Movement()), "", xOffset, yOffset)
def AddTip(self, label, description, x, y):
tip = {}
tip['surface'] = self._font.render(label, 0, FONTCOLOR)
tip['rect'] = tip['surface'].get_rect()
tip['rect'].top = y
tip['rect'].left = x
tip['description'] = description
self._toolTips.append(tip)
self.surface.blit(tip['surface'],tip['rect'])
return tip['rect'].bottom
class Menu(InfoBox):
def __init__(self, title, menuItems, font, x, y, width, height,text="",ActionItems=[]):
textwidth=width-40#max width of text
textList=TextChunks(text,int(width/12),[])
super(Menu,self).__init__(title, font, x, y, width, height+15*len(textList)+15)
self._x=x
self._y=y
self._width=width
self._height=height
self._ActionItems=ActionItems
#print(ActionItems)
self._indent = 20
itemY = self._titlePosition.bottom + 10
itemX = self._titlePosition.left + self._indent
for item in textList:
textItem = {}
textItem['name'] = item
textItem['surface'] = self._font.render(item, 0, VIOLET)
textItem['position'] = textItem['surface'].get_rect()
textItem['position'].top = itemY
textItem['position'].left = itemX
itemY = textItem['position'].bottom + 5
self.surface.blit(textItem['surface'], textItem['position'])
self._currentMenuItem = 0
self._BlipSound = pygame.mixer.Sound("sound/blip.wav")
self._menuItems = []
for item in menuItems:
menuItem = {}
menuItem['name'] = item
menuItem['surface'] = self._font.render(item, 0, FONTCOLOR)
menuItem['position'] = menuItem['surface'].get_rect()
menuItem['position'].top = itemY
menuItem['position'].left = itemX
itemY = menuItem['position'].bottom + 5
self.surface.blit(menuItem['surface'], menuItem['position'])
self._menuItems.append(menuItem)
self._menuY=itemY
self._itemY=itemY
self._itemX=itemX
def setActive(self, itemNumber):
self._menuItems[self._currentMenuItem]['surface'] = self._font.render(self._menuItems[self._currentMenuItem]['name'],0,FONTCOLOR, BACKGROUNDCOLOR)
self.surface.blit(self._menuItems[self._currentMenuItem]['surface'], self._menuItems[self._currentMenuItem]['position'])
self._menuItems[itemNumber]['surface'] = self._font.render(self._menuItems[itemNumber]['name'],0,BACKGROUNDCOLOR, FONTCOLOR)
self.surface.blit(self._menuItems[itemNumber]['surface'], self._menuItems[itemNumber]['position'])
if self._currentMenuItem != itemNumber:
self._BlipSound.play(loops=0)
self._currentMenuItem = itemNumber
#draws the description text.
#eraserRect = pygame.rect(0,self._menuY,self._width, self._height-self._menuY)
if self._ActionItems !=[]:
pygame.draw.rect(self.surface,BACKGROUNDCOLOR,(0,self._menuY,self._width, self._height-self._menuY+30))
self._itemY=self._menuY
itemDescription = {}
itemDescriptionText = self._ActionItems[self._menuItems[itemNumber]['name']][1]
itemLevel= self._ActionItems[self._menuItems[itemNumber]['name']][3]
if self._menuItems[itemNumber]['name'] in [MOVE, CANCEL, WAIT]:
textList= TextChunks(itemDescriptionText,int(self._width/13),[])
else:
levelString = [str("Level: "+ str(itemLevel)) ]
textList= levelString+TextChunks(itemDescriptionText,15,[])
#print(textList)
for item in textList:
#print(itemDescriptionText)
itemDescription['name'] = item
itemDescription['surface'] = self._font.render(item, 0, BLUE)
itemDescription['position'] = itemDescription['surface'].get_rect()
itemDescription['position'].top = self._itemY
itemDescription['position'].left = self._itemX
self._itemY = itemDescription['position'].bottom + 5
self.surface.blit(itemDescription['surface'], itemDescription['position'])
def input(self, event):
itemNumber = self._currentMenuItem
if (event.type==KEYDOWN):
if (event.key == K_DOWN):
if (self._currentMenuItem == len(self._menuItems)-1):
itemNumber = 0
else:
itemNumber = self._currentMenuItem + 1
elif (event.key == K_UP):
if (self._currentMenuItem == 0):
itemNumber = len(self._menuItems) - 1
else:
itemNumber = self._currentMenuItem - 1
elif (event.key == K_RETURN):
return self._menuItems[self._currentMenuItem]['name']
self.setActive(itemNumber)
#print(itemNumber)
elif (event.type == MOUSEMOTION):
itemNumber = self.mouseOverItem()
if itemNumber is not None:
self.setActive(itemNumber)
elif (event.type == MOUSEBUTTONDOWN):
itemNumber = self.mouseOverItem()
#itemNumber = self._currentMenuItem
if itemNumber is not None:
#print('From UI',self._menuItems[itemNumber]['name'])
return self._menuItems[itemNumber]['name']
def mouseOverItem(self):
x,y=pygame.mouse.get_pos()
for itemNumber in range(len(self._menuItems)):
if self._menuItems[itemNumber]['position'].collidepoint(x - self.rect.left, y - self.rect.top):
return itemNumber
def TextChunks(l,n,list):#still recursive
l=l.strip()
if l==[]:
return list
elif len(l)<n:
list.append(l[0:len(l)])
return list
else:
k=PreviousWord(l,n)
if k==n:
list.append(l[0:n])
return list+TextChunks(l[n:len(l)],n,[])
else:
list.append(l[0:k])
return list+TextChunks(l[k:len(l)],n,[])
def PreviousWord(l,j): #returns the end of the first full word prior to the index.
#print("previous word list", l)
if len(l)<j:
return 0
if j==0:
return 0
for i in range(j):
#print("len of l",len(l))
#print("j",j)
#print("i",i)
#print("j-i",j-i)
if len(l) <=j-i:
return j-i
elif l[j-i]==" ":
return j-i
elif j>i and l[j-i]!=" " and l[j-i-1]==" ":
return j-i
return 0