modification de la boucle principale pour mettre à jour l'affichage un respectant...
[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
21 @staticmethod
22 def _get_theme_images(name) :
23 basePath = os.path.abspath(__file__).split(os.path.sep)[:-1]
24 basePath.append('data')
25 basePath.append(name)
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
29
30
31 def __init__(self, theme='black', duration=75, blink=True):
32 pygame.sprite.Sprite.__init__(self)
33 pygame.mouse.set_visible(False)
34 imagesPath, images = WarpingCursor._get_theme_images(theme)
35 flashImage = images.pop(images.index('flash.png'))
36 flashImagePath = os.path.sep.join([imagesPath, flashImage])
37 self.flashImage = pygame.image.load(flashImagePath).convert_alpha()
38 images.sort(lambda a, b : cmp(*[int(os.path.splitext(f)[0]) for f in [a, b]]))
39
40 self.images = []
41 for img in images :
42 imagePath = os.path.sep.join([imagesPath, img])
43 img = pygame.image.load(imagePath).convert_alpha()
44 self.images.append(img)
45
46 # assumes that all images have same dimensions
47 self.width = self.images[0].get_width()
48 self.height = self.images[0].get_height()
49 self.duration = duration
50
51 self.image = self.images[0]
52 self.rect = pygame.Rect((0,0), (self.width, self.height))
53
54 self.blink = blink
55 if blink :
56 self._startBlink()
57
58 def _startBlink(self) :
59 pygame.time.set_timer(TIMEOUT, self.duration)
60 self.iterator = self.iterImages()
61
62 def iterImages(self) :
63 for img in cycle(self.images) :
64 yield img
65
66 @event_handler(TIMEOUT)
67 def loadNext(self, event) :
68 if self.blink :
69 self.image = self.iterator.next()
70
71 @event_handler(pygame.MOUSEBUTTONDOWN)
72 def flashOn(self, event) :
73 self.blink=False
74 self.image = self.flashImage
75
76 @event_handler(pygame.MOUSEBUTTONUP)
77 def flashOff(self, event) :
78 self.blink = True
79 self.loadNext(event)
80
81 @event_handler(pygame.MOUSEMOTION)
82 def move(self, event) :
83 x, y = event.rel
84 self.rect.centerx += x
85 self.rect.centery += y
86 #self.rect.move_ip(*rel)