162f1826d438df0f84a11be398ed88c07f38a716
[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 from musicxml import Tone
17
18 from config import FRAMERATE
19 from config import BORDER
20 from config import FIRST_HUE
21 from config import OFF_LUMINANCE
22 from config import OFF_SATURATION
23 from config import ON_TOP_LUMINANCE
24 from config import ON_BOTTOM_LUMINANCE
25 from config import ON_SATURATION
26 from config import ON_COLUMN_OVERSIZING
27 from config import ON_COLUMN_ALPHA
28 from config import FONT
29 from config import FONT_COLOR
30
31 class _PlayingScreenBase(pygame.sprite.LayeredDirty, EventHandlerMixin) :
32
33 def __init__(self, distinctNotes=[]) :
34 """
35 distinctNotes : notes disctinctes présentes dans la chanson
36 triées du plus grave au plus aigu.
37 """
38 super(_PlayingScreenBase, self).__init__()
39 self.distinctNotes = distinctNotes
40 self.keyboardLength = 0
41 self.keyboardRects = []
42 self.cursor = None
43 self._initRects()
44 self._initColumns()
45 self._running = False
46 self.draw(pygame.display.get_surface())
47 self._initCursor()
48
49
50
51 def _initRects(self) :
52 """ création des espaces réservés pour
53 afficher les colonnes.
54 """
55 ambitus = self.distinctNotes[-1].midi - self.distinctNotes[0].midi
56 if ambitus <= 12 :
57 self.keyboardLength = 8
58 else :
59 self.keyboardLength = 11
60
61 screen = pygame.display.get_surface()
62
63 # taille de la zone d'affichage utile (bordure autour)
64 dispWidth = screen.get_width() - 2 * BORDER
65 dispHeight = screen.get_height() - 2 * BORDER
66
67 columnWidth = int(round(float(dispWidth) / self.keyboardLength))
68
69 rects = []
70 for i in range(self.keyboardLength) :
71 upperLeftCorner = (i*columnWidth + BORDER, BORDER)
72 rect = pygame.Rect(upperLeftCorner, (columnWidth, dispHeight))
73 rects.append(rect)
74
75 self.keyboardRects = rects
76
77 def _initColumns(self) :
78
79 hueStep = FIRST_HUE / (self.keyboardLength - 1)
80 for i, rect in enumerate(self.keyboardRects) :
81 hue = FIRST_HUE - hueStep * i
82 tone = self.distinctNotes[i]
83 c = Column(self, hue, rect, tone)
84 self.add(c, layer=0)
85
86 def _initCursor(self) :
87 self.cursor = WarpingCursor(blinkMode=True)
88 self.add(self.cursor, layer=2)
89
90 def run(self):
91 self._running = True
92 clock = pygame.time.Clock()
93 pygame.display.flip()
94 while self._running :
95 EventDispatcher.dispatchEvents()
96 dirty = self.draw(pygame.display.get_surface())
97 pygame.display.update(dirty)
98 clock.tick(FRAMERATE)
99
100 @event_handler(pygame.KEYDOWN)
101 def handleKeyDown(self, event) :
102 if event.key == pygame.K_q:
103 self._running = False
104
105
106 @event_handler(pygame.MOUSEMOTION)
107 def handleMouseMotion(self, event) :
108 pass
109
110
111 class PlayingScreen(_PlayingScreenBase) :
112 "fenêtre de jeu pour improvisation"
113 scale = [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
114
115 def __init__(self) :
116 distinctNotes = []
117 for midi in self.scale :
118 tone = Tone(midi)
119 distinctNotes.append(tone)
120
121 super(PlayingScreen, self).__init__(distinctNotes)
122
123
124 class SongPlayingScreen(_PlayingScreenBase) :
125
126 def __init__(self, song) :
127 super(SongPlayingScreen, self).__init__(song.distinctNotes)
128 self.song = song
129
130 class SongPlayingScreenTest(_PlayingScreenBase) :
131 def __init__(self) :
132 class C:pass
133 o = C()
134 o.midi=1
135 super(SongPlayingScreenTest, self).__init__([o])
136
137
138 class Column(pygame.sprite.DirtySprite, EventHandlerMixin) :
139
140 def __init__(self, group, hue, rect, tone) :
141 pygame.sprite.DirtySprite.__init__(self, group)
142
143 toneName = FONT.render(tone.nom, True, (0,0,0))
144 sur = pygame.surface.Surface(rect.size)
145 rgba = hls_to_rgba_8bits(hue, OFF_LUMINANCE, OFF_SATURATION)
146 sur.fill(rgba)
147 w, h = rect.w, rect.h
148 tw, th, = toneName.get_size()
149 toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
150 sur.blit(toneName, toneRect)
151 self.stateOff = sur
152 self.rectOff = rect
153
154 topRgba = hls_to_rgba_8bits(hue, ON_TOP_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
155 bottomRgba = hls_to_rgba_8bits(hue, ON_BOTTOM_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
156 onWidth = rect.width * ON_COLUMN_OVERSIZING
157 onLeft = rect.centerx - onWidth / 2
158 rectOn = pygame.Rect((onLeft, 0),
159 (onWidth, rect.height))
160 self.stateOn = gradients.vertical(rectOn.size, topRgba, bottomRgba)
161 w, h = rectOn.w, rectOn.h
162 toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
163 self.stateOn.blit(toneName, toneRect)
164 self.rectOn = rectOn
165
166 self.image = self.stateOff
167 self.rect = rect
168
169 def update(self, state) :
170 group = self.groups()[0]
171 if state :
172 group.change_layer(self, 1)
173 self.image = self.stateOn
174 self.rect = self.rectOn
175 else :
176 group.change_layer(self, 0)
177 self.image = self.stateOff
178 self.rect = self.rectOff
179
180 @event_handler(pygame.MOUSEBUTTONDOWN)
181 def onMouseDown(self, event) :
182 if self.rect.collidepoint(*event.pos) :
183 self.update(True)
184
185 @event_handler(pygame.MOUSEBUTTONUP)
186 def onMouseUp(self, event) :
187 self.update(False)
188
189 def raiseNoteOn(self) :
190 pass
191
192 def raiseNoteOff(self) :
193 pass
194
195
196
197 def hls_to_rgba_8bits(h, l, s, a=1) :
198 #convert to rgb ranging from 0 to 255
199 rgba = [floor(255 * i) for i in hls_to_rgb(h, l, s) + (a,)]
200 return tuple(rgba)