a5001d39c04aaf364ad2c1c89f9b02abef8299ab
[minwii.git] / src / app / widgets / column.py
1 # -*- coding: utf-8 -*-
2 """
3 bande qui compose le clavier minwii
4
5 $Id$
6 $URL$
7 """
8 import pygame
9 from colorsys import hls_to_rgb
10 from gradients import gradients
11 import events
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
25
26
27 class Column(pygame.sprite.DirtySprite, EventHandlerMixin) :
28
29 def __init__(self, group, hue, rect, tone) :
30 pygame.sprite.DirtySprite.__init__(self, group)
31 self.state = False
32
33 # nom de l'intonation
34 self.tone = tone
35 toneName = FONT.render(tone.nom, True, FONT_COLOR)
36
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)
40 sur.fill(rgba)
41 w, h = rect.w, rect.h
42 tw, th, = toneName.get_size()
43 toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
44 sur.blit(toneName, toneRect)
45 self.surOff = sur
46 self.rectOff = rect
47
48
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)
60 self.rectOn = rectOn
61
62 self.image = self.surOff
63 self.rect = rect
64 #EventDispatcher.addEventListener(pygame.MOUSEBUTTONDOWN, self.onMouseDown)
65 #EventDispatcher.addEventListener(pygame.MOUSEBUTTONUP, self.onMouseUp)
66
67 def update(self, state, syllabus='') :
68 group = self.groups()[0]
69 if state == self.state :
70 # no changes
71 return
72 if state :
73 group.change_layer(self, FOREGROUND_LAYER)
74 sur = self.surOn
75 if syllabus :
76 sur = sur.copy()
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)
82
83 self.image = sur
84 self.rect = self.rectOn
85 else :
86 group.change_layer(self, BACKGROUND_LAYER)
87 self.image = self.surOff
88 self.rect = self.rectOff
89 self.state = state
90 self.dirty = 1
91
92 @event_handler(pygame.MOUSEBUTTONDOWN)
93 def onMouseDown(self, event) :
94 if self.rect.collidepoint(*event.pos) :
95 self.update(True)
96 self.raiseNoteOn()
97
98 @event_handler(pygame.MOUSEBUTTONUP)
99 def onMouseUp(self, event) :
100 self.update(False)
101 self.raiseNoteOff()
102
103 def raiseNoteOn(self) :
104 evt = pygame.event.Event(events.NOTEON, tone=self.tone)
105 pygame.event.post(evt)
106
107 def raiseNoteOff(self) :
108 evt = pygame.event.Event(events.NOTEOFF, tone=self.tone)
109 pygame.event.post(evt)
110
111
112
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,)]
116 return tuple(rgba)