Bugfix : doubleclick uniquement sur la liste.
[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
77 instrumentDescription = self.selectInstrument()
78 if not instrumentDescription :
79 continue
80
81 WT.resume()
82 self.runPlayingScreen(songFile, playMode, instrumentDescription)
83 WT.pause()
84
85
86 def selectSongAndOptions(self, songFile, playMode, wiimoteIndex) :
87 """ lance l'écran de paramétrage et retourne un tuple comportant :
88 - drapeau de sortie de l'application (booléen)
89 - chemin du fichier de la chanson
90 - mode (entier)
91 - wiimote sélectionnée (entier)
92 """
93 home = Home(songPath=SONG_FILE_PATH,
94 songFile=songFile,
95 playMode=playMode,
96 wiimoteIndex=wiimoteIndex,
97 nwiimotes=self.nwiimotes)
98 app = self.app
99 home.connect(QUIT, app.quit)
100 app.run(home)
101 app.close(home)
102
103 #logging
104 if home.exitApp :
105 console.debug("sortie de l'application")
106 else :
107 actual_wiimotes = self.WT.get_count()
108 if self.firstSong :
109 self.firstSong = False
110 else :
111 envLogger.info('NEW_LOG_FILE')
112 console.info('chanson : %s', home.songFile)
113 console.info('mode : %s', home.modeSelect.value)
114 if actual_wiimotes is None :
115 console.info('HID : souris')
116 elif actual_wiimotes == 0 :
117 console.info('HID : souris (pas de wiimote trouvée)')
118 else :
119 console.info('HID : wiimote %d', home.selectedWiimote.value + 1)
120 #---
121
122 return (home.exitApp,
123 home.songFile,
124 home.selectedPlayMode,
125 home.selectedWiimoteIndex)
126
127 def selectInstrument(self) :
128 """ lance l'écran de sélection de l'instrument et retourne
129 un dictionnaire comportant la description de l'instrument
130 """
131 selector = InstrumentSelector()
132 selector.run()
133 selector.stop()
134 pygame.event.clear()
135 EventDispatcher.reset()
136 instru = selector.selectedInstrument
137 if instru :
138 console.info('instrument : %s', instru['name'])
139 console.info('preset : %d', instru['preset'])
140 console.info('bank : %d', instru['bank'])
141 console.info('ajustement octave : %d', instru['octave'])
142 return instru
143
144 def runPlayingScreen(self, songFile, playMode, instrumentDescription) :
145 """ Lance l'écran de jeu principal avec la chanson 'songFile' dans le mode 'playMode'
146 avec l'instrument midi 'instrumentDescription'.
147 """
148 playMode = PLAYING_MODES_DICT[playMode]
149 bank, preset = instrumentDescription['bank'], instrumentDescription['preset']
150 octave = instrumentDescription['octave']
151 self.synth.adjust_octave(0, octave)
152 self.synth.program_select(0, bank, preset)
153 if playMode == PLAYING_MODES_DICT['IMPRO'] :
154 playingScreen = PlayingScreen(self.synth)
155 else :
156 song = musicXml2Song(songFile)
157 playingScreen = SongPlayingScreen(self.synth, song, mode=playMode)
158 playingScreen.run()
159 pygame.event.clear()
160 EventDispatcher.reset()
161
162
163 class _WTFacade :
164 """ Classe utilitaire pour singer l'api
165 de pygame_wiimouse en cas d'abscence de wiimote.
166 """
167 selectWimoteIndex = 0
168 def pause(self):
169 pass
170 def resume(self):
171 pass
172 def selectWiimote(self, i):
173 pass
174 def get_count(self) :
175 return None