remplacement de la flûte.
[minwii.git] / src / app / widgets / instrumentselector.py
index 70654f6..9bafbc3 100755 (executable)
@@ -27,6 +27,7 @@ class InstrumentSelector(pygame.sprite.LayeredDirty, EventHandlerMixin) :
         #self._initRects()
         self._initTiles()
         self._initCursor()
+        self._inflatedTile = None
     
     def _initTiles(self) :
         screen = pygame.display.get_surface()
@@ -69,8 +70,8 @@ class InstrumentSelector(pygame.sprite.LayeredDirty, EventHandlerMixin) :
         if event.key == pygame.K_q:
             self.stop()
 
-    #@event_handler(pygame.MOUSEMOTION)
-    @event_handler(pygame.MOUSEBUTTONDOWN)
+    @event_handler(pygame.MOUSEMOTION)
+    #@event_handler(pygame.MOUSEBUTTONDOWN)
     def onMouseMove(self, event) :
         for tile in reversed(self.sprites()[:-1]) :
             if tile.rect.collidepoint(*event.pos) :
@@ -78,8 +79,23 @@ class InstrumentSelector(pygame.sprite.LayeredDirty, EventHandlerMixin) :
                 break
     
     def raiseTileOver(self, tile) :
-        self.change_layer(tile, FOREGROUND_LAYER)
-        tile.inflate(tile.coords)
+        if not tile.inflated :
+            self.change_layer(tile, FOREGROUND_LAYER)
+            tile.inflate(tile.coords)
+            
+            if self._inflatedTile :
+                self._inflatedTile.deflate()
+                self.change_layer(self._inflatedTile, BACKGROUND_LAYER)
+            
+            self._inflatedTile = tile
+        
+    @event_handler(pygame.MOUSEBUTTONDOWN)
+    def selectInstrument(self, event) :
+        for tile in reversed(self.sprites()[:-1]) :
+            if tile.rect.collidepoint(*event.pos) :
+                self.selectedInstrument = tile
+                self.stop()
+                break
 
 
 
@@ -94,19 +110,32 @@ class InstrumentTile(pygame.sprite.DirtySprite) :
         return os.path.sep.join(imagePath)
     
     BORDER = 10
-    INFLATE_ZOOM = 0.25
+    INFLATE_ZOOM = 0.4
 
     def __init__(self, name, group, rect, coords) :
         pygame.sprite.DirtySprite.__init__(self, group)
+        self.inflated = False
         self.name = name
         self.rect = rect
+        self._baseRect = rect.copy()
         self.coords = coords
-        
-        
+        imagePath = InstrumentTile._get_instrument_image(name)
+        self._img = pygame.image.load(imagePath)
+        self.update()
+    
+
+    def update(self) :
         innerWidth, innerHeight = [l-self.BORDER*2 for l in self.rect.size]
+        innerSize = innerWidth, innerHeight
         
-        imagePath = InstrumentTile._get_instrument_image(name)
-        img = pygame.image.load(imagePath)
+        border = pygame.Surface(self.rect.size)
+        border.fill((0,0,0,255))
+        
+        bg = pygame.Surface(innerSize)
+        bg.fill((255,255,255,255))
+        bgRect = pygame.Rect((self.BORDER, self.BORDER), innerSize)
+
+        img = self._img
         iWidth, iHeight = img.get_size()
         imgRatio = float(iWidth) / iHeight
         
@@ -118,31 +147,36 @@ class InstrumentTile(pygame.sprite.DirtySprite) :
             ih = innerHeight
             iw = int(round(innerHeight * imgRatio))
         
-        position = ((innerWidth - iw) / 2 + self.BORDER, (innerHeight - ih) / 2 + self.BORDER)
-        
+        imgPosition = ((innerWidth - iw) / 2, (innerHeight - ih) / 2)
+        imgRect = pygame.Rect(imgPosition, (iw, ih))
         img = pygame.transform.smoothscale(img, (iw, ih))
         
-        bg = pygame.Surface(self.rect.size)
-        bg.fill((255,255,255,255))
-        bg.blit(img, pygame.Rect(position, (iw, ih)))
-        
-        self.image = bg
+        bg.blit(img, imgRect)
+        border.blit(bg, bgRect)
+        self.image = border
     
+
     def inflate(self, refPoint) :
+        self.inflated = True
         keep = {}
         for name in REF_POINTS[refPoint] :
             keep[name] = getattr(self.rect, name)
         
         self.rect.inflate_ip(*[l*self.INFLATE_ZOOM for l in self.rect.size])
         
-        img = pygame.transform.smoothscale(self.image, self.rect.size)
-        self.image = img
-        
         for k, v in keep.items() :
             setattr(self.rect, k, v)
         
+        self.update()
         self.dirty = 1
-        
+    
+
+    def deflate(self) :
+        self.inflated = False
+        self.rect = self._baseRect.copy()
+        self.update()
+        self.dirty = 1
+            
 
 
 REF_POINTS = {