<commit_before># -*- coding: utf-8-*-

"""
Author: <NAME> <<EMAIL>>
Copyright © 2008-2009

Code to deal with user input, via mouse, keyboard and joysticks
"""

from mdlib.log import ConsoleLogger, DEBUG, INFO
logger = ConsoleLogger("input", INFO)

from pandac.PandaModules import ModifierButtons
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task

import event

BASE_SCHEME = "base"

__all__ = ["SafeDirectObject", "Command", "InputScheme", "InputManager", 
           "BASE_SCHEME"]


class SafeDirectObject(DirectObject):
    def destroy(self):
        self.ignoreAll() 

class Command(object):
    """
    A Command can be either an Event or a Callback. An event is sent using the
    event manager while the callback is executed directly. The command also
    stores arguments and the key used to fire it. 
    """
    
    EVENT = 0x1
    CALLBACK = 0x2
    
    def __init__(self, name, type, args=[]):
        self.name= name
        self.args = args
        self.pressed = False
        self.type = type
        
    def __repr__(self):
        s = "%s name:\t %s\n args:\t %s\n pressed:\t %s\n type:\t %s\n"\
            % (self.__class__.__name__, self.name, self.args, self.pressed, self.type)
        return s

class InputScheme(object):
    """ 
    An InputScheme is a sequence of commands that can be enabled or disabled
    in the InputManager. 
    """
    def __init__(self, name):
        self._name = name
        self._enabled = True
        self._commands = {}
    
    def __repr__(self):
        return self._name
    
    def getCommands(self):
        return self._commands  
    
    def __repr__(self):
        return "Name: %s" % (self._name)
    
    name = property(fget=lambda self: self._name, fset=None)
    

class InputWatcher(object):
    """ An InputWatcher receives raw input events from the InputManager """
    
    def acceptUp(self, key):
        pass
    
    def acceptDown(self, key):
        pass
    
        
class InputManager(SafeDirectObject):
    """
    The InputManager class manage all the input coming from the player and
    send events according to the pressed buttons.
    
    It supports multiple InputScheme where a scheme is basically a map
    of commands. 
    There is a special input scheme, the BASE_SCHEME scheme, whose values are
    always checked. It is a sort of global input scheme whose commands are
    always executed.
    
    Each key can be bound to an unlimited number of commands.
    """
    
    def __init__(self, base=None):
        
        # get rid of alt button
        if base is not None:
            buttons = base.mouseWatcherNode.getModifierButtons()
            if buttons.getNumButtons() > 2:
                buttons.removeButton(buttons.getButton(2))
                base.mouseWatcherNode.setModifierButtons(buttons)
                base.buttonThrowers[0].node().setModifierButtons(buttons)
            
            base.buttonThrowers[0].node().setButtonDownEvent('button')
            base.buttonThrowers[0].node().setButtonUpEvent('buttonUp')
            base.buttonThrowers[0].node().setButtonRepeatEvent('buttonRepeat')
        
        self.accept("button", self._onButtonDown)
        self.accept("buttonRepeat", self._onButtonRepeat)
        self.accept("buttonUp", self._onButtonUp)
        
        self._currentScheme = InputScheme(BASE_SCHEME)
        self._schemes = {BASE_SCHEME: self._currentScheme}
        self._lastCmdTime = 0
        self._watchers = []
    
    
    def addWatcher(self, watcher):
        self._watchers.append(watcher)

    def removeWatcher(self, watcher):
        if watcher in self._watchers:
            self._watchers.remove(watcher)
    
    def switchSchemeTo(self, name):
        if name in self._schemes.keys():
            logger.info("Switching to scheme: %s" % name)
            self._currentScheme = self._schemes[name]
        else:
            logger.warning("Can't switch to non existent scheme: %s" % name)<commit_msg>removed (definitely ?) base dependency<commit_after># -*- coding: utf-8-*-

"""
Author: <NAME> <<EMAIL>.com>
Copyright © 2008-2009

Code to deal with user input, via mouse, keyboard and joysticks
"""

from mdlib.log import ConsoleLogger, DEBUG, INFO
logger = ConsoleLogger("input", INFO)

from pandac.PandaModules import ModifierButtons
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task

import event

BASE_SCHEME = "base"

__all__ = ["SafeDirectObject", "Command", "InputScheme", "InputManager", 
           "BASE_SCHEME"]


class SafeDirectObject(DirectObject):
    def destroy(self):
        self.ignoreAll() 

class Command(object):
    """
    A Command can be either an Event or a Callback. An event is sent using the
    event manager while the callback is executed directly. The command also
    stores arguments and the key used to fire it. 
    """
    
    EVENT = 0x1
    CALLBACK = 0x2
    
    def __init__(self, name, type, args=[]):
        self.name= name
        self.args = args
        self.pressed = False
        self.type = type
        
    def __repr__(self):
        s = "%s name:\t %s\n args:\t %s\n pressed:\t %s\n type:\t %s\n"\
            % (self.__class__.__name__, self.name, self.args, self.pressed, self.type)
        return s

class InputScheme(object):
    """ 
    An InputScheme is a sequence of commands that can be enabled or disabled
    in the InputManager. 
    """
    def __init__(self, name):
        self._name = name
        self._enabled = True
        self._commands = {}
    
    def __repr__(self):
        return self._name
    
    def getCommands(self):
        return self._commands  
    
    def __repr__(self):
        return "Name: %s" % (self._name)
    
    name = property(fget=lambda self: self._name, fset=None)
    

class InputWatcher(object):
    """ An InputWatcher receives raw input events from the InputManager """
    
    def acceptUp(self, key):
        pass
    
    def acceptDown(self, key):
        pass
    
        
class InputManager(SafeDirectObject):
    """
    The InputManager class manage all the input coming from the player and
    send events according to the pressed buttons.
    
    It supports multiple InputScheme where a scheme is basically a map
    of commands. 
    There is a special input scheme, the BASE_SCHEME scheme, whose values are
    always checked. It is a sort of global input scheme whose commands are
    always executed.
    
    Each key can be bound to an unlimited number of commands.
    """
    
    def __init__(self, disableAlt=False):
        
        # get rid of alt button
        if disableAlt:
            self.disableAlt()
            
        base.buttonThrowers[0].node().setButtonDownEvent('button')
        base.buttonThrowers[0].node().setButtonUpEvent('buttonUp')
        base.buttonThrowers[0].node().setButtonRepeatEvent('buttonRepeat')
        
        self.accept("button", self._onButtonDown)
        self.accept("buttonRepeat", self._onButtonRepeat)
        self.accept("buttonUp", self._onButtonUp)
        
        self._currentScheme = InputScheme(BASE_SCHEME)
        self._schemes = {BASE_SCHEME: self._currentScheme}
        self._lastCmdTime = 0
        self._watchers = []
    
    
    def disableAlt(self):
        buttons = base.mouseWatcherNode.getModifierButtons()
        if buttons.getNumButtons() > 2:
            buttons.removeButton(buttons.getButton(2))
            base.mouseWatcherNode.setModifierButtons(buttons)
            base.buttonThrowers[0].node().setModifierButtons(buttons)
        
    def addWatcher(self, watcher):
        self._watchers.append(watcher)

    def removeWatcher(self, watcher):
        if watcher in self._watchers:
            self._watchers.remove(watcher)
    
    def switchSchemeTo(self, name):
        if name in self._schemes.keys():
            logger.info("Switching to scheme: %s" % name)
            self._currentScheme = self._schemes[name]
        else:
            logger.warning("Can't switch to non existent scheme: %s" % name)
{'fraction_non_alphanumeric': 0.09479024873159263, 'fraction_numerical': 0.004949882440292043, 'mean_word_length': 2.4508966695132366, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4646895', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2303, 'n_words': 680}