init.
[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
12
13 class WarpingCursor(pygame.sprite.Sprite):
14 '''
15 The class for animating the warping cursor
16
17 durations:
18 The duration of each image in the animation
19 centerPosition:
20 The Position of the center of the cursor
21 _imagePointer:
22 A pointer to the current image
23 _animationOffset:
24 The time elapsed since when the current image should have been displayed
25 '''
26 #screen = None
27 #images = None
28 #durations = None
29 #centerPosition = None
30 #_imagePointer = None
31 #_animationOffset = None
32
33 @staticmethod
34 def _get_theme_images(name) :
35 basePath = os.path.abspath(__file__).split(os.path.sep)[:-1]
36 basePath.append('data')
37 basePath.append(name)
38 basePath = os.path.sep.join(basePath)
39 images = [f for f in os.listdir(basePath) if os.path.splitext(f)[1] == '.png']
40 return basePath, images
41
42
43 def __init__(self, theme='black', duration=75):
44 pygame.sprite.Sprite.__init__(self)
45 imagesPath, images = WarpingCursor._get_theme_images(theme)
46 flashImage = images.pop(images.index('flash.png'))
47 images.sort(lambda a, b : cmp(*[int(os.path.splitext(f)[0]) for f in [a, b]]))
48
49 self.images = []
50 for img in images :
51 imagePath = os.path.sep.join([imagesPath, img])
52 self.images = pygame.image.load(imagePath).convert_alpha()
53
54 self.duration = duration
55
56 #self.flashImagePath = flashImage
57 #self.durations = durations
58 #self.centerPosition = initCenterPosition
59 #self.flashLength = 100
60 #self.flashing = False
61 #self.image = pygame.image.load(self.images[0]).convert_alpha()
62 #self._imagePointer = 0
63 #self._animationOffset = 0
64 #self._flashTimer = 0
65
66 def update(self, elapsedTime, centerPosition):
67 '''
68 Update the cursor's look and position
69
70 elapsedTime:
71 The time passed since the previous update
72 centerPosition:
73 the new position of the creep
74 '''
75 self._updateImage(elapsedTime)
76 self.centerPosition = centerPosition
77 if self.flashing :
78 self._flashTimer += elapsedTime
79 if self._flashTimer > self.flashLength:
80 self.flashing = False
81
82 def _updateImage(self, elapsedTime):
83 '''
84 Update the cursor's image
85
86 elapsedTime:
87 The time passed since the previous update
88 '''
89 self._animationOffset += elapsedTime
90
91 if self._animationOffset > self.durations[self._imagePointer]:
92 #New animation offset is computed first, before updating the pointer
93 self._animationOffset -= self.durations[self._imagePointer]
94 #point to the next image (restarts from the beginning when it reaches the end)
95 self._imagePointer = (self._imagePointer + 1) % len(self.images)
96
97 if self.flashing:
98 self.image = pygame.image.load(self.flashImagePath).convert_alpha()
99 else :
100 self.image = pygame.image.load(self.images[self._imagePointer]).convert_alpha()
101
102 def flash(self,flashLength = None):
103 self._flashTimer = 0
104 self.flashing = True
105 if flashLength:
106 self.flashlength = flashLength
107
108 def blit(self,surface):
109 '''
110 Draw the circle on surface
111 '''
112
113 newPos = (self.centerPosition[0] - self.image.get_width() / 2, self.centerPosition[1] - self.image.get_height() / 2)
114 surface.blit(self.image, newPos)