65772968a6dd5ccb9ac53da07aeeca83743becfe
[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 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 ''' colonne utilisée pour l'affichage d'une touche du clavier de jeu.
28 '''
29
30 def __init__(self, group, index, hue, rect, tone) :
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 sur.blit(toneName, toneRect)
47 self.surOff = sur
48 self.rectOff = rect
49
50
51 # état on : surface dégradée avec nom de la note avec largeur agrandie
52 topRgba = hls_to_rgba_8bits(hue, ON_TOP_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
53 bottomRgba = hls_to_rgba_8bits(hue, ON_BOTTOM_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
54 onWidth = rect.width * ON_COLUMN_OVERSIZING
55 onLeft = rect.centerx - onWidth / 2
56 rectOn = pygame.Rect((onLeft, 0),
57 (onWidth, rect.height))
58 self.surOn = gradients.vertical(rectOn.size, topRgba, bottomRgba)
59 w, h = rectOn.w, rectOn.h
60 toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
61 self.surOn.blit(toneName, toneRect)
62 self.rectOn = rectOn
63
64 self.image = self.surOff
65 self.rect = rect
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 rect = self.rectOn
78 renderedSyl = LYRICS_FONT.render(syllabus, True, FONT_COLOR)
79 sw, sh, = renderedSyl.get_size()
80 w, h = self.rectOn.w, self.rectOn.h
81
82 if sw > self.rectOn.w :
83 sur = pygame.transform.scale(sur, (sw, h))
84 rect = rect.inflate(sw - w, 0)
85 w = sw
86
87 screenWidth = group.dispWidth
88 if rect.left < 0 :
89 rect.left = 0
90 elif rect.right > screenWidth :
91 rect.right = screenWidth
92
93 sylRect = pygame.Rect(((w - sw) / 2, (h - sh) / 2), (sw, sh))
94 sur.blit(renderedSyl, sylRect)
95
96 self.image = sur
97 self.rect = rect
98 else :
99 group.change_layer(self, BACKGROUND_LAYER)
100 self.image = self.surOff
101 self.rect = self.rectOff
102
103 self.state = state
104 self.dirty = 1
105
106 evt = pygame.event.Event(events.COLSTATECHANGE, column=self, state=state, syllabus=syllabus)
107 pygame.event.post(evt)