2 Created on 12 nov. 2009
4 @author: Samuel Benveniste
15 from pygame
.locals import *
17 from pgu
import gui
as pguGui
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
31 class PGUConfiguration(pguGui
.Desktop
):
37 def __init__(self
,window
):
41 pguGui
.Desktop
.__init
__(self
)
42 self
.extendedScale
= False
45 self
.scale
= scaleDict
["majorScale"]
50 self
.activeWiimotes
= [False for i
in range(4)]
51 self
.alwaysDown
= False
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)
60 self
.browseButton
= pguGui
.Button(self
.createLabel("Choisir..."))
61 self
.browseButton
.connect(pguGui
.CLICK
, self
.open_file_browser
, None)
63 self
.songSwitch
= pguGui
.Switch(False)
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)
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
)
74 self
.goButton
= pguGui
.Button(self
.createLabel("Go"))
75 self
.goButton
.connect(pguGui
.CLICK
,self
.goButtonClicked
,None)
77 self
.quitButton
= pguGui
.Button(self
.createLabel("Fin"))
78 self
.quitButton
.connect(pguGui
.CLICK
,self
.quitButtonClicked
,None)
80 self
.connect(pguGui
.QUIT
,self
.quit
,None)
84 ##The table code is entered much like HTML.
87 self
.mainTable
= pguGui
.Table()
92 # self.mainTable.td(self.createLabel("MINWii",self.titleFont),colspan = 4)
94 self
.run(self
.mainTable
)
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
)
102 def handle_file_browser_closed(self
,dlg
):
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
])
112 label
= self
.createLabel(key
)
113 elif self
.file.endswith('.xml') :
114 self
.song
= musicXml2Song(self
.file, printNotes
=True)
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()
125 def fillMainTable(self
):
128 self
.mainTable
.td(pguGui
.Spacer(*self
.spaceSize
))
131 self
.mainTable
.td(self
.createLabel("Chanson :"))
132 self
.mainTable
.td(self
.browseButton
,colspan
=2)
133 self
.mainTable
.td(self
.songSwitch
)
136 self
.mainTable
.td(pguGui
.Spacer(*self
.spaceSize
))
139 self
.mainTable
.td(self
.createLabel("Niveau :"))
140 self
.mainTable
.td(self
.modeSelect
,colspan
=3)
143 self
.mainTable
.td(pguGui
.Spacer(*self
.spaceSize
))
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)
154 self
.mainTable
.td(pguGui
.Spacer(*self
.spaceSize
))
157 self
.mainTable
.td(self
.goButton
)
158 self
.mainTable
.td(self
.quitButton
,colspan
=3)
161 self
.mainTable
.td(pguGui
.Spacer(500,500))
163 def createLabel(self
,text
,font
= None):
166 w
,h
= self
.font
.size(text
)
167 label
= pguGui
.Label(text
,width
=w
,height
=h
,font
= font
)
170 def songSelectChanged(self
,data
=None):
171 self
.song
= songDict
[self
.songSelect
.value
]
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
181 def modeSelectChanged(self
,data
= None):
182 self
.mode
= modeDict
[self
.modeSelect
.value
]
184 def hasActiveWiimote(self
):
186 for i
in self
.activeWiimotes
:
191 def quitButtonClicked(self
,data
= None):
194 for isActive
in self
.activeWiimotes
:
198 def goButtonClicked(self
,data
= None):
201 if not self
.hasActiveWiimote():
202 self
.activeWiimotes
[0] = True
204 instruments
= [Instrument(self
.scale
, i
+ 1, "".join(["../instruments/instrumentImages/", instrumentImagePathList
[i
], ".jpg"]), octaves
[i
]) for i
in range(9)]
206 joys
= [[id,pygame
.joystick
.Joystick(id).get_name()] for id in range(pygame
.joystick
.get_count())]
209 if joy
[1] in joyNames
:
211 pygame
.joystick
.Joystick(joy
[0]).init()
215 ports
= [pygame
.midi
.get_device_info(id)[1] for id in range(pygame
.midi
.get_count())]
216 portOffset
= ports
.index(portNames
[0])
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]))]
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
)]
228 if self
.song
!= None and self
.songSwitch
.value
:
231 self
.extendedScale
= self
.song
.requiresExtendedScale
234 self
.alwaysDown
= True
235 elif self
.mode
== 1 :
236 self
.extendedScale
= self
.song
.requiresExtendedScale
240 self
.extendedScale
= self
.song
.requiresExtendedScale
244 self
.extendedScale
= True
246 self
.easyMode
= False
248 choice
= InstrumentChoice(instruments
, wiimotes
, self
.window
, screen
, clock
, joys
, portOffset
,self
.activeWiimotes
, scaleFactor
= songScaleFactor
)
249 play
= SongPlayingScreen(choice
, self
.song
,self
.cascade
, self
.extendedScale
,self
.easyMode
,self
.alwaysDown
)
254 self
.extendedScale
= False
256 elif self
.mode
== 1 :
257 self
.extendedScale
= True
260 self
.extendedScale
= False
263 self
.extendedScale
= True
266 choice
= InstrumentChoice(instruments
, wiimotes
, self
.window
, screen
, clock
, joys
, portOffset
,self
.activeWiimotes
)
267 play
= PlayingScreen(choice
, None,self
.cascade
, self
.extendedScale
)
269 while play
.backToInstrumentChoice
== True :
271 for wiimote
in wiimotes
:
274 wiimotes
= [Wiimote(i
, i
+ portOffset
, None, None, cursors
[i
]) for i
in range(wiimoteCount
)]
275 previousEventLog
= choice
.eventLog
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
)
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
)
284 for wiimote
in wiimotes
:
288 filePath
= "".join([self
.fileName
,str(i
),".mwi"])
289 while os
.path
.exists(filePath
):
291 filePath
= "".join([self
.fileName
,str(i
),".mwi"])
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
)
304 if __name__
== "__main__" :
306 modeResolution
= (1024,768)
307 window
= pygame
.display
.set_mode(modeResolution
,pygame
.FULLSCREEN
)
308 pgu
= PGUConfiguration(window
)