gestion du flash du curseur.
[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 MOUSEBUTTONDOWN, MOUSEBUTTONUP, 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, 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]]))
53
54 self.images = []
55 for img in images :
56 imagePath = os.path.sep.join([imagesPath, img])
57 img = pygame.image.load(imagePath).convert_alpha()
58 self.images.append(img)
59
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
64
65 self.image = self.images[0]
66 self.rect = pygame.Rect((0,0), (self.width, self.height))
67
68 surface = pygame.display.get_surface()
69 surface.blit(self.image, self.rect)
70 self.blink = blink
71 if blink :
72 self._startBlink()
73
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
82 #self._flashTimer = 0
83
84 def _startBlink(self) :
85 pygame.time.set_timer(TIMEOUT, self.duration)
86 self.iterator = self.iterImages()
87
88 def iterImages(self) :
89 for img in cycle(self.images) :
90 yield img
91
92 @event_handler(TIMEOUT)
93 def loadNext(self, event) :
94 if self.blink :
95 self.image = self.iterator.next()
96 surface = pygame.display.get_surface()
97 surface.blit(self.image, self.rect)
98
99 @event_handler(MOUSEBUTTONDOWN)
100 def flashOn(self, event) :
101 self.blink=False
102 self.image = self.flashImage
103 surface = pygame.display.get_surface()
104 surface.blit(self.image, self.rect)
105
106 @event_handler(MOUSEBUTTONUP)
107 def flashOff(self, event) :
108 self.blink = True
109 self.loadNext(event)
110
111 def update(self) :
112 print 'cursor update'
113
114 # def update(self, elapsedTime, centerPosition):
115 # '''
116 # Update the cursor's look and position
117 #
118 # elapsedTime:
119 # The time passed since the previous update
120 # centerPosition:
121 # the new position of the creep
122 # '''
123 # self._updateImage(elapsedTime)
124 # self.centerPosition = centerPosition
125 # if self.flashing :
126 # self._flashTimer += elapsedTime
127 # if self._flashTimer > self.flashLength:
128 # self.flashing = False
129
130 def _updateImage(self, elapsedTime):
131 '''
132 Update the cursor's image
133
134 elapsedTime:
135 The time passed since the previous update
136 '''
137 self._animationOffset += elapsedTime
138
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)
144
145 if self.flashing:
146 self.image = pygame.image.load(self.flashImagePath).convert_alpha()
147 else :
148 self.image = pygame.image.load(self.images[self._imagePointer]).convert_alpha()
149
150 def flash(self,flashLength = None):
151 self._flashTimer = 0
152 self.flashing = True
153 if flashLength:
154 self.flashlength = flashLength
155
156 def blit(self,surface):
157 '''
158 Draw the circle on surface
159 '''
160
161 newPos = (self.centerPosition[0] - self.image.get_width() / 2, self.centerPosition[1] - self.image.get_height() / 2)
162 surface.blit(self.image, newPos)