b52bfb36d8c2d47756945ca6916e3da765691239
1 # -*- coding: utf-8 -*-
11 from eventutils
import EventHandlerMixin
, event_handler
12 from itertools
import cycle
13 from pygame
.locals import USEREVENT
14 TIMEOUT
= USEREVENT
+ 1
16 class WarpingCursor(pygame
.sprite
.Sprite
, EventHandlerMixin
):
18 The class for animating the warping cursor
21 The duration of each image in the animation
23 The Position of the center of the cursor
25 A pointer to the current image
27 The time elapsed since when the current image should have been displayed
32 #centerPosition = None
34 #_animationOffset = None
37 def _get_theme_images(name
) :
38 basePath
= os
.path
.abspath(__file__
).split(os
.path
.sep
)[:-1]
39 basePath
.append('data')
41 basePath
= os
.path
.sep
.join(basePath
)
42 images
= [f
for f
in os
.listdir(basePath
) if os
.path
.splitext(f
)[1] == '.png']
43 return basePath
, images
46 def __init__(self
, theme
='black', duration
=75):
47 pygame
.sprite
.Sprite
.__init
__(self
)
48 imagesPath
, images
= WarpingCursor
._get
_theme
_images
(theme
)
49 self
.flashImage
= images
.pop(images
.index('flash.png'))
50 images
.sort(lambda a
, b
: cmp(*[int(os
.path
.splitext(f
)[0]) for f
in [a
, b
]]))
54 imagePath
= os
.path
.sep
.join([imagesPath
, img
])
55 img
= pygame
.image
.load(imagePath
).convert_alpha()
56 self
.images
.append(img
)
58 self
._imageLength
= len(self
.images
)
59 # assumes that all images have same dimensions
60 self
.width
= self
.images
[0].get_width()
61 self
.height
= self
.images
[0].get_height()
62 self
.duration
= duration
64 self
.image
= self
.images
[0]
65 self
.rect
= pygame
.Rect((0,0), (self
.width
, self
.height
))
67 surface
= pygame
.display
.get_surface()
68 surface
.blit(self
.image
, self
.rect
)
71 #self.flashImagePath = flashImage
72 #self.durations = durations
73 #self.centerPosition = initCenterPosition
74 #self.flashLength = 100
75 #self.flashing = False
76 #self.image = pygame.image.load(self.images[0]).convert_alpha()
77 #self._imagePointer = 0
78 #self._animationOffset = 0
81 def _startBlink(self
) :
82 pygame
.time
.set_timer(TIMEOUT
, self
.duration
)
83 self
.iterator
= self
.iterImages()
85 def iterImages(self
) :
86 for img
in cycle(self
.images
) :
89 @event_handler(TIMEOUT
)
90 def loadNext(self
, event
) :
91 self
.image
= self
.iterator
.next()
92 surface
= pygame
.display
.get_surface()
93 surface
.blit(self
.image
, self
.rect
)
98 # def update(self, elapsedTime, centerPosition):
100 # Update the cursor's look and position
103 # The time passed since the previous update
105 # the new position of the creep
107 # self._updateImage(elapsedTime)
108 # self.centerPosition = centerPosition
110 # self._flashTimer += elapsedTime
111 # if self._flashTimer > self.flashLength:
112 # self.flashing = False
114 def _updateImage(self
, elapsedTime
):
116 Update the cursor's image
119 The time passed since the previous update
121 self
._animationOffset
+= elapsedTime
123 if self
._animationOffset
> self
.duration
:
124 #New animation offset is computed first, before updating the pointer
125 self
._animationOffset
-= self
.duration
126 #point to the next image (restarts from the beginning when it reaches the end)
127 self
._imagePointer
= (self
._imagePointer
+ 1) % len(self
.images
)
130 self
.image
= pygame
.image
.load(self
.flashImagePath
).convert_alpha()
132 self
.image
= pygame
.image
.load(self
.images
[self
._imagePointer
]).convert_alpha()
134 def flash(self
,flashLength
= None):
138 self
.flashlength
= flashLength
140 def blit(self
,surface
):
142 Draw the circle on surface
145 newPos
= (self
.centerPosition
[0] - self
.image
.get_width() / 2, self
.centerPosition
[1] - self
.image
.get_height() / 2)
146 surface
.blit(self
.image
, newPos
)