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