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