
XTc           @   s8   d  Z  d d l Z d d l Z d e f d     YZ d S(   sA   
Rects are a convenience class for managing rectangular regions.
iNt   Rectc           B   s   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s  
    Rect represents a rectangle and provides some useful features. Rects can 
    be specified 3 ways in the constructor:

    #. Four numbers, ``x``, ``y``, ``width``, ``height``
    #. Two tuples, ``(x, y)`` and `(width, height)`
    #. Another rect, which is copied

    >>> rect1 = spyral.Rect(10, 10, 64, 64)               # Method 1
    >>> rect2 = spyral.Rect((10, 10), (64, 64))           # Method 2
    >>> rect3 = spyral.Rect(rect1.topleft, rect1.size)    # Method 2
    >>> rect4 = spyral.Rect(rect3)                        # Method 3

    Rects support all the usual :ref:`anchor points <ref.anchors>` as
    attributes, so you can both get ``rect.center`` and assign to it.
    Rects also support attributes of ``right``, ``left``, ``top``, ``bottom``,
    ``x``, and ``y``.

    >>> rect1.x
    10
    >>> rect1.centerx
    42.0
    >>> rect1.width
    64
    >>> rect1.topleft
    Vec2D(10, 10)
    >>> rect1.bottomright
    Vec2D(74, 74)
    >>> rect1.center
    Vec2D(42.0, 42.0)
    >>> rect1.size
    Vec2D(64, 64)

    c         G   s   t  |  d k rQ | d } | j | j |  _ |  _ | j | j |  _ |  _ n t  |  d k r | d \ |  _ |  _ | d \ |  _ |  _ n? t  |  d k r | \ |  _	 |  _
 |  _ |  _ n t d   d  S(   Ni   i    i   i   s   You done goofed.(   t   lent   xt   yt   _xt   _yt   wt   ht   _wt   _ht   leftt   topt   widtht   heightt
   ValueError(   t   selft   argst   r(    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   __init__+   s    
!c         C   s  t  j } | d k r# |  j |  j S| d k s; | d k rB |  j S| d k sZ | d k ra |  j S| d k r{ |  j |  j S| d k r | |  j |  j |  j  S| d k r | |  j |  j |  j  S| d	 k s | d
 k r | |  j |  j  S| d k r| |  j |  j |  j |  j  S| d k r=|  j |  j d S| d k r[|  j |  j d S| d k r| |  j |  j d |  j |  j d  S| d k r| |  j |  j |  j d  S| d k r| |  j |  j |  j |  j d  S| d k r| |  j |  j d |  j  S| d k rF| |  j |  j d |  j |  j  S| d k re| |  j |  j  S| d k s}| d k r|  j S| d k s| d k r|  j St d |   d  S(   Nt   rightR
   R   R   R   t   bottomt   toprightt
   bottomleftt   topleftt   post   bottomrightt   centerxg       @t   centeryt   centert   midleftt   midrightt   midtopt	   midbottomt   sizeR   R   R   R   s(   type object 'rect' has no attribute '%s'(   t   spyralt   Vec2DR   R   R   R	   t   AttributeError(   R   t   namet   v(    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   __getattr__8   sN    	!)%%c         C   s5  | d d k r' t  |  |  j | <d  S| d k rF | |  j |  _ n| d k r^ | |  _ n| d k rv | |  _ n| d k r | |  j |  _ n| d k s | d k r | \ |  _ |  _ no| d	 k r | d |  j |  _ | d
 |  _ n?| d k r"| d |  _ | d
 |  j |  _ n| d k rY| d |  j |  _ | d |  j |  _ n| d k sq| d k r}| |  _ n| d k s| d k r| |  _ n| d k r| \ |  _ |  _ no| d k r| |  j d |  _ nL| d k r| |  j d |  _ n)| d k rG| d |  j d |  _ | d
 |  j d |  _ n | d k r{| d |  j d |  _ | d
 |  _ n | d k r| d |  _ | d
 |  j d |  _ n | d k r| d |  j d |  _ | d
 |  j |  _ nG | d k r%| d |  j |  _ | d
 |  j d |  _ n t d   d  S(   Ni    t   _R   R
   R   R   R   R   R   i   R   R   R   R   R   R   R!   R   g       @R   R   R   R   R    R   s   You done goofed!(   t   intt   __dict__R   R   R   R	   R$   (   R   R%   t   val(    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   __setattr__b   s`    c         C   s   t  |  j |  j |  j |  j  S(   sb   
        Returns a copy of this rect

        :returns: A new :class:`Rect <spyral.Rect>`
        (   R    R   R   R   R	   (   R   (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   copy   s    c         C   s   t  | | |  j |  j  S(   s   
        Returns a copy of this rect offset by *x* and *y*.

        :param float x: The horizontal offset.
        :param float y: The vertical offset.
        :returns: A new :class:`Rect <spyral.Rect>`
        (   R    R   R	   (   R   R   R   (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   move   s    c         C   s%   |  j  | |  j | |  _  |  _ d S(   s   
        Moves this rect by *x* and *y*.

        :param float x: The horizontal offset.
        :param float y: The vertical offset.
        N(   R   R   (   R   R   R   (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   move_ip   s    c         C   s?   |  j  } |  j   } |  j | |  j | f | _ | | _  | S(   s   
        Returns a copy of this rect inflated by *width* and *height*.

        :param float width: The amount to add horizontally.
        :param float height: The amount to add vertically.
        :returns: A new :class:`Rect <spyral.Rect>`
        (   R   R-   R   R	   R!   (   R   R   R   t   ct   n(    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   inflate   s
    		c         C   s3   |  j  } |  j | |  j | f |  _ | |  _  d S(   s   
        Inflates this rect by *width*, *height*.

        :param float width: The amount to add horizontally.
        :param float height: The amount to add vertically.
        N(   R   R   R	   R!   (   R   R   R   R0   (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt
   inflate_ip   s    	c         C   su   t  |  j | j  } t  |  j | j  } t |  j | j  } t |  j | j  } t | | f | | | | f  S(   s>  
        Returns a new rect which represents the union of this rect
        with other -- in other words, a new rect is created that can fit both
        original rects.

        :param other: The other Rect.
        :type other: :class:`Rect <spyral.Rect>`
        :returns: A new :class:`Rect <spyral.Rect>`
        (   t   minR   R
   t   maxR   R   R    (   R   t   otherR   R
   R   R   (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   union   s
    
c         C   s~   t  |  j | j  } t  |  j | j  } t |  j | j  } t |  j | j  } | | |  _ |  _ | | |  _ |  _ d S(   s   
        Modifies this rect to be the union of it and the other -- in other
        words, this rect will expand to include the other rect.

        :param other: The other Rect.
        :type other: :class:`Rect <spyral.Rect>`
        N(   R4   R   R
   R5   R   R   (   R   R6   R   R
   R   R   (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   union_ip   s    c         C   s  | } |  } y | j  Wn t k
 r6 t |  } n X| j  | j  k rn | j  | j  | j k  rn | j  } nP | j  | j  k r | j  | j  | j k  r | j  } n t | j  | j d d  S| j  | j | j  k r| j  | j | j  | j k r| j  | j | } ni | j  | j | j  k r^| j  | j | j  | j k r^| j  | j | } n t | j  | j d d  S| j | j k r| j | j | j k  r| j } nP | j | j k r| j | j | j k  r| j } n t | j  | j d d  S| j | j | j k rN| j | j | j | j k rN| j | j | } ni | j | j | j k r| j | j | j | j k r| j | j | } n t | j  | j d d  St | | | |  S(   s;  
        Returns a Rect which is cropped to be completely inside of other.
        If the other does not overlap with this rect, a rect of size 0 is
        returned.

        :param other: The other Rect.
        :type other: :class:`Rect <spyral.Rect>`
        :returns: A new :class:`Rect <spyral.Rect>`
        i    (   R   t	   TypeErrorR    R   R   R	   (   R   R6   t   Bt   AR   R   R   R   (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   clip   s6    
++99++99c         C   s,   |  j  |  } | j | j |  _ |  _ d S(   s  
        Modifies this rect to be cropped completely inside of other.
        If the other does not overlap with this rect, this rect will have a size
        of 0.

        :param other: The other Rect.
        :type other: :class:`Rect <spyral.Rect>`
        N(   R<   R   R!   (   R   R6   t   new_rect(    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   clip_ip  s    	c         C   s"   | j  |  j  o! | j  |  j  S(   s  
        Returns `True` if the other rect is contained inside this rect.

        :param other: The other Rect.
        :type other: :class:`Rect <spyral.Rect>`
        :returns: A `bool` indicating whether this rect is contained within
                  another.
        (   t   collide_pointR   R   (   R   R6   (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   contains%  s    	c         C   s.   |  j  |  j d k p- | j  |   j d k S(   s
  
        Returns `True` if this rect collides with the other rect.

        :param other: The other Rect.
        :type other: :class:`Rect <spyral.Rect>`
        :returns: A `bool` indicating whether this rect is contained within
                  another.
        i    (   i    i    (   i    i    (   R<   R!   (   R   R6   (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   collide_rect1  s    	c         C   sJ   | d |  j  k oI | d |  j k  oI | d |  j k oI | d |  j k  S(   s   
        :param point: The point.
        :type point: :class:`Vec2D <spyral.Vec2D>`
        :returns: A `bool` indicating whether the point is contained within this
                  rect.
        i    i   (   R
   R   R   R   (   R   t   point(    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyR?   =  s    &c         C   s+   t  j |  j |  j f |  j |  j f f  S(   s   
        Internal method for creating a Pygame compatible rect from this rect.

        :returns: A :class:`pygame.Rect`
        (   t   pygameR    R
   R   R   R   (   R   (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt
   _to_pygameH  s    c      
   C   sL   d j  d t |  j  d t |  j  d t |  j  d t |  j  d g	  S(   Nt    s   <rect(t   ,s   )>(   t   joint   strR   R   R   R	   (   R   (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   __str__P  s    	c         C   s
   |  j    S(   N(   RI   (   R   (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   __repr__[  s    (   t   __name__t
   __module__t   __doc__R   R'   R,   R-   R.   R/   R2   R3   R7   R8   R<   R>   R@   RA   R?   RD   RI   RJ   (    (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyR       s&   "		*	4		
							/						(   RM   RC   R"   t   objectR    (    (    (    s/   /usr/lib/python2.7/site-packages/spyral/rect.pyt   <module>   s   