Gestion explicite de KeyboardInterrupt pour être sûr de quitter le jeu sur un ^C.
[minwii.git] / src / minwii / widgets / playingscreen.py
index 9296646..90a6d2a 100755 (executable)
@@ -9,6 +9,8 @@ $URL$
 import pygame
 import types
 
+import kinect.pygamedisplay as kinect
+
 import minwii.events as events
 from minwii.log import eventLogger
 from minwii.eventutils import event_handler, EventDispatcher, EventHandlerMixin
@@ -18,6 +20,7 @@ from minwii.config import FIRST_HUE
 from minwii.config import MIDI_VELOCITY_RANGE
 from minwii.config import MIDI_PAN_RANGE
 from minwii.config import MIDI_VELOCITY_WRONG_NOTE_ATTN
+from minwii.config import SCREEN_RESOLUTION
 from minwii.globals import BACKGROUND_LAYER
 from minwii.globals import CURSOR_LAYER
 from minwii.globals import PLAYING_MODES_DICT
@@ -27,7 +30,7 @@ from column import Column
 
 class PlayingScreenBase(pygame.sprite.LayeredDirty, EventHandlerMixin) :
 
-    def __init__(self, synth, distinctNotes=[]) :
+    def __init__(self, synth, distinctNotes=[], displayNotes=True) :
         """
         distinctNotes : notes disctinctes présentes dans la chanson
         triées du plus grave au plus aigu.
@@ -35,6 +38,7 @@ class PlayingScreenBase(pygame.sprite.LayeredDirty, EventHandlerMixin) :
         super(PlayingScreenBase, self).__init__()
         self.synth = synth
         self.distinctNotes = distinctNotes
+        self.displayNotes = displayNotes
         self.keyboardLength = 0
         self.keyboardRects = []
         self.cursor = None
@@ -42,8 +46,10 @@ class PlayingScreenBase(pygame.sprite.LayeredDirty, EventHandlerMixin) :
         self.columns = {}
         self._initColumns()
         self._running = False
-        self.draw(pygame.display.get_surface())
-        self._initCursor()    
+        self.kinectRgb = kinect.RGBSprite(alpha=128, size=SCREEN_RESOLUTION)
+        self.add(self.kinectRgb, layer=CURSOR_LAYER)
+        self._initCursor()
+        
     
     def _initRects(self) :
         """ création des espaces réservés pour
@@ -64,7 +70,7 @@ class PlayingScreenBase(pygame.sprite.LayeredDirty, EventHandlerMixin) :
             rect = pygame.Rect(upperLeftCorner, (columnWidth, dispHeight))
             rects.append(rect)
         
-        # la dernière colonne a la largeur du reste
+        # la dernière colonne à la largeur du reste
         upperLeftCorner = ((i+1) * columnWidth, 0)
         rect = pygame.Rect(upperLeftCorner, (dispWidth - (self.keyboardLength - 1) * columnWidth , dispHeight))
         rects.append(rect)
@@ -77,7 +83,7 @@ class PlayingScreenBase(pygame.sprite.LayeredDirty, EventHandlerMixin) :
         for i, rect in enumerate(self.keyboardRects) :
             hue = FIRST_HUE - hueStep * i
             tone = self.distinctNotes[i]
-            c = Column(self, i, hue, rect, tone)
+            c = Column(self, i, hue, rect, tone, displayNote=self.displayNotes)
             self.add(c, layer=BACKGROUND_LAYER)
             self.columns[tone.midi] = c
         
@@ -92,10 +98,15 @@ class PlayingScreenBase(pygame.sprite.LayeredDirty, EventHandlerMixin) :
         pygame.display.flip()
         pygame.mouse.set_visible(False)
         while self._running :
-            EventDispatcher.dispatchEvents()
-            dirty = self.draw(pygame.display.get_surface())
-            pygame.display.update(dirty)
-            clock.tick(FRAMERATE)
+            try :
+                EventDispatcher.dispatchEvents()
+                self.kinectRgb.update()
+                dirty = self.draw(pygame.display.get_surface())
+                pygame.display.update(dirty)
+                clock.tick(FRAMERATE)
+            except KeyboardInterrupt :
+                self.stop()
+                raise
     
     def stop(self) :
         self._running = False
@@ -105,9 +116,8 @@ class PlayingScreenBase(pygame.sprite.LayeredDirty, EventHandlerMixin) :
 
     @event_handler(pygame.KEYDOWN)       
     def handleKeyDown(self, event) :
-        if event.key == pygame.K_q or \
-            event.unicode == u'q' or \
-            pygame.K_ESCAPE:
+        if event.key in (pygame.K_q, pygame.K_ESCAPE) or \
+            event.unicode == u'q' :
             self.stop()
     
     @event_handler(pygame.MOUSEBUTTONDOWN)
@@ -166,14 +176,14 @@ class PlayingScreen(PlayingScreenBase) :
     
     scale = [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
 
-    def __init__(self, synth) :
+    def __init__(self, synth, displayNotes=True) :
         distinctNotes = []
         self.currentColumn = None
         for midi in self.scale :
             tone = Tone(midi)
             distinctNotes.append(tone)
         
-        super(PlayingScreen, self).__init__(synth, distinctNotes)
+        super(PlayingScreen, self).__init__(synth, distinctNotes, displayNotes=displayNotes)
             
     @event_handler(events.COLDOWN)
     def noteon(self, event) :
@@ -191,10 +201,11 @@ class PlayingScreen(PlayingScreenBase) :
 
 class SongPlayingScreen(PlayingScreenBase) :
     
-    def __init__(self, synth, song, mode=PLAYING_MODES_DICT['NORMAL']) :
-        super(SongPlayingScreen, self).__init__(synth, song.distinctNotes)
+    def __init__(self, synth, song, mode=PLAYING_MODES_DICT['NORMAL'], displayNotes=True, tempoTrim=0) :
+        super(SongPlayingScreen, self).__init__(synth, song.distinctNotes, displayNotes=displayNotes)
         self.song = song
         self.quarterNoteDuration = song.quarterNoteDuration
+        self.tempoTrim = tempoTrim
         self.currentColumn = None
         self.noteIterator = self.song.iterNotes()
         self.displayNext()
@@ -229,10 +240,7 @@ class SongPlayingScreen(PlayingScreenBase) :
         col = event.column
         if col.state and not self.currentNotePlayed :
             self.playnote(col, event.pos)
-            SongPlayingScreen.setNoteTimeout(
-                        int(self.currentNote.duration * \
-                            self.quarterNoteDuration)
-                                            )
+            self.setNoteTimeout()
             self.currentNotePlayed = True
 
     def handleEasyColumnOver(self, event) :
@@ -241,10 +249,7 @@ class SongPlayingScreen(PlayingScreenBase) :
            self.cursor.pressed and \
            not self.currentNotePlayed :
             self.playnote(col, event.pos)
-            SongPlayingScreen.setNoteTimeout(
-                        int(self.currentNote.duration * \
-                            self.quarterNoteDuration)
-                                            )
+            self.setNoteTimeout()
             self.currentNotePlayed = True
 
     
@@ -267,10 +272,7 @@ class SongPlayingScreen(PlayingScreenBase) :
         if col.state and \
            not self.currentNotePlayed :
             self.playnote(col, event.pos)
-            SongPlayingScreen.setNoteTimeout(
-                        int(self.currentNote.duration * \
-                            self.quarterNoteDuration)
-                                            )
+            self.setNoteTimeout()
             self.currentNotePlayed = True
 
 
@@ -327,10 +329,20 @@ class SongPlayingScreen(PlayingScreenBase) :
         self.synth.noteoff(0, self.currentNote.midi)
         self.displayNext()
     
-    @staticmethod
-    def setNoteTimeout(delay) :
+    def setNoteTimeout(self) :
+        delay = self.currentNote.duration * self.quarterNoteDuration
+        delay = delay + delay * self.tempoTrim
+        delay = int(delay)
+        if delay < 1 :
+            delay = 1 # durée minimale, car 0 désactiverait le timer.
         pygame.time.set_timer(events.NOTEEND, delay)
     
+    def tempoTrimUp(self, step=0.1) :
+        self.tempoTrim = round(self.tempoTrim - step, 1)
+    
+    def tempoTrimDown(self, step=0.1) :
+        self.tempoTrim = round(self.tempoTrim + step, 1)
+    
     def stop(self) :
         pygame.time.set_timer(events.NOTEEND, 0)
         super(SongPlayingScreen, self).stop()