1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# animation.py
#
# Copyright 2012 S. Daniel Francis <francis@sugarlabs.org>,
# Agustin Zubiaga <aguz@sugarlabs.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import gtk
import gobject
import logging
_logger = logging.getLogger('animate-animation')
_logger.setLevel(logging.DEBUG)
logging.basicConfig()
class Animation(gtk.Image):
def __init__(self, width, height, sleep_time=1):
gtk.Image.__init__(self)
self._current_image = 0
self._timeout = None
self._return = True
self._running = False
self.images = []
self._size = width, height
self._sleep_time = sleep_time * 1000
def set_return(self, _return=True):
"""Defines if when get to the last image,
should return to the first"""
self._return = _return
def add_image(self, image_path):
"""Appends an image to the list"""
self.images.append(image_path)
def run(self):
"""Runs the animation"""
if not self._running:
try:
pixbuf = gtk.gdk.pixbuf_new_from_file(self.images[
self._current_image])
width = pixbuf.get_width()
height = pixbuf.get_height()
max_size = max(width, height)
if max_size is width:
scaled_pixbuf = pixbuf.scale_simple(self._size[0],
self._size[0] * height / width,
gtk.gdk.INTERP_TILES)
else:
scaled_pixbuf = pixbuf.scale_simple(self._size[1],
self.size[1] * width / height,
gtk.gdk.INTERP_TILES)
self.set_from_pixbuf(scaled_pixbuf)
self._running = True
self._timeout = gobject.timeout_add(self._sleep_time, self.next)
_logger.info(" Running!")
except IndexError:
_logger.error(" There aren't images")
else:
_logger.error(" The animation is running already")
def set_pos(self, pos):
self._current_image = pos
pixbuf = gtk.gdk.pixbuf_new_from_file(self.images[self._current_image])
width = pixbuf.get_width()
height = pixbuf.get_height()
max_size = max(width, height)
if max_size is width:
scaled_pixbuf = pixbuf.scale_simple(self._size[0],
self._size[0] * height / width,
gtk.gdk.INTERP_TILES)
else:
scaled_pixbuf = pixbuf.scale_simple(self._size[1],
self._size[1] * width / height,
gtk.gdk.INTERP_TILES)
self.set_from_pixbuf(scaled_pixbuf)
self.set_from_pixbuf(scaled_pixbuf)
def pause(self):
"""Pause the animation"""
gobject.source_remove(self._timeout)
self._running = False
def next(self):
"""Shows the next frame"""
if self._current_image != len(self.images) - 1:
self._current_image += 1
elif not self._current_image != len(self.images) - 1 and self._return:
self._current_image = 0
self.set_pos(self._current_image)
return True
def back(self):
"""Shows the previous frame"""
if self._current_image != 0:
self._current_image -= 1
elif self._current_image == 0 and self._return:
self._current_image = -1
self.set_pos(self._current_image)
|