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