b52bfb36d8c2d47756945ca6916e3da765691239
[minwii.git] / src / app / widgets / cursors.py
1 # -*- coding: utf-8 -*-
2 """
3 Curseurs winwii
4
5 $Id$
6 $URL$
7 """
8
9 import pygame
10 import os
11 from eventutils import EventHandlerMixin, event_handler
12 from itertools import cycle
13 from pygame.locals import USEREVENT
14 TIMEOUT = USEREVENT + 1
15
16 class WarpingCursor(pygame.sprite.Sprite, EventHandlerMixin):
17 '''
18 The class for animating the warping cursor
19
20 duration:
21 The duration of each image in the animation
22 centerPosition:
23 The Position of the center of the cursor
24 _imagePointer:
25 A pointer to the current image
26 _animationOffset:
27 The time elapsed since when the current image should have been displayed
28 '''
29 #screen = None
30 #images = None
31 #durations = None
32 #centerPosition = None
33 #_imagePointer = None
34 #_animationOffset = None
35
36 @staticmethod
37 def _get_theme_images(name) :
38 basePath = os.path.abspath(__file__).split(os.path.sep)[:-1]
39 basePath.append('data')
40 basePath.append(name)
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
44
45
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]]))
51
52 self.images = []
53 for img in images :
54 imagePath = os.path.sep.join([imagesPath, img])
55 img = pygame.image.load(imagePath).convert_alpha()
56 self.images.append(img)
57
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
63
64 self.image = self.images[0]
65 self.rect = pygame.Rect((0,0), (self.width, self.height))
66
67 surface = pygame.display.get_surface()
68 surface.blit(self.image, self.rect)
69 self._startBlink()
70
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
79 #self._flashTimer = 0
80
81 def _startBlink(self) :
82 pygame.time.set_timer(TIMEOUT, self.duration)
83 self.iterator = self.iterImages()
84
85 def iterImages(self) :
86 for img in cycle(self.images) :
87 yield img
88
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)
94
95 def update(self) :
96 print 'cursor update'
97
98 # def update(self, elapsedTime, centerPosition):
99 # '''
100 # Update the cursor's look and position
101 #
102 # elapsedTime:
103 # The time passed since the previous update
104 # centerPosition:
105 # the new position of the creep
106 # '''
107 # self._updateImage(elapsedTime)
108 # self.centerPosition = centerPosition
109 # if self.flashing :
110 # self._flashTimer += elapsedTime
111 # if self._flashTimer > self.flashLength:
112 # self.flashing = False
113
114 def _updateImage(self, elapsedTime):
115 '''
116 Update the cursor's image
117
118 elapsedTime:
119 The time passed since the previous update
120 '''
121 self._animationOffset += elapsedTime
122
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)
128
129 if self.flashing:
130 self.image = pygame.image.load(self.flashImagePath).convert_alpha()
131 else :
132 self.image = pygame.image.load(self.images[self._imagePointer]).convert_alpha()
133
134 def flash(self,flashLength = None):
135 self._flashTimer = 0
136 self.flashing = True
137 if flashLength:
138 self.flashlength = flashLength
139
140 def blit(self,surface):
141 '''
142 Draw the circle on surface
143 '''
144
145 newPos = (self.centerPosition[0] - self.image.get_width() / 2, self.centerPosition[1] - self.image.get_height() / 2)
146 surface.blit(self.image, newPos)