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 event_handler(eventType
) :
28 m
.__name
__ = 'eventHandler%s' % eventType
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 @event_handler(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
))
125 @event_handler(pygame
.MOUSEMOTION
)
126 def handleMouseMotion(self
, event
) :
131 class SongPlayingScreen(_PlayingScreenBase
) :
133 def __init__(self
, song
) :
134 super(SongPlayingScreen
, self
).__init
__(song
.distinctNotes
)
137 class SongPlayingScreenTest(_PlayingScreenBase
) :
142 super(SongPlayingScreenTest
, self
).__init
__([o
])
145 class Column(pygame
.sprite
.Sprite
) :
147 def __init__(self
, hue
, rect
) :
148 pygame
.sprite
.Sprite
.__init
__(self
)
149 sur
= pygame
.surface
.Surface(rect
.size
)
150 rgba
= hls_to_rgba_8bits(hue
, OFF_LUMINANCE
, OFF_SATURATION
)
155 topRgba
= hls_to_rgba_8bits(hue
, ON_TOP_LUMINANCE
, ON_SATURATION
)
156 bottomRgba
= hls_to_rgba_8bits(hue
, ON_BOTTOM_LUMINANCE
, ON_SATURATION
)
157 rectOn
= rect
.inflate(ON_COLUMN_OVERSIZING
* rect
.width
, 0)
158 self
.stateOn
= gradients
.vertical(rectOn
.size
, topRgba
, bottomRgba
)
161 self
.image
= self
.stateOff
164 def update(self
, state
) :
166 self
.image
= self
.stateOn
167 self
.rect
= self
.rectOn
169 self
.image
= self
.stateOff
170 self
.rect
= self
.rectOff
172 def hls_to_rgba_8bits(h
, l
, s
) :
173 #convert to rgb ranging from 0 to 255
174 rgba
= [floor(255 * i
) for i
in hls_to_rgb(h
, l
, s
) + (1,)]