a9b62d22487bf2778e14958d0447408c07489930
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
22 ON_COLUMN_OVERSIZING
= 1.5
24 class _PlayingScreenBase(pygame
.sprite
.OrderedUpdates
) :
25 def __init__(self
, distinctNotes
=[]) :
27 distinctNotes : notes disctinctes présentes dans la chanson
28 triées du plus grave au plus aigu.
30 super(_PlayingScreenBase
, self
).__init
__()
31 self
.distinctNotes
= distinctNotes
32 self
.keyboardLength
= 0
33 self
.keyboardRects
= []
40 def _initRects(self
) :
41 """ création des espaces réservés pour
42 afficher les colonnes.
44 ambitus
= self
.distinctNotes
[-1].midi
- self
.distinctNotes
[0].midi
46 self
.keyboardLength
= 8
48 self
.keyboardLength
= 11
50 screen
= pygame
.display
.get_surface()
52 # taille de la zone d'affichage utile (bordure autour)
53 dispWidth
= screen
.get_width() - 2 * BORDER
54 dispHeight
= screen
.get_height() - 2 * BORDER
56 columnWidth
= int(round(float(dispWidth
) / self
.keyboardLength
))
59 for i
in range(self
.keyboardLength
) :
60 upperLeftCorner
= (i
*columnWidth
+ BORDER
, BORDER
)
61 rect
= pygame
.Rect(upperLeftCorner
, (columnWidth
, dispHeight
))
64 self
.keyboardRects
= rects
66 def _initColumns(self
) :
68 hueStep
= FIRST_HUE
/ (self
.keyboardLength
- 1)
69 for i
, rect
in enumerate(self
.keyboardRects
) :
70 hue
= FIRST_HUE
- hueStep
* i
76 def highlightColumn(self
, index
) :
77 for i
, sprite
in enumerate(self
.sprites()) :
78 sprite
.update(i
==index
)
79 self
.draw(pygame
.display
.get_surface())
85 events
= pygame
.event
.get()
90 class SongPlayingScreen(_PlayingScreenBase
) :
92 def __init__(self
, song
) :
93 super(SongPlayingScreen
, self
).__init
__(song
.distinctNotes
)
96 class SongPlayingScreenTest(_PlayingScreenBase
) :
101 super(SongPlayingScreenTest
, self
).__init
__([o
])
103 def input(self
, event
) :
104 if event
.type == pygame
.KEYDOWN
:
105 if event
.key
== pygame
.K_q
:
106 self
._running
= False
109 if uni
.isdigit() and int(uni
) <=8 :
110 self
.highlightColumn(int(uni
))
113 class Column(pygame
.sprite
.Sprite
) :
115 def __init__(self
, hue
, rect
) :
116 pygame
.sprite
.Sprite
.__init
__(self
)
117 sur
= pygame
.surface
.Surface(rect
.size
)
118 rgba
= hls_to_rgba_8bits(hue
, OFF_LUMINANCE
, OFF_SATURATION
)
123 topRgba
= hls_to_rgba_8bits(hue
, ON_TOP_LUMINANCE
, ON_SATURATION
)
124 bottomRgba
= hls_to_rgba_8bits(hue
, ON_BOTTOM_LUMINANCE
, ON_SATURATION
)
125 rectOn
= rect
.inflate(ON_COLUMN_OVERSIZING
* rect
.width
, 0)
126 self
.stateOn
= gradients
.vertical(rectOn
.size
, topRgba
, bottomRgba
)
129 self
.image
= self
.stateOff
132 def update(self
, state
) :
134 self
.image
= self
.stateOn
135 self
.rect
= self
.rectOn
137 self
.image
= self
.stateOff
138 self
.rect
= self
.rectOff
140 def hls_to_rgba_8bits(h
, l
, s
) :
141 #convert to rgb ranging from 0 to 255
142 rgba
= [floor(255 * i
) for i
in hls_to_rgb(h
, l
, s
) + (1,)]