955a3ab6c2c74bcc27f94673950c1c7708d0f36e
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
14 # TODO : positionner cette constance en fonction de la résolution d'affichage
15 # externaliser la conf.
20 ON_TOP_LUMINANCE
= 0.6
21 ON_BOTTOM_LUMINANCE
= 0.9
23 ON_COLUMN_OVERSIZING
= 1.5
26 def rename_method(newName
) :
33 class MetaRenamer(type):
34 def __new__(mcs
, name
, bases
, dict) :
35 for k
, v
in dict.items() :
36 if isinstance(v
, types
.FunctionType
) :
38 print 'renommage de %s en %s' % (k
, v
.__name
__)
41 return type.__new
__(mcs
, name
, bases
, dict)
44 class _PlayingScreenBase(pygame
.sprite
.OrderedUpdates
) :
46 __metaclass__
= MetaRenamer
48 def __init__(self
, distinctNotes
=[]) :
50 distinctNotes : notes disctinctes présentes dans la chanson
51 triées du plus grave au plus aigu.
53 super(_PlayingScreenBase
, self
).__init
__()
54 self
.distinctNotes
= distinctNotes
55 self
.keyboardLength
= 0
56 self
.keyboardRects
= []
63 def _initRects(self
) :
64 """ création des espaces réservés pour
65 afficher les colonnes.
67 ambitus
= self
.distinctNotes
[-1].midi
- self
.distinctNotes
[0].midi
69 self
.keyboardLength
= 8
71 self
.keyboardLength
= 11
73 screen
= pygame
.display
.get_surface()
75 # taille de la zone d'affichage utile (bordure autour)
76 dispWidth
= screen
.get_width() - 2 * BORDER
77 dispHeight
= screen
.get_height() - 2 * BORDER
79 columnWidth
= int(round(float(dispWidth
) / self
.keyboardLength
))
82 for i
in range(self
.keyboardLength
) :
83 upperLeftCorner
= (i
*columnWidth
+ BORDER
, BORDER
)
84 rect
= pygame
.Rect(upperLeftCorner
, (columnWidth
, dispHeight
))
87 self
.keyboardRects
= rects
89 def _initColumns(self
) :
91 hueStep
= FIRST_HUE
/ (self
.keyboardLength
- 1)
92 for i
, rect
in enumerate(self
.keyboardRects
) :
93 hue
= FIRST_HUE
- hueStep
* i
99 def highlightColumn(self
, index
) :
100 for i
, sprite
in enumerate(self
.sprites()) :
101 sprite
.update(i
==index
)
102 self
.draw(pygame
.display
.get_surface())
106 while self
._running
:
107 pygame
.display
.flip()
108 events
= pygame
.event
.get()
112 def input(self
, event
) :
113 handler
= getattr(self
, 'eventHandler%s' % event
.type, lambda e
:None)
116 @rename_method('eventHandler%s' % pygame
.KEYDOWN
)
117 def handleKeyDown(self
, event
) :
118 if event
.key
== pygame
.K_q
:
119 self
._running
= False
122 if uni
.isdigit() and int(uni
) <=8 :
123 self
.highlightColumn(int(uni
))
128 class SongPlayingScreen(_PlayingScreenBase
) :
130 def __init__(self
, song
) :
131 super(SongPlayingScreen
, self
).__init
__(song
.distinctNotes
)
134 class SongPlayingScreenTest(_PlayingScreenBase
) :
139 super(SongPlayingScreenTest
, self
).__init
__([o
])
142 class Column(pygame
.sprite
.Sprite
) :
144 def __init__(self
, hue
, rect
) :
145 pygame
.sprite
.Sprite
.__init
__(self
)
146 sur
= pygame
.surface
.Surface(rect
.size
)
147 rgba
= hls_to_rgba_8bits(hue
, OFF_LUMINANCE
, OFF_SATURATION
)
152 topRgba
= hls_to_rgba_8bits(hue
, ON_TOP_LUMINANCE
, ON_SATURATION
)
153 bottomRgba
= hls_to_rgba_8bits(hue
, ON_BOTTOM_LUMINANCE
, ON_SATURATION
)
154 rectOn
= rect
.inflate(ON_COLUMN_OVERSIZING
* rect
.width
, 0)
155 self
.stateOn
= gradients
.vertical(rectOn
.size
, topRgba
, bottomRgba
)
158 self
.image
= self
.stateOff
161 def update(self
, state
) :
163 self
.image
= self
.stateOn
164 self
.rect
= self
.rectOn
166 self
.image
= self
.stateOff
167 self
.rect
= self
.rectOff
169 def hls_to_rgba_8bits(h
, l
, s
) :
170 #convert to rgb ranging from 0 to 255
171 rgba
= [floor(255 * i
) for i
in hls_to_rgb(h
, l
, s
) + (1,)]