def mainloop(canvas): global win, fonts, fullscreen, batch #if a maxsize is given, use it if canvas.maxsize: mw, mh = [int(x.strip()) for x in canvas.maxsize.split(',')] win.set_maximum_size(mw, mh) #or use the given value else: mw, mh = canvas.width, canvas.height #if the given size is too big, resize to the biggest allowed if canvas.width > mw or canvas.height > mh: win.set_size(mw, mh) #or just use what they passed else: win.set_size(canvas.width, canvas.height) # if the canvas has the fullscreen attribute set make it fullsize if canvas._elem.getAttribute('fullscreen').lower() == 'true': toggleFullscreen(True) #set the window's events to go to the right methods in Event.py win.on_key_press = Events.on_key_press win.on_resize = ReSizeGLScene win.on_mouse_press = Events.on_mouse_press win.on_mouse_release = Events.on_mouse_release win.on_mouse_drag = Events.on_mouse_drag win.on_mouse_scroll = Events.on_mouse_scroll win.on_key_release = Events.on_key_release #! EXPERMENTAL and currently disabled because it's too slow #win.on_mouse_motion = Events.on_mouse_motion #retrieve the color of the canvas to use, or white by default cr, cg, cb = Helpers.HTMLColorToRGB(getattr(canvas, 'bgcolor', False) or "0xffffff") #set up some GL stuff.. glClearColor(cr, cg, cb, 1.0) # This will clear the background color to what the canvas is glEnable(GL_BLEND) # Enables alpha blending glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) #does something! really! #enact the FPS cap (canvas.framerate) canvas.framerate = int(canvas.framerate) #schedule the animation routine to run each frame pyglet.clock.schedule_interval(Animation.doAnimations, 1./canvas.framerate) #we are done loading, show the window win.set_visible(True) #Traverse the Dom and register views with the VBO screen manager ViewProjector.register(canvas, batch) #run the pyglet main loop, sit back, and enjoy! pyglet.app.run()
def on_mouse_motion(x, y, lx, ly): global lastMouseOver Nodes.canvas.__dict__['mousex'] = x Nodes.canvas.__dict__['mousey'] = Nodes.canvas.height-y over = ViewProjector.findFocus() if lastMouseOver and over != lastMouseOver: lastMouseOver.touch('mouseout') lastMouseOver = over over.touch('mouseover')
def doAnimations(dt): global animateSchedules framerate = Nodes.canvas.framerate for key in animateSchedules.keys(): a = animateSchedules[key] #if we finished last time around, set the final value if nescessary #and erase the animation if a.finished: if a.what[a.attrname] != a.targetValue: a.what[a.attrname] = a.targetValue del animateSchedules[key] continue #exactly how far to move this frame. inc = (a.inc*dt) #if we are going up, test for end if a.goingUp: #are we at or near enough target value? if a.targetValue <= a.what[a.attrname]+inc: a.finished = True continue #if we are going down, test for end else: #are we at or near enough target value? if a.targetValue >= a.what[a.attrname]+inc: a.finished = True continue #move the object, now adjusted for how much time elapsed to make nice animations a.what[a.attrname] += inc if ['x', 'y', 'width', 'height'].count(a.attrname): v = ViewProjector.viewToVertice(a.what.x, a.what.y, a.what.width, a.what.height) ViewProjector.self.quads[a.what.viewindex].vertices = v
def registerView(view): ViewProjector.add()
def on_mouse_press(x, y, button, modifiers): Nodes.canvas.mousex, Nodes.canvas.mousey = x, Nodes.canvas.height-y Nodes.canvas.focused = ViewProjector.findFocus() Nodes.canvas.focused.mousedown = (button, modifiers)