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