ó
X–Tc        
   @   s  d  Z  d d l Z y d d l Z Wn e k
 rA d d l Z n Xd d l Z d d l Z d d l Z d d l Z d d l	 m
 Z d „  Z
 d a d a d e f d „  ƒ  YZ d d d	 d
 d d d d d d g
 Z d d d d d g Z d „  Z d d d „ Z d d d „ Z d d d d d „ Z d d d d d „ Z d d d d d „ Z d d d d d „ Z d d „ Z d d „ Z d  „  Z d! e f d" „  ƒ  YZ d# e f d$ „  ƒ  YZ d% e f d& „  ƒ  YZ d' e f d( „  ƒ  YZ  d) e f d* „  ƒ  YZ! e! ƒ  Z" e  ƒ  Z# d S(+   sš  This module contains functions and classes for creating and issuing events.
For a list of the events that are built into Spyral, check the
:ref:`Event List<ref.events>`.

    .. attribute:: keys

        A special attribute for accessing the constants associated with a given
        key. For instance, ``spyral.keys.down`` and ``spyral.keys.f``. This is
        useful for testing for keyboard events. A complete list of all the key
        constants can be found in the
        :ref:`Keyboard Keys <ref.keys>` appendix.

    .. attribute:: mods

        A special attribute for accessing the constants associated with a given
        mod key. For instance, ``spyral.mods.lshift`` (left shift) and
        ``spyral.mods.ralt`` (Right alt). This is useful for testing for keyboard
        events. A complete list of all the key
        constants can be found in the
        :ref:`Keyboard Modifiers <ref.mods>` appendix.

iÿÿÿÿN(   t
   WeakMethodc         C   s'   y t  |  ƒ SWn t k
 r" |  SXd  S(   N(   t   _wmt	   TypeError(   t   func(    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyR    #   s    t   Eventc           B   s   e  Z d  Z d „  Z RS(   s9  
    A class for building for attaching data to an event.
    Keyword arguments will be named attributes of the Event when it is passed
    into :func:`queue <spyral.event.queue>`::

        collision_event = Event(ball=ball, paddle=paddle)
        spyral.event.queue("ball.collides.paddle", collision_event)
    c         K   s   |  j  j | ƒ d  S(   N(   t   __dict__t   update(   t   selft   kwargs(    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   __init__5   s    (   t   __name__t
   __module__t   __doc__R	   (    (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyR   ,   s   t   QUITt   ACTIVEEVENTt   KEYDOWNt   KEYUPt   MOUSEMOTIONt   MOUSEBUTTONUPt   VIDEORESIZEt   VIDEOEXPOSEt	   USEREVENTt   MOUSEBUTTONDOWNt   leftt   middlet   rightt	   scroll_upt   scroll_downc           C   sÊ   i	 t  ƒ  t j 6d t j 6d t j 6d t j 6d t j 6d t j 6d t j 6d t j	 6t  ƒ  t j
 6a i	 d t j 6d t j 6d t j 6d t j 6d t j 6d t j 6d t j 6d t j	 6d t j
 6a d S(   sp   
    Initializes the Event system, which requires mapping the Pygame event
    constants to Spyral strings.
    t   gaint   statet   unicodet   keyt   modt   post   relt   buttonst   buttont   sizet   wt   hs   system.quits   system.focus_changes   input.keyboard.downs   input.keyboard.ups   input.mouse.motions   input.mouse.ups   input.mouse.downs   system.video_resizes   system.video_exposeN(   R   s   state(   s   unicodes   keys   mod(   s   keys   mod(   s   poss   rels   buttons(   s   poss   button(   s   poss   button(   s   sizeR&   R'   (   t   tuplet   pygameR   R   R   R   R   R   R   R   R   t   _TYPE_TO_ATTRSt   _TYPE_TO_TYPE(    (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   _init>   s(    














c         C   s/   | d k r t j ƒ  } n  | j |  | ƒ d S(   s9  
    Queues a new event in the system, meaning that it will be run at the next
    available opportunity.

    :param str event_name: The type of event (e.g., ``"system.quit"``,
                           ``"input.mouse.up"``, or ``"pong.score"``.
    :param event: An Event object that holds properties for the event.
    :type event: :class:`Event <spyral.event.Event>`
    :param scene: The scene to queue this event on; if `None` is given, the
                   currently executing scene will be used.
    :type scene: :class:`Scene <spyral.Scene>` or `None`.
    N(   t   Nonet   spyralt   _get_executing_scenet   _queue_event(   t
   event_namet   eventt   scene(    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   queue]   s    c         C   s/   | d k r t j ƒ  } n  | j |  | ƒ d S(   st  
    Instructs spyral to execute the handlers for this event right now. When you
    have a custom event, this is the function you call to have the event occur.

    :param str event_name: The type of event (e.g., ``"system.quit"``,
                           ``"input.mouse.up"``, or ``"pong.score"``.
    :param event: An Event object that holds properties for the event.
    :type event: :class:`Event <spyral.event.Event>`
    :param scene: The scene to queue this event on; if ``None`` is given, the
                   currently executing scene will be used.
    :type scene: :class:`Scene <spyral.Scene>` or ``None``.
    N(   R-   R.   R/   t   _handle_event(   R1   R2   R3   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   handlen   s    i    c         C   sD   | d k r t j ƒ  } n  | j |  t | ƒ f | | | t ƒ d S(   sŸ  
    Registers an event `handler` to a namespace. Whenever an event in that
    `event_namespace` is fired, the event `handler` will execute with that
    event.

    :param event_namespace: the namespace of the event, e.g.
                            ``"input.mouse.left.click"`` or ``"pong.score"``.
    :type event_namespace: str
    :param handler: A function that will handle the event. The first
                    argument to the function will be the event.
    :type handler: function
    :param args: any additional arguments that need to be passed in
                 to the handler.
    :type args: sequence
    :param kwargs: any additional keyword arguments that need to be
                   passed into the handler.
    :type kwargs: dict
    :param int priority: the higher the `priority`, the sooner this handler will
                         be called in reaction to the event, relative to the
                         other event handlers registered.
    :param scene: The scene to register this event on; if it is ``None``, then
                  it will be attached to the currently running scene.
    :type scene: :class:`Scene <spyral.Scene>` or ``None``
    N(   R-   R.   R/   t   _reg_internalR    t   False(   t   event_namespacet   handlert   argsR   t   priorityR3   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   register   s    c         C   s>   | d k r t j ƒ  } n  | j |  | f | | | t ƒ d S(   sv  
    Similar to :func:`spyral.event.register` function, except that instead
    of passing in a function, you pass in the name of a property of this
    scene that holds a function.

    Example::

        class MyScene(Scene):
            def __init__(self):
                ...
                self.register_dynamic("orc.dies", "future_function")
                ...

    :param str event_namespace: The namespace of the event, e.g.
                                ``"input.mouse.left.click"`` or ``"pong.score"``.
    :param str handler: The name of an attribute on this scene that will hold
                        a function. The first argument to the function will be
                        the event.
    :param args: any additional arguments that need to be passed in
                 to the handler.
    :type args: sequence
    :param kwargs: any additional keyword arguments that need to be
                   passed into the handler.
    :type kwargs: dict
    :param int priority: the higher the `priority`, the sooner this handler will
                         be called in reaction to the event, relative to the
                         other event handlers registered.
    :param scene: The scene to register this event on; if it is ``None``, then
                  it will be attached to the currently running scene.
    :type scene: :class:`Scene <spyral.Scene>` or ``None``
    N(   R-   R.   R/   R7   t   True(   R9   t   handler_stringR;   R   R<   R3   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   register_dynamicž   s    !c         C   sD   | d k r t j ƒ  } n  | j |  t t | ƒ | | | t ƒ d S(   sN  
    Similar to :func:`spyral.event.register` function, except a sequence of
    `handlers` are be given instead of just one.

    :param str event_namespace: the namespace of the event, e.g.
                            ``"input.mouse.left.click"`` or ``"pong.score"``.
    :type event_namespace: string
    :param handler: A list of functions that will be run on this event.
    :type handler: list of functions
    :param args: any additional arguments that need to be passed in
                 to the handler.
    :type args: sequence
    :param kwargs: any additional keyword arguments that need to be
                   passed into the handler.
    :type kwargs: dict
    :param int priority: the higher the `priority`, the sooner this handler will
                         be called in reaction to the event, relative to the
                         other event handlers registered.
    :param scene: The scene to register this event on; if it is ``None``, then
                  it will be attached to the currently running scene.
    :type scene: :class:`Scene <spyral.Scene>` or ``None``
    N(   R-   R.   R/   R7   t   mapR    R8   (   R9   t   handlersR;   R   R<   R3   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   register_multipleÄ   s    c         C   s;   | d k r t j ƒ  } n  | j |  | | | | t ƒ d S(   sÎ  
    Similar to :func:`spyral.Scene.register` function, except a sequence of
    strings representing handlers can be given instead of just one.

    :param event_namespace: the namespace of the event, e.g.
                            ``"input.mouse.left.click"`` or ``"pong.score"``.
    :type event_namespace: string
    :param handler: A list of names of an attribute on this scene that will
                    hold a function. The first argument to the function will
                    be the event.
    :type handler: list of strings
    :param args: any additional arguments that need to be passed in
                 to the handler.
    :type args: sequence
    :param kwargs: any additional keyword arguments that need to be
                   passed into the handler.
    :type kwargs: dict
    :param int priority: the higher the `priority`, the sooner this handler will
                         be called in reaction to the event, relative to the
                         other event handlers registered.
    :param scene: The scene to register this event on; if it is ``None``, then
                  it will be attached to the currently running scene.
    :type scene: :class:`Scene <spyral.Scene>` or ``None``
    N(   R-   R.   R/   R7   R>   (   R9   t   handler_stringsR;   R   R<   R3   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   register_multiple_dynamicá   s    c         C   s/   | d k r t j ƒ  } n  | j |  | ƒ d S(   sË  
    Unregisters a registered handler for that namespace. Dynamic handler
    strings are supported as well.

    :param str event_namespace: An event namespace
    :param handler: The handler to unregister.
    :type handler: a function or string.
    :param scene: The scene to unregister the event; if it is ``None``, then
                  it will be attached to the currently running scene.
    :type scene: :class:`Scene <spyral.Scene>` or ``None``
    N(   R-   R.   R/   t   _unregister(   R9   R:   R3   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt
   unregister   s    c         C   s,   | d k r t j ƒ  } n  | j |  ƒ d S(   so  
    Clears all handlers from namespaces that are at least as specific as the
    provided `namespace`.

    :param str namespace: The complete namespace.
    :param scene: The scene to clear the namespace of; if it is ``None``, then
                  it will be attached to the currently running scene.
    :type scene: :class:`Scene <spyral.Scene>` or ``None``
    N(   R-   R.   R/   t   _clear_namespace(   t	   namespaceR3   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   clear_namespace  s    
c         C   s“  t  |  j } t |  j } t ƒ  } x' | D] } t | | t |  | ƒ ƒ q* W| j d ƒ r| t | d | j d ƒ d ƒ n  | j d ƒ r´ t j	 j
 |  j d ƒ } | d | 7} n  | j d ƒ rê t t |  j ƒ \ | _ | _ | _ nh | j d ƒ rRy% t |  j d	 } t | d
 | ƒ Wn  t k
 r@t |  j ƒ } n X| d | 7} n  | j d ƒ r‰t j | j ƒ t j j ƒ  j | _ n  | | f S(   se   
    Convert a Pygame event to a Spyral event, correctly converting arguments to
    attributes.
    t   inputt   typet   .iÿÿÿÿs   input.keyboardt   unknowns   input.mouse.motions   input.mousei   R$   (   R*   RL   R+   R   t   setattrt   getattrt
   startswitht   splitt   keyst   reverse_mapt   getR   RA   t   boolR#   R   R   R   t	   MOUSE_MAPR$   t
   IndexErrort   strR.   t   Vec2DR!   t   directort	   get_scenet   _scale(   R2   t   event_attrst
   event_typet   et   attrt   kt   m(    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   _pygame_to_spyral  s,    	 '(t   EventHandlerc           B   s,   e  Z d  Z d „  Z d „  Z g  d „ Z RS(   s#   
    Base event handler class.
    c         C   s   g  |  _  d |  _ d  S(   Ni    (   i    i    (   t   _eventst
   _mouse_pos(   R   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyR	   ?  s    	c         C   s   d S(   s  
        Should be called at the beginning of update cycle. For the
        event handler which is part of a scene, this function will be
        called automatically. For any additional event handlers, you
        must call this function manually.
        N(    (   R   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   tickC  s    c         C   sº   y | d Wn* t  k
 r n t k
 r8 | f } n X| g  k r[ |  j } g  |  _ | Sg  |  j D] } | d | k re | ^ qe } g  |  j D] } | d | k r‘ | ^ q‘ |  _ | S(   sˆ   
        Gets events from the event handler. Types is an optional
        iterable which has types which you would like to get.
        i    RL   (   RX   R   Rf   (   R   t   typest   retR`   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyRU   L  s    		,/(   R
   R   R   R	   Rh   RU   (    (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyRe   ;  s   			t   LiveEventHandlerc           B   s,   e  Z d  Z d d „ Z d „  Z d „  Z RS(   s  
    An event handler which pulls events from the operating system.

    The optional output_file argument specifies the path to a file
    where the event handler will save a custom json file that can
    be used with the `ReplayEventHandler` to show replays of a
    game in action, or be used for other clever purposes.

    .. note::

        If you use the output_file parameter, this function will
        reseed the random number generator, save the seed used. It
        will then be restored by the ReplayEventHandler.
    c         C   s   t  j |  ƒ | d  k	 |  _ |  j r‰ t | d ƒ |  _ t j d ƒ } i t j	 | ƒ d 6} t
 j | ƒ |  j j t j | ƒ d ƒ n  d  S(   NR&   i   t   random_seeds   
(   Re   R	   R-   t   _savet   opent   _filet   ost   urandomt   base64t   encodestringt   randomt   seedt   writet   jsont   dumps(   R   t   output_fileRu   t   info(    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyR	   q  s    	c         C   s;   t  j j ƒ  } t  j j ƒ  } | |  _ |  j j | ƒ d  S(   N(   R)   t   mouset   get_posR2   RU   Rg   Rf   t   extend(   R   R{   t   events(    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyRh   {  s    	c         C   s   |  j  r |  j j ƒ  n  d  S(   N(   Rm   Ro   t   close(   R   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   __del__„  s    	N(   R
   R   R   R-   R	   Rh   R€   (    (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyRk   b  s   
		t   ReplayEventHandlerc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   sp   
    An event handler which replays the events from a custom json
    file saved by the `LiveEventHandler`.
    c         C   s[   t  j |  ƒ t | ƒ |  _ t j |  j j ƒ  ƒ } t j t	 j
 | d ƒ ƒ t |  _ d  S(   NRl   (   Re   R	   Rn   Ro   Rw   t   loadst   readlineRt   Ru   Rr   t   decodestringR8   t   paused(   R   t
   input_fileRz   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyR	   Ž  s
    c         C   s   t  |  _ d S(   sg   
        Pauses the replay of the events, making tick() a noop until
        resume is called.
        N(   R>   R…   (   R   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   pause•  s    c         C   s   t  |  _ d S(   s/   
        Resumes the replay of events.
        N(   R8   R…   (   R   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   resumeœ  s    c         C   s”   |  j  r d  Sy t j |  j j ƒ  ƒ } Wn t k
 rI t j j ƒ  n X| d } g  | D] } t	 | ƒ ^ q[ } | d |  _
 |  j j | ƒ d  S(   NR~   R{   (   R…   Rw   R‚   Ro   Rƒ   t
   ValueErrorR.   R[   t   popt	   EventDictRg   Rf   R}   (   R   t   dR~   R`   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyRh   ¢  s    	
(   R
   R   R   R	   R‡   Rˆ   Rh   (    (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyR   ‰  s
   			t   Modsc           B   s   e  Z d  „  Z RS(   c         C   sˆ   t  j |  _ t  j |  _ t  j |  _ t  j |  _ t  j	 |  _
 t  j |  _ t  j |  _ t  j |  _ t  j |  _ t  j |  _ t  j |  _ d  S(   N(   R)   t	   KMOD_NONEt   nonet   KMOD_LSHIFTt   lshiftt   KMOD_RSHIFTt   rshiftt
   KMOD_SHIFTt   shiftt	   KMOD_CAPSt   capst	   KMOD_CTRLt   ctrlt
   KMOD_LCTRLt   lctrlt
   KMOD_RCTRLt   rctrlt	   KMOD_LALTt   laltt	   KMOD_RALTt   raltt   KMOD_ALTt   alt(   R   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyR	   ¯  s    (   R
   R   R	   (    (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyR   ®  s   t   Keysc           B   s,   e  Z d  „  Z d „  Z d „  Z d „  Z RS(   c         C   s7   i  |  _  |  j t j ƒ  d ƒ |  j d d g ƒ d  S(   Ns"   resources/default_key_mappings.txtt   returnt   entert   breakt   brk(   s   returnR¦   (   s   breakR¨   (   RT   t   load_keys_from_fileR.   t   _get_spyral_patht   _fix_bad_names(   R   (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyR	   ¾  s
    		c         C   sA   x: | D]2 \ } } t  |  | t |  | ƒ ƒ t |  | ƒ q Wd S(   sM   
        Used to replace any binding names with non-python keywords.
        N(   RO   RP   t   delattr(   R   t   renamest   originalt   new(    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyR«   Å  s    c         C   sñ   t  | ƒ } | j ƒ  } | j ƒ  xÈ | D]À } | d  j d ƒ } t | ƒ d k r) | d d d !d k r­ t |  | d t | d d ƒ ƒ | d |  j t | d d ƒ <qé t |  | d t | d ƒ ƒ | d |  j t | d ƒ <q) q) Wd  S(   Niÿÿÿÿt    i   i   i    t   0xi   (   Rn   t	   readlinesR   RR   t   lenRO   t   intRT   (   R   t   filenamet   fpt   key_mapst   single_mappingt   mapping(    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyR©   Î  s    
!!c         C   s   t  |  | | ƒ d  S(   N(   RO   (   R   t   namet   number(    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   add_key_mappingÜ  s    (   R
   R   R	   R«   R©   R¼   (    (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyR¤   ¼  s   				($   R   R)   Rw   t   ImportErrort
   simplejsonR.   Rp   Rt   Rr   t
   weakmethodR    R   R-   R*   R+   t   objectR   t   _EVENT_NAMESRW   R,   R4   R6   R=   R@   RC   RE   RG   RJ   Rd   Re   Rk   R   R   R¤   RS   t   mods(    (    (    s0   /usr/lib/python2.7/site-packages/spyral/event.pyt   <module>   sJ   			%	''%#	