def load(i): print "load game ",i (slide, music) = globals.loadGame('save.'+str(i)) #The parsing of Save file has ended, we can launch the game #start music if music is not None: globals.currentage.getMusic(music).playmusic() SlideManager.resetStackTo([slide])
def _handleevent(self, event): if event.type == 'mouseenter': #If enter, update rect to full size self._currentrect = self._rect elif event.type == 'mouseexit': #If exit, update rect to small size self._currentrect = self._minrect elif event.type == pygame.MOUSEBUTTONDOWN: #If click display menu #If menu is already displayed, do nothing if SlideManager.getCurrentSlide().gettype() != 'menu': #If any non looping movies playing do nothing if not len([item for item in globals.playing if not item.islooping()]): SlideManager.pushMenu(self._menuslide)
def newgameenter(): # FIXME: at this time there is no code to clean up paths added by 'push_age' if parameters.testCustomAge is not None: exec """from $AGE import $AGE filemanager.push_age('$AGE') globals.currentage = $AGE()""".replace("$AGE", parameters.testCustomAge) else: from DniChamberAge import DniAge filemanager.push_age('Dni') globals.currentage = DniAge() #from FooAge import FooAge #filemanager.push_age('FooAge') #globals.currentage = FooAge() SlideManager.resetStackTo([globals.currentage.getslide(globals.currentage.getStartLocation())]) return None
def isslidevisible(slide): if slide == None: #If the slide is none, of course it is not displayed. Return 0 return 0 current_slide = SlideManager.getCurrentSlide() if current_slide == slide and slide.isvisible(): #If the slide is the current slide, current UI slide and that slide is visible, return 1 return 1 else: #otherwise return 0 return 0
def menuenter(): if globals.playerinventory != None: globals.playerinventory.update(visible = 0, enabled = 0) if globals.currentage is None or SlideManager.getBaseSlide().gettype() == 'menu': # The save menu should not be reachable when no age has been played menumain.detachhotspotById('save') else: menumain.attachhotspot((500, 460, 200, 40), 'grab', {'dest' : 'savemenu','zip':0, 'push':1},'save') musicmain.playmusic()
def quitmenuexit(): nbHotspots=menumain.gethotspotsize() for j in range((nbHotspots-1),-1,-1): menumain.detachhotspot(j) menumain.attachhotspot((500, 300, 200, 40), 'grab', {'dest' :'return_root','zip':0}) #append a hotspot to this slide menumain.attachhotspot((500, 340, 200, 40), 'grab', {'dest' :'return','zip':0}) #append a hotspot to this slide menumain.attachhotspot((500, 380, 200, 40), 'grab', {'dest' : 'loadmenu','zip':0, 'push':1}) menumain.attachhotspot((500, 420, 200, 40), 'grab', {'action' : 'optionmenuenter','zip':0, 'push':1}) if (SlideManager.getCurrentSlide().getrealname()=="intro"): # The save menu should not be reachable when no age has been played menumain.detachhotspotById('save') else: menumain.attachhotspot((500, 460, 200, 40), 'grab', {'dest' : 'savemenu','zip':0, 'push':1},'save') menumain.attachhotspot((500, 500, 200, 40), 'grab', {'action' : 'quitmenumainenter','zip':0}) quitmenumain.update(visible = 0)
def processClick(self): #If there is a current hotspot if slide._currenthotspot: #If action, run it. if slide._currenthotspot.has_key('action'): if globals.functions.has_key(slide._currenthotspot.get('action')): globals.functions[slide._currenthotspot.get('action')]() else: slide._currenthotspot.get('action')() return (None, False) #If draggable hotspot, return the drag function (What's that??) if slide._currenthotspot.has_key('drag'): return (slide._currenthotspot.get('drag'), False) #If destination is a slide if slide._currenthotspot.has_key('dest'): nextslide = slide._currenthotspot.get('dest') #If this is a menu or object "return" if nextslide == 'return': SlideManager.popSlide() return (None, True) else: #Destination is a normal slide #Display the new slide, do run entrance functions if globals.currentage is not None: s = globals.currentage.getslide(nextslide) else: s = globals.menuslides[nextslide] if s.is3D(): s.yaw_angle = slide._currenthotspot['destOrientation'] s.pitch_angle = 0.0 if slide._currenthotspot is not None and slide._currenthotspot.has_key('push') and slide._currenthotspot['push']: SlideManager.pushSlide(s) else: SlideManager.replaceTopSlide(s) return (None, True) return (None, False)
def _click(self): #Display the items slide SlideManager.pushSlide(self._slide)
def menusave(i): print "save game ",i #Open index and search the last try: tmpfile=open(os.path.join(parameters.getsavepath(), 'index.sav'),'rb') data=tmpfile.readlines() tmpfile.close() except: pass if not os.path.exists(parameters.getsavepath()): os.makedirs(parameters.getsavepath()) #Build the save file specific name = 'save.'+str(i) f = file(os.path.join(parameters.getsavepath(), name),'w') #print 'saved slide ',slide._save(slide._currentslide) hd = XMLGenerator(f) # handler's creation hd.startDocument() hd.startElement("Save", AttributesImpl({})) hd.startElement("age", AttributesImpl({})) hd.characters(globals.currentage.getFileName()) hd.endElement("age") hd.startElement("music", AttributesImpl({"name": music.getcurrentStdMusic().getname()})) hd.endElement("music") hd.startElement("slide", AttributesImpl({})) # items in the stack beyond the first item are supposed to be menus, ignore them hd.characters(SlideManager.getBaseSlide().getrealname()) hd.endElement("slide") hd.startElement("states", AttributesImpl({})) import pickle import base64 for (k,v) in globals.menuslides.items(): if (isinstance(v,slide)): #We remove the slideload and menu_root if (v.getrealname().startswith("slideload")==False and v.getrealname().startswith("menu_root")==False): # serialize the state dictionary using Pickle, then encode it in base64 so that it fits well in XML state = base64.b64encode(pickle.dumps(v.getAllStateVariables())) hd.startElement("state", AttributesImpl({"name": k, "state": state})) hd.endElement("state") for (k,v) in globals.currentage.getAllSlides().items(): # serialize the state dictionary using Pickle, then encode it in base64 so that it fits well in XML state = base64.b64encode(pickle.dumps(v.getAllStateVariables())) hd.startElement("state", AttributesImpl({"name": k, "state": state})) hd.endElement("state") hd.endElement("states") hd.endElement("Save") hd.endDocument() f.close() date = strftime("%a, %d %b %Y %H:%M:%S", localtime()) currentslideimg = SlideManager.getBaseSlide().getFullFilePath() #Build the key of saveIndex name = 'save'+str(i) #Retrieve the Save object : if (globals.saves.has_key(name)): mySave=globals.saves[name] mySave.setdate(date) mySave.setimage(currentslideimg) mySave.setage(globals.currentage.getFileName()) else: globals.saves[name] = save(name, globals.currentage.getFileName(), currentslideimg, date) globals.saveSaveIndex('saveIndex.xml') #We exit the game quitgame()
def mainloop(): pygame.init() globals.init() pygame.mouse.set_visible(0) parameters.setcursorpath(os.path.join('engine', 'enginedata', 'cursors')) parameters.settextpath(os.path.join('engine', 'enginedata', 'text')) initcursors() globals.curcursor = globals.cursors['forward'] filemanager.push_dir(os.path.join('data', 'menu')) for curitem in globals.userareasordered: curitem._handleevent(fakeevent('init')) if curitem.isvisible(): curitem._draw() menubar = menubararea((0, 0, parameters.getscreensize()[0], 40), (0, 0, parameters.getscreensize()[0], 5), menumain, 1) defaultui = None startslides = parameters.getfirstslides() if not startslides[1]: print "==> Slide 1 is nul!! Will use bg.bmp" defaultui = slide('bg.bmp', type = 'ui') defaultui._bmpbuffer = pygame.Surface(globals.screenrect.size) defaultui._bmpbuffer.fill(parameters.getbackgroundcolor()) startslides[1] = defaultui startslides[0]=defaultui # for the start, init the state manager manually... (FIXME) SlideManager._slide_stack.append(startslides[0]) startslides[0].enter(None) globals.quit = 0 drag = None # The area the cursor is currently in uha = None lastpos = (0,0) defaultui = None startslides = parameters.getfirstslides() if not startslides[1]: defaultui = slide('background','bg.bmp', type = 'ui') defaultui._bmpbuffer = pygame.Surface(globals.screenrect.size) defaultui._bmpbuffer.fill(parameters.getbackgroundcolor()) uislide = defaultui startslides[1] = uislide #globals.transoverride = 'initfade' WIDTH = parameters.getscreensize()[0] HEIGHT = parameters.getscreensize()[1] previous_time = pygame.time.get_ticks() while not globals.quit: new_time = pygame.time.get_ticks() dt = new_time - previous_time # 16 milliseconds is roughly 60 FPS. Cap after that. if dt < 16: pygame.time.wait(16 - dt) new_time = pygame.time.get_ticks() dt = new_time - previous_time previous_time = new_time for event in pygame.event.get(): if globals.eventhandlers.has_key(event.type): globals.eventhandlers[event.type](event) elif event.type == pygame.USEREVENT: #print "event.type==USEREVENT" globals.timers[event.timerid].trigger() elif event.type == pygame.MOUSEMOTION and globals.curcursor.isenabled(): if event.pos != lastpos: lastpos = event.pos #print event.pos if uha != None: if not uha.getrect().collidepoint(event.pos): tmpevent = fakeevent('mouseexit') uha._handleevent(tmpevent) uha = None #Route mouse event to proper object if drag != None: update = drag(event) if update: globals.curcursor._hide() #Update mouse cursor with new location globals.curcursor._handlemousemove(event) globals.curcursor._show() else: #Mouse movement #Hide mouse cursor globals.curcursor._hide() #Update mouse cursor with new location globals.curcursor._handlemousemove(event) uhatrapped = 0 for area in [item for item in globals.userareasordered if item.isenabled()]: if area.getrect().collidepoint(event.pos): if uha != None: if area != uha: tmpevent = fakeevent('mouseexit') uha._handleevent(tmpevent) uha = area tmpevent = fakeevent('mouseenter') uha._handleevent(tmpevent) else: uha = area tmpevent = fakeevent('mouseenter') uha._handleevent(tmpevent) uhatrapped = uha._handleevent(event) break if not uhatrapped: current_slide = SlideManager.getCurrentSlide() if current_slide.gettype() == 'menu': if globals.screenrect.collidepoint(event.pos): if current_slide.isvisible(): #print "MOUSE MOVE1 slide._currentuislide.name ",slide._currentuislide._realname current_slide._mousemove(event.pos) else: if globals.screenrect.collidepoint(event.pos): #print "if globals" if current_slide.isvisible(): #print "if slide._currenslide" current_slide._mousemove(event.pos) globals.curcursor._show() elif event.type == pygame.MOUSEBUTTONDOWN and globals.curcursor.isenabled(): drag = None uhatrapped = 0 for area in [item for item in globals.userareasordered if item.isenabled()]: if area.getrect().collidepoint(event.pos): uhatrapped = area._handleevent(event) break if not uhatrapped: current_slide = SlideManager.getCurrentSlide() if current_slide.gettype() == 'menu': if globals.screenrect.collidepoint(event.pos): if current_slide.isvisible(): (drag, transitionOccurred) = current_slide.processClick() else: if globals.screenrect.collidepoint(event.pos): if current_slide.isvisible(): (drag, transitionOccurred) = current_slide.processClick() if drag: update = drag(event) if update: globals.curcursor._hide() #Update mouse cursor with new location globals.curcursor._handlemousemove(event) globals.curcursor._show() # Ignore mouse movement that may have occurred during the transition if transitionOccurred: if mode == CENTER_MOUSE: pygame.mouse.set_pos([WIDTH/2, HEIGHT/2]) elif event.type == pygame.MOUSEBUTTONUP and globals.curcursor.isenabled(): #print "event.type == pygame.MOUSEBUTTONUP and globals.curcursor.isenabled()" if drag != None: drag(event) drag = None uhatrapped = 0 for area in [item for item in globals.userareasordered if item.isenabled()]: if area.getrect().collidepoint(event.pos): uhatrapped = area._handleevent(event) break elif event.type == pygame.KEYDOWN: uhatrapped = 0 for area in [item for item in globals.userareasordered if item.isenabled()]: uhatrapped = area._handleevent(event) if uhatrapped: break if not uhatrapped: if event.key == pygame.K_q: globals.quit = 1 if event.key == pygame.K_ESCAPE: globals.stopallsound() music.stopmusic() current_slide = SlideManager.getCurrentSlide() if current_slide.gettype() == 'menu': SlideManager.popSlide() else: SlideManager.pushSlide(menumain) elif event.type == pygame.QUIT: globals.quit = 1 # == Rendering s = SlideManager.getCurrentSlide() if s.is3D(): globals.prepare3DViewport() (mx, my) = pygame.mouse.get_pos() if mode == CENTER_MOUSE: dx = mx - WIDTH/2 dy = my - HEIGHT/2 pygame.mouse.set_pos([WIDTH/2, HEIGHT/2]) if is_in_3d_slide: # avoid jump when entering slide by calculating delta the 2nd time only s.yaw_angle += dt*0.006 * dx if s.yaw_angle < 0: s.yaw_angle += 360 elif s.yaw_angle > 360: s.yaw_angle -= 360 s.pitch_angle += dt*0.006 * dy if s.pitch_angle < -60:s. pitch_angle = -60 elif s.pitch_angle > 60: s.pitch_angle = 60 else: if my < MOVE_MARGIN: s.pitch_angle -= dt*0.1 * float(MOVE_MARGIN - my)/float(MOVE_MARGIN) if s.pitch_angle < -60: s.pitch_angle = -60 elif my > HEIGHT - MOVE_MARGIN: s.pitch_angle += dt*0.1 * (1.0 - float(HEIGHT - my)/float(MOVE_MARGIN)) if s.pitch_angle > 60: s.pitch_angle = 60 if mx < MOVE_MARGIN: s.yaw_angle -= dt*0.1 * float(MOVE_MARGIN - mx)/float(MOVE_MARGIN) if s.yaw_angle < 0: s.yaw_angle += 360 elif mx > WIDTH - MOVE_MARGIN: s.yaw_angle += dt*0.1 * (1.0 - float(WIDTH - mx)/float(MOVE_MARGIN)) if s.yaw_angle > 359: s.yaw_angle -= 360 # rotate camera glRotatef(s.pitch_angle, 1, 0, 0) glRotatef(s.yaw_angle, 0, 1, 0) is_in_3d_slide = True else: is_in_3d_slide = False globals.prepare2DViewport() # No need for transparency to render slides (don't move this class into Slide.render because # transitions still need it enabled) glDisable(GL_BLEND) if s.is3D(): s.render() else: s.render() if s.is3D(): s.pickHotSpot(mx, my) # back to 2D for the rest globals.prepare2DViewport() if globals.curcursor is not None: glEnable(GL_BLEND) if s.is3D() and mode == CENTER_MOUSE: globals.curcursor.renderAt(WIDTH/2, HEIGHT/2) else: globals.curcursor.render() pygame.display.flip() for curtimer in globals.timers.values(): curtimer.stop()
def enter(self, oldslide, enter = 1): print "slide.py Enter BEGIN :",self._realname, self._type #Display the proper way for the new slide if self._type == 'menu': #Set transition type to fade slide._currenttrans = 'fade' if enter: #Stop timers for timer in [timer for timer in globals.timers.values() if timer.isrunning()]: timer.pause() self._pausedtimers.append(timer) elif self._type == 'ui': slide._currenttrans = 'fade' elif self._type == 'object': #Set transition type to fade slide._currenttrans = 'fade' #Append the last slide to the list of slides that can be "return"ed to if enter: #Stop timers for timer in [timer for timer in globals.timers.values() if timer.isrunning()]: timer.pause() self._pausedtimers.append(timer) else: #Std slide print "slide.py :display STD SlideManager.getCurrentSlide() =", SlideManager.getCurrentSlide().getrealname() #If this is the first Std slide displayed, no transition (UI will do it for us) if len(SlideManager._slide_stack) == 0: slide._currenttrans = 'none' else: #Otherwise Get the current hotspots transition or 'fade' if there is none if slide._currenthotspot != None: if slide._currenthotspot.has_key('transover'): #Hotspot is overriding the default transition slide._currenttrans = slide._currenthotspot['transover'] else: #Use the default transition slide._currenttrans = globals.cursors[slide._currenthotspot['cursor']].gettransition() else: slide._currenttrans = 'fade' print "slide.py :display STD NOW after change _currentslide name=", self._realname #Perform the transition between the last slide and this one globals.dotrans(self, oldslide) #For all slide types: #Clear the current hotspot slide._currenthotspot = None #Set this slide to be visible self._visible = 1 #If entering this slide run it's entrance functions if enter: if self._onenterfn: if globals.functions.has_key(self._onenterfn): globals.functions[self._onenterfn]() else: #print "slide.py :onenterfct",self._onenterfn self._onenterfn() pos = pygame.mouse.get_pos() pygame.mouse.set_pos((pos[0] + 1, pos[1])) #If this is a delay slide, count down the delay and display the dest of the first hotspot if self._delay > 0: pygame.time.delay(self._delay * 1000) nextslide = self._state[0] nextslide.enter(oldslide, self) print "slide.py : END of slide.display"
def init(self,valid_raw_input): slideManager = SlideManager(valid_raw_input) slideManager.makeSlideElements()