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
) :
98 @event_handler(pygame
.MOUSEBUTTONUP
)
99 def onMouseUp(self
, event
) :
103 def raiseNoteOn(self
) :
104 evt
= pygame
.event
.Event(events
.NOTEON
, tone
=self
.tone
)
105 pygame
.event
.post(evt
)
107 def raiseNoteOff(self
) :
108 evt
= pygame
.event
.Event(events
.NOTEOFF
, tone
=self
.tone
)
109 pygame
.event
.post(evt
)
113 def hls_to_rgba_8bits(h
, l
, s
, a
=1) :
114 #convert to rgb ranging from 0 to 255
115 rgba
= [floor(255 * i
) for i
in hls_to_rgb(h
, l
, s
) + (a
,)]