Amélioration ld.
[minwii.git] / src / minwii / 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 math import floor
10 import minwii.gradients as gradients
11 from minwii.globals import BACKGROUND_LAYER
12 from minwii.globals import FOREGROUND_LAYER
13 from minwii.globals import hls_to_rgba_8bits
14 from minwii.config import OFF_LUMINANCE
15 from minwii.config import OFF_SATURATION
16 from minwii.config import ON_TOP_LUMINANCE
17 from minwii.config import ON_BOTTOM_LUMINANCE
18 from minwii.config import ON_SATURATION
19 from minwii.config import ON_COLUMN_OVERSIZING
20 from minwii.config import ON_COLUMN_ALPHA
21 from minwii.config import LYRICS_FONT, NOTES_FONT
22 from minwii.config import FONT_COLOR
23 import minwii.events as events
24
25
26 class Column(pygame.sprite.DirtySprite) :
27 ''' colonne utilisée pour l'affichage d'une touche du clavier de jeu.
28 '''
29
30 def __init__(self, group, index, hue, rect, tone, displayNote=True) :
31 pygame.sprite.DirtySprite.__init__(self, group)
32 self.index = index
33 self.state = False
34
35 # nom de l'intonation
36 self.tone = tone
37 toneName = NOTES_FONT.render(tone.nom, True, FONT_COLOR)
38
39 # état off : surface unie et nom de l'intonation
40 sur = pygame.surface.Surface(rect.size)
41 rgba = hls_to_rgba_8bits(hue, OFF_LUMINANCE, OFF_SATURATION)
42 sur.fill(rgba)
43 w, h = rect.w, rect.h
44 tw, th, = toneName.get_size()
45 toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
46 if displayNote :
47 sur.blit(toneName, toneRect)
48 self.surOff = sur
49 self.rectOff = rect
50
51
52 # état on : surface dégradée avec nom de la note avec largeur agrandie
53 topRgba = hls_to_rgba_8bits(hue, ON_TOP_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
54 bottomRgba = hls_to_rgba_8bits(hue, ON_BOTTOM_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
55 onWidth = rect.width * ON_COLUMN_OVERSIZING
56 onLeft = rect.centerx - onWidth / 2
57 rectOn = pygame.Rect((onLeft, 0),
58 (onWidth, rect.height))
59 self.surOn = gradients.vertical(rectOn.size, topRgba, bottomRgba)
60 w, h = rectOn.w, rectOn.h
61 toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
62 if displayNote :
63 self.surOn.blit(toneName, toneRect)
64 self.rectOn = rectOn
65
66 self.image = self.surOff
67 self.rect = rect
68
69 def update(self, state, syllabus='') :
70 group = self.groups()[0]
71 if state == self.state :
72 # no changes
73 return
74 if state :
75 group.change_layer(self, FOREGROUND_LAYER)
76 sur = self.surOn
77 rect = self.rectOn
78 if syllabus :
79 sur = sur.copy()
80 renderedSyl = LYRICS_FONT.render(syllabus, True, FONT_COLOR)
81 sw, sh, = renderedSyl.get_size()
82 w, h = self.rectOn.w, self.rectOn.h
83
84 if sw > self.rectOn.w :
85 sur = pygame.transform.scale(sur, (sw, h))
86 rect = rect.inflate(sw - w, 0)
87 w = sw
88
89 screenWidth = group.dispWidth
90 if rect.left < 0 :
91 rect.left = 0
92 elif rect.right > screenWidth :
93 rect.right = screenWidth
94
95 sylRect = pygame.Rect(((w - sw) / 2, (h - sh) / 2), (sw, sh))
96 sur.blit(renderedSyl, sylRect)
97
98 self.image = sur
99 self.rect = rect
100 else :
101 group.change_layer(self, BACKGROUND_LAYER)
102 self.image = self.surOff
103 self.rect = self.rectOff
104
105 self.state = state
106 self.dirty = 1
107
108 evt = pygame.event.Event(events.COLSTATECHANGE, column=self, state=state, syllabus=syllabus)
109 pygame.event.post(evt)