Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. # encoding: utf-8
  2.  
  3. import pygame
  4. from pygame.locals import *
  5. from pygame import event, key, mouse
  6. from pygame.time import Clock, wait
  7.  
  8. from pyguane.core.singleton import Singleton
  9.  
  10. import sys
  11.  
  12.  
  13. @Singleton
  14. class EventManager(object):
  15.    
  16.     def __init__(self):
  17.         pygame.init()
  18.         key.set_repeat(20)
  19.         self._keys_state = {}
  20.         self._user_events = {}
  21.         self._clock = Clock()
  22.    
  23.     def _keyboardCallback(self, keysdown):
  24.         if "escape" in keysdown:
  25.             quit()
  26.    
  27.     def _mouseCallback(self, buttons_down, pos, relpos):
  28.         for i,but in enumerate(buttons_down):
  29.             if but: print "Button",i,"pressed at pos",pos,"  rel:",relpos
  30.            
  31.     def keyboard(self, func):
  32.         """ register a function as a keyboard callback """
  33.         self._keyboardCallback = func
  34.         return self._keyboardCallback
  35.        
  36.     def mouse(self, func):
  37.         """ register a function as a mouse callback """
  38.         self._mouseCallback = func
  39.         return self._mouseCallback
  40.    
  41.     def _setKeyState(self, key, state):
  42.         self._keys_state[key] = state
  43.    
  44.     def getKeysDown(self):
  45.         """ get a list of the keys currently pressed """
  46.         return [k for k in self._keys_state if self._keys_state[k] == KEYDOWN]
  47.    
  48.     def isKeyDown(self, key):
  49.         """ return True is the key is down """
  50.         return self._keys_state[key] == KEYDOWN
  51.                  
  52.     @property
  53.     def buttons_down(self): return mouse.get_pressed()
  54.     @property
  55.     def mouse_pos(self): return mouse.get_pos()
  56.     @property
  57.     def mouse_relpos(self): return mouse.get_rel()
  58.              
  59.     def update(self):
  60.         """Update the event loop
  61.        
  62.           Calls the callbacks binded to the mouse and the keyboard
  63.           Calls USEREVENT callback
  64.        """
  65.         for e in event.get():
  66.             if e.type == QUIT:
  67.                 pygame.quit()
  68.                 sys.exit()
  69.             elif e.type in (KEYDOWN, KEYUP):
  70.                 self._setKeyState(key.name(e.key), e.type)
  71.             elif e.type in (MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION):
  72.                 self._mouseCallback(self.buttons_down, self.mouse_pos, self.mouse_relpos)
  73.             elif e.type == USEREVENT:
  74.                 try:
  75.                     e.callback()
  76.                 except AttributeError:
  77.                     print e," is a USEREVENT with no callback function defined."
  78.                          
  79.         self._keyboardCallback(self.getKeysDown())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement