4f29319f126556a0f09b99f6b4651bd799194562
[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
22
23 class MinWii(object):
24
25 def __init__(self, wimoteSupport=True) :
26 LaunchScreen()
27 app = Desktop()
28 synth = Synth()
29
30 modeResolution = (1024,768)
31
32 if wimoteSupport :
33 from pywiiuse import pygame_wiimouse
34 from pywiiuse.PyWiiUse import IR_BELOW
35 pygame_wiimouse.init(4, 5) # look for 4, wait 5 seconds
36 nwiimotes = pygame_wiimouse.get_count()
37 print '%d wiimotes' % nwiimotes
38 WT = pygame_wiimouse.WT
39 WT.pause()
40 wm = pygame_wiimouse.Wiimote(0) # access the wiimote object
41 wm.enable_accels(0) # turn on acceleration reporting
42 wm.enable_ir(1, vres = modeResolution, position=IR_BELOW)
43 else :
44 nwiimotes = 0
45 class _WTFacade :
46 def pause(self):
47 pass
48 def resume(self):
49 pass
50 WT = _WTFacade()
51
52 pygame.display.set_mode(modeResolution)
53 pygame.display.set_caption('MinWii')
54
55 while True :
56 # sélection de la chanson
57 home = Home(songPath=SONG_FILE_PATH,
58 nwiimotes=nwiimotes)
59 home.connect(QUIT, app.quit)
60 app.run(home)
61 app.close(home)
62 returnValue = home.returnValue
63 if not returnValue :
64 break
65
66 # sélection de l'instrument
67 WT.resume()
68 selector = InstrumentSelector()
69 selector.run()
70 selector.stop()
71 pygame.event.clear()
72 EventDispatcher.reset()
73 instrumentDescription = selector.selectedInstrument
74
75 # lancement du lecteur / clavier
76 songFile = home.songFile
77 playMode = home.modeSelect.value
78 playMode = PLAYING_MODES_DICT[playMode]
79 song = musicXml2Song(songFile)
80 bank, preset = instrumentDescription['bank'], instrumentDescription['preset']
81 octave = instrumentDescription.get('octave', 0)
82 synth.adjust_octave(0, octave)
83 synth.program_select(0, bank, preset)
84 playingScreen = SongPlayingScreen(synth, song, mode=playMode)
85 playingScreen.run()
86 pygame.event.clear()
87 EventDispatcher.reset()
88 WT.pause()