c0d8455957c358274b96cf4a4087bf4c6b7229b8
[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) and self.state :
95 self.raiseKeyDown()
96
97 @event_handler(pygame.MOUSEBUTTONUP)
98 def onMouseUp(self, event) :
99 self.raiseKeyUp()
100
101 def raiseKeyDown(self) :
102 evt = pygame.event.Event(events.KEYDOWN, column=self)
103 pygame.event.post(evt)
104 print 'down', self.tone.nom
105
106 def raiseKeyUp(self) :
107 if self.state :
108 evt = pygame.event.Event(events.KEYUP, column=self)
109 pygame.event.post(evt)
110 print 'up', self.tone.nom
111
112 # def raiseNoteOn(self) :
113 # evt = pygame.event.Event(events.NOTEON, tone=self.tone)
114 # pygame.event.post(evt)
115 #
116 # def raiseNoteOff(self) :
117 # evt = pygame.event.Event(events.NOTEOFF, tone=self.tone)
118 # pygame.event.post(evt)
119
120
121
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,)]
125 return tuple(rgba)