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