def prepare2DViewport(): width = parameters.getscreensize()[0] height = parameters.getscreensize()[1] glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0, width, height, 0) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def pickHotSpot_getMatrices(self): width = parameters.getscreensize()[0] height = parameters.getscreensize()[1] modelviewMatrix = glGetDoublev(GL_MODELVIEW_MATRIX) if Slide3D.p_matrix is None: Slide3D.p_matrix = glGetDoublev(GL_PROJECTION_MATRIX) viewport = [0, 0, width, height] return (modelviewMatrix, Slide3D.p_matrix, viewport)
def prepare3DViewport(): width = parameters.getscreensize()[0] height = parameters.getscreensize()[1] glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(50.0, float(width)/float(height), 0.1, 500.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def rightscroll(newslide, oldslide): # Prepare OpenGL globals.prepare2DViewport() previous_time = pygame.time.get_ticks() #Fade new image into old image, from alpha = 1 to alpha = 255 with a configured step for i in range(1, 255, parameters.getfadevelocity()): #Pause configurable delay if i > 1: new_time = pygame.time.get_ticks() already_elapsed_time = new_time - previous_time time_to_wait = parameters.getdelay() - already_elapsed_time if time_to_wait > 0: pygame.time.delay(time_to_wait) previous_time = new_time oldslide.render(-(i/255.0)*parameters.getscreensize()[0], 0) newslide.render(-(i/255.0)*parameters.getscreensize()[0] + parameters.getscreensize()[0], 0) pygame.display.flip()
def pickHotSpot(self, mx, my): # check hotspots by using simple OpenGL picking. # width = parameters.getscreensize()[0] height = parameters.getscreensize()[1] # modelviewMatrix = glGetDoublev(GL_MODELVIEW_MATRIX) # projectionMatrix = glGetDoublev(GL_PROJECTION_MATRIX) # viewport = [0, 0, width, height] (modelviewMatrix, projectionMatrix, viewport) = self.pickHotSpot_getMatrices() mouse_p = (mx, height - my) (min_x, max_x) = self.getHotspotMinMaxX(self.yaw_angle) hotspotFound = False for curr in self._hotspots: if curr["mapType"] == "polygon": continue # TODO: implement polygon hotspots rect = list(curr["map"]) # format (x, y, w, h) x = rect[0] y = rect[1] w = rect[2] h = rect[3] (x, w) = self.restrainHotspotTo(x, w, min_x, max_x) if w == 0: continue # Convert to OpenGL coords (we use a cube of size 20) # The X coordinate indicates on which face of the cube a hotspot is # The front face has X coordinates 0 to 1023, the right face has 1024 to 2047, etc... x = (x % 1024) / 1024.0 * 20.0 - 10.0 y = (1.0 - y / 1024.0) * 20.0 - 10.0 w = w / 1024.0 * 20.0 h = h / 1024.0 * 20.0 if rect[0] < 1024: # North # Project the coords in 2D screen space p1 = gluProject(x, y, -10, modelviewMatrix, projectionMatrix, viewport) p2 = gluProject(x + w, y, -10, modelviewMatrix, projectionMatrix, viewport) p3 = gluProject(x + w, y - h, -10, modelviewMatrix, projectionMatrix, viewport) p4 = gluProject(x, y - h, -10, modelviewMatrix, projectionMatrix, viewport) elif rect[0] < 1024 * 2: # East # Project the coords in 2D screen space p1 = gluProject(10, y, x, modelviewMatrix, projectionMatrix, viewport) p2 = gluProject(10, y, x + w, modelviewMatrix, projectionMatrix, viewport) p3 = gluProject(10, y - h, x + w, modelviewMatrix, projectionMatrix, viewport) p4 = gluProject(10, y - h, x, modelviewMatrix, projectionMatrix, viewport) elif rect[0] < 1024 * 3: # South # Project the coords in 2D screen space p1 = gluProject(-x, y, 10, modelviewMatrix, projectionMatrix, viewport) p2 = gluProject(-x - w, y, 10, modelviewMatrix, projectionMatrix, viewport) p3 = gluProject(-x - w, y - h, 10, modelviewMatrix, projectionMatrix, viewport) p4 = gluProject(-x, y - h, 10, modelviewMatrix, projectionMatrix, viewport) elif rect[0] < 1024 * 4: # West # Project the coords in 2D screen space p1 = gluProject(-10, y, -x, modelviewMatrix, projectionMatrix, viewport) p2 = gluProject(-10, y, -x - w, modelviewMatrix, projectionMatrix, viewport) p3 = gluProject(-10, y - h, -x - w, modelviewMatrix, projectionMatrix, viewport) p4 = gluProject(-10, y - h, -x, modelviewMatrix, projectionMatrix, viewport) else: raise Exception("Hotspot coords too large") # x1_at_my = x4 + (x1 - x4)*(my - y1)/(y4 - y1) # x2_at_my = x3 + (x2 - x3)*(my - y2)/(y3 - y2) # mouse_inside = (mx >= min(x1_at_my, x2_at_my) and mx <= max(x1_at_my, x2_at_my)) mouse_inside = self.pointInTriangle(p1, p2, p3, mouse_p) or self.pointInTriangle(p1, p3, p4, mouse_p) if mouse_inside: slide._currenthotspot = curr globals.curcursor = globals.cursors.get(curr.get("cursor")) if globals.curcursor is None: raise Exception("Cannot find cursor '" + curr.get("cursor") + "'") hotspotFound = True break if not hotspotFound: # no hotspot under the mouse # Set the current hotspot to None slide._currenthotspot = None # Return the default hotspot for the slide type if self._type == "ui": globals.curcursor = globals.cursors.get("arrow") else: globals.curcursor = globals.cursors.get("forward")
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()