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