layers sous forme de constantes globales.
[minwii.git] / src / cursor / WarpingCursor.py
1 '''
2 Created on 29 mai 2009
3
4 @author: samsam
5 '''
6
7 import os, sys, math
8
9 import pygame
10 from pygame.sprite import Sprite
11
12 class WarpingCursor(Sprite):
13 '''
14 The class for animating the warping cursor
15
16 screen:
17 the screen on which the WarpingCursor is painted
18 images:
19 The images constituting the animation of the cursor
20 durations:
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
37 def __init__(self, scr, images, durations, initCenterPosition,flashImage):
38 '''
39 Constructor
40
41 scr:
42 the screen on which the WarpingCursor is painted
43 images:
44 The images constituting the animation of the cursor
45 durations:
46 The duration of each image in the animation
47 initCenterPosition:
48 The Position of the center of the cursor at the beginning
49 '''
50 self.screen = scr
51 self.images = images
52 self.flashImagePath = flashImage
53 self.durations = durations
54 self.centerPosition = initCenterPosition
55 self.flashLength = 100
56 self.flashing = False
57 self.image = pygame.image.load(self.images[0]).convert_alpha()
58 self._imagePointer = 0
59 self._animationOffset = 0
60 self._flashTimer = 0
61
62 def update(self, elapsedTime, centerPosition):
63 '''
64 Update the cursor's look and position
65
66 elapsedTime:
67 The time passed since the previous update
68 centerPosition:
69 the new position of the creep
70 '''
71 self._updateImage(elapsedTime)
72 self.centerPosition = centerPosition
73 if self.flashing :
74 self._flashTimer += elapsedTime
75 if self._flashTimer > self.flashLength:
76 self.flashing = False
77
78 def _updateImage(self, elapsedTime):
79 '''
80 Update the cursor's image
81
82 elapsedTime:
83 The time passed since the previous update
84 '''
85 self._animationOffset += elapsedTime
86
87 if self._animationOffset > self.durations[self._imagePointer]:
88 #New animation offset is computed first, before updating the pointer
89 self._animationOffset -= self.durations[self._imagePointer]
90 #point to the next image (restarts from the beginning when it reaches the end)
91 self._imagePointer = (self._imagePointer + 1) % len(self.images)
92
93 if self.flashing:
94 self.image = pygame.image.load(self.flashImagePath).convert_alpha()
95 else :
96 self.image = pygame.image.load(self.images[self._imagePointer]).convert_alpha()
97
98 def flash(self,flashLength = None):
99 self._flashTimer = 0
100 self.flashing = True
101 if flashLength:
102 self.flashlength = flashLength
103
104 def blit(self,surface):
105 '''
106 Draw the circle on surface
107 '''
108
109 newPos = (self.centerPosition[0] - self.image.get_width() / 2, self.centerPosition[1] - self.image.get_height() / 2)
110 surface.blit(self.image, newPos)
111
112 def createImageListFromPath(path, imageCount):
113 '''
114 Create a list of images for a cursor (the concatenation of the original and reversed lists of images).
115 Images must be stored as path/imageNumber.png
116
117 path:
118 The folder where the images for that cursor are stored
119 imageCount:
120 The number of images in the folder
121 '''
122
123 tempImages = [''.join([path, '/', str(i), '.png']) for i in range(imageCount)]
124 #tempImagesReversed = tempImages[:]
125 #tempImagesReversed.reverse()
126 #return(tempImages+tempImagesReversed)
127 return(tempImages)
128
129 #testing
130 if __name__ == "__main__" :
131 window = pygame.display.set_mode((1680, 1050), pygame.FULLSCREEN)
132 screen = pygame.display.get_surface()
133 clock = pygame.time.Clock()
134
135 images = createImageListFromPath('cursorImages/black',11)
136 durations = [50 for i in range(22)]
137 position = (400, 300)
138 cursor = WarpingCursor(screen, images, durations, position)
139
140 while True:
141 # Limit frame speed to 50 FPS
142 #
143 timePassed = clock.tick(50)
144
145 for event in pygame.event.get():
146 if event.type == pygame.QUIT:
147 sys.exit()
148 if event.type == pygame.MOUSEMOTION:
149 position = event.pos
150
151 cursor.update(timePassed, position)
152 cursor.blit(screen)
153 pygame.display.flip()
154
155