Rotation mutators
[naja.git] / naja / resources / mutators.py
1 '''Mutations to apply to images'''
2
3 from pygame.transform import rotate, flip
4
5
6 class Mutator(object):
7     def __init__(self, func, *args):
8         self._func = func
9         self._args = tuple(args)
10
11     def __call__(self, image):
12         return self._func(image, *self._args)
13
14     def __hash__(self):
15         return hash((self._func, self._args))
16
17     def __eq__(self, other):
18         if not isinstance(other, Mutator):
19             return NotImplemented
20         return (self._func == other._func) and (self._args == other._args)
21
22     def __repr__(self):
23         return '<%s %r>' % (self.__class__.__name__, self._args)
24
25
26 def rotator(angle):
27     return Mutator(rotate, angle)
28
29
30 # Identity mutator
31 NULL = Mutator(lambda x: x)
32
33 # Rotation
34 R90 = rotator(90)
35 R180 = rotator(180)
36 R270 = rotator(-90)
37
38 FLIP_H = Mutator(flip, True, False)
39 FLIP_V = Mutator(flip, False, True)