29ce61cb41d8ff890947e686b2bd72f0c25a4590
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
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, blink
=True):
47 pygame
.sprite
.Sprite
.__init
__(self
)
48 imagesPath
, images
= WarpingCursor
._get
_theme
_images
(theme
)
49 flashImage
= images
.pop(images
.index('flash.png'))
50 flashImagePath
= os
.path
.sep
.join([imagesPath
, flashImage
])
51 self
.flashImage
= pygame
.image
.load(flashImagePath
).convert_alpha()
52 images
.sort(lambda a
, b
: cmp(*[int(os
.path
.splitext(f
)[0]) for f
in [a
, b
]]))
56 imagePath
= os
.path
.sep
.join([imagesPath
, img
])
57 img
= pygame
.image
.load(imagePath
).convert_alpha()
58 self
.images
.append(img
)
60 # assumes that all images have same dimensions
61 self
.width
= self
.images
[0].get_width()
62 self
.height
= self
.images
[0].get_height()
63 self
.duration
= duration
65 self
.image
= self
.images
[0]
66 self
.rect
= pygame
.Rect((0,0), (self
.width
, self
.height
))
68 surface
= pygame
.display
.get_surface()
69 surface
.blit(self
.image
, self
.rect
)
74 #self.flashImagePath = flashImage
75 #self.durations = durations
76 #self.centerPosition = initCenterPosition
77 #self.flashLength = 100
78 #self.flashing = False
79 #self.image = pygame.image.load(self.images[0]).convert_alpha()
80 #self._imagePointer = 0
81 #self._animationOffset = 0
84 def _startBlink(self
) :
85 pygame
.time
.set_timer(TIMEOUT
, self
.duration
)
86 self
.iterator
= self
.iterImages()
88 def iterImages(self
) :
89 for img
in cycle(self
.images
) :
92 @event_handler(TIMEOUT
)
93 def loadNext(self
, event
) :
95 self
.image
= self
.iterator
.next()
96 surface
= pygame
.display
.get_surface()
97 surface
.blit(self
.image
, self
.rect
)
99 @event_handler(MOUSEBUTTONDOWN
)
100 def flashOn(self
, event
) :
102 self
.image
= self
.flashImage
103 surface
= pygame
.display
.get_surface()
104 surface
.blit(self
.image
, self
.rect
)
106 @event_handler(MOUSEBUTTONUP
)
107 def flashOff(self
, event
) :
112 print 'cursor update'
114 # def update(self, elapsedTime, centerPosition):
116 # Update the cursor's look and position
119 # The time passed since the previous update
121 # the new position of the creep
123 # self._updateImage(elapsedTime)
124 # self.centerPosition = centerPosition
126 # self._flashTimer += elapsedTime
127 # if self._flashTimer > self.flashLength:
128 # self.flashing = False
130 def _updateImage(self
, elapsedTime
):
132 Update the cursor's image
135 The time passed since the previous update
137 self
._animationOffset
+= elapsedTime
139 if self
._animationOffset
> self
.duration
:
140 #New animation offset is computed first, before updating the pointer
141 self
._animationOffset
-= self
.duration
142 #point to the next image (restarts from the beginning when it reaches the end)
143 self
._imagePointer
= (self
._imagePointer
+ 1) % len(self
.images
)
146 self
.image
= pygame
.image
.load(self
.flashImagePath
).convert_alpha()
148 self
.image
= pygame
.image
.load(self
.images
[self
._imagePointer
]).convert_alpha()
150 def flash(self
,flashLength
= None):
154 self
.flashlength
= flashLength
156 def blit(self
,surface
):
158 Draw the circle on surface
161 newPos
= (self
.centerPosition
[0] - self
.image
.get_width() / 2, self
.centerPosition
[1] - self
.image
.get_height() / 2)
162 surface
.blit(self
.image
, newPos
)