/usr/share/pyshared/pgm/timing/controller.py is in python-pgm 0.3.12-2.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | # -*- mode: python; coding: utf-8 -*-
#
# Pigment Python tools
#
# Copyright © 2006, 2007, 2008 Fluendo Embedded S.L.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# TODO: - Timing (ac/de)celeration
"""
"""
from pgm.utils import classinit, maths
from pgm.timing import modifier, ticker
import math, time, gobject
# Repeat behavior constants
FORWARD, REVERSE = 0, 1
# End behavior constants
BEGIN, HOLD, END = 2, 3, 4
# Transformation constants
LINEAR, ACCELERATE, DECELERATE, SMOOTH = 5, 6, 7, 8
# Repeat constant
INFINITE = -1
class Controller(object):
"""
"""
# Allows property fget/fset/fdel/doc overriding
__metaclass__ = classinit.ClassInitMeta
__classinit__ = classinit.build_properties
# The ticker used by controllers
_ticker = None
def __init__(self, begin=0, duration=1000, resolution=5,
repeat_behavior=FORWARD, repeat_count=1, end_behavior=BEGIN,
modifiers=[], transformation=LINEAR, end_callback=None):
self.begin = begin
self.duration = duration
self.resolution = resolution
self.repeat_behavior = repeat_behavior
self.repeat_count = repeat_count
self.end_behavior = end_behavior
self.modifiers = modifiers
self.transformation = transformation
self.end_callback = end_callback
self._start_time = 0
self._update_time = 0
self._begin_time = 0
self._repeat_counter = 0
self._started = False
self._tag = None
def __repr__(self):
string = {FORWARD:'FORWARD', REVERSE:'REVERSE',
BEGIN:'BEGIN', HOLD:'HOLD', END:'END'}
return "<controller.Controller(begin=%r, duration=%r, resolution=%r, " \
"repeat_behavior=%s, repeat_count=%r, end_behavior=%s, " \
"modifiers=%r)>" \
% (self._begin, self._duration, self._resolution,
string[self._repeat_behavior], self._repeat_count,
string[self._end_behavior], self._modifiers)
# Property definitions
def begin__get(self):
"""The time (in ms) to wait after a start()"""
return self._begin
def begin__set(self, begin):
self._begin = max(0, int(begin))
def duration__get(self):
"""The duration (in ms) of each animation"""
return self._duration
def duration__set(self, duration):
self._duration = max(0, int(duration))
def resolution__get(self):
"""The update frequency (in ms)"""
return self._resolution
def resolution__set(self, resolution):
self._resolution = max(0, int(resolution))
def repeat_behavior__get(self):
"""The repeat behavior at the end of an animation"""
return self._repeat_behavior
def repeat_behavior__set(self, repeat_behavior):
if repeat_behavior == FORWARD or repeat_behavior == REVERSE:
self._repeat_behavior = repeat_behavior
else:
self._repeat_behavior = FORWARD
def repeat_count__get(self):
"""The number of iteration"""
return self._repeat_count
def repeat_count__set(self, repeat_count):
# -1 is the INFINITE constant for the repeat mode
self._repeat_count = max(-1, int(repeat_count))
def end_behavior__get(self):
"""The behavior at the end of an animation"""
return self._end_behavior
def end_behavior__set(self, end_behavior):
if end_behavior == BEGIN or end_behavior == HOLD \
or end_behavior == END:
self._end_behavior = end_behavior
def modifiers__get(self):
"""The list of object modifiers"""
return self._modifiers
def modifiers__set(self, modifiers):
if isinstance(modifiers, (list, tuple)):
try:
for i in modifiers:
if not isinstance(i, modifier.Modifier):
raise TypeError, 'Not a modifier.Modifier object'
# modifiers is an empty list
except IndexError:
self._modifiers = []
# All the objects in the list are Modifier
else:
self._modifiers = list(modifiers)
else:
raise TypeError, 'Modifiers must be list or tuple'
self._modifiers = []
def transformation__get(self):
"""The transformation type"""
return self._transformation
def transformation__set(self, transformation):
if transformation in (LINEAR, ACCELERATE, DECELERATE, SMOOTH):
self._transformation = transformation
def end_callback__get(self):
"""
The callback called at the end of the controller
(duration*repeat_count ms).
"""
return self._end_callback
def end_callback__set(self, end_callback):
if callable(end_callback):
self._end_callback = end_callback
else:
self._end_callback = None
def started__get(self):
"""Is the controller started ?"""
return self._started
# Class methods
@classmethod
def set_ticker(cls, ticker):
"""
Define the Ticker to use for all the controllers that will be instanced.
If no ticker is set, the controller uses GLib timeout sources in the
mainloop to update object properties.
"""
cls._ticker = ticker
@classmethod
def get_ticker(cls):
"""
Get the Ticker used by controllers.
"""
return cls._ticker
# Public methods
def start(self):
"""
"""
if not self._started:
self._start_time = long(time.time() * 1000)
self._update_time = self._start_time
self._begin_time = self._start_time + self._begin
self._repeat_counter = 0
# If a ticker has been given by the user, we don't use GLib timeout
# sources in the mainloop
if Controller._ticker:
# Add the controller to the ticker
Controller._ticker.add_controller(self)
self.update()
else:
# Add the timeout source to the mainloop
self._tag = gobject.timeout_add(self._resolution, self.update)
self._started = True
def stop(self):
"""
"""
if self._started:
self._started = False
# If a ticker has been given by the user, we don't use GLib timeout
# sources in the mainloop
if Controller._ticker:
# Remove the controller from the ticker
Controller._ticker.remove_controller(self)
elif self._tag:
# Remove the source from the mainloop
gobject.source_remove(self._tag)
self._tag = None
# Respect the end behavior by sending the appropriate factor to
# the modifiers.
if self._end_behavior == BEGIN:
factor = 0.0
elif self._end_behavior == END:
factor = 1.0
elif self._end_behavior == HOLD:
if self._duration != 0:
factor = self._compute_factor()
else:
factor = 1.0
for modifier in self._modifiers:
modifier.update(factor)
# Reset internal data
self._start_time = 0
self._update_time = 0
self._begin_time = 0
self._repeat_counter = 0
# Let's call the end callback
if self._end_callback:
self._end_callback(self)
# Protected methods
def update(self, update_time=0):
# Compute elapsed time
if update_time != 0:
self._update_time = update_time
else:
self._update_time = long(time.time() * 1000)
diff_begin_time = self._update_time - self._begin_time
# Is the controller duration elapsed ?
if diff_begin_time >= self._duration:
self._repeat_counter += 1
# Repeat the animation
if (self._repeat_count == INFINITE \
or self._repeat_counter < self._repeat_count) \
and self._duration != 0:
self._begin_time += self._duration
# Stop the animation
else:
self.stop()
return False
factor = self._compute_factor()
# Update the modifiers
#print 'updating', len(self._modifiers), 'modifiers in', self
for modifier in self._modifiers:
modifier.update(factor)
# Return True to keep the source in the mainloop
return True
# Private methods
def _compute_factor(self):
# Compute the factor
elapsed_time = self._update_time - self._begin_time
factor = min(1.0, float(elapsed_time) / self._duration)
# Reverse the factor if we are in REVERSE mode
if self._repeat_behavior == REVERSE and self._repeat_counter % 2:
factor = 1.0 - factor
# Apply the transformation on the time
if self._transformation == SMOOTH:
sinus = math.sin(maths.lerp(-maths.PI2, maths.PI2, factor))
factor = (sinus + 1.0) * 0.5
elif self._transformation == DECELERATE:
factor = math.sin(maths.lerp(0, maths.PI2, factor))
elif self._transformation == ACCELERATE:
factor = math.sin(maths.lerp(-maths.PI2, 0, factor)) + 1.0
return factor
|