def mainloop(self): # don't known why, but pygame required a resize event # for opengl, before mainloop... window reinit ? self.dispatch('on_resize', *self.size) while not EventLoop.quit and EventLoop.status == 'started': try: self._mainloop() except BaseException as inst: # use exception manager first r = ExceptionManager.handle_exception(inst) if r == ExceptionManager.RAISE: stopTouchApp() raise else: pass # force deletion of window sdl.teardown_window()
def close(self): sdl.teardown_window() self.dispatch('on_close')
class WindowSDL(WindowBase): def create_window(self): use_fake = True use_fullscreen = False # never stay with a None pos, application using w.center will be fired. self._pos = (0, 0) # setup ! w, h = self._size self._size = sdl.setup_window(w, h, use_fake, use_fullscreen) super(WindowSDL, self).create_window() # auto add input provider Logger.info('Window: auto add sdl input provider') from kivy.base import EventLoop SDLMotionEventProvider.win = self EventLoop.add_input_provider(SDLMotionEventProvider('sdl', '')) def close(self): sdl.teardown_window() self.dispatch('on_close') def set_title(self, title): sdl.set_window_title(title) def set_icon(self, filename): return def screenshot(self, *largs, **kwargs): return filename = super(WindowPygame, self).screenshot(*largs, **kwargs) if filename is None: return None from kivy.core.gl import glReadPixels, GL_RGB, GL_UNSIGNED_BYTE width, height = self.size data = glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE) data = str(buffer(data)) surface = pygame.image.fromstring(data, self.size, 'RGB', True) pygame.image.save(surface, filename) Logger.debug('Window: Screenshot saved at <%s>' % filename) return filename def flip(self): sdl.flip() super(WindowSDL, self).flip() def _mainloop(self): EventLoop.idle() while True: event = sdl.poll() if event is False: break if event is None: continue #print 'sdl received', event action, args = event[0], event[1:] if action == 'quit': EventLoop.quit = True self.close() break elif action in ('fingermotion', 'fingerdown', 'fingerup'): # for finger, pass the raw event to SDL motion event provider SDLMotionEventProvider.q.appendleft(event) if action == 'mousemotion': x, y = args self.dispatch('on_mouse_move', x, y, self.modifiers) elif action in ('mousebuttondown', 'mousebuttonup'): x, y, button = args btn = 'left' if button == 3: btn = 'right' elif button == 2: btn = 'middle' eventname = 'on_mouse_down' if action == 'mousebuttonup': eventname = 'on_mouse_up' self.dispatch(eventname, x, y, btn, self.modifiers) # video resize elif action == 'windowresized': self._size = args # don't use trigger here, we want to delay the resize event cb = self._do_resize Clock.unschedule(cb) Clock.schedule_once(cb, .1) elif action == 'windowresized': self.canvas.ask_update() elif action == 'windowminimized': self.do_pause() elif action == 'windowrestored': pass elif action in ('keydown', 'keyup'): mod, key, scancode, unicode = args # XXX ios keyboard suck, when backspace is hit, the delete # keycode is sent. fix it. if key == 127: key = 8 self._pygame_update_modifiers(mod) if action == 'keyup': self.dispatch('on_key_up', key, scancode) continue # don't dispatch more key if down event is accepted if self.dispatch('on_key_down', key, scancode, unicode, self.modifiers): continue self.dispatch('on_keyboard', key, scancode, unicode, self.modifiers) elif action == 'textinput': key = args[0][0] # XXX on IOS, keydown/up don't send unicode anymore. # With latest sdl, the text is sent over textinput # Right now, redo keydown/up, but we need to seperate both call # too. (and adapt on_key_* API.) self.dispatch('on_key_down', key, None, args[0], self.modifiers) self.dispatch('on_keyboard', None, None, args[0], self.modifiers) self.dispatch('on_key_up', key, None, args[0], self.modifiers) # # video resize # elif event.type == pygame.VIDEORESIZE: # self._size = event.size # # don't use trigger here, we want to delay the resize event # cb = self._do_resize # Clock.unschedule(cb) # Clock.schedule_once(cb, .1) # elif event.type == pygame.VIDEOEXPOSE: # self.canvas.ask_update() # # ignored event # elif event.type == pygame.ACTIVEEVENT: # pass # # unhandled event ! # else: # Logger.debug('WinPygame: Unhandled event %s' % str(event)) def _do_resize(self, dt): Logger.debug('Window: Resize window to %s' % str(self._size)) sdl.resize_window(*self._size) self.dispatch('on_resize', *self._size) def do_pause(self): # should go to app pause mode. from kivy.app import App from kivy.base import stopTouchApp app = App.get_running_app() if not app: Logger.info('WindowSDL: No running App found, exit.') stopTouchApp() return if not app.dispatch('on_pause'): Logger.info('WindowSDL: App doesn\'t support pause mode, stop.') stopTouchApp() return # XXX FIXME wait for sdl resume while True: event = sdl.poll() if event is False: continue if event is None: continue #print 'sdl received', event action, args = event[0], event[1:] if action == 'quit': EventLoop.quit = True self.close() break elif action == 'windowrestored': #print 'app goes live again!' break #print 'dispatch app on_resume' app.dispatch('on_resume') def mainloop(self): # don't known why, but pygame required a resize event # for opengl, before mainloop... window reinit ? self.dispatch('on_resize', *self.size) #print 'dispatched on_resize, size is', self.size while not EventLoop.quit and EventLoop.status == 'started': try: self._mainloop() except BaseException, inst: # use exception manager first r = ExceptionManager.handle_exception(inst) if r == ExceptionManager.RAISE: stopTouchApp() raise else: pass # force deletion of window sdl.teardown_window()