Ça exporte en smwi mais, peu de chances que ça marche.
[minwii.git] / src / gui / PGUConfiguration.py
1 '''
2 Created on 12 nov. 2009
3
4 @author: Samuel Benveniste
5 '''
6
7 import os
8 import sys
9 import subprocess
10
11 import pygame
12 import pygame.midi
13 import pickle
14
15 from pygame.locals import *
16
17 from pgu import gui as pguGui
18
19 from PlayingScreen import PlayingScreen
20 from SongPlayingScreen import SongPlayingScreen
21 from InstrumentChoice import InstrumentChoice
22 from instruments.Instrument import Instrument
23 from cursor.WarpingCursor import *
24 from controllers.Wiimote import Wiimote
25 from logging.Log import Log
26 from songs.Song import Song,loadSong
27 from constants import *
28 from MINWiiDialog import MINWiiDialog
29
30 class PGUConfiguration(pguGui.Desktop):
31 '''
32 classdocs
33 '''
34
35
36 def __init__(self,window):
37 '''
38 Constructor
39 '''
40 pguGui.Desktop.__init__(self)
41 self.extendedScale = False
42 self.cascade = False
43 self.song = None
44 self.scale = scaleDict["majorScale"]
45 self.log = None
46 self.done = False
47 self.easyMode = True
48 self.mode = 0
49 self.activeWiimotes = [False for i in range(4)]
50 self.alwaysDown = False
51
52 self.file = None
53 #fileName is the path for the log file
54 self.fileName = fileName
55 self.titleFont = pygame.font.Font(None,100)
56 self.font = pygame.font.Font(None,70)
57 self.spaceSize = (100,100)
58
59 self.browseButton = pguGui.Button(self.createLabel("Choisir..."))
60 self.browseButton.connect(pguGui.CLICK, self.open_file_browser, None)
61
62 self.songSwitch = pguGui.Switch(False)
63
64 self.modeSelect = pguGui.Select()
65 for key in modeDict.keys() :
66 self.modeSelect.add(self.createLabel(reversedReadabilityDict[key]),key)
67 self.modeSelect.connect(pguGui.CHANGE,self.modeSelectChanged,None)
68
69 self.activeWiimoteSwitches = [pguGui.Switch(False) for i in range(4)]
70 for i in range(len(self.activeWiimoteSwitches)) :
71 self.activeWiimoteSwitches[i].connect(pguGui.CHANGE,self.activeWiimoteSwitchesChanged,i)
72
73 self.goButton = pguGui.Button(self.createLabel("Go"))
74 self.goButton.connect(pguGui.CLICK,self.goButtonClicked,None)
75
76 self.quitButton = pguGui.Button(self.createLabel("Fin"))
77 self.quitButton.connect(pguGui.CLICK,self.quitButtonClicked,None)
78
79 self.connect(pguGui.QUIT,self.quit,None)
80
81 self.window = window
82
83 ##The table code is entered much like HTML.
84 ##::
85
86 self.mainTable = pguGui.Table()
87
88 self.fillMainTable()
89
90 # self.mainTable.tr()
91 # self.mainTable.td(self.createLabel("MINWii",self.titleFont),colspan = 4)
92
93 self.run(self.mainTable)
94
95 def open_file_browser(self,data=None):
96 d = MINWiiDialog(font = self.font,width = 800, height = 600,path = "../songs/smwis")
97 d.connect(pguGui.CHANGE, self.handle_file_browser_closed, d)
98 d.open()
99
100
101 def handle_file_browser_closed(self,dlg):
102 if dlg.value:
103 if os.path.isfile(dlg.value):
104 self.file = dlg.value
105 self.song = loadSong(self.file)
106 key = os.path.basename(self.file)[:-5]
107 if key in reversedReadabilityDict :
108 label = self.createLabel(reversedReadabilityDict[key])
109 else :
110 label = self.createLabel(key)
111 self.browseButton = pguGui.Button(label)
112 self.browseButton.connect(pguGui.CLICK, self.open_file_browser, None)
113 if not self.songSwitch.value :
114 self.songSwitch.click()
115 self.mainTable.clear()
116 self.fillMainTable()
117
118 def fillMainTable(self):
119
120 self.mainTable.tr()
121 self.mainTable.td(pguGui.Spacer(*self.spaceSize))
122
123 self.mainTable.tr()
124 self.mainTable.td(self.createLabel("Chanson :"))
125 self.mainTable.td(self.browseButton,colspan=2)
126 self.mainTable.td(self.songSwitch)
127
128 self.mainTable.tr()
129 self.mainTable.td(pguGui.Spacer(*self.spaceSize))
130
131 self.mainTable.tr()
132 self.mainTable.td(self.createLabel("Niveau :"))
133 self.mainTable.td(self.modeSelect,colspan=3)
134
135 self.mainTable.tr()
136 self.mainTable.td(pguGui.Spacer(*self.spaceSize))
137
138 self.mainTable.tr()
139 self.mainTable.td(self.createLabel("Joueurs :", self.font))
140 playerTable = pguGui.Table()
141 for i in range(len(self.activeWiimoteSwitches)):
142 playerTable.td(self.createLabel(" " + str(i+1)+" ", self.font))
143 playerTable.td(self.activeWiimoteSwitches[i])
144 self.mainTable.td(playerTable,colspan = 3)
145
146 self.mainTable.tr()
147 self.mainTable.td(pguGui.Spacer(*self.spaceSize))
148
149 self.mainTable.tr()
150 self.mainTable.td(self.goButton)
151 self.mainTable.td(self.quitButton,colspan=3)
152
153 self.mainTable.tr()
154 self.mainTable.td(pguGui.Spacer(500,500))
155
156 def createLabel(self,text,font = None):
157 if font == None :
158 font = self.font
159 w,h = self.font.size(text)
160 label = pguGui.Label(text,width=w,height=h,font = font)
161 return(label)
162
163 def songSelectChanged(self,data=None):
164 self.song = songDict[self.songSelect.value]
165
166 def activeWiimoteSwitchesChanged(self,data = None):
167 if self.activeWiimoteSwitches[data].value :
168 for i in range(len(self.activeWiimoteSwitches)) :
169 if self.activeWiimoteSwitches[i].value and data != i :
170 self.activeWiimoteSwitches[i].click()
171 for i in range(len(self.activeWiimoteSwitches)) :
172 self.activeWiimotes[i] = self.activeWiimoteSwitches[i].value
173
174 def modeSelectChanged(self,data = None):
175 self.mode = modeDict[self.modeSelect.value]
176
177 def hasActiveWiimote(self):
178 hasActive = False
179 for i in self.activeWiimotes:
180 if i :
181 hasActive = True
182 return(hasActive)
183
184 def quitButtonClicked(self,data = None):
185 self.done = True
186 print 'puti'
187 for isActive in self.activeWiimotes :
188 print isActive
189 self.quit()
190
191 def goButtonClicked(self,data = None):
192 pygame.font.init()
193
194 if not self.hasActiveWiimote():
195 self.activeWiimotes[0] = True
196 pygame.midi.init()
197 instruments = [Instrument(self.scale, i + 1, "".join(["../instruments/instrumentImages/", instrumentImagePathList[i], ".jpg"]), octaves[i]) for i in range(9)]
198
199 joys = [[id,pygame.joystick.Joystick(id).get_name()] for id in range(pygame.joystick.get_count())]
200 for joy in joys:
201 print joy[1]
202 if joy[1] in joyNames:
203 print "On"
204 pygame.joystick.Joystick(joy[0]).init()
205 else :
206 print "off"
207
208 ports = [pygame.midi.get_device_info(id)[1] for id in range(pygame.midi.get_count())]
209 portOffset = ports.index(portNames[0])
210 print(portOffset)
211
212 screen = pygame.display.get_surface()
213 clock = pygame.time.Clock()
214 cursorImages=[['../cursor/cursorImages/black/10.png'],['../cursor/cursorImages/red/10.png'],['../cursor/cursorImages/blue/10.png'],['../cursor/cursorImages/green/10.png']]
215 durations = [75 for i in range(len(cursorImages[0]))]
216
217 wiimoteCount = 4
218 cursors = [WarpingCursor(None, cursorImages[i], durations, (300 * i, 300 * i),flashImage = '../cursor/cursorImages/black/flash.png' ) for i in range(wiimoteCount)]
219 wiimotes = [Wiimote(i, i + portOffset, None, None, cursors[i]) for i in range(wiimoteCount)]
220
221 if self.song != None and self.songSwitch.value :
222
223 if self.mode == 0 :
224 self.extendedScale = self.song.requiresExtendedScale
225 self.cascade = True
226 self.easyMode = True
227 self.alwaysDown = True
228 elif self.mode == 1 :
229 self.extendedScale = self.song.requiresExtendedScale
230 self.cascade = True
231 self.easyMode = True
232 elif self.mode == 2:
233 self.extendedScale = self.song.requiresExtendedScale
234 self.cascade = False
235 self.easyMode = True
236 elif self.mode == 3:
237 self.extendedScale = True
238 self.cascade = False
239 self.easyMode = False
240
241 choice = InstrumentChoice(instruments, wiimotes, self.window, screen, clock, joys, portOffset,self.activeWiimotes, scaleFactor = songScaleFactor)
242 play = SongPlayingScreen(choice, self.song,self.cascade, self.extendedScale,self.easyMode,self.alwaysDown)
243
244 else:
245
246 if self.mode == 0 :
247 self.extendedScale = False
248 self.cascade = False
249 elif self.mode == 1 :
250 self.extendedScale = True
251 self.cascade = False
252 elif self.mode == 2:
253 self.extendedScale = False
254 self.cascade = True
255 elif self.mode == 3:
256 self.extendedScale = True
257 self.cascade = True
258
259 choice = InstrumentChoice(instruments, wiimotes, self.window, screen, clock, joys, portOffset,self.activeWiimotes)
260 play = PlayingScreen(choice, None,self.cascade, self.extendedScale)
261
262 while play.backToInstrumentChoice == True :
263
264 for wiimote in wiimotes:
265 del wiimote.port
266
267 wiimotes = [Wiimote(i, i + portOffset, None, None, cursors[i]) for i in range(wiimoteCount)]
268 previousEventLog = choice.eventLog
269
270 if self.song != None :
271 choice = InstrumentChoice(instruments, wiimotes, self.window, screen, clock, joys, portOffset, self.activeWiimotes,eventLog = previousEventLog, replay = False, scaleFactor = songScaleFactor)
272 play = SongPlayingScreen(choice, self.song, False, self.extendedScale,self.easyMode)
273 else:
274 choice = InstrumentChoice(instruments, wiimotes, self.window, screen, clock, joys, portOffset,self.activeWiimotes, eventLog = previousEventLog, replay = False)
275 play = PlayingScreen(choice, None, self.cascade, self.extendedScale)
276
277 for wiimote in wiimotes:
278 del wiimote.port
279
280 i = 1
281 filePath = "".join([self.fileName,str(i),".mwi"])
282 while os.path.exists(filePath):
283 i += 1
284 filePath = "".join([self.fileName,str(i),".mwi"])
285
286 f = file(filePath, 'w')
287 self.log = Log(play.eventLog,self.scale,self.extendedScale,self.cascade,self.song,self.mode,self.activeWiimotes)
288 pickler = pickle.Pickler(f)
289 pickler.dump(self.log)
290
291 f.close()
292
293 pygame.midi.quit()
294
295 self.repaint()
296
297 if __name__ == "__main__" :
298 pygame.init()
299 modeResolution = (1024,768)
300 window = pygame.display.set_mode(modeResolution,pygame.FULLSCREEN)
301 pgu = PGUConfiguration(window)
302 pygame.quit()
303
304
305
306