0e78285e0c528c36ea4b8ee99737873a204adbc6
[minwii.git] / src / app / widgets / playingscreen.py
1 # -*- coding: utf-8 -*-
2 """
3 Écran de jeu MinWii :
4 bandes arc-en-ciel représentant un clavier.
5
6 $Id$
7 $URL$
8 """
9 import pygame
10 from colorsys import hls_to_rgb
11 from gradients import gradients
12 # TODO : positionner cette constance en fonction de la résolution d'affichage
13 # externaliser la conf.
14 BORDER = 5 # 5px
15 FIRST_HUE = 0.6
16 OFF_LUMINANCE = 0.1
17 OFF_SATURATION = 1
18 ON_TOP_LUMINANCE = 0.9
19 ON_BOTTOM_LUMINANCE = 0.6
20 ON_SATURATION = 1
21
22 class _PlayingScreenBase(object) :
23 def __init__(self, distinctNotes=[]) :
24 """
25 distinctNotes : notes disctinctes présentes dans la chanson
26 triées du plus grave au plus aigu.
27 """
28 self.distinctNotes = distinctNotes
29 self.keyboardLength = 0
30 self.keyboardRects = []
31 self._initRects()
32 self.drawColumns()
33
34
35 def _initRects(self) :
36 """ création des espaces réservés pour
37 afficher les colonnes.
38 """
39 ambitus = self.distinctNotes[-1].midi - self.distinctNotes[0].midi
40 if ambitus <= 12 :
41 self.keyboardLength = 8
42 else :
43 self.keyboardLength = 11
44
45 screen = pygame.display.get_surface()
46
47 # taille de la zone d'affichage utile (bordure autour)
48 dispWidth = screen.get_width() - 2 * BORDER
49 dispHeight = screen.get_height() - 2 * BORDER
50
51 columnWidth = int(round(float(dispWidth) / keyboardLength))
52
53 rects = []
54 for i in range(keyboardLength) :
55 upperLeftCorner = (i*columnWidth + BORDER, BORDER)
56 rect = pygame.Rect(upperLeftCorner, (columnWidth, dispHeight))
57 rects.append(rect)
58
59 self.keyboardRects = rects
60
61 def _initColumns(self) :
62
63 hueStep = FIRST_HUE / (self.keyboardLength - 1)
64 for i, rect in enumerate(self.keyboardRects) :
65 hue = FIRST_HUE - hueStep * i
66 c = Column(hue)
67
68
69 def drawColumns(self) :
70 pass
71
72 def highLightColumn(self) :
73 pass
74
75
76 class SongPlayingScreen(_PlayingScreenBase) :
77
78 def __init__(self, song) :
79 super(SongPlayingScreen, self).__init__(song.)
80 self.song = song
81
82
83
84 class Column(pygame.sprite.Sprite) :
85
86 def __init__(self, hue, rect) :
87 pygame.sprite.Sprite.__init__(self)
88 sur = Surface(rect.size)
89 rgba = hls_to_rgb(hue, OFF_LUMINANCE, OFF_SATURATION) + (255,)
90 sur.fill(rgba)
91 self.stateOff = sur
92
93 topRgba = hls_to_rgb(hue, ON_TOP_LUMINANCE, ON_SATURATION) + (255,)
94 bottomRgba = hls_to_rgb(hue, ON_BOTTOM_LUMINANCE, ON_SATURATION) + (255,)
95 size = rect.inflate(2*rect.width,0).size
96 self.stateOn = gradients.vertical(size, topRgba, bottomRgba)
97