ó
XTc           @   sx   d  Z  d d l Z d d l Z d d l Z d   Z e j j d    Z d e f d     YZ	 d e f d     YZ
 d S(	   s   When they have no other home, functions and classes are added here.
Eventually, they should be refactored to a more permanent home.i’’’’Nc         C   s:  | } | } |  } | d k r' d } n| d k rB | d f } nė | d k ra | d d f } nĢ | d k r| d | f } n± | d k r | | f } n | d k r¶ | d | f } nw | d	 k rÕ d | d f } nX | d
 k rō | | d f } n9 | d k r| d | d f } n | t  j d d  } t  j |  S(   sw  
    Given an `anchor` position (either a string or a 2-tuple position), finds
    the correct offset in a rectangle of size (`width`, `height`). If the
    `anchor` is a 2-tuple (or Vec2D), then it multiplies both components by -1.

    >>> anchor_offset("topleft", 100, 100)
    Vec2D(0,0)
    >>> anchor_offset("bottomright", 100, 100)
    Vec2D(100,100)
    >>> anchor_offset("center", 100, 100)
    Vec2D(50,50)
    >>> anchor_offset((10, 10), 100, 100)
    Vec2D(-10,-10)

    For a complete list of the anchor positions, see `Anchor Offset Lists`_.

    :param anchor: The (possibly named) position to offset by.
    :type anchor: string or :class:`Vec2D <spyral.Vec2D>`
    :param width: the width of the rectangle to offset in.
    :type width: int
    :param height: the height of the rectangle to offset in.
    :type height: int
    :rtype: :class:`Vec2D <spyral.Vec2D>`
    t   toplefti    t   toprightt   midtopg       @t
   bottomleftt   bottomrightt	   midbottomt   midleftt   midrightt   centeri’’’’(   i    i    (   t   spyralt   Vec2D(   t   anchort   widtht   heightt   wt   ht   at   offset(    (    s/   /usr/lib/python2.7/site-packages/spyral/util.pyt   _anchor_offset   s.    	c         C   sp   t  t j | d   t  t j | d   f } | |  j   k rH |  St j j |  | t j j	 |   } | S(   s!  
    Internal method to scale a surface `s` by a float `factor`. Uses memoization
    to improve performance.

    :param target_size: The end size of the surface
    :type target_size: :class:`Vec2D <spyral.Vec2D>`
    :returns: A new surface, or the original (both pygame surfaces).
    i    i   (
   t   intt   matht   ceilt   get_sizet   pygamet	   transformt   smoothscaleR	   t   imaget   _new_spyral_surface(   t   st   target_sizet   new_sizet   t(    (    s/   /usr/lib/python2.7/site-packages/spyral/util.pyt   scale_surface:   s    
t   _Blitc        	   B   sS   e  Z d  Z d d d d d d d d d	 g	 Z d
   Z d   Z d   Z d   Z RS(   s  
    An internal class to represent a drawable `surface` with additional data
    (e.g. `rect` representing its location on screen, whether it's `static`).

    .. attribute::surface

        The internal Pygame source surface use to render this blit.

    .. attribute::position

        The current position of this Blit (:class:`Vec2D <spyral.Vec2D>`).

    .. attribute::area

        The portion of the surface to be drawn to the screen
        (:class:`Rect <spyral.Rect>`).

    .. attribute::layer

        The layer of this blit within the scene (`str).

    .. attribute::flags

        Any drawing flags (presently unused).

    .. attribute::static

        Whether this Blit is static.

    .. attribute::final_size

        The final size, set seperately to defer scaling.

    .. attribute::rect

        A rect representing the final position and size of this blit
        (:class:`Rect <spyral.Rect>`).

    t   surfacet   positiont   rectt   areat   layert   flagst   statict   clippingt
   final_sizec         C   s[   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ t j | j    |  _	 d  |  _ d  S(   N(   R"   R#   R%   R&   R'   R(   R	   R
   R   R*   t   NoneR$   (   t   selfR"   R#   R%   R&   R'   R(   (    (    s/   /usr/lib/python2.7/site-packages/spyral/util.pyt   __init__v   s    						c         C   sM   |  j  | |  _  |  j | |  _ t j |  j j | |  j j |  |  _ d S(   s   
        Applies the scaling factor to this blit.

        :param scale: The scaling factor
        :type scale: :class:`Vec2D <spyral.Vec2D>`
        N(   R#   R*   R	   t   RectR%   R    t   size(   R,   t   scale(    (    s/   /usr/lib/python2.7/site-packages/spyral/util.pyt   apply_scale   s    c         C   s"   |  j  j t j |   |  _  d S(   s„   
        Applies any necessary cropping to this blit

        :param rect: The new maximal size of the blit.
        :type rect: :class:`Rect <spyral.Rect>`
        N(   R%   t   clipR	   R.   (   R,   R$   (    (    s/   /usr/lib/python2.7/site-packages/spyral/util.pyR2      s    c         C   sl   t  |  j |  j  |  _ |  j j |  j j    |  _ t j |  j d |  j d f |  j j	    |  _
 d S(   sd   
        Performs all the final calculations for this blit and calculates the
        rect.
        i    i   N(   R    R"   R*   t
   subsurfaceR%   t
   _to_pygameR   R.   R#   R   R$   (   R,   (    (    s/   /usr/lib/python2.7/site-packages/spyral/util.pyt   finalize   s    (   t   __name__t
   __module__t   __doc__t	   __slots__R-   R1   R2   R5   (    (    (    s/   /usr/lib/python2.7/site-packages/spyral/util.pyR!   L   s   '				t   _CollisionBoxc           B   sA   e  Z d  Z d d d g Z d   Z d   Z d   Z d   Z RS(   s  
    An internal class for managing the collidable area for a sprite or view.
    In many ways, this is a reduced form of a _Blit.

    .. attribute::position

        The current position of this CollisionBox
        (:class:`Vec2D <spyral.Vec2D>`).

    .. attribute::area

        The current offset and size of this CollisionBox.
        (:class:`Rect <spyral.Rect>`).

    .. attribute::rect

        A rect representing the final position and size of this CollisionBox.
        (:class:`Rect <spyral.Rect>`).

    R#   R$   R%   c         C   s   | |  _  | |  _ d  |  _ d  S(   N(   R#   R%   R+   R$   (   R,   R#   R%   (    (    s/   /usr/lib/python2.7/site-packages/spyral/util.pyR-   ø   s    		c         C   s=   |  j  | |  _  t j |  j j | |  j j |  |  _ d  S(   N(   R#   R	   R.   R%   R    R/   (   R,   R0   (    (    s/   /usr/lib/python2.7/site-packages/spyral/util.pyR1   ½   s    c         C   s"   |  j  j t j |   |  _  d  S(   N(   R%   R2   R	   R.   (   R,   R$   (    (    s/   /usr/lib/python2.7/site-packages/spyral/util.pyR2   Ā   s    c         C   s"   t  j |  j |  j j  |  _ d  S(   N(   R	   R.   R#   R%   R/   R$   (   R,   (    (    s/   /usr/lib/python2.7/site-packages/spyral/util.pyR5   Å   s    (   R6   R7   R8   R9   R-   R1   R2   R5   (    (    (    s/   /usr/lib/python2.7/site-packages/spyral/util.pyR:   ¢   s   			(   R8   R	   R   R   R   t   memoizet   _ImageMemoizeR    t   objectR!   R:   (    (    (    s/   /usr/lib/python2.7/site-packages/spyral/util.pyt   <module>   s   	2V