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