Skip to content

Instantly share code, notes, and snippets.

@hansent
Created November 20, 2009 10:55
Show Gist options
  • Save hansent/239423 to your computer and use it in GitHub Desktop.
Save hansent/239423 to your computer and use it in GitHub Desktop.
'''
Window Pygame: windowing provider based on Pygame
'''
__all__ = ('MTWindowPygame', )
from . import BaseWindow
from ...logger import pymt_logger
from ...utils import curry
from ...base import stopTouchApp, getEventLoop
import ctypes
try:
import pygame
except:
pymt_logger.warning('Pygame is not installed !')
raise
class MTWindowPygame(BaseWindow):
def __init__(self, **kwargs):
# init some opengl, same as before.
self.flags = pygame.HWSURFACE | pygame.OPENGL | pygame.DOUBLEBUF
pygame.display.init()
pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLEBUFFERS, 1)
pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES, 4)
pygame.display.gl_set_attribute(pygame.GL_DEPTH_SIZE, 16)
pygame.display.gl_set_attribute(pygame.GL_STENCIL_SIZE, 1)
pygame.display.gl_set_attribute(pygame.GL_ALPHA_SIZE, 8)
pygame.event.set_allowed(pygame.SYSWMEVENT)
self.win_id = pygame.display.get_wm_info()['window']
super(MTWindowPygame, self).__init__(**kwargs)
def create_window(self):
self.flags &= ~pygame.RESIZABLE
self._pygame_set_mode((1, 1))
pygame.display.set_caption('pymt')
super(MTWindowPygame, self).create_window()
def configure(self, params):
if params['fullscreen']:
pymt_logger.debug('Set window to fullscreen mode')
self.flags |= pygame.FULLSCREEN
self.size = params['width'], params['height']
super(MTWindowPygame, self).configure(params)
def close(self):
import sys
sys.exit(0)
def on_keyboard(self, key, scancode=None, unicode=None):
if key == 27:
stopTouchApp()
self.close() #not sure what to do here
return True
super(MTWindowPygame, self).on_keyboard(key, scancode, unicode)
def flip(self):
pygame.display.flip()
super(MTWindowPygame, self).flip()
def get_win_id(self):
return pygame.display.get_wm_info()['window']
def mainloop(self):
#self.win_id = pygame.display.get_wm_info()['window']
# don't known why, but pygame required a resize event
# for opengl, before mainloop... window reinit ?
self.dispatch_event('on_resize', *self.size)
evloop = getEventLoop()
while not evloop.quit:
evloop.idle()
#pygame.event.set_allowed(pygame.SYSWMEVENT)
for sysev in pygame.event.get(pygame.SYSWMEVENT):
print sysev.msg, ctypes.windll.user32.IsTouchWindow(sysev.hwnd, 0)
for event in pygame.event.get():
# kill application (SIG_TERM)
if event.type == pygame.QUIT:
evloop.quit = True
self.close()
# mouse move
elif event.type == pygame.MOUSEMOTION:
# don't dispatch motion if no button are pressed
if event.buttons == (0, 0, 0):
continue
x, y = event.pos
self.dispatch_event('on_mouse_move', x, y, self.modifiers)
# mouse action
elif event.type in (pygame.MOUSEBUTTONDOWN,
pygame.MOUSEBUTTONUP):
self._pygame_update_modifiers()
x, y = event.pos
btn = 'left'
if event.button == 3:
btn = 'right'
elif event.button == 2:
btn = 'middle'
eventname = 'on_mouse_down'
if event.type == pygame.MOUSEBUTTONUP:
eventname = 'on_mouse_up'
self.dispatch_event(eventname, x, y, btn, self.modifiers)
# keyboard action
elif event.type in (pygame.KEYDOWN, pygame.KEYUP):
self._pygame_update_modifiers(event.mod)
# atm, don't handle keyup
if event.type == pygame.KEYUP:
continue
self.dispatch_event('on_keyboard', event.key,
event.scancode, event.unicode)
# video resize
elif event.type == pygame.VIDEORESIZE:
pass
# ignored event
elif event.type in (pygame.ACTIVEEVENT, pygame.VIDEOEXPOSE):
pass
# unhandled event !
else:
pymt_logger.debug('Unhandled event %s' % str(event))
# force deletion of window
pygame.display.quit()
def _set_size(self, size):
# set pygame mode only if size have really changed
if super(MTWindowPygame, self)._set_size(size):
self._pygame_set_mode()
#
# Pygame wrapper
#
def _pygame_set_mode(self, size=None):
if size is None:
size = self.size
pygame.display.set_mode(size, self.flags)
def _pygame_update_modifiers(self, mods=None):
# Available mod, from dir(pygame)
# 'KMOD_ALT', 'KMOD_CAPS', 'KMOD_CTRL', 'KMOD_LALT',
# 'KMOD_LCTRL', 'KMOD_LMETA', 'KMOD_LSHIFT', 'KMOD_META',
# 'KMOD_MODE', 'KMOD_NONE'
if mods is None:
mods = pygame.key.get_mods()
self._modifiers = []
if mods & (pygame.KMOD_SHIFT | pygame.KMOD_LSHIFT):
self._modifiers.append('shift')
if mods & (pygame.KMOD_ALT | pygame.KMOD_LALT):
self._modifiers.append('alt')
if mods & (pygame.KMOD_CTRL | pygame.KMOD_LCTRL):
self._modifiers.append('ctrl')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment