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