a7b21f72c194a3163506aec21394617b83a1278e
[minwii.git] / src / gui / FamiliarizerPGUConfiguration.py
1 '''
2 Created on 12 nov. 2009
3
4 @author: Samuel Benveniste
5 '''
6 from logging.FamiliarizerLog import FamiliarizerLog
7
8 import os
9 import sys
10 import subprocess
11
12 import pygame
13 import pygame.midi
14 import pickle
15
16 from pygame.locals import *
17
18 from pgu import gui
19
20 from StaticFamiliarizer import StaticFamiliarizer
21 from SongFamiliarizer import SongFamiliarizer
22 from instruments.Instrument import Instrument
23 from cursor.WarpingCursor import *
24 from controllers.Wiimote import Wiimote
25 from logging.FamiliarizerLog import FamiliarizerLog
26 from songs.Song import Song
27 from constants import *
28 from SongPlayingScreen import SongPlayingScreen
29 from DummyInstrumentChoice import DummyInstrumentChoice
30
31 class FamiliarizerPGUConfiguration(gui.Desktop):
32 '''
33 classdocs
34 '''
35
36
37 def __init__(self,window,defaultParams = False):
38 '''
39 Constructor
40 '''
41 gui.Desktop.__init__(self)
42 self.done = False
43 self.level = 0
44 self.scale = scaleDict["majorScale"]
45 self.fileName = fileName
46 self.log = None
47 self.activeWiimotes = [False for i in range(4)]
48
49 pygame.font.init()
50
51 self.titleFont = pygame.font.Font(None,100)
52 self.font = pygame.font.Font(None,70)
53 self.spaceSize = (100,100)
54
55 self.goButton = gui.Button(self.createLabel("Go"))
56 self.goButton.connect(gui.CLICK,self.goButtonClicked,None)
57
58 self.quitButton = gui.Button(self.createLabel("Fin"))
59 self.quitButton.connect(gui.CLICK,self.quitButtonClicked,None)
60
61 self.levelSelect = gui.Select()
62 for i in range(3) :
63 self.levelSelect.add(self.createLabel(str(i+1)),i)
64 self.levelSelect.connect(gui.CHANGE,self.levelSelectChanged,None)
65
66 self.activeWiimoteSwitches = [gui.Switch(False) for i in range(4)]
67 for i in range(len(self.activeWiimoteSwitches)) :
68 self.activeWiimoteSwitches[i].connect(gui.CHANGE,self.activeWiimoteSwitchesChanged,i)
69
70 self.connect(gui.QUIT,self.quit,None)
71
72 self.window = window
73
74 ##The table code is entered much like HTML.
75 ##::
76 mainContainer = gui.Table()
77
78 c = gui.Table()
79
80 # c.tr()
81 # c.td(self.createLabel("MINWii",self.titleFont),colspan = 4)
82
83 c.tr()
84 c.td(gui.Spacer(*self.spaceSize))
85
86 c.tr()
87 c.td(self.createLabel("Niveau :"))
88 c.td(self.levelSelect,colspan=3)
89
90 c.tr()
91 c.td(gui.Spacer(*self.spaceSize))
92
93 c.tr()
94 c.td(self.createLabel("Joueurs :", self.font))
95 playerTable = gui.Table()
96 for i in range(len(self.activeWiimoteSwitches)):
97 playerTable.td(self.createLabel(" " + str(i+1)+" ", self.font))
98 playerTable.td(self.activeWiimoteSwitches[i])
99 c.td(playerTable,colspan = 3)
100
101 c.tr()
102 c.td(gui.Spacer(*self.spaceSize))
103
104 c.tr()
105 c.td(self.goButton)
106 c.td(self.quitButton,colspan=3)
107
108 c.tr()
109 c.td(gui.Spacer(500,500))
110
111 mainContainer.add(c,0,0)
112
113 if defaultParams :
114 self.goButtonClicked()
115 else :
116 self.run(mainContainer)
117
118 def open_file_browser(self,data=None):
119 d = gui.FileDialog()
120 d.connect(gui.CHANGE, self.handle_file_browser_closed, d)
121 d.open()
122
123
124 def handle_file_browser_closed(self,dlg):
125 if dlg.value:
126 self.file = dlg.value
127
128 def createLabel(self,text,font = None):
129 if font == None :
130 font = self.font
131 w,h = self.font.size(text)
132 label = gui.Label(text,width=w,height=h,font = font)
133 return(label)
134
135 def levelSelectChanged(self,data=None):
136 self.level = self.levelSelect.value
137
138 def quitButtonClicked(self,data = None):
139 self.done = True
140 self.quit()
141
142 def activeWiimoteSwitchesChanged(self,data = None):
143 if self.activeWiimoteSwitches[data].value :
144 for i in range(len(self.activeWiimoteSwitches)) :
145 if self.activeWiimoteSwitches[i].value and data != i :
146 self.activeWiimoteSwitches[i].click()
147 for i in range(len(self.activeWiimoteSwitches)) :
148 self.activeWiimotes[i] = self.activeWiimoteSwitches[i].value
149
150 def hasActiveWiimote(self):
151 hasActive = False
152 for active in self.activeWiimotes :
153 if active :
154 hasActive = True
155 break
156 return(hasActive)
157
158
159
160
161 def goButtonClicked(self,data = None):
162
163 pygame.midi.init()
164 instruments = [Instrument(self.scale, i + 1, "".join(["../instruments/instrumentImages/", instrumentImagePathList[i], ".jpg"]), octaves[i]) for i in range(9)]
165
166 joys = [[id,pygame.joystick.Joystick(id).get_name()] for id in range(pygame.joystick.get_count())]
167 for joy in joys:
168 if joy[1] in joyNames:
169 pygame.joystick.Joystick(joy[0]).init()
170
171 ports = [pygame.midi.get_device_info(id)[1] for id in range(pygame.midi.get_count())]
172 portOffset = ports.index(portNames[0])
173 print(portOffset)
174
175 screen = pygame.display.get_surface()
176 clock = pygame.time.Clock()
177 cursorImages=[['../cursor/cursorImages/black/10.png'],['../cursor/cursorImages/red/10.png'],['../cursor/cursorImages/blue/10.png'],['../cursor/cursorImages/green/10.png']]
178 durations = [75 for i in range(len(cursorImages))]
179
180 extsc = True
181 casc = False
182 easyMode = True
183
184 song = Song(scaleDict["majorScale"],[3,9,6,4,1,8,5,7,2,10],True)
185
186 wiimoteCount = 4
187
188 cursors = [WarpingCursor(None, cursorImages[i], durations, (300 * i, 300 * i),'../cursor/cursorImages/black/flash.png') for i in range(wiimoteCount)]
189 wiimotes = [Wiimote(i, i + portOffset, None, instruments[0], cursors[i]) for i in range(wiimoteCount)]
190 dummyInstrumentChoice = DummyInstrumentChoice(wiimotes, self.window, screen, clock, joys, portOffset, self.activeWiimotes)
191 if not self.hasActiveWiimote():
192 self.activeWiimotes[0] = True
193 if self.level < 2 :
194 familiarize = StaticFamiliarizer(wiimotes, self.window, screen, clock, joys, portOffset,self.activeWiimotes,level = self.level)
195 elif familiarize.nextLevel == 2 :
196 familiarize = SongFamiliarizer(wiimotes, self.window, screen, clock, joys, portOffset,song,self.activeWiimotes,casc,extsc,easyMode)
197 else :
198 familiarize = SongPlayingScreen(dummyInstrumentChoice,songDict["clairdelalune"],easyMode = True)
199
200 while familiarize.nextLevel != None :
201 if familiarize.nextLevel < 2 :
202 familiarize = StaticFamiliarizer(wiimotes, self.window, screen, clock, joys, portOffset,self.activeWiimotes,level = familiarize.nextLevel,eventLog = familiarize.eventLog)
203 elif familiarize.nextLevel == 2 :
204 familiarize = SongFamiliarizer(wiimotes, self.window, screen, clock, joys, portOffset,song,self.activeWiimotes,casc,extsc,easyMode,eventLog = familiarize.eventLog)
205 else :
206 familiarize = SongPlayingScreen(dummyInstrumentChoice,songDict["clairdelalune"],easyMode = True,eventLog = familiarize.eventLog)
207
208 for wiimote in wiimotes:
209 del wiimote.port
210
211 pygame.midi.quit()
212
213 i = 1
214 filePath = "".join([self.fileName,str(i),".fmwi"])
215 while os.path.exists(filePath):
216 i += 1
217 filePath = "".join([self.fileName,str(i),".fmwi"])
218
219 f = file(filePath, 'w')
220 self.log = FamiliarizerLog(familiarize.eventLog,self.level,self.activeWiimotes)
221 pickler = pickle.Pickler(f)
222 pickler.dump(self.log)
223
224 f.close()
225
226 self.repaint()
227
228 if __name__ == "__main__" :
229 pygame.init()
230 modeResolution = (1024,768)
231 window = pygame.display.set_mode(modeResolution,pygame.FULLSCREEN)
232 familiarizer = FamiliarizerPGUConfiguration(window)
233 pygame.quit()
234
235
236
237