Source

Fat x Fast / FatxFast / input.py

# This file is part of FatxFast.
#
#    FatxFast is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    FatxFast is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with FatxFast.  If not, see <http://www.gnu.org/licenses/>.

# Versioning based on: 
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage
__author__ = "dryatu (c) 2013"
__version__ = "1.2.5"

import pygame
from FatxFast.config import Config

class KeyListener(object):

    def keys_held(self):
        #ygame.event.pump()
        return pygame.key.get_pressed()

    def key_pressed(self, key, utf, mod):
        pass

    def key_released(self, key):
        pass

class MouseListener(object):

    def mouse_moved(self, pos, rel):
        pass
    
    def mouse_pressed(self, button, pos):
        pass

    def mouse_released(self, button):
        pass

class SystemListener(object):
    
    def quit(self):
        pass

class Input(object):
    
    def __init__(self):
        self.mouse_listeners = []
        self.key_listeners = []
        self.system_listeners = []
        pygame.key.set_repeat(0,0)

    def notify_listeners(self, listeners, function, *args):
        listeners = list(listeners)
        for l in reversed(listeners):
            getattr(l, function)(*args)

    def add_mouse_listener(self, listener):
        if listener not in self.mouse_listeners:
            self.mouse_listeners.append(listener)

    def add_key_listener(self, listener):
        if listener not in self.key_listeners:
            self.key_listeners.append(listener)
    
    def remove_key_listener(self, listener):
        if listener in self.key_listeners:
            self.key_listeners.remove(listener)

    def remove_mouse_listener(self, listener):
        if listener in self.mouse_listeners:
            self.mouse_listeners.remove(listener)

    def add_system_listener(self, listener):
        if listener not in self.system_listeners:
            self.system_listeners.append(listener)
    
    def update(self):
        pygame.event.pump()
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                self.notify_listeners(self.key_listeners, "key_pressed", 
                    event.key, event.unicode, event.mod)

            elif event.type == pygame.KEYUP:
                self.notify_listeners(self.key_listeners, "key_released", 
                    event.key)
            
            elif event.type == pygame.MOUSEBUTTONDOWN:
                self.notify_listeners(self.mouse_listeners, "mouse_pressed", 
                    event.button, event.pos)

            elif event.type == pygame.MOUSEBUTTONUP:
                self.notify_listeners(self.mouse_listeners, "mouse_released", 
                    event.button)

            elif event.type == pygame.MOUSEMOTION:
                self.notify_listeners(self.mouse_listeners, "mouse_moved", 
                    event.pos, event.rel)

            elif event.type == pygame.QUIT:
                self.notify_listeners(self.system_listeners, "quit")