doc++
[minwii.git] / src / app / minwii.py
1 # -*- coding: utf-8 -*-
2 """
3 l'application winwii
4
5 $Id$
6 $URL$
7 """
8
9 import pygame
10 from pgu.gui import Desktop
11 from pgu.gui import QUIT
12 from widgets.launch import LaunchScreen
13 from widgets.home import Home
14 from widgets.playingscreen import SongPlayingScreen, PlayingScreen
15 from widgets.instrumentselector import InstrumentSelector
16 from synth import Synth
17 from eventutils import EventDispatcher
18 from musicxml import musicXml2Song
19 from config import SONG_FILE_PATH
20 from globals import PLAYING_MODES_DICT
21 from log import console
22
23
24 class MinWii(object):
25
26 def __init__(self, wiimoteSupport=True) :
27 self.wiimoteSupport = wiimoteSupport
28 LaunchScreen()
29 self.app = Desktop()
30 self.synth = Synth()
31 self.screenResolution = (1024,768)
32 self.nwiimotes = 0
33 self.initWiimotes()
34
35 def initWiimotes(self) :
36 if self.wiimoteSupport :
37 from pywiiuse import pygame_wiimouse
38 pygame_wiimouse.init(4, 5, self.screenResolution) # look for 4, wait 5 seconds
39 self.nwiimotes = nwiimotes = pygame_wiimouse.get_count()
40 console.debug('%d wiimotes found', nwiimotes)
41 self.WT = WT = pygame_wiimouse.WT
42 WT.pause()
43 else :
44 self.WT = _WTFacade()
45
46 def run(self) :
47 "manage the screen sequence display"
48
49 pygame.display.set_mode(self.screenResolution)
50 pygame.display.set_caption('MinWii')
51 WT = self.WT
52
53 while True :
54
55 exit, songFile, playMode, selectedWiimoteIndex = self.selectSongAndOptions()
56 if exit : break
57
58 WT.selectWiimote(selectedWiimoteIndex)
59 WT.resume()
60
61 instrumentDescription = self.selectInstrument()
62 self.runPlayingScreen(songFile, playMode, instrumentDescription)
63
64 WT.pause()
65
66
67 def selectSongAndOptions(self) :
68 """ lance l'écran de paramétrage et retourne un tuple comportant :
69 - drapeau de sortie de l'application (booléen)
70 - chemin du fichier de la chanson
71 - mode (entier)
72 - wiimote sélectionnée (entier)
73 """
74 home = Home(songPath=SONG_FILE_PATH,
75 nwiimotes=self.nwiimotes)
76 app = self.app
77 home.connect(QUIT, app.quit)
78 app.run(home)
79 app.close(home)
80
81
82 actual_wiimotes = self.WT.get_count()
83 if actual_wiimotes is None :
84 msg = 'chanson : %s\nmode : %s\nHID : souris'
85 msg = msg % (home.songFile,
86 home.modeSelect.value)
87 elif actual_wiimotes == 0 :
88 msg = 'chanson : %s\nmode : %s\nHID : souris (pas de wiimote trouvée)'
89 msg = msg % (home.songFile,
90 home.modeSelect.value)
91 else :
92 msg = 'chanson : %s\nmode : %s\nHID : wiimote %d'
93 msg = msg % (home.songFile,
94 home.modeSelect.value,
95 home.selectedWiimote.value + 1)
96
97 console.info(msg)
98
99 return (home.exitApp,
100 home.songFile,
101 home.modeSelect.value,
102 home.selectedWiimote.value)
103
104 def selectInstrument(self) :
105 """ lance l'écran de sélection de l'instrument et retourne
106 un dictionnaire comportant la description de l'instrument
107 """
108 selector = InstrumentSelector()
109 selector.run()
110 selector.stop()
111 pygame.event.clear()
112 EventDispatcher.reset()
113 return selector.selectedInstrument
114
115 def runPlayingScreen(self, songFile, playMode, instrumentDescription) :
116 """ Lance l'écran de jeu principal avec la chanson 'songFile' dans le mode 'playMode'
117 avec l'instrument midi 'instrumentDescription'.
118 """
119 playMode = PLAYING_MODES_DICT[playMode]
120 song = musicXml2Song(songFile)
121 bank, preset = instrumentDescription['bank'], instrumentDescription['preset']
122 octave = instrumentDescription.get('octave', 0)
123 self.synth.adjust_octave(0, octave)
124 self.synth.program_select(0, bank, preset)
125 playingScreen = SongPlayingScreen(self.synth, song, mode=playMode)
126 playingScreen.run()
127 pygame.event.clear()
128 EventDispatcher.reset()
129
130
131 class _WTFacade :
132 """ Classe utilitaire pour singer l'api
133 de pygame_wiimouse en cas d'abscence de wiimote.
134 """
135 selectWimoteIndex = 0
136 def pause(self):
137 pass
138 def resume(self):
139 pass
140 def selectWiimote(self, i):
141 pass
142 def get_count(self) :
143 return None