162f1826d438df0f84a11be398ed88c07f38a716
1 # -*- coding: utf-8 -*-
4 bandes arc-en-ciel représentant un clavier.
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
16 from musicxml
import Tone
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
31 class _PlayingScreenBase(pygame
.sprite
.LayeredDirty
, EventHandlerMixin
) :
33 def __init__(self
, distinctNotes
=[]) :
35 distinctNotes : notes disctinctes présentes dans la chanson
36 triées du plus grave au plus aigu.
38 super(_PlayingScreenBase
, self
).__init
__()
39 self
.distinctNotes
= distinctNotes
40 self
.keyboardLength
= 0
41 self
.keyboardRects
= []
46 self
.draw(pygame
.display
.get_surface())
51 def _initRects(self
) :
52 """ création des espaces réservés pour
53 afficher les colonnes.
55 ambitus
= self
.distinctNotes
[-1].midi
- self
.distinctNotes
[0].midi
57 self
.keyboardLength
= 8
59 self
.keyboardLength
= 11
61 screen
= pygame
.display
.get_surface()
63 # taille de la zone d'affichage utile (bordure autour)
64 dispWidth
= screen
.get_width() - 2 * BORDER
65 dispHeight
= screen
.get_height() - 2 * BORDER
67 columnWidth
= int(round(float(dispWidth
) / self
.keyboardLength
))
70 for i
in range(self
.keyboardLength
) :
71 upperLeftCorner
= (i
*columnWidth
+ BORDER
, BORDER
)
72 rect
= pygame
.Rect(upperLeftCorner
, (columnWidth
, dispHeight
))
75 self
.keyboardRects
= rects
77 def _initColumns(self
) :
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
)
86 def _initCursor(self
) :
87 self
.cursor
= WarpingCursor(blinkMode
=True)
88 self
.add(self
.cursor
, layer
=2)
92 clock
= pygame
.time
.Clock()
95 EventDispatcher
.dispatchEvents()
96 dirty
= self
.draw(pygame
.display
.get_surface())
97 pygame
.display
.update(dirty
)
100 @event_handler(pygame
.KEYDOWN
)
101 def handleKeyDown(self
, event
) :
102 if event
.key
== pygame
.K_q
:
103 self
._running
= False
106 @event_handler(pygame
.MOUSEMOTION
)
107 def handleMouseMotion(self
, event
) :
111 class PlayingScreen(_PlayingScreenBase
) :
112 "fenêtre de jeu pour improvisation"
113 scale
= [55, 57, 59, 60, 62, 64, 65, 67, 69, 71, 72]
117 for midi
in self
.scale
:
119 distinctNotes
.append(tone
)
121 super(PlayingScreen
, self
).__init
__(distinctNotes
)
124 class SongPlayingScreen(_PlayingScreenBase
) :
126 def __init__(self
, song
) :
127 super(SongPlayingScreen
, self
).__init
__(song
.distinctNotes
)
130 class SongPlayingScreenTest(_PlayingScreenBase
) :
135 super(SongPlayingScreenTest
, self
).__init
__([o
])
138 class Column(pygame
.sprite
.DirtySprite
, EventHandlerMixin
) :
140 def __init__(self
, group
, hue
, rect
, tone
) :
141 pygame
.sprite
.DirtySprite
.__init
__(self
, group
)
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
)
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
)
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
)
166 self
.image
= self
.stateOff
169 def update(self
, state
) :
170 group
= self
.groups()[0]
172 group
.change_layer(self
, 1)
173 self
.image
= self
.stateOn
174 self
.rect
= self
.rectOn
176 group
.change_layer(self
, 0)
177 self
.image
= self
.stateOff
178 self
.rect
= self
.rectOff
180 @event_handler(pygame
.MOUSEBUTTONDOWN
)
181 def onMouseDown(self
, event
) :
182 if self
.rect
.collidepoint(*event
.pos
) :
185 @event_handler(pygame
.MOUSEBUTTONUP
)
186 def onMouseUp(self
, event
) :
189 def raiseNoteOn(self
) :
192 def raiseNoteOff(self
) :
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
,)]