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 constants
import *
28 from MINWiiDialog
import MINWiiDialog
30 class PGUConfiguration(pguGui
.Desktop
):
36 def __init__(self
,window
):
40 pguGui
.Desktop
.__init
__(self
)
41 self
.extendedScale
= False
44 self
.scale
= scaleDict
["majorScale"]
49 self
.activeWiimotes
= [False for i
in range(4)]
50 self
.alwaysDown
= False
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)
59 self
.browseButton
= pguGui
.Button(self
.createLabel("Choisir..."))
60 self
.browseButton
.connect(pguGui
.CLICK
, self
.open_file_browser
, None)
62 self
.songSwitch
= pguGui
.Switch(False)
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)
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
)
73 self
.goButton
= pguGui
.Button(self
.createLabel("Go"))
74 self
.goButton
.connect(pguGui
.CLICK
,self
.goButtonClicked
,None)
76 self
.quitButton
= pguGui
.Button(self
.createLabel("Fin"))
77 self
.quitButton
.connect(pguGui
.CLICK
,self
.quitButtonClicked
,None)
79 self
.connect(pguGui
.QUIT
,self
.quit
,None)
83 ##The table code is entered much like HTML.
86 self
.mainTable
= pguGui
.Table()
91 # self.mainTable.td(self.createLabel("MINWii",self.titleFont),colspan = 4)
93 self
.run(self
.mainTable
)
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
)
101 def handle_file_browser_closed(self
,dlg
):
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
])
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()
118 def fillMainTable(self
):
121 self
.mainTable
.td(pguGui
.Spacer(*self
.spaceSize
))
124 self
.mainTable
.td(self
.createLabel("Chanson :"))
125 self
.mainTable
.td(self
.browseButton
,colspan
=2)
126 self
.mainTable
.td(self
.songSwitch
)
129 self
.mainTable
.td(pguGui
.Spacer(*self
.spaceSize
))
132 self
.mainTable
.td(self
.createLabel("Niveau :"))
133 self
.mainTable
.td(self
.modeSelect
,colspan
=3)
136 self
.mainTable
.td(pguGui
.Spacer(*self
.spaceSize
))
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)
147 self
.mainTable
.td(pguGui
.Spacer(*self
.spaceSize
))
150 self
.mainTable
.td(self
.goButton
)
151 self
.mainTable
.td(self
.quitButton
,colspan
=3)
154 self
.mainTable
.td(pguGui
.Spacer(500,500))
156 def createLabel(self
,text
,font
= None):
159 w
,h
= self
.font
.size(text
)
160 label
= pguGui
.Label(text
,width
=w
,height
=h
,font
= font
)
163 def songSelectChanged(self
,data
=None):
164 self
.song
= songDict
[self
.songSelect
.value
]
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
174 def modeSelectChanged(self
,data
= None):
175 self
.mode
= modeDict
[self
.modeSelect
.value
]
177 def hasActiveWiimote(self
):
179 for i
in self
.activeWiimotes
:
184 def quitButtonClicked(self
,data
= None):
187 for isActive
in self
.activeWiimotes
:
191 def goButtonClicked(self
,data
= None):
194 if not self
.hasActiveWiimote():
195 self
.activeWiimotes
[0] = True
197 instruments
= [Instrument(self
.scale
, i
+ 1, "".join(["../instruments/instrumentImages/", instrumentImagePathList
[i
], ".jpg"]), octaves
[i
]) for i
in range(9)]
199 joys
= [[id,pygame
.joystick
.Joystick(id).get_name()] for id in range(pygame
.joystick
.get_count())]
202 if joy
[1] in joyNames
:
204 pygame
.joystick
.Joystick(joy
[0]).init()
208 ports
= [pygame
.midi
.get_device_info(id)[1] for id in range(pygame
.midi
.get_count())]
209 portOffset
= ports
.index(portNames
[0])
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]))]
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
)]
221 if self
.song
!= None and self
.songSwitch
.value
:
224 self
.extendedScale
= self
.song
.requiresExtendedScale
227 self
.alwaysDown
= True
228 elif self
.mode
== 1 :
229 self
.extendedScale
= self
.song
.requiresExtendedScale
233 self
.extendedScale
= self
.song
.requiresExtendedScale
237 self
.extendedScale
= True
239 self
.easyMode
= False
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
)
247 self
.extendedScale
= False
249 elif self
.mode
== 1 :
250 self
.extendedScale
= True
253 self
.extendedScale
= False
256 self
.extendedScale
= True
259 choice
= InstrumentChoice(instruments
, wiimotes
, self
.window
, screen
, clock
, joys
, portOffset
,self
.activeWiimotes
)
260 play
= PlayingScreen(choice
, None,self
.cascade
, self
.extendedScale
)
262 while play
.backToInstrumentChoice
== True :
264 for wiimote
in wiimotes
:
267 wiimotes
= [Wiimote(i
, i
+ portOffset
, None, None, cursors
[i
]) for i
in range(wiimoteCount
)]
268 previousEventLog
= choice
.eventLog
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
)
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
)
277 for wiimote
in wiimotes
:
281 filePath
= "".join([self
.fileName
,str(i
),".mwi"])
282 while os
.path
.exists(filePath
):
284 filePath
= "".join([self
.fileName
,str(i
),".mwi"])
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
)
297 if __name__
== "__main__" :
299 modeResolution
= (1024,768)
300 window
= pygame
.display
.set_mode(modeResolution
,pygame
.FULLSCREEN
)
301 pgu
= PGUConfiguration(window
)