"""Subclassable Rect for Older Python

UserRect, python class wrapped around the pygame Rect type. This allows
you to do things like inherit the rectangle object into a sprite class. While
this makes for some neat features, current testing has shown there is a bit of
a peformance penalty. (as opposed to just keeping a Rect value inside the class)

Note, this might be worthless now, as python-2.2 allows you to subclass real
python types, no need for this placeholder. Still, it may provide some assistance
to somebody out there.

Before the class/type unification that was included in Python 2.2, data
types implemented in C (such as pygame's Rect class) weren't considered
true classes - they couldn't be subclassed. Pete included the UserRect
module in the pygame distribution to get around that - it's just the
thinnest possible python wrapper around Rect. I dont think anyone ever
used it for anything, but in case someone misses it, here it is. It
might be handy for a game like Breakout, where everything is
rectangular, and you could create a simple sprite class as
"class Sprite(UserRect):". However, it would be cleaner and faster
just to upgrade to python 2.2, and subclass the actual Rect class.
"""
__author__ = "Pete Shinners"
__copyright__ = "Copyright (c) 2001 Pete Shinners"
__license__ = "Public Domain"
__version__ = "1.0"


from pygame import Rect


class UserRect:
    """UserRect() -> class instance
Python class for the pygame Rect type

This python class can be treated exactly like a normal Rect
object. The only difference is it is a real python class
object, not a C extension type.
"""
    def __init__(self, *args):
        try: self.__dict__['rect'] = apply(Rect, args)
        except TypeError:
            raise TypeError, 'Argument must be rectstyle object'
        for a in dir(self.rect):
            self.__dict__[a] = getattr(self.rect, a)

    def __getattr__(self, name):
        return getattr(self.rect, name)
    def __setattr__(self, name, val):
        if name is 'rect':
            self.__dict__['rect'][:] = val
        else:
            try: setattr(self.__dict__['rect'], name, val)
            except (AttributeError, KeyError): self.__dict__[name] = val
    def __len__(self): return 4
    def __getitem__(self, i): return self.rect[i]
    def __setitem__(self, i, val): self.rect[i] = val
    def __getslice__(self, i, j): return self.rect[i:j]
    def __setslice__(self, i, j, val): self.rect[i:j] = val
    def __nonzero__(self): return nonzero(self.rect)
    def __cmp__(self, o): return cmp(self.rect, o)
    def __repr__(self):
        return '<UserRect(%d, %d, %d, %d)>' % tuple(self.rect)
    def __str__(self):
        return '<UserRect(%d, %d, %d, %d)>' % tuple(self.rect)

