2 Created on 15 juil. 2009
4 @author: Samuel Benveniste
13 from numpy
import array
14 from numpy
.linalg
import norm
16 from math
import floor
18 from gui
.constants
import *
19 from PlayingScreen
import PlayingScreen
20 from instruments
.Instrument
import Instrument
21 from cursor
.WarpingCursor
import *
22 from controllers
.Wiimote
import Wiimote
23 from logging
.EventLog
import EventLog
24 from logging
.PickleableEvent
import PickleableEvent
26 class InstrumentChoice
:
28 The screen for choosing instruments
31 The available instruments
33 The wiimotes used in this session
35 The main display window
37 The main display surface
39 The clock used to animate the screen
41 The background that is painted every time
43 The buffer for painting everything before bliting
45 The width of the window in pixels
47 The height of the window in pixels
49 Goes to True when all instruments have been selected
51 The positions of the cursors on the screen, in pixels
53 The rectangles where the images of the instruments are located
55 The numbers of the instruments currently in focus
58 def __init__(self
, instruments
, wiimotes
, window
, screen
, clock
, joys
, portOffset
, activeWiimotes
, eventLog
=None, replay
= False, logFilePath
= None, scaleFactor
= 1):
63 The instruments for this session
65 The wiimotes used in this session
67 self
.scaleFactor
= scaleFactor
69 self
.instruments
= instruments
70 self
.wiimotes
= wiimotes
74 self
.width
= int(floor(screen
.get_width()*self
.scaleFactor
))
75 self
.height
= int(floor(screen
.get_height()*self
.scaleFactor
))
76 self
.blitOrigin
= ((self
.screen
.get_width()-self
.width
)/2,(self
.screen
.get_height()-self
.height
)/2)
78 self
.portOffset
= portOffset
80 self
.activeWiimotes
= activeWiimotes
82 self
.currentWiimote
= 0
83 while not self
.activeWiimotes
[self
.currentWiimote
] :
84 self
.currentWiimote
+= 1
87 self
.cursorPositions
= []
89 self
.savedImageRects
= []
94 self
.eventLog
= EventLog()
97 self
.eventLog
= eventLog
101 #There are 3 instruments per row, up to 9 instruments
102 #Draw their images on the screen
103 self
.savedScreen
= pygame
.Surface(self
.screen
.get_size())
104 self
.savedScreen
.fill((255, 255, 255))
105 for i
in range(len(self
.instruments
)) :
106 drawPos
= array([(self
.width
/ 3) * (i
% 3), (self
.height
/ 3) * (i
/ 3)])
107 curImage
= pygame
.image
.load(self
.instruments
[i
].image
).convert_alpha()
108 scaledImage
= pygame
.transform
.smoothscale(curImage
, (self
.width
/ 3, self
.height
/ 3))
109 self
.imageRects
.append(self
.savedScreen
.blit(scaledImage
, drawPos
+ self
.blitOrigin
))
110 self
.savedImageRects
= self
.imageRects
[:]
111 #Draw the initial cursor on the buffer
112 self
.playerScreen
= pygame
.Surface(self
.savedScreen
.get_size())
113 self
.playerScreen
.blit(self
.savedScreen
, (0, 0))
115 for i
in range(len(self
.wiimotes
)):
116 #Create the list of instrument focus (one focus per wiimote)
118 self
.zoomed
.append(None)
119 #Set the screen for the cursors (it can't be set before)
120 self
.wiimotes
[i
].cursor
.screen
= self
.playerScreen
121 self
.cursorPositions
.append(self
.wiimotes
[i
].cursor
.centerPosition
)
123 self
.wiimotes
[self
.currentWiimote
].cursor
.blit(self
.playerScreen
)
126 while self
.done
== False :
128 #Clear the cursors from the screen
129 self
.playerScreen
.blit(self
.savedScreen
, (0, 0))
131 # Limit frame speed to 50 FPS
133 timePassed
= self
.clock
.tick(50)
136 self
.eventLog
.update(timePassed
)
137 pickledEventsToPost
= self
.eventLog
.getPickledEvents()
138 for pickledEvent
in pickledEventsToPost
:
139 pygame
.event
.post(pickledEvent
.event
)
141 events
= pygame
.event
.get()
144 pickledEvents
= [PickleableEvent(event
.type,event
.dict) for event
in events
if self
.eventFilter(event
)]
145 if pickledEvents
!= [] :
146 self
.eventLog
.appendEventGroup(pickledEvents
)
152 if self
.zoomed
[self
.currentWiimote
] != None :
153 self
.imageRects
= self
.savedImageRects
[:]
154 #inflate the chosen rect
155 zoomedRectNumber
= self
.zoomed
[self
.currentWiimote
]
156 newRect
= zoomRect(self
.imageRects
[zoomedRectNumber
], 1.3)
157 self
.imageRects
[zoomedRectNumber
] = newRect
158 curImage
= pygame
.image
.load(self
.instruments
[zoomedRectNumber
].image
).convert_alpha()
159 self
.scaledImage
= pygame
.transform
.smoothscale(curImage
, newRect
.size
)
160 self
.playerScreen
.blit(self
.scaledImage
, newRect
.topleft
)
162 for i
in range(len(self
.wiimotes
)):
163 self
.wiimotes
[i
].cursor
.update(timePassed
, self
.cursorPositions
[i
])
165 self
.wiimotes
[self
.currentWiimote
].cursor
.blit(self
.playerScreen
)
167 if self
.zoomed
[self
.currentWiimote
] != None and self
.imageRects
[self
.zoomed
[self
.currentWiimote
]].collidepoint(self
.cursorPositions
[self
.currentWiimote
]):
168 self
.focus
[self
.currentWiimote
] = self
.zoomed
[self
.currentWiimote
]
169 pygame
.draw
.rect(self
.playerScreen
, pygame
.Color(0, 255, 0, 255), self
.imageRects
[self
.zoomed
[self
.currentWiimote
]], 10)
171 for i
in range(len(self
.imageRects
)) :
172 if self
.imageRects
[i
].collidepoint(self
.cursorPositions
[self
.currentWiimote
]):
173 self
.focus
[self
.currentWiimote
] = i
174 pygame
.draw
.rect(self
.playerScreen
, pygame
.Color(0, 255, 0, 255), self
.imageRects
[i
], 10)
175 if self
.zoomed
[self
.currentWiimote
] != None:
176 self
.playerScreen
.blit(self
.scaledImage
, self
.imageRects
[self
.zoomed
[self
.currentWiimote
]].topleft
)
178 self
.screen
.blit(self
.playerScreen
, (0, 0))
180 pygame
.display
.flip()
182 def input(self
, event
):
183 if event
.type == pygame
.QUIT
:
186 if event
.type == pygame
.JOYAXISMOTION
:
187 self
.updateCursorPositionFromJoy(event
)
188 if event
.type == pygame
.JOYBUTTONDOWN
:
189 self
.assignInstrumentToWiimote(event
)
190 if event
.type == pygame
.MOUSEBUTTONDOWN
:
191 if self
.zoomed
[self
.currentWiimote
] == self
.focus
[self
.currentWiimote
]:
192 self
.assignInstrumentToMouse(event
)
194 self
.zoomed
[self
.currentWiimote
] = self
.focus
[self
.currentWiimote
]
195 if event
.type == pygame
.MOUSEMOTION
:
196 self
.updateCursorPositionFromMouse(event
)
198 def updateCursorPositionFromJoy(self
, joyEvent
):
199 joyName
= pygame
.joystick
.Joystick(joyEvent
.joy
).get_name()
201 correctedJoyId
= joyNames
.index(joyName
)
202 if self
.activeWiimotes
[correctedJoyId
]:
203 if correctedJoyId
< len(self
.cursorPositions
):
204 if joyEvent
.axis
== 0 :
205 self
.cursorPositions
[correctedJoyId
] = (int((joyEvent
.value
+ 1) / 2 * self
.screen
.get_width()), self
.cursorPositions
[correctedJoyId
][1])
206 if joyEvent
.axis
== 1 :
207 self
.cursorPositions
[correctedJoyId
] = (self
.cursorPositions
[correctedJoyId
][0], int((joyEvent
.value
+ 1) / 2 * self
.screen
.get_height()))
209 def assignInstrumentToWiimote(self
, joyEvent
):
210 joyName
= pygame
.joystick
.Joystick(joyEvent
.joy
).get_name()
211 correctedJoyId
= joyNames
.index(joyName
)
212 if self
.activeWiimotes
[correctedJoyId
]:
213 if self
.zoomed
[correctedJoyId
] == self
.focus
[correctedJoyId
]:
214 self
.wiimotes
[correctedJoyId
].instrument
= self
.instruments
[self
.focus
[correctedJoyId
]]
215 self
.zoomed
[correctedJoyId
] = None
216 self
.imageRects
= self
.savedImageRects
[:]
217 if self
.currentWiimote
<len(self
.wiimotes
)-1:
218 self
.currentWiimote
= self
.currentWiimote
+1
220 self
.zoomed
[correctedJoyId
] = self
.focus
[correctedJoyId
]
221 if self
.hasFinished():
224 def updateCursorPositionFromMouse(self
, mouseEvent
):
226 while not self
.activeWiimotes
[correctedJoyId
] :
228 self
.cursorPositions
[correctedJoyId
] = mouseEvent
.pos
230 def assignInstrumentToMouse(self
, mouseEvent
):
232 while not self
.activeWiimotes
[correctedJoyId
] :
234 self
.wiimotes
[correctedJoyId
].instrument
= self
.instruments
[self
.focus
[correctedJoyId
]]
235 if self
.hasFinished():
238 def hasFinished(self
):
240 for i
in range(len(self
.wiimotes
)):
241 if self
.wiimotes
[i
].instrument
== None and self
.activeWiimotes
[i
]:
245 def eventFilter(self
, event
):
249 elif c
== pygame
.MOUSEMOTION
or pygame
.MOUSEBUTTONDOWN
or pygame
.MOUSEBUTTONUP
or pygame
.JOYAXISMOTION
or pygame
.JOYBUTTONDOWN
or pygame
.JOYBUTTONUP
or pygame
.KEYDOWN
:
254 def zoomRect(rect
, ratio
):
255 zoomedRect
= rect
.inflate(int(floor((ratio
- 1) * rect
.width
)), int(floor((ratio
- 1) * rect
.height
)))
258 if __name__
== "__main__":
260 #pygame.event.set_blocked([pygame.MOUSEBUTTONDOWN,pygame.MOUSEBUTTONUP,pygame.MOUSEMOTION])
263 instruments
= [Instrument(majorScale
, i
+ 1, "".join(["../instruments/instrumentImages/", instrumentImagePathList
[i
], ".jpg"]), octaves
[i
]) for i
in range(9)]
265 joys
= [pygame
.joystick
.Joystick(id).get_name() for id in range(pygame
.joystick
.get_count())]
266 joyOffset
= joys
.index(joyNames
[0])
267 pygame
.joystick
.Joystick(joyOffset
).init()
270 ports
= [pygame
.midi
.get_device_info(id)[1] for id in range(pygame
.midi
.get_count())]
271 portOffset
= ports
.index(portNames
[0])
274 window
= pygame
.display
.set_mode((1280, 1024),pygame
.FULLSCREEN
)
275 screen
= pygame
.display
.get_surface()
276 clock
= pygame
.time
.Clock()
277 cursorImages
= createImageListFromPath('../cursor/cursorImages/black', 11)
278 durations
= [75 for i
in range(len(cursorImages
))]
283 jadbt
= [3, 4, 5, 3, 4, 4, 5, 6, 6, 5, 3, 3, 4, 5, 3, 4, 4, 5, 6, 7, 3]
286 cursors
= [WarpingCursor(None, cursorImages
, durations
, (300 * i
, 300 * i
)) for i
in range(1)]
287 wiimotes
= [Wiimote(i
, i
+ portOffset
, None, None, cursors
[i
]) for i
in range(1)]
288 choice
= InstrumentChoice(instruments
, wiimotes
, window
, screen
, clock
, joyOffset
, portOffset
)
289 play
= PlayingScreen(choice
, song
, casc
, extsc
)
290 for wiimote
in wiimotes
:
293 f
= file('temp.pkl', 'w')
294 pickler
= pickle
.Pickler(f
)
295 pickler
.dump(play
.eventLog
.eventGroups
)
296 pickler
.dump(play
.eventLog
.times
)
299 f
= file('temp.pkl', 'r')
300 unpickler
= pickle
.Unpickler(f
)
301 eventGroups
= unpickler
.load()
302 times
= unpickler
.load()
304 eventLog
= EventLog(eventGroups
,times
)
306 cursors
= [WarpingCursor(None, cursorImages
, durations
, (300 * i
, 300 * i
)) for i
in range(1)]
307 wiimotes
= [Wiimote(i
, i
+ portOffset
, None, None, cursors
[i
]) for i
in range(1)]
308 choice2
= InstrumentChoice(instruments
, wiimotes
, window
, screen
, clock
, joyOffset
, portOffset
, eventLog
)
309 play
= PlayingScreen(choice2
, song
, casc
, extsc
)
311 for wiimote
in wiimotes
: