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 math
import floor
13 # TODO : positionner cette constance en fonction de la résolution d'affichage
14 # externaliser la conf.
19 ON_TOP_LUMINANCE
= 0.6
20 ON_BOTTOM_LUMINANCE
= 0.9
23 class _PlayingScreenBase(pygame
.sprite
.OrderedUpdates
) :
24 def __init__(self
, distinctNotes
=[]) :
26 distinctNotes : notes disctinctes présentes dans la chanson
27 triées du plus grave au plus aigu.
29 super(_PlayingScreenBase
, self
).__init
__()
30 self
.distinctNotes
= distinctNotes
31 self
.keyboardLength
= 0
32 self
.keyboardRects
= []
38 def _initRects(self
) :
39 """ création des espaces réservés pour
40 afficher les colonnes.
42 ambitus
= self
.distinctNotes
[-1].midi
- self
.distinctNotes
[0].midi
44 self
.keyboardLength
= 8
46 self
.keyboardLength
= 11
48 screen
= pygame
.display
.get_surface()
50 # taille de la zone d'affichage utile (bordure autour)
51 dispWidth
= screen
.get_width() - 2 * BORDER
52 dispHeight
= screen
.get_height() - 2 * BORDER
54 columnWidth
= int(round(float(dispWidth
) / self
.keyboardLength
))
57 for i
in range(self
.keyboardLength
) :
58 upperLeftCorner
= (i
*columnWidth
+ BORDER
, BORDER
)
59 rect
= pygame
.Rect(upperLeftCorner
, (columnWidth
, dispHeight
))
62 self
.keyboardRects
= rects
64 def _initColumns(self
) :
66 hueStep
= FIRST_HUE
/ (self
.keyboardLength
- 1)
67 for i
, rect
in enumerate(self
.keyboardRects
) :
68 hue
= FIRST_HUE
- hueStep
* i
73 def highlightColumn(self
, index
) :
74 for i
, sprite
in enumerate(self
.sprites()) :
75 sprite
.update(i
==index
)
76 self
.draw(pygame
.display
.get_surface())
80 class SongPlayingScreen(_PlayingScreenBase
) :
82 def __init__(self
, song
) :
83 super(SongPlayingScreen
, self
).__init
__(song
.distinctNotes
)
86 class SongPlayingScreenTest(_PlayingScreenBase
) :
91 super(SongPlayingScreenTest
, self
).__init
__([o
])
93 #pygame.display.flip()
97 while self
.__running
:
99 events
= pygame
.event
.get()
103 def input(self
, event
) :
104 if event
.type == pygame
.KEYDOWN
:
105 if event
.key
== pygame
.K_q
:
106 self
.__running
= False
108 if uni
.isdigit() and int(uni
) <=8 :
109 self
.highlightColumn(int(uni
))
112 # if event.key == pygame.K_i:
113 # self.backToInstrumentChoice = True
116 # if event.key == pygame.K_w:
120 # if event.key == pygame.K_e:
124 # if event.key == pygame.K_r:
128 # if event.key == pygame.K_t:
133 class Column(pygame
.sprite
.Sprite
) :
135 def __init__(self
, hue
, rect
) :
136 pygame
.sprite
.Sprite
.__init
__(self
)
137 sur
= pygame
.surface
.Surface(rect
.size
)
138 rgba
= hls_to_rgba_8bits(hue
, OFF_LUMINANCE
, OFF_SATURATION
)
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 size
= rect
.inflate(2*rect
.width
,0).size
145 print rect
.size
, size
146 self
.stateOn
= gradients
.vertical(size
, topRgba
, bottomRgba
)
148 self
.image
= self
.stateOff
151 def update(self
, state
) :
153 self
.image
= self
.stateOn
155 self
.image
= self
.stateOff
157 def hls_to_rgba_8bits(h
, l
, s
) :
158 #convert to rgb ranging from 0 to 255
159 rgba
= [floor(255 * i
) for i
in hls_to_rgb(h
, l
, s
) + (1,)]