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