a9b62d22487bf2778e14958d0447408c07489930
[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 from math import floor
13 # TODO : positionner cette constance en fonction de la résolution d'affichage
14 # externaliser la conf.
15 BORDER = 5 # 5px
16 FIRST_HUE = 0.6
17 OFF_LUMINANCE = 0.2
18 OFF_SATURATION = 1
19 ON_TOP_LUMINANCE = 0.6
20 ON_BOTTOM_LUMINANCE = 0.9
21 ON_SATURATION = 1
22 ON_COLUMN_OVERSIZING = 1.5
23
24 class _PlayingScreenBase(pygame.sprite.OrderedUpdates) :
25 def __init__(self, distinctNotes=[]) :
26 """
27 distinctNotes : notes disctinctes présentes dans la chanson
28 triées du plus grave au plus aigu.
29 """
30 super(_PlayingScreenBase, self).__init__()
31 self.distinctNotes = distinctNotes
32 self.keyboardLength = 0
33 self.keyboardRects = []
34 self._initRects()
35 self._initColumns()
36 self._running = False
37
38
39
40 def _initRects(self) :
41 """ création des espaces réservés pour
42 afficher les colonnes.
43 """
44 ambitus = self.distinctNotes[-1].midi - self.distinctNotes[0].midi
45 if ambitus <= 12 :
46 self.keyboardLength = 8
47 else :
48 self.keyboardLength = 11
49
50 screen = pygame.display.get_surface()
51
52 # taille de la zone d'affichage utile (bordure autour)
53 dispWidth = screen.get_width() - 2 * BORDER
54 dispHeight = screen.get_height() - 2 * BORDER
55
56 columnWidth = int(round(float(dispWidth) / self.keyboardLength))
57
58 rects = []
59 for i in range(self.keyboardLength) :
60 upperLeftCorner = (i*columnWidth + BORDER, BORDER)
61 rect = pygame.Rect(upperLeftCorner, (columnWidth, dispHeight))
62 rects.append(rect)
63
64 self.keyboardRects = rects
65
66 def _initColumns(self) :
67
68 hueStep = FIRST_HUE / (self.keyboardLength - 1)
69 for i, rect in enumerate(self.keyboardRects) :
70 hue = FIRST_HUE - hueStep * i
71 print hue
72 c = Column(hue, rect)
73 self.add(c)
74
75
76 def highlightColumn(self, index) :
77 for i, sprite in enumerate(self.sprites()) :
78 sprite.update(i==index)
79 self.draw(pygame.display.get_surface())
80
81 def run(self):
82 self._running = True
83 while self._running :
84 pygame.display.flip()
85 events = pygame.event.get()
86 for event in events:
87 self.input(event)
88
89
90 class SongPlayingScreen(_PlayingScreenBase) :
91
92 def __init__(self, song) :
93 super(SongPlayingScreen, self).__init__(song.distinctNotes)
94 self.song = song
95
96 class SongPlayingScreenTest(_PlayingScreenBase) :
97 def __init__(self) :
98 class C:pass
99 o = C()
100 o.midi=1
101 super(SongPlayingScreenTest, self).__init__([o])
102
103 def input(self, event) :
104 if event.type == pygame.KEYDOWN:
105 if event.key == pygame.K_q:
106 self._running = False
107 uni = event.unicode
108
109 if uni.isdigit() and int(uni) <=8 :
110 self.highlightColumn(int(uni))
111
112
113 class Column(pygame.sprite.Sprite) :
114
115 def __init__(self, hue, rect) :
116 pygame.sprite.Sprite.__init__(self)
117 sur = pygame.surface.Surface(rect.size)
118 rgba = hls_to_rgba_8bits(hue, OFF_LUMINANCE, OFF_SATURATION)
119 sur.fill(rgba)
120 self.stateOff = sur
121 self.rectOff = rect
122
123 topRgba = hls_to_rgba_8bits(hue, ON_TOP_LUMINANCE, ON_SATURATION)
124 bottomRgba = hls_to_rgba_8bits(hue, ON_BOTTOM_LUMINANCE, ON_SATURATION)
125 rectOn = rect.inflate(ON_COLUMN_OVERSIZING * rect.width, 0)
126 self.stateOn = gradients.vertical(rectOn.size, topRgba, bottomRgba)
127 self.rectOn = rectOn
128
129 self.image = self.stateOff
130 self.rect = rect
131
132 def update(self, state) :
133 if state :
134 self.image = self.stateOn
135 self.rect = self.rectOn
136 else :
137 self.image = self.stateOff
138 self.rect = self.rectOff
139
140 def hls_to_rgba_8bits(h, l, s) :
141 #convert to rgb ranging from 0 to 255
142 rgba = [floor(255 * i) for i in hls_to_rgb(h, l, s) + (1,)]
143 return tuple(rgba)
144