1 # -*- coding: utf-8 -*-
11 from eventutils
import EventHandlerMixin
, event_handler
12 from itertools
import cycle
13 from pygame
.locals import MOUSEBUTTONDOWN
, MOUSEBUTTONUP
, USEREVENT
14 TIMEOUT
= USEREVENT
+ 1
16 class WarpingCursor(pygame
.sprite
.Sprite
, EventHandlerMixin
):
18 The class for animating the warping cursor
22 def _get_theme_images(name
) :
23 basePath
= os
.path
.abspath(__file__
).split(os
.path
.sep
)[:-1]
24 basePath
.append('data')
26 basePath
= os
.path
.sep
.join(basePath
)
27 images
= [f
for f
in os
.listdir(basePath
) if os
.path
.splitext(f
)[1] == '.png']
28 return basePath
, images
31 def __init__(self
, theme
='black', duration
=75, blink
=True):
32 pygame
.sprite
.Sprite
.__init
__(self
)
33 imagesPath
, images
= WarpingCursor
._get
_theme
_images
(theme
)
34 flashImage
= images
.pop(images
.index('flash.png'))
35 flashImagePath
= os
.path
.sep
.join([imagesPath
, flashImage
])
36 self
.flashImage
= pygame
.image
.load(flashImagePath
).convert_alpha()
37 images
.sort(lambda a
, b
: cmp(*[int(os
.path
.splitext(f
)[0]) for f
in [a
, b
]]))
41 imagePath
= os
.path
.sep
.join([imagesPath
, img
])
42 img
= pygame
.image
.load(imagePath
).convert_alpha()
43 self
.images
.append(img
)
45 # assumes that all images have same dimensions
46 self
.width
= self
.images
[0].get_width()
47 self
.height
= self
.images
[0].get_height()
48 self
.duration
= duration
50 self
.image
= self
.images
[0]
51 self
.rect
= pygame
.Rect((0,0), (self
.width
, self
.height
))
58 def _startBlink(self
) :
59 pygame
.time
.set_timer(TIMEOUT
, self
.duration
)
60 self
.iterator
= self
.iterImages()
62 def iterImages(self
) :
63 for img
in cycle(self
.images
) :
66 @event_handler(TIMEOUT
)
67 def loadNext(self
, event
) :
69 self
.image
= self
.iterator
.next()
72 @event_handler(MOUSEBUTTONDOWN
)
73 def flashOn(self
, event
) :
75 self
.image
= self
.flashImage
78 @event_handler(MOUSEBUTTONUP
)
79 def flashOff(self
, event
) :
84 surface
= pygame
.display
.get_surface()
85 surface
.blit(self
.image
, self
.rect
)