-'''\r
-Created on 15 juil. 2009\r
-\r
-@author: Samuel Benveniste\r
-'''\r
-\r
-import pygame\r
-import pygame.midi\r
-import sys\r
-import time\r
-import pickle\r
-\r
-from numpy import array\r
-from numpy.linalg import norm\r
-\r
-from math import floor\r
-\r
-from gui.constants import *\r
-from PlayingScreen import PlayingScreen\r
-from instruments.Instrument import Instrument\r
-from cursor.WarpingCursor import *\r
-from controllers.Wiimote import Wiimote\r
-from logging.EventLog import EventLog\r
-from logging.PickleableEvent import PickleableEvent \r
-\r
-class InstrumentChoice:\r
- '''\r
- The screen for choosing instruments\r
- \r
- instruments: \r
- The available instruments \r
- wiimotes: \r
- The wiimotes used in this session\r
- window:\r
- The main display window\r
- screen:\r
- The main display surface\r
- clock:\r
- The clock used to animate the screen\r
- savedScreen:\r
- The background that is painted every time\r
- playerScreen:\r
- The buffer for painting everything before bliting\r
- width:\r
- The width of the window in pixels\r
- height:\r
- The height of the window in pixels\r
- done:\r
- Goes to True when all instruments have been selected\r
- cursorPositions:\r
- The positions of the cursors on the screen, in pixels\r
- imageRects:\r
- The rectangles where the images of the instruments are located\r
- focus:\r
- The numbers of the instruments currently in focus\r
- '''\r
- \r
- def __init__(self, instruments, wiimotes, window, screen, clock, joys, portOffset, activeWiimotes, eventLog=None, replay = False, logFilePath = None, scaleFactor = 1):\r
- '''\r
- Constructor\r
- \r
- instruments: \r
- The instruments for this session \r
- wiimotes: \r
- The wiimotes used in this session\r
- '''\r
- self.scaleFactor = scaleFactor\r
- \r
- self.instruments = instruments\r
- self.wiimotes = wiimotes\r
- self.window = window\r
- self.screen = screen\r
- self.clock = clock\r
- self.width = int(floor(screen.get_width()*self.scaleFactor))\r
- self.height = int(floor(screen.get_height()*self.scaleFactor))\r
- self.blitOrigin = ((self.screen.get_width()-self.width)/2,(self.screen.get_height()-self.height)/2) \r
- self.joys = joys\r
- self.portOffset = portOffset\r
- \r
- self.activeWiimotes = activeWiimotes\r
- \r
- self.currentWiimote = 0\r
- while not self.activeWiimotes[self.currentWiimote] :\r
- self.currentWiimote += 1\r
- self.done = False \r
- \r
- self.cursorPositions = []\r
- self.imageRects = []\r
- self.savedImageRects = []\r
- self.focus = []\r
- self.zoomed = []\r
- \r
- if eventLog == None:\r
- self.eventLog = EventLog()\r
- self.replay = False\r
- else:\r
- self.eventLog = eventLog\r
- self.replay = replay\r
- print self.replay\r
- \r
- #There are 3 instruments per row, up to 9 instruments\r
- #Draw their images on the screen\r
- self.savedScreen = pygame.Surface(self.screen.get_size())\r
- self.savedScreen.fill((255, 255, 255))\r
- for i in range(len(self.instruments)) :\r
- drawPos = array([(self.width / 3) * (i % 3), (self.height / 3) * (i / 3)])\r
- curImage = pygame.image.load(self.instruments[i].image).convert_alpha()\r
- scaledImage = pygame.transform.smoothscale(curImage, (self.width / 3, self.height / 3))\r
- self.imageRects.append(self.savedScreen.blit(scaledImage, drawPos + self.blitOrigin))\r
- self.savedImageRects = self.imageRects[:]\r
- #Draw the initial cursor on the buffer\r
- self.playerScreen = pygame.Surface(self.savedScreen.get_size())\r
- self.playerScreen.blit(self.savedScreen, (0, 0))\r
- \r
- for i in range(len(self.wiimotes)):\r
- #Create the list of instrument focus (one focus per wiimote)\r
- self.focus.append(0)\r
- self.zoomed.append(None)\r
- #Set the screen for the cursors (it can't be set before)\r
- self.wiimotes[i].cursor.screen = self.playerScreen\r
- self.cursorPositions.append(self.wiimotes[i].cursor.centerPosition)\r
- \r
- self.wiimotes[self.currentWiimote].cursor.blit(self.playerScreen)\r
- \r
- #The main loop\r
- while self.done == False :\r
- \r
- #Clear the cursors from the screen\r
- self.playerScreen.blit(self.savedScreen, (0, 0))\r
- \r
- # Limit frame speed to 50 FPS\r
- #\r
- timePassed = self.clock.tick(50)\r
- \r
- if self.replay:\r
- self.eventLog.update(timePassed)\r
- pickledEventsToPost = self.eventLog.getPickledEvents() \r
- for pickledEvent in pickledEventsToPost:\r
- pygame.event.post(pickledEvent.event)\r
- \r
- events = pygame.event.get()\r
- \r
- if not self.replay:\r
- pickledEvents = [PickleableEvent(event.type,event.dict) for event in events if self.eventFilter(event)]\r
- if pickledEvents != [] :\r
- self.eventLog.appendEventGroup(pickledEvents)\r
- \r
- for event in events:\r
- self.input(event)\r
- \r
- \r
- if self.zoomed[self.currentWiimote] != None :\r
- self.imageRects = self.savedImageRects[:]\r
- #inflate the chosen rect\r
- zoomedRectNumber = self.zoomed[self.currentWiimote]\r
- newRect = zoomRect(self.imageRects[zoomedRectNumber], 1.3)\r
- self.imageRects[zoomedRectNumber] = newRect\r
- curImage = pygame.image.load(self.instruments[zoomedRectNumber].image).convert_alpha()\r
- self.scaledImage = pygame.transform.smoothscale(curImage, newRect.size)\r
- self.playerScreen.blit(self.scaledImage, newRect.topleft)\r
- \r
- for i in range(len(self.wiimotes)):\r
- self.wiimotes[i].cursor.update(timePassed, self.cursorPositions[i])\r
- \r
- self.wiimotes[self.currentWiimote].cursor.blit(self.playerScreen) \r
- \r
- if self.zoomed[self.currentWiimote] != None and self.imageRects[self.zoomed[self.currentWiimote]].collidepoint(self.cursorPositions[self.currentWiimote]):\r
- self.focus[self.currentWiimote] = self.zoomed[self.currentWiimote]\r
- pygame.draw.rect(self.playerScreen, pygame.Color(0, 255, 0, 255), self.imageRects[self.zoomed[self.currentWiimote]], 10)\r
- else:\r
- for i in range(len(self.imageRects)) :\r
- if self.imageRects[i].collidepoint(self.cursorPositions[self.currentWiimote]):\r
- self.focus[self.currentWiimote] = i\r
- pygame.draw.rect(self.playerScreen, pygame.Color(0, 255, 0, 255), self.imageRects[i], 10)\r
- if self.zoomed[self.currentWiimote] != None:\r
- self.playerScreen.blit(self.scaledImage, self.imageRects[self.zoomed[self.currentWiimote]].topleft)\r
- \r
- self.screen.blit(self.playerScreen, (0, 0))\r
- \r
- pygame.display.flip()\r
- \r
- def input(self, event):\r
- if event.type == pygame.QUIT:\r
- pygame.midi.quit()\r
- sys.exit()\r
- if event.type == pygame.JOYAXISMOTION:\r
- self.updateCursorPositionFromJoy(event)\r
- if event.type == pygame.JOYBUTTONDOWN :\r
- self.assignInstrumentToWiimote(event)\r
- if event.type == pygame.MOUSEBUTTONDOWN:\r
- if self.zoomed[self.currentWiimote] == self.focus[self.currentWiimote]:\r
- self.assignInstrumentToMouse(event)\r
- else:\r
- self.zoomed[self.currentWiimote] = self.focus[self.currentWiimote]\r
- if event.type == pygame.MOUSEMOTION:\r
- self.updateCursorPositionFromMouse(event) \r
- \r
- def updateCursorPositionFromJoy(self, joyEvent):\r
- joyName = pygame.joystick.Joystick(joyEvent.joy).get_name()\r
- print joyName\r
- correctedJoyId = joyNames.index(joyName)\r
- if self.activeWiimotes[correctedJoyId]: \r
- if correctedJoyId < len(self.cursorPositions):\r
- if joyEvent.axis == 0 :\r
- self.cursorPositions[correctedJoyId] = (int((joyEvent.value + 1) / 2 * self.screen.get_width()), self.cursorPositions[correctedJoyId][1])\r
- if joyEvent.axis == 1 :\r
- self.cursorPositions[correctedJoyId] = (self.cursorPositions[correctedJoyId][0], int((joyEvent.value + 1) / 2 * self.screen.get_height())) \r
- \r
- def assignInstrumentToWiimote(self, joyEvent):\r
- joyName = pygame.joystick.Joystick(joyEvent.joy).get_name()\r
- correctedJoyId = joyNames.index(joyName)\r
- if self.activeWiimotes[correctedJoyId]:\r
- if self.zoomed[correctedJoyId] == self.focus[correctedJoyId]:\r
- self.wiimotes[correctedJoyId].instrument = self.instruments[self.focus[correctedJoyId]]\r
- self.zoomed[correctedJoyId] = None\r
- self.imageRects = self.savedImageRects[:]\r
- if self.currentWiimote<len(self.wiimotes)-1:\r
- self.currentWiimote = self.currentWiimote+1\r
- else:\r
- self.zoomed[correctedJoyId] = self.focus[correctedJoyId]\r
- if self.hasFinished():\r
- self.done = True\r
- \r
- def updateCursorPositionFromMouse(self, mouseEvent):\r
- correctedJoyId = 0\r
- while not self.activeWiimotes[correctedJoyId] :\r
- correctedJoyId += 1\r
- self.cursorPositions[correctedJoyId] = mouseEvent.pos\r
- \r
- def assignInstrumentToMouse(self, mouseEvent):\r
- correctedJoyId = 0\r
- while not self.activeWiimotes[correctedJoyId] :\r
- correctedJoyId += 1\r
- self.wiimotes[correctedJoyId].instrument = self.instruments[self.focus[correctedJoyId]]\r
- if self.hasFinished():\r
- self.done = True\r
- \r
- def hasFinished(self):\r
- finished = True\r
- for i in range(len(self.wiimotes)):\r
- if self.wiimotes[i].instrument == None and self.activeWiimotes[i]:\r
- finished = False\r
- return(finished)\r
- \r
- def eventFilter(self, event):\r
- c = event.type\r
- if c == 17:\r
- return False\r
- elif c == pygame.MOUSEMOTION or pygame.MOUSEBUTTONDOWN or pygame.MOUSEBUTTONUP or pygame.JOYAXISMOTION or pygame.JOYBUTTONDOWN or pygame.JOYBUTTONUP or pygame.KEYDOWN:\r
- return True\r
- else:\r
- return False\r
-\r
-def zoomRect(rect, ratio):\r
- zoomedRect = rect.inflate(int(floor((ratio - 1) * rect.width)), int(floor((ratio - 1) * rect.height)))\r
- return(zoomedRect) \r
-\r
-if __name__ == "__main__":\r
- pygame.init()\r
- #pygame.event.set_blocked([pygame.MOUSEBUTTONDOWN,pygame.MOUSEBUTTONUP,pygame.MOUSEMOTION])\r
- \r
- pygame.midi.init()\r
- instruments = [Instrument(majorScale, i + 1, "".join(["../instruments/instrumentImages/", instrumentImagePathList[i], ".jpg"]), octaves[i]) for i in range(9)]\r
- \r
- joys = [pygame.joystick.Joystick(id).get_name() for id in range(pygame.joystick.get_count())]\r
- joyOffset = joys.index(joyNames[0])\r
- pygame.joystick.Joystick(joyOffset).init()\r
- print(joyOffset) \r
- \r
- ports = [pygame.midi.get_device_info(id)[1] for id in range(pygame.midi.get_count())]\r
- portOffset = ports.index(portNames[0])\r
- print(portOffset)\r
- \r
- window = pygame.display.set_mode((1280, 1024),pygame.FULLSCREEN)\r
- screen = pygame.display.get_surface()\r
- clock = pygame.time.Clock() \r
- cursorImages = createImageListFromPath('../cursor/cursorImages/black', 11)\r
- durations = [75 for i in range(len(cursorImages))]\r
- \r
- extsc = False\r
- casc = False\r
- \r
- jadbt = [3, 4, 5, 3, 4, 4, 5, 6, 6, 5, 3, 3, 4, 5, 3, 4, 4, 5, 6, 7, 3]\r
- song = None\r
- \r
- cursors = [WarpingCursor(None, cursorImages, durations, (300 * i, 300 * i)) for i in range(1)]\r
- wiimotes = [Wiimote(i, i + portOffset, None, None, cursors[i]) for i in range(1)]\r
- choice = InstrumentChoice(instruments, wiimotes, window, screen, clock, joyOffset, portOffset)\r
- play = PlayingScreen(choice, song, casc, extsc)\r
- for wiimote in wiimotes:\r
- del wiimote.port \r
- \r
- f = file('temp.pkl', 'w')\r
- pickler = pickle.Pickler(f)\r
- pickler.dump(play.eventLog.eventGroups)\r
- pickler.dump(play.eventLog.times)\r
- f.close()\r
- \r
- f = file('temp.pkl', 'r')\r
- unpickler = pickle.Unpickler(f)\r
- eventGroups = unpickler.load()\r
- times = unpickler.load()\r
- f.close()\r
- eventLog = EventLog(eventGroups,times)\r
- \r
- cursors = [WarpingCursor(None, cursorImages, durations, (300 * i, 300 * i)) for i in range(1)]\r
- wiimotes = [Wiimote(i, i + portOffset, None, None, cursors[i]) for i in range(1)]\r
- choice2 = InstrumentChoice(instruments, wiimotes, window, screen, clock, joyOffset, portOffset, eventLog)\r
- play = PlayingScreen(choice2, song, casc, extsc)\r
- \r
- for wiimote in wiimotes:\r
- del wiimote.port\r
- \r
- pygame.midi.quit()\r
- sys.exit()\r