1
"""Gamename input setup handler, part of SOLARWOLF."""
2
# Copyright (C) 2002 Aaron "APS" Schlaegel, LGPL, see lgpl.txt
5
from pygame.locals import *
22
# ship direction constants
33
def load_game_resources():
34
global images, namefont, namefontheight, textfont, textfontheight, delimage, addimage, allimage
35
img = pygame.transform.rotate(gfx.load('ship-up.png'), -90)
36
images.append((img, img.get_rect()))
39
font = txt.Font(None, 50)
40
t = font.text((220, 210, 180), 'Setup Controls', (gfx.rect.centerx, 30))
42
t = txt.Font('sans', 12).text((180, 220, 180), '(You can Pause the game with the PAUSE or P buttons)', (400, 590))
46
namefont = txt.Font(None, 46)
48
textfont = txt.Font(None, textfontheight)
49
smallfont = txt.Font('sans', 12)
51
snd.preload('select_choose', 'select_move', 'incorrect', 'delete')
53
delimage = gfx.load('btn-delete.gif')
54
addimage = gfx.load('btn-add.gif')
55
#allimage = gfx.load('btn-all.gif')
59
def __init__(self, prevhandler):
60
self.prevhandler = prevhandler
62
self.inputstate = BUTTONS
64
self.buildbuttonlist()
65
self.controlrectlist = []
66
self.buildcontrolrectlist()
68
self.clearactionlist()
69
self.buildactionlist()
70
self.currentaction = 0
71
self.currentbutton = 0
74
self.shipdir = SHIPRIGHT
76
self.statusimage = None
79
self.moveto(self.targetbutton())
81
def moveto(self, pos):
82
if self.shipdir == SHIPUP:
83
self.shippos = pos[0] - (self.images[0][1].width / 2), pos[1]
84
elif self.shipdir == SHIPDOWN:
85
self.shippos = pos[0] - (self.images[0][1].width / 2), pos[1] - self.images[0][1].height
86
elif self.shipdir == SHIPRIGHT:
87
self.shippos = pos[0] - self.images[0][1].width, pos[1] - (self.images[0][1].height / 2)
89
self.shippos == pos[0], pos[1] - (self.images[0][1].height / 2)
91
def displayevent(self, i):
92
if i.normalized != None:
93
msg = input.input_text(i.type, i.normalized)
94
if msg not in (self.status, None):
102
self.inputstate = DONE
103
self.clearactionlist()
105
snd.play('select_choose')
110
snd.play('select_choose')
111
self.clearactionlist()
112
self.display[input.actions_order[self.currentaction]].append((i.type, i.normalized))
113
input.setdisplay(self.display)
114
self.buildactionlist()
115
self.drawactionlist()
117
snd.play('incorrect')
118
self.inputstate = BUTTONS
119
self.moveto(self.targetbutton())
123
self.clearactionlist()
124
del self.display[input.actions_order[self.currentaction]][self.currentcontrol]
125
input.setdisplay(self.display)
126
self.buildactionlist()
127
self.drawactionlist()
128
self.inputstate = BUTTONS
129
self.moveto(self.targetbutton())
131
def selectdelete(self):
133
return x[0] != NOEVENT
134
mutable = len(filter(ignoreall, self.display[input.actions_order[self.currentaction]]))
136
snd.play('select_choose')
137
self.inputstate = DELETING
138
self.currentcontrol = 0
139
self.moveto(self.targetcontrol())
141
snd.play('incorrect')
144
if len(self.display[input.actions_order[self.currentaction]]) <= 12:
145
snd.play('select_choose')
146
self.inputstate = ADDING
147
self.currentcontrol = len(self.display[input.actions_order[self.currentaction]])
148
self.moveto(self.targetcontrol())
150
snd.play('incorrect')
153
snd.play('select_choose')
154
self.clearactionlist()
155
if not input.translations.has_key(NOEVENT):
156
input.translations[NOEVENT] = {}
157
input.translations[NOEVENT][KEYDOWN] = input.actions_order[self.currentaction]
158
input.translations[NOEVENT][JOYBUTTONDOWN] = input.actions_order[self.currentaction]
159
self.display = input.getdisplay()
160
self.buildactionlist()
161
self.drawactionlist()
162
self.inputstate = BUTTONS
163
self.moveto(self.targetbutton())
169
if self.inputstate == DONE:
173
#APS switch done to the inputstate
174
if self.inputstate == BUTTONS:
175
if i.translated == input.ABORT:
177
if i.translated == input.PRESS:
178
if self.currentbutton == 0:
180
#elif self.currentbutton == 1:
184
if i.translated in ( input.DOWN, input.UP, input.LEFT, input.RIGHT):
185
if i.translated == input.DOWN:
186
self.currentaction = (self.currentaction + 1) % len(self.actionlist)
187
elif i.translated == input.UP:
188
self.currentaction = (self.currentaction - 1) % len(self.actionlist)
189
elif i.translated == input.LEFT:
190
self.currentbutton = (self.currentbutton - 1) % len(self.buttonlist)
191
elif i.translated == input.RIGHT:
192
self.currentbutton = (self.currentbutton + 1) % len(self.buttonlist)
193
snd.play('select_move')
194
self.moveto(self.targetbutton())
195
elif self.inputstate == DELETING:
196
if i.translated == input.ABORT:
197
snd.play('select_choose')
198
self.inputstate = BUTTONS
199
self.moveto(self.targetbutton())
200
if i.translated == input.PRESS:
202
if i.translated in ( input.DOWN, input.UP, input.LEFT, input.RIGHT):
203
if i.translated == input.DOWN or i.translated == input.UP:
204
currentcontrol = (self.currentcontrol + 6) % 12
205
if currentcontrol < len(self.display[input.actions_order[self.currentaction]]):
206
self.currentcontrol = currentcontrol
207
elif i.translated == input.LEFT:
208
self.currentcontrol = (self.currentcontrol - 1) % 6 + 6 * (self.currentcontrol / 6)
209
if self.currentcontrol >= len(self.display[input.actions_order[self.currentaction]]):
210
self.currentcontrol = len(self.display[input.actions_order[self.currentaction]]) - 1
211
elif i.translated == input.RIGHT:
212
self.currentcontrol = (self.currentcontrol + 1) % 6 + 6 * (self.currentcontrol / 6)
213
if self.currentcontrol >= len(self.display[input.actions_order[self.currentaction]]):
214
self.currentcontrol = 6 * (self.currentcontrol / 6)
215
snd.play('select_move')
216
self.moveto(self.targetcontrol())
218
elif self.inputstate == ADDING:
221
def targetbutton(self):
222
x = self.buttonlist[self.currentbutton][1].left
223
y = self.actionlist[self.currentaction][1].top + self.buttonlist[self.currentbutton][1].centery
226
def targetcontrol(self):
227
x = self.controlrectlist[self.currentcontrol].left
228
y = self.actionlist[self.currentaction][1].top + self.controlrectlist[self.currentcontrol].centery
235
r = self.background(self.images[0][1])
239
gfx.updatestars(self.background, gfx)
241
if self.inputstate != DONE:
242
self.drawactionlist()
244
for img in self.images:
245
r = gfx.surface.blit(img[0], img[1])
248
self.clearactionlist()
250
game.handler = self.prevhandler
252
for img in self.images[1:]:
253
r = self.background(img[1])
256
def buildcontrolrectlist(self):
257
global textfontheight
259
x = 90 + 100 * (l % 6)
260
y = 36 + 22 * (l / 6)
263
r = pygame.Rect(x, y, w, h)
264
self.controlrectlist.append(r)
266
def buildbuttonlist(self):
268
#for img in (addimage, allimage, delimage):
269
for img in (addimage, delimage):
270
rect = img.get_rect().move(300 + 250 * i, 10)
271
self.buttonlist.append((img, rect))
275
def buildactionlist(self):
282
self.display = input.getdisplay()
283
for a in input.actions_order:
284
if gfx.surface.get_bitsize() > 8:
285
img = pygame.Surface((sizex, sizey))
287
img = pygame.Surface((sizex, sizey), 0, 32)
291
subimgs.append((namefont.render(input.actions_text[a], 1, clr), (80, 0)))
293
for l in range(len(self.display[a])):
294
text = input.input_text(self.display[a][l][0],self.display[a][l][1])
295
subimg = textfont.render(text, 1, clr2)
296
r = subimg.get_rect()
297
r.topleft = self.controlrectlist[l].topleft
298
subimgs.append((subimg, r))
300
for b in self.buttonlist:
305
for sub, pos in subimgs:
307
img.set_colorkey(bgd, RLEACCEL)
309
rect = img.get_rect().move(0, offsety)
311
self.actionlist.append((img, rect))
315
def clearactionlist(self):
316
for g in self.actionlist:
317
self.background(g[1])
320
def drawactionlist(self):
321
for g in self.actionlist:
322
r = gfx.surface.blit(g[0], g[1])
325
def buildstatus(self):
326
if self.status != '...':
327
statustext = "(Latest Input Event: %s)" % self.status
328
self.statusimage = textfont.text((255, 250, 160), statustext, (gfx.rect.centerx, 560))
330
self.statusimage = None
332
def drawstatus(self):
334
r = gfx.surface.blit(self.statusimage[0], self.statusimage[1])
337
def clearstatus(self):
339
r = self.background(self.statusimage[1])
342
def background(self, area):
343
return gfx.surface.fill((0, 0, 0), area)
346
pos = list(self.images[0][1].topleft)
347
if pos[0] + self.shipmovex < self.shippos[0]:
348
pos[0] += self.shipmovex
349
elif pos[0] < self.shippos[0]:
350
pos[0] = self.shippos[0]
352
if pos[0] - self.shipmovex > self.shippos[0]:
353
pos[0] -= self.shipmovex
354
elif pos[0] > self.shippos[0]:
355
pos[0] = self.shippos[0]
357
if pos[1] + self.shipmovey < self.shippos[1]:
358
pos[1] += self.shipmovey
359
elif pos[1] < self.shippos[1]:
360
pos[1] = self.shippos[1]
362
if pos[1] - self.shipmovey > self.shippos[1]:
363
pos[1] -= self.shipmovey
364
elif pos[1] > self.shippos[1]:
365
pos[1] = self.shippos[1]
367
self.images[0][1].topleft = pos