fc6c801b519abf54fdf337e347e0c5c32f84d922
[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 cursors import WarpingCursor
11 from column import Column
12 import events
13 from eventutils import event_handler, EventDispatcher, EventHandlerMixin
14 import types
15 from musicxml import Tone
16
17 from config import FRAMERATE
18 from config import BORDER
19 from config import FIRST_HUE
20 from config import DEFAULT_MIDI_VELOCITY
21
22 from globals import BACKGROUND_LAYER
23 from globals import CURSOR_LAYER
24 from globals import PLAYING_MODES
25
26 class _PlayingScreenBase(pygame.sprite.LayeredDirty, EventHandlerMixin) :
27
28 def __init__(self, synth, distinctNotes=[]) :
29 """
30 distinctNotes : notes disctinctes présentes dans la chanson
31 triées du plus grave au plus aigu.
32 """
33 super(_PlayingScreenBase, self).__init__()
34 self.synth = synth
35 self.distinctNotes = distinctNotes
36 self.keyboardLength = 0
37 self.keyboardRects = []
38 self.cursor = None
39 self._initRects()
40 self.columns = {}
41 self._initColumns()
42 self._running = False
43 self.draw(pygame.display.get_surface())
44 self._initCursor()
45
46
47 def _initRects(self) :
48 """ création des espaces réservés pour
49 afficher les colonnes.
50 """
51 #ambitus = self.distinctNotes[-1].midi - self.distinctNotes[0].midi
52 #if ambitus <= 12 :
53 # self.keyboardLength = 8
54 #else :
55 # self.keyboardLength = 11
56 self.keyboardLength = len(self.distinctNotes)
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 tone = self.distinctNotes[i]
80 c = Column(self, hue, rect, tone)
81 self.add(c, layer=BACKGROUND_LAYER)
82 self.columns[tone.midi] = c
83
84
85 def _initCursor(self) :
86 self.cursor = WarpingCursor(blinkMode=True)
87 self.add(self.cursor, layer=CURSOR_LAYER)
88
89 def run(self):
90 self._running = True
91 clock = pygame.time.Clock()
92 pygame.display.flip()
93 pygame.mouse.set_visible(False)
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 pygame.mouse.set_visible(True)
101 self.cursor._stopBlink()
102
103 @event_handler(pygame.KEYDOWN)
104 def handleKeyDown(self, event) :
105 if event.key == pygame.K_q:
106 self._running = False
107
108
109 class PlayingScreen(_PlayingScreenBase) :
110 "fenêtre de jeu pour improvisation"
111
112 scale = [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
113
114 def __init__(self, synth) :
115 distinctNotes = []
116 for midi in self.scale :
117 tone = Tone(midi)
118 distinctNotes.append(tone)
119
120 super(PlayingScreen, self).__init__(synth, distinctNotes)
121
122 @event_handler(events.NOTEON)
123 def noteon(self, evt) :
124 tone = evt.tone
125 self.synth.noteon(0, tone.midi, DEFAULT_MIDI_VELOCITY)
126
127 @event_handler(events.NOTEOFF)
128 def noteoff(self, evt) :
129 tone = evt.tone
130 self.synth.noteoff(0, tone.midi)
131
132
133 class SongPlayingScreen(_PlayingScreenBase) :
134
135 def __init__(self, synth, song, mode=PLAYING_MODES['NORMAL']) :
136 super(SongPlayingScreen, self).__init__(synth, song.distinctNotes)
137 self.song = song
138 self.noteIterator = self.song.iterNotes()
139 self.play()
140
141 def play(self) :
142 note, verseIndex = self.noteIterator.next()
143 syllabus = note.lyrics[verseIndex].syllabus()
144 column = self.columns[note.midi]
145 column.update(True, syllabus)
146
147
148 @event_handler(events.NOTEON)
149 def noteon(self, evt) :
150 tone = evt.tone
151 self.synth.noteon(0, tone.midi, DEFAULT_MIDI_VELOCITY)
152
153 @event_handler(events.NOTEOFF)
154 def noteoff(self, evt) :
155 tone = evt.tone
156 self.synth.noteoff(0, tone.midi)
157
158
159
160 #class Column(pygame.sprite.DirtySprite, EventHandlerMixin) :
161 #
162 # def __init__(self, group, hue, rect, tone) :
163 # pygame.sprite.DirtySprite.__init__(self, group)
164 # self.state = False
165 #
166 # # nom de l'intonation
167 # self.tone = tone
168 # toneName = FONT.render(tone.nom, True, (0,0,0))
169 #
170 # # état off : surface unie et nom de l'intonation
171 # sur = pygame.surface.Surface(rect.size)
172 # rgba = hls_to_rgba_8bits(hue, OFF_LUMINANCE, OFF_SATURATION)
173 # sur.fill(rgba)
174 # w, h = rect.w, rect.h
175 # tw, th, = toneName.get_size()
176 # toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
177 # sur.blit(toneName, toneRect)
178 # self.surOff = sur
179 # self.rectOff = rect
180 #
181 #
182 # # état on : surface dégradée avec nom de la note avec largeur agrandie
183 # topRgba = hls_to_rgba_8bits(hue, ON_TOP_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
184 # bottomRgba = hls_to_rgba_8bits(hue, ON_BOTTOM_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
185 # onWidth = rect.width * ON_COLUMN_OVERSIZING
186 # onLeft = rect.centerx - onWidth / 2
187 # rectOn = pygame.Rect((onLeft, 0),
188 # (onWidth, rect.height))
189 # self.surOn = gradients.vertical(rectOn.size, topRgba, bottomRgba)
190 # w, h = rectOn.w, rectOn.h
191 # toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
192 # self.surOn.blit(toneName, toneRect)
193 # self.rectOn = rectOn
194 #
195 # self.image = self.surOff
196 # self.rect = rect
197 # #EventDispatcher.addEventListener(pygame.MOUSEBUTTONDOWN, self.onMouseDown)
198 # #EventDispatcher.addEventListener(pygame.MOUSEBUTTONUP, self.onMouseUp)
199 #
200 # def update(self, state, syllabus='') :
201 # group = self.groups()[0]
202 # if state == self.state :
203 # # no changes
204 # return
205 # if state :
206 # group.change_layer(self, FOREGROUND_LAYER)
207 # sur = self.surOn
208 # if syllabus :
209 # sur = sur.copy()
210 # renderedSyl = FONT.render(syllabus, True, (0,0,0))
211 # sw, sh, = renderedSyl.get_size()
212 # w, h = self.rectOn.w, self.rectOn.h
213 # sylRect = pygame.Rect(((w - sw) / 2, (h - sh) / 2), (sw, sh))
214 # sur.blit(renderedSyl, sylRect)
215 #
216 # self.image = sur
217 # self.rect = self.rectOn
218 # else :
219 # group.change_layer(self, BACKGROUND_LAYER)
220 # self.image = self.surOff
221 # self.rect = self.rectOff
222 # self.state = state
223 # self.dirty = 1
224 #
225 # @event_handler(pygame.MOUSEBUTTONDOWN)
226 # def onMouseDown(self, event) :
227 # if self.rect.collidepoint(*event.pos) :
228 # self.update(True)
229 # self.raiseNoteOn()
230 #
231 # @event_handler(pygame.MOUSEBUTTONUP)
232 # def onMouseUp(self, event) :
233 # self.update(False)
234 # self.raiseNoteOff()
235 #
236 # def raiseNoteOn(self) :
237 # evt = pygame.event.Event(events.NOTEON, tone=self.tone)
238 # pygame.event.post(evt)
239 #
240 # def raiseNoteOff(self) :
241 # evt = pygame.event.Event(events.NOTEOFF, tone=self.tone)
242 # pygame.event.post(evt)
243 #
244 #
245 #
246 #def hls_to_rgba_8bits(h, l, s, a=1) :
247 # #convert to rgb ranging from 0 to 255
248 # rgba = [floor(255 * i) for i in hls_to_rgb(h, l, s) + (a,)]
249 # return tuple(rgba)
250 #