Fenêtre de jeu : agrandissement comme il faut en bordure.
[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 FONT
22 from config import FONT_COLOR
23
24
25 class Column(pygame.sprite.DirtySprite) :
26
27 def __init__(self, group, hue, rect, tone, atBorder) :
28 pygame.sprite.DirtySprite.__init__(self, group)
29 self.state = False
30
31 # nom de l'intonation
32 self.tone = tone
33 toneName = FONT.render(tone.nom, True, FONT_COLOR)
34
35 # état off : surface unie et nom de l'intonation
36 sur = pygame.surface.Surface(rect.size)
37 rgba = hls_to_rgba_8bits(hue, OFF_LUMINANCE, OFF_SATURATION)
38 sur.fill(rgba)
39 w, h = rect.w, rect.h
40 tw, th, = toneName.get_size()
41 toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
42 sur.blit(toneName, toneRect)
43 self.surOff = sur
44 self.rectOff = rect
45
46
47 # état on : surface dégradée avec nom de la note avec largeur agrandie
48 topRgba = hls_to_rgba_8bits(hue, ON_TOP_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
49 bottomRgba = hls_to_rgba_8bits(hue, ON_BOTTOM_LUMINANCE, ON_SATURATION, ON_COLUMN_ALPHA)
50 onWidth = rect.width * ON_COLUMN_OVERSIZING
51 onLeft = rect.centerx - onWidth / 2
52 rectOn = pygame.Rect((onLeft, 0),
53 (onWidth, rect.height))
54 if atBorder :
55 setattr(rectOn, atBorder, getattr(rect, atBorder))
56
57 self.surOn = gradients.vertical(rectOn.size, topRgba, bottomRgba)
58 w, h = rectOn.w, rectOn.h
59 toneRect = pygame.Rect(((w - tw) / 2, h - th), (tw, th))
60 self.surOn.blit(toneName, toneRect)
61 self.rectOn = rectOn
62
63 self.image = self.surOff
64 self.rect = rect
65
66 def update(self, state, syllabus='') :
67 group = self.groups()[0]
68 if state == self.state :
69 # no changes
70 return
71 if state :
72 group.change_layer(self, FOREGROUND_LAYER)
73 sur = self.surOn
74 if syllabus :
75 sur = sur.copy()
76 renderedSyl = FONT.render(syllabus, True, FONT_COLOR)
77 sw, sh, = renderedSyl.get_size()
78 w, h = self.rectOn.w, self.rectOn.h
79 sylRect = pygame.Rect(((w - sw) / 2, (h - sh) / 2), (sw, sh))
80 sur.blit(renderedSyl, sylRect)
81
82 self.image = sur
83 self.rect = self.rectOn
84 else :
85 group.change_layer(self, BACKGROUND_LAYER)
86 self.image = self.surOff
87 self.rect = self.rectOff
88 self.state = state
89 self.dirty = 1
90