0e6479503c02ca7cd4180b23c51b82f013c3b5ae
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
.Group
) :
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 drawColumns(self
) :
76 def highlightColumn(self
, index
) :
77 for i
, sprite
in enumerate(self
.sprites()) :
78 sprite
.update(i
==index
)
82 class SongPlayingScreen(_PlayingScreenBase
) :
84 def __init__(self
, song
) :
85 super(SongPlayingScreen
, self
).__init
__(song
.distinctNotes
)
88 class SongPlayingScreenTest(_PlayingScreenBase
) :
93 super(SongPlayingScreenTest
, self
).__init
__([o
])
96 #while self.__running :
97 # events = pygame.event.get()
98 # for event in events:
100 # pygame.display.flip()
102 def input(self
, event
) :
103 if event
.type == pygame
.KEYDOWN
:
104 if event
.key
== pygame
.K_q
:
105 self
.__running
= False
107 if uni
.isdigit() and int(uni
) <=8 :
108 self
.highlightColumn(int(uni
))
111 # if event.key == pygame.K_i:
112 # self.backToInstrumentChoice = True
115 # if event.key == pygame.K_w:
119 # if event.key == pygame.K_e:
123 # if event.key == pygame.K_r:
127 # if event.key == pygame.K_t:
132 class Column(pygame
.sprite
.Sprite
) :
134 def __init__(self
, hue
, rect
) :
135 pygame
.sprite
.Sprite
.__init
__(self
)
136 sur
= pygame
.surface
.Surface(rect
.size
)
137 rgba
= hls_to_rgba_8bits(hue
, OFF_LUMINANCE
, OFF_SATURATION
)
141 topRgba
= hls_to_rgba_8bits(hue
, ON_TOP_LUMINANCE
, ON_SATURATION
)
142 bottomRgba
= hls_to_rgba_8bits(hue
, ON_BOTTOM_LUMINANCE
, ON_SATURATION
)
143 size
= rect
.inflate(2*rect
.width
,0).size
144 self
.stateOn
= gradients
.vertical(rect
.size
, topRgba
, bottomRgba
)
146 self
.image
= self
.stateOn
149 def update(self
, state
) :
151 self
.image
= self
.stateOn
153 self
.image
= self
.stateOff
155 def hls_to_rgba_8bits(h
, l
, s
) :
156 #convert to rgb ranging from 0 to 255
157 rgba
= [floor(255 * i
) for i
in hls_to_rgb(h
, l
, s
) + (1,)]