import Sort from eduGames import * import os class MouseAction: def __init__(self, command): self.type = command[0].upper() if self.type == "MOVE": x = int(command[1]) y = int(command[2]) self.params = (x,y) elif self.type == "WAIT": self.params = int(command[1]) class MousePointer(ImageControl): (WAITING, MOVING, MOUSE_BUTTON_DOWN, WAITINGMOVES) = (0,1,2,3) def __init__(self, game, path): mousePointerImagePath = os.path.split(path)[0] mousePointerImagePath = os.path.join(mousePointerImagePath, "mousePointer.png") x = game.getScreen().get_width()//2 y = game.getScreen().get_height()//2 ImageControl.__init__(self, game, x, y, mousePointerImagePath, "", 1) def isClickable(self): return False class DemoEvent: def __init__(self, type, params): self.type = type self.params = params class DemoPlayer(Sort.Sort): def __init__(self, screen, uiMgr, sndMgr, screenMgr, path): Sort.Sort.__init__(self, screen, uiMgr, sndMgr, screenMgr, path) self.mousePointer = MousePointer(self, path) self.eventQueue = [] filepath = os.path.join(path, "demoscript.txt") self.demoIsProcessing = False self.actionsQueue = [] self.loadQueue(filepath) self.currentAction = None self.timer = 0 def loadQueue(self, filepath): self.actionsQueue = [] file = open(filepath, "r") text = file.readlines() for line in text: if line.strip() <> "": command = line.split() self.actionsQueue.append(MouseAction(command)) def showCurrentScreen(self): Sort.Sort.showCurrentScreen(self) self.getUiMgr().addControl(self.mousePointer) def advanceQueue(self): if not len(self.actionsQueue) == 0: action = self.actionsQueue.pop(0) if action.type == "MOVE": target = action.params self.mousePointer.startMovement(target, 7, LINEAR) self.currentAction = action self.eventQueue.append(DemoEvent("MOUSEMOTION", (self.mousePointer.getX(), self.mousePointer.getY()))) if action.type == "MOUSE_BUTTON_DOWN": self.eventQueue.append(DemoEvent("MOUSE_BUTTON_DOWN", (self.mousePointer.getX(), self.mousePointer.getY()))) if action.type == "WAITMOVS": self.currentAction = action if action.type == "WAIT": self.currentAction = action self.timer = action.params else: self.finished = True def updateState(self): Sort.Sort.updateState(self) if self.currentAction == None: self.advanceQueue() else: mousePointerMoving = self.mousePointer.isMoving() if mousePointerMoving: self.eventQueue.append(DemoEvent("MOUSEMOTION", (self.mousePointer.getX(), self.mousePointer.getY()))) if self.currentAction.type == "WAIT": self.timer = self.timer - 1 if self.timer == 0: self.currentAction = None elif self.currentAction.type == "MOVE": if not mousePointerMoving: self.currentAction = None elif self.currentAction.type == "WAITMOVS": if not self.somethingMoves(): self.currentAction = None self.processEvents() def somethingMoves(self): for control in self.getUiMgr().controls: if control.isMoving() and not control is self.mousePointer: return True return False def on_mouse_button_up(self, clickedControl): if self.demoIsProcessing: Sort.Sort.on_mouse_button_up(self, clickedControl) def on_mouse_button_down(self, clickedControl): if self.demoIsProcessing: Sort.Sort.on_mouse_button_down(self, clickedControl) def on_mouse_hover(self, clickedControl): #print clickedControl if self.demoIsProcessing: Sort.Sort.on_mouse_hover(self, clickedControl) def processEvents(self): self.demoIsProcessing = True if self.getSoundMgr().thereAreSoundsPlayingSynchronously(): pygame.mouse.set_cursor(*pygame.cursors.diamond) else: if len(self.eventQueue) > 0: event = self.eventQueue.pop(0) control = self.getAffectedControl() if event.type == "MOUSE_BUTTON_UP": if not control is None: self.on_mouse_button_up(control) if event.type == "MOUSE_BUTTON_DOWN": if not control is None: self.on_mouse_button_down(control) if event.type == "MOUSEMOTION": if not control is None: self.on_mouse_hover(control) self.demoIsProcessing = False def getAffectedControl(self): xMouse = self.mousePointer.getX() yMouse = self.mousePointer.getY() for control in self.getUiMgr().controls: if control.isClickable(): if control.collidesWithMousePosition(xMouse, yMouse): return control