workarrund bug pygame.
[minwii.git] / src / app / widgets / playingscreen.py
1 # -*- coding: utf-8 -*-
2 """
3 Écran de jeu MinWii :
4 bandes arc-en-ciel représentant un clavier.
5
6 $Id$
7 $URL$
8 """
9 import pygame
10 from colorsys import hls_to_rgb
11 from gradients import gradients
12 from cursors import WarpingCursor
13 from eventutils import event_handler, EventDispatcher, EventHandlerMixin
14 from math import floor
15 import types
16 # TODO : positionner cette constance en fonction de la résolution d'affichage
17 # externaliser la conf.
18 BORDER = 0 # 5px
19 FIRST_HUE = 0.6
20 OFF_LUMINANCE = 0.2
21 OFF_SATURATION = 1
22 ON_TOP_LUMINANCE = 0.6
23 ON_BOTTOM_LUMINANCE = 0.9
24 ON_SATURATION = 1
25 ON_COLUMN_OVERSIZING = 2
26
27
28 class _PlayingScreenBase(pygame.sprite.OrderedUpdates, EventHandlerMixin) :
29
30 def __init__(self, distinctNotes=[]) :
31 """
32 distinctNotes : notes disctinctes présentes dans la chanson
33 triées du plus grave au plus aigu.
34 """
35 super(_PlayingScreenBase, self).__init__()
36 self.distinctNotes = distinctNotes
37 self.keyboardLength = 0
38 self.keyboardRects = []
39 self.cursor = None
40 self._initRects()
41 self._initColumns()
42 self._running = False
43 self.draw(pygame.display.get_surface())
44 self._initCursor()
45
46
47
48 def _initRects(self) :
49 """ création des espaces réservés pour
50 afficher les colonnes.
51 """
52 ambitus = self.distinctNotes[-1].midi - self.distinctNotes[0].midi
53 if ambitus <= 12 :
54 self.keyboardLength = 8
55 else :
56 self.keyboardLength = 11
57
58 screen = pygame.display.get_surface()
59
60 # taille de la zone d'affichage utile (bordure autour)
61 dispWidth = screen.get_width() - 2 * BORDER
62 dispHeight = screen.get_height() - 2 * BORDER
63
64 columnWidth = int(round(float(dispWidth) / self.keyboardLength))
65
66 rects = []
67 for i in range(self.keyboardLength) :
68 upperLeftCorner = (i*columnWidth + BORDER, BORDER)
69 rect = pygame.Rect(upperLeftCorner, (columnWidth, dispHeight))
70 rects.append(rect)
71
72 self.keyboardRects = rects
73
74 def _initColumns(self) :
75
76 hueStep = FIRST_HUE / (self.keyboardLength - 1)
77 for i, rect in enumerate(self.keyboardRects) :
78 hue = FIRST_HUE - hueStep * i
79 c = Column(hue, rect)
80 self.add(c)
81
82 def _initCursor(self) :
83 self.cursor = WarpingCursor(blinkMode=True)
84 self.add(self.cursor)
85
86
87 def highlightColumn(self, index) :
88 for i, sprite in enumerate(self.sprites()) :
89 sprite.update(i==index)
90 self.draw(pygame.display.get_surface())
91
92 def run(self):
93 self._running = True
94 clock = pygame.time.Clock()
95 pygame.display.flip()
96 while self._running :
97 EventDispatcher.dispatchEvents()
98 dirty = self.draw(pygame.display.get_surface())
99 pygame.display.update(dirty)
100 clock.tick(50)
101
102 @event_handler(pygame.KEYDOWN)
103 def handleKeyDown(self, event) :
104 if event.key == pygame.K_q:
105 self._running = False
106 uni = event.unicode
107
108 if uni.isdigit() and int(uni) <=8 :
109 self.highlightColumn(int(uni))
110
111 @event_handler(pygame.MOUSEMOTION)
112 def handleMouseMotion(self, event) :
113 pass
114
115
116
117
118 class SongPlayingScreen(_PlayingScreenBase) :
119
120 def __init__(self, song) :
121 super(SongPlayingScreen, self).__init__(song.distinctNotes)
122 self.song = song
123
124 class SongPlayingScreenTest(_PlayingScreenBase) :
125 def __init__(self) :
126 class C:pass
127 o = C()
128 o.midi=1
129 super(SongPlayingScreenTest, self).__init__([o])
130
131
132 class Column(pygame.sprite.Sprite, EventHandlerMixin) :
133
134 def __init__(self, hue, rect) :
135 pygame.sprite.Sprite.__init__(self)
136 sur = pygame.surface.Surface(rect.size)
137 rgba = hls_to_rgba_8bits(hue, OFF_LUMINANCE, OFF_SATURATION)
138 sur.fill(rgba)
139 self.stateOff = sur
140 self.rectOff = rect
141
142 topRgba = hls_to_rgba_8bits(hue, ON_TOP_LUMINANCE, ON_SATURATION)
143 bottomRgba = hls_to_rgba_8bits(hue, ON_BOTTOM_LUMINANCE, ON_SATURATION)
144 rectOn = pygame.Rect((rect.left - rect.width/2, 0),
145 (rect.width * ON_COLUMN_OVERSIZING, rect.height))
146 self.stateOn = gradients.vertical(rectOn.size, topRgba, bottomRgba)
147 self.rectOn = rectOn
148
149 self.image = self.stateOff
150 self.rect = rect
151
152 def update(self, state) :
153 if state :
154 self.image = self.stateOn
155 self.rect = self.rectOn
156 else :
157 self.image = self.stateOff
158 self.rect = self.rectOff
159
160 @event_handler(pygame.MOUSEBUTTONDOWN)
161 def onMouseDown(self, event) :
162 if self.rect.collidepoint(*event.pos) :
163 self.update(True)
164
165 @event_handler(pygame.MOUSEBUTTONUP)
166 def onMouseUp(self, event) :
167 self.update(False)
168
169 def hls_to_rgba_8bits(h, l, s) :
170 #convert to rgb ranging from 0 to 255
171 rgba = [floor(255 * i) for i in hls_to_rgb(h, l, s) + (1,)]
172 return tuple(rgba)