X-Git-Url: https://scm.cri.ensmp.fr/git/minwii.git/blobdiff_plain/346a9b8e1fcfe30629f0d1ee4675e9e8f89890cf..4c4732c6ed8cb0aaa70fb2d4c6e5a958868c5349:/src/gui/InstrumentChoice.py diff --git a/src/gui/InstrumentChoice.py b/src/gui/InstrumentChoice.py deleted file mode 100755 index d5f073e..0000000 --- a/src/gui/InstrumentChoice.py +++ /dev/null @@ -1,315 +0,0 @@ -''' -Created on 15 juil. 2009 - -@author: Samuel Benveniste -''' - -import pygame -import pygame.midi -import sys -import time -import pickle - -from numpy import array -from numpy.linalg import norm - -from math import floor - -from gui.constants import * -from PlayingScreen import PlayingScreen -from instruments.Instrument import Instrument -from cursor.WarpingCursor import * -from controllers.Wiimote import Wiimote -from logging.EventLog import EventLog -from logging.PickleableEvent import PickleableEvent - -class InstrumentChoice: - ''' - The screen for choosing instruments - - instruments: - The available instruments - wiimotes: - The wiimotes used in this session - window: - The main display window - screen: - The main display surface - clock: - The clock used to animate the screen - savedScreen: - The background that is painted every time - playerScreen: - The buffer for painting everything before bliting - width: - The width of the window in pixels - height: - The height of the window in pixels - done: - Goes to True when all instruments have been selected - cursorPositions: - The positions of the cursors on the screen, in pixels - imageRects: - The rectangles where the images of the instruments are located - focus: - The numbers of the instruments currently in focus - ''' - - def __init__(self, instruments, wiimotes, window, screen, clock, joys, portOffset, activeWiimotes, eventLog=None, replay = False, logFilePath = None, scaleFactor = 1): - ''' - Constructor - - instruments: - The instruments for this session - wiimotes: - The wiimotes used in this session - ''' - self.scaleFactor = scaleFactor - - self.instruments = instruments - self.wiimotes = wiimotes - self.window = window - self.screen = screen - self.clock = clock - self.width = int(floor(screen.get_width()*self.scaleFactor)) - self.height = int(floor(screen.get_height()*self.scaleFactor)) - self.blitOrigin = ((self.screen.get_width()-self.width)/2,(self.screen.get_height()-self.height)/2) - self.joys = joys - self.portOffset = portOffset - - self.activeWiimotes = activeWiimotes - - self.currentWiimote = 0 - while not self.activeWiimotes[self.currentWiimote] : - self.currentWiimote += 1 - self.done = False - - self.cursorPositions = [] - self.imageRects = [] - self.savedImageRects = [] - self.focus = [] - self.zoomed = [] - - if eventLog == None: - self.eventLog = EventLog() - self.replay = False - else: - self.eventLog = eventLog - self.replay = replay - print self.replay - - #There are 3 instruments per row, up to 9 instruments - #Draw their images on the screen - self.savedScreen = pygame.Surface(self.screen.get_size()) - self.savedScreen.fill((255, 255, 255)) - for i in range(len(self.instruments)) : - drawPos = array([(self.width / 3) * (i % 3), (self.height / 3) * (i / 3)]) - curImage = pygame.image.load(self.instruments[i].image).convert_alpha() - scaledImage = pygame.transform.smoothscale(curImage, (self.width / 3, self.height / 3)) - self.imageRects.append(self.savedScreen.blit(scaledImage, drawPos + self.blitOrigin)) - self.savedImageRects = self.imageRects[:] - #Draw the initial cursor on the buffer - self.playerScreen = pygame.Surface(self.savedScreen.get_size()) - self.playerScreen.blit(self.savedScreen, (0, 0)) - - for i in range(len(self.wiimotes)): - #Create the list of instrument focus (one focus per wiimote) - self.focus.append(0) - self.zoomed.append(None) - #Set the screen for the cursors (it can't be set before) - self.wiimotes[i].cursor.screen = self.playerScreen - self.cursorPositions.append(self.wiimotes[i].cursor.centerPosition) - - self.wiimotes[self.currentWiimote].cursor.blit(self.playerScreen) - - #The main loop - while self.done == False : - - #Clear the cursors from the screen - self.playerScreen.blit(self.savedScreen, (0, 0)) - - # Limit frame speed to 50 FPS - # - timePassed = self.clock.tick(50) - - if self.replay: - self.eventLog.update(timePassed) - pickledEventsToPost = self.eventLog.getPickledEvents() - for pickledEvent in pickledEventsToPost: - pygame.event.post(pickledEvent.event) - - events = pygame.event.get() - - if not self.replay: - pickledEvents = [PickleableEvent(event.type,event.dict) for event in events if self.eventFilter(event)] - if pickledEvents != [] : - self.eventLog.appendEventGroup(pickledEvents) - - for event in events: - self.input(event) - - - if self.zoomed[self.currentWiimote] != None : - self.imageRects = self.savedImageRects[:] - #inflate the chosen rect - zoomedRectNumber = self.zoomed[self.currentWiimote] - newRect = zoomRect(self.imageRects[zoomedRectNumber], 1.3) - self.imageRects[zoomedRectNumber] = newRect - curImage = pygame.image.load(self.instruments[zoomedRectNumber].image).convert_alpha() - self.scaledImage = pygame.transform.smoothscale(curImage, newRect.size) - self.playerScreen.blit(self.scaledImage, newRect.topleft) - - for i in range(len(self.wiimotes)): - self.wiimotes[i].cursor.update(timePassed, self.cursorPositions[i]) - - self.wiimotes[self.currentWiimote].cursor.blit(self.playerScreen) - - if self.zoomed[self.currentWiimote] != None and self.imageRects[self.zoomed[self.currentWiimote]].collidepoint(self.cursorPositions[self.currentWiimote]): - self.focus[self.currentWiimote] = self.zoomed[self.currentWiimote] - pygame.draw.rect(self.playerScreen, pygame.Color(0, 255, 0, 255), self.imageRects[self.zoomed[self.currentWiimote]], 10) - else: - for i in range(len(self.imageRects)) : - if self.imageRects[i].collidepoint(self.cursorPositions[self.currentWiimote]): - self.focus[self.currentWiimote] = i - pygame.draw.rect(self.playerScreen, pygame.Color(0, 255, 0, 255), self.imageRects[i], 10) - if self.zoomed[self.currentWiimote] != None: - self.playerScreen.blit(self.scaledImage, self.imageRects[self.zoomed[self.currentWiimote]].topleft) - - self.screen.blit(self.playerScreen, (0, 0)) - - pygame.display.flip() - - def input(self, event): - if event.type == pygame.QUIT: - pygame.midi.quit() - sys.exit() - if event.type == pygame.JOYAXISMOTION: - self.updateCursorPositionFromJoy(event) - if event.type == pygame.JOYBUTTONDOWN : - self.assignInstrumentToWiimote(event) - if event.type == pygame.MOUSEBUTTONDOWN: - if self.zoomed[self.currentWiimote] == self.focus[self.currentWiimote]: - self.assignInstrumentToMouse(event) - else: - self.zoomed[self.currentWiimote] = self.focus[self.currentWiimote] - if event.type == pygame.MOUSEMOTION: - self.updateCursorPositionFromMouse(event) - - def updateCursorPositionFromJoy(self, joyEvent): - joyName = pygame.joystick.Joystick(joyEvent.joy).get_name() - print joyName - correctedJoyId = joyNames.index(joyName) - if self.activeWiimotes[correctedJoyId]: - if correctedJoyId < len(self.cursorPositions): - if joyEvent.axis == 0 : - self.cursorPositions[correctedJoyId] = (int((joyEvent.value + 1) / 2 * self.screen.get_width()), self.cursorPositions[correctedJoyId][1]) - if joyEvent.axis == 1 : - self.cursorPositions[correctedJoyId] = (self.cursorPositions[correctedJoyId][0], int((joyEvent.value + 1) / 2 * self.screen.get_height())) - - def assignInstrumentToWiimote(self, joyEvent): - joyName = pygame.joystick.Joystick(joyEvent.joy).get_name() - correctedJoyId = joyNames.index(joyName) - if self.activeWiimotes[correctedJoyId]: - if self.zoomed[correctedJoyId] == self.focus[correctedJoyId]: - self.wiimotes[correctedJoyId].instrument = self.instruments[self.focus[correctedJoyId]] - self.zoomed[correctedJoyId] = None - self.imageRects = self.savedImageRects[:] - if self.currentWiimote