c5961af4ddb9ea7ca9b7c61e47e9654368670032
[minwii.git] / src / app / minwii.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 widgets.launch import LaunchScreen
13 from widgets.home import Home
14 from widgets.playingscreen import SongPlayingScreen, PlayingScreen
15 from widgets.instrumentselector import InstrumentSelector
16 from synth import Synth
17 from eventutils import EventDispatcher
18 from musicxml import musicXml2Song
19 from config import SONG_FILE_PATH
20 from globals import PLAYING_MODES_DICT
21 from log import console
22
23
24 class MinWii(object):
25
26 def __init__(self, wiimoteSupport=True) :
27 self.wiimoteSupport = wiimoteSupport
28 LaunchScreen()
29 self.app = Desktop()
30 self.synth = Synth()
31 self.screenResolution = (1024,768)
32 self.nwiimotes = 0
33 self.initWiimotes()
34
35 def initWiimotes(self) :
36 if self.wiimoteSupport :
37 from pywiiuse import pygame_wiimouse
38 pygame_wiimouse.init(4, 5, screenResolution) # look for 4, wait 5 seconds
39 self.nwiimotes = nwiimotes = pygame_wiimouse.get_count()
40 console.debug('%d wiimotes found', nwiimotes)
41 self.WT = WT = pygame_wiimouse.WT
42 WT.pause()
43 else :
44 self.WT = _WTFacade()
45
46 def run(self) :
47 "manage the screen sequence display"
48
49 pygame.display.set_mode(self.screenResolution)
50 pygame.display.set_caption('MinWii')
51 WT = self.WT
52
53 while True :
54
55 exit, songFile, playMode, selectedWiimoteIndex = self.selectSongAndOptions()
56 if exit : break
57
58 WT.selectWiimote(selectedWiimoteIndex)
59 console.info('wiimote sélectionnée : %d', selectedWiimoteIndex)
60 WT.resume()
61
62 instrumentDescription = self.selectInstrument()
63 self.runPlayingScreen(songFile, playMode, instrumentDescription)
64
65 WT.pause()
66
67
68 def selectSongAndOptions(self) :
69 """ lance l'écran de paramétrage et retourne un tuple comportant :
70 - drapeau de sortie de l'application (booléen)
71 - chemin du fichier de la chanson
72 - mode (entier)
73 - wiimote sélectionnée (entier)
74 """
75 home = Home(songPath=SONG_FILE_PATH,
76 nwiimotes=self.nwiimotes)
77 app = self.app
78 home.connect(QUIT, app.quit)
79 app.run(home)
80 app.close(home)
81 #console.debug('wiimote selected: %d', home.selecctedWiimoteIndex)
82 return (home.exitApp,
83 home.songFile,
84 home.modeSelect.value,
85 home.selectedWiimote.value)
86
87 def selectInstrument(self) :
88 """ lance l'écran de sélection de l'instrument et retourne
89 un dictionnaire comportant la description de l'instrument
90 """
91 selector = InstrumentSelector()
92 selector.run()
93 selector.stop()
94 pygame.event.clear()
95 EventDispatcher.reset()
96 return selector.selectedInstrument
97
98 def runPlayingScreen(self, songFile, playMode, instrumentDescription) :
99 """ Lance l'écran de jeu principal avec la chanson 'songFile' dans le mode 'playMode'
100 avec l'instrument midi 'instrumentDescription'.
101 """
102 playMode = PLAYING_MODES_DICT[playMode]
103 song = musicXml2Song(songFile)
104 bank, preset = instrumentDescription['bank'], instrumentDescription['preset']
105 octave = instrumentDescription.get('octave', 0)
106 self.synth.adjust_octave(0, octave)
107 self.synth.program_select(0, bank, preset)
108 playingScreen = SongPlayingScreen(self.synth, song, mode=playMode)
109 playingScreen.run()
110 pygame.event.clear()
111 EventDispatcher.reset()
112
113
114 class _WTFacade :
115 selectWimoteIndex = 0
116 def pause(self):
117 pass
118 def resume(self):
119 pass
120 def selectWiimote(self, i):
121 pass