1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 from pgm.graph.group import Group
18 from pgm.graph.image import Image
19 from pgm.timing.implicit import *
20 import pgm
21 import os
22
23
25
27 Group.__init__(self, canvas, layer)
28
29 self._pictures_paths = []
30 self._current_index = None
31 self._target_index = None
32 self._frame_size = 0.1
33
34 self._current = Image()
35 self._current.bg_color = (0, 0, 0, 0)
36 self._current.visible = True
37 self._animated_current = AnimatedObject(self._current)
38 self._animated_current.mode = REPLACE
39
40 self._hidden = Image()
41 self._hidden.bg_color = (0, 0, 0, 0)
42 self._hidden.visible = True
43 self._animated_hidden = AnimatedObject(self._hidden)
44 self._animated_hidden.mode = REPLACE
45
46
47 self._mouse_zone = Image()
48 self._mouse_zone.opacity = 0
49 self._mouse_zone.position = (0.0, 0.0, 0.0)
50 self._mouse_zone.visible = True
51
52 self.delay = 500
53
54 self.add(self._current)
55 self.add(self._hidden)
56 self.add(self._mouse_zone)
57
58 self._current_position = (0.0, 0.0, 0.0)
59 self.size = self._current.size
60
61 self._moved = False
62 self._moved_orig_dx = 0.0
63 self._loaded_paths = {self._hidden: "", self._current: ""}
64
65
73
88
90 try:
91 if index < 0 or index >= len(self._pictures_paths):
92 raise
93 path = self._pictures_paths[index]
94 if self._loaded_paths[drawable] != path:
95 self._loaded_paths[drawable] = path
96 drawable.set_from_file(path, 1024)
97 except:
98 drawable.clear()
99 self._loaded_paths[drawable] = ""
100
106
108 return len(self._pictures_paths)
109
111 index = max(0, index)
112 index = min(len(self._pictures_paths)-1, index)
113
114 if len(self._pictures_paths) == 0:
115 return
116
117 self._target_index = index
118
119 if not self._animated_current.is_animated() and \
120 not self._animated_hidden.is_animated():
121 self._update_current_index()
122
124 return self._target_index
125
147
148
150 if not self._moved:
151 self._current.position = self._current_position
152 self._hidden.position = from_position
153 else:
154 self._moved = False
155
156 self._current, self._hidden = self._hidden, self._current
157 self._animated_current, self._animated_hidden = self._animated_hidden, self._animated_current
158
159 self._load_picture_from_index(self._current, self._current_index)
160
161 self._animated_current.position = self._current_position
162 self._animated_hidden.position = to_position
163
164
165
168
170 self._current.size = size
171 self._hidden.size = size
172
173 self._previous_position = (self._current_position[0] - \
174 self._canvas.width - \
175 self._frame_size*size[0], \
176 self._current_position[1],
177 self._current_position[2])
178 self._next_position = (self._current_position[0] + \
179 self._canvas.width + \
180 self._frame_size*size[0], \
181 self._current_position[1],
182 self._current_position[2])
183
184 self._mouse_zone.size = size
185
187 return self._current.size
188
191
194
197
200
203
204 - def move(self, dx):
205
206 if not self._moved:
207 self._moved = True
208 self._moved_orig_dx = self._current.x
209 self._animated_current.stop_animations()
210 self._animated_hidden.stop_animations()
211
212 self._current.x = self._moved_orig_dx + dx
213 if self._current.x > 0:
214 self._hidden.x = self._previous_position[0] + self._moved_orig_dx + dx
215 self._load_picture_from_index(self._hidden, self._current_index - 1)
216 elif self._current.x < 0:
217 self._hidden.x = self._next_position[0] + self._moved_orig_dx + dx
218 self._load_picture_from_index(self._hidden, self._current_index + 1)
219
221
222 self._moved = False
223 self._animated_current.position = self._current_position
224 if abs(self._hidden.x - self._previous_position[0]) < \
225 abs(self._hidden.x - self._next_position[0]):
226 self._animated_hidden.position = self._previous_position
227 else:
228 self._animated_hidden.position = self._next_position
229
230
232 import pgm
233 import gobject
234
235
236 def on_delete(viewport, event):
237 pgm.main_quit()
238
239
240 def on_key_press(viewport, event, slideshow):
241 if event.keyval == pgm.keysyms.q or event.keyval == pgm.keysyms.Escape:
242 pgm.main_quit()
243
244 elif event.keyval == pgm.keysyms.Right:
245 slideshow.current_index += 1
246
247 elif event.keyval == pgm.keysyms.Left:
248 slideshow.current_index -= 1
249
250 def on_drag_begin(drawable, x, y, z, button, time, slideshow):
251 global start_position
252 start_position = (x, y)
253 slideshow.move(0.0)
254 return True
255
256 def on_drag_motion(drawable, x, y, z, button, time, slideshow):
257 global start_position
258 if start_position != None:
259 dx = x - start_position[0]
260 slideshow.move(dx)
261 return True
262
263 return False
264
265 def on_drag_end(drawable, x, y, z, button, time, slideshow):
266 global start_position
267 if start_position != None:
268 dx = start_position[0] - x
269 dy = start_position[1] - y
270
271 if dx > 0.3:
272 if slideshow.current_index < slideshow.length-1:
273 slideshow.current_index += 1
274 else:
275 slideshow.release()
276 elif dx < -0.3:
277 if slideshow.current_index > 0:
278 slideshow.current_index -= 1
279 else:
280 slideshow.release()
281 else:
282 slideshow.release()
283
284 start_position = None
285 return True
286
287 return False
288
289
290
291 factory = pgm.ViewportFactory('opengl')
292 gl = factory.create()
293 gl.title = 'Implicit animation'
294
295
296 canvas = pgm.Canvas()
297
298
299 gl.set_canvas(canvas)
300
301
302
303 images = args
304 slideshow = Slideshow(canvas, pgm.DRAWABLE_MIDDLE)
305 slideshow.pictures_paths = images
306 slideshow.current_index = 0
307
308
309 slideshow.size = canvas.size
310 slideshow.visible = True
311
312 start_position = None
313 slideshow.connect('drag_begin', on_drag_begin, slideshow)
314 slideshow.connect('drag_motion', on_drag_motion, slideshow)
315 slideshow.connect('drag_end', on_drag_end, slideshow)
316
317
318 gobject.timeout_add(15, gl.update)
319 gl.connect('delete-event', on_delete)
320 gl.connect('key-press-event', on_key_press, slideshow)
321 gl.show()
322 pgm.main()
323
324
325 if __name__ == '__main__':
326 import sys
327 sys.exit(main(sys.argv[1:]))
328