330d6cb91a083ac9fbb5d380c3a6757df8b02876
[minwii.git] / src / app / widgets / instrumentselector.py
1 # -*- coding: utf-8 -*-
2 """
3 Écran de sélection de l'instrument
4
5 $Id$
6 $URL$
7 """
8 import pygame
9 from eventutils import event_handler, EventDispatcher, EventHandlerMixin
10 from cursors import WarpingCursor
11 from config import FRAMERATE
12 from globals import BACKGROUND_LAYER
13 from globals import CURSOR_LAYER
14 from globals import hls_to_rgba_8bits
15
16
17 class InstrumentSelector(pygame.sprite.LayeredDirty, EventHandlerMixin) :
18
19 rows = 3
20 cols = 3
21
22 def __init__(self) :
23 super(InstrumentSelector, self).__init__()
24 self._initCursor()
25 self._initRects()
26 self._initTiles()
27
28 def _initRects(self) :
29 screen = pygame.display.get_surface()
30 tileWidth = int(round(float(screen.get_width()) / self.cols))
31 tileHeight = int(round(float(screen.get_height()) / self.rows))
32
33 rects = []
34 for y in range(self.cols) :
35 for x in range(self.rows) :
36 upperLeftCorner = (y * tileWidth, x * tileHeight)
37 rect = pygame.Rect(upperLeftCorner, (tileWidth, tileHeight))
38 rects.append(rect)
39 self.rects = rects
40
41 def _initTiles(self) :
42 for rect in self.rects :
43 tile = InstrumentTile(self, rect)
44 self.add(tile, layer=BACKGROUND_LAYER)
45
46
47
48 def _initCursor(self) :
49 self.cursor = WarpingCursor(blinkMode=True)
50 self.add(self.cursor, layer=CURSOR_LAYER)
51
52
53 def run(self):
54 self._running = True
55 clock = pygame.time.Clock()
56 pygame.display.flip()
57 pygame.mouse.set_visible(False)
58 while self._running :
59 EventDispatcher.dispatchEvents()
60 dirty = self.draw(pygame.display.get_surface())
61 pygame.display.update(dirty)
62 clock.tick(FRAMERATE)
63
64 def stop(self) :
65 self._running = False
66 pygame.mouse.set_visible(True)
67 self.cursor._stopBlink()
68
69 @event_handler(pygame.KEYDOWN)
70 def handleKeyDown(self, event) :
71 if event.key == pygame.K_q:
72 self.stop()
73
74
75 class InstrumentTile(pygame.sprite.DirtySprite) :
76
77 def __init__(self, group, rect) :
78 pygame.sprite.DirtySprite.__init__(self, group)
79 self.rect = rect
80 self.image = pygame.Surface(rect.size)
81 self.image.fill((0,255,255,64))