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