c0d8455957c358274b96cf4a4087bf4c6b7229b8
1 # -*- coding: utf-8 -*-
3 bande qui compose le clavier minwii
9 from colorsys
import hls_to_rgb
10 from gradients
import gradients
12 from eventutils
import event_handler
, EventDispatcher
, EventHandlerMixin
13 from math
import floor
14 from globals import BACKGROUND_LAYER
15 from globals import FOREGROUND_LAYER
16 from config
import OFF_LUMINANCE
17 from config
import OFF_SATURATION
18 from config
import ON_TOP_LUMINANCE
19 from config
import ON_BOTTOM_LUMINANCE
20 from config
import ON_SATURATION
21 from config
import ON_COLUMN_OVERSIZING
22 from config
import ON_COLUMN_ALPHA
23 from config
import FONT
24 from config
import FONT_COLOR
27 class Column(pygame
.sprite
.DirtySprite
, EventHandlerMixin
) :
29 def __init__(self
, group
, hue
, rect
, tone
) :
30 pygame
.sprite
.DirtySprite
.__init
__(self
, group
)
35 toneName
= FONT
.render(tone
.nom
, True, FONT_COLOR
)
37 # état off : surface unie et nom de l'intonation
38 sur
= pygame
.surface
.Surface(rect
.size
)
39 rgba
= hls_to_rgba_8bits(hue
, OFF_LUMINANCE
, OFF_SATURATION
)
42 tw
, th
, = toneName
.get_size()
43 toneRect
= pygame
.Rect(((w
- tw
) / 2, h
- th
), (tw
, th
))
44 sur
.blit(toneName
, toneRect
)
49 # état on : surface dégradée avec nom de la note avec largeur agrandie
50 topRgba
= hls_to_rgba_8bits(hue
, ON_TOP_LUMINANCE
, ON_SATURATION
, ON_COLUMN_ALPHA
)
51 bottomRgba
= hls_to_rgba_8bits(hue
, ON_BOTTOM_LUMINANCE
, ON_SATURATION
, ON_COLUMN_ALPHA
)
52 onWidth
= rect
.width
* ON_COLUMN_OVERSIZING
53 onLeft
= rect
.centerx
- onWidth
/ 2
54 rectOn
= pygame
.Rect((onLeft
, 0),
55 (onWidth
, rect
.height
))
56 self
.surOn
= gradients
.vertical(rectOn
.size
, topRgba
, bottomRgba
)
57 w
, h
= rectOn
.w
, rectOn
.h
58 toneRect
= pygame
.Rect(((w
- tw
) / 2, h
- th
), (tw
, th
))
59 self
.surOn
.blit(toneName
, toneRect
)
62 self
.image
= self
.surOff
64 #EventDispatcher.addEventListener(pygame.MOUSEBUTTONDOWN, self.onMouseDown)
65 #EventDispatcher.addEventListener(pygame.MOUSEBUTTONUP, self.onMouseUp)
67 def update(self
, state
, syllabus
='') :
68 group
= self
.groups()[0]
69 if state
== self
.state
:
73 group
.change_layer(self
, FOREGROUND_LAYER
)
77 renderedSyl
= FONT
.render(syllabus
, True, FONT_COLOR
)
78 sw
, sh
, = renderedSyl
.get_size()
79 w
, h
= self
.rectOn
.w
, self
.rectOn
.h
80 sylRect
= pygame
.Rect(((w
- sw
) / 2, (h
- sh
) / 2), (sw
, sh
))
81 sur
.blit(renderedSyl
, sylRect
)
84 self
.rect
= self
.rectOn
86 group
.change_layer(self
, BACKGROUND_LAYER
)
87 self
.image
= self
.surOff
88 self
.rect
= self
.rectOff
92 #@event_handler(pygame.MOUSEBUTTONDOWN)
93 def onMouseDown(self
, event
) :
94 if self
.rect
.collidepoint(*event
.pos
) and self
.state
:
97 @event_handler(pygame
.MOUSEBUTTONUP
)
98 def onMouseUp(self
, event
) :
101 def raiseKeyDown(self
) :
102 evt
= pygame
.event
.Event(events
.KEYDOWN
, column
=self
)
103 pygame
.event
.post(evt
)
104 print 'down', self
.tone
.nom
106 def raiseKeyUp(self
) :
108 evt
= pygame
.event
.Event(events
.KEYUP
, column
=self
)
109 pygame
.event
.post(evt
)
110 print 'up', self
.tone
.nom
112 # def raiseNoteOn(self) :
113 # evt = pygame.event.Event(events.NOTEON, tone=self.tone)
114 # pygame.event.post(evt)
116 # def raiseNoteOff(self) :
117 # evt = pygame.event.Event(events.NOTEOFF, tone=self.tone)
118 # pygame.event.post(evt)
122 def hls_to_rgba_8bits(h
, l
, s
, a
=1) :
123 #convert to rgb ranging from 0 to 255
124 rgba
= [floor(255 * i
) for i
in hls_to_rgb(h
, l
, s
) + (a
,)]