sugar4.graphics.animator

The animator module provides a simple framework to create animations.

Example

Animate the size of a window:

from gi.repository import Gtk
from sugar4.graphics.animator import Animator, Animation

# Construct a window to animate
w = Gtk.Window()
w.connect('destroy', lambda w: app.quit())

# Start the animation when the window is shown
w.connect('realize', lambda self: animator.start())
w.present()

# Construct a 5 second animator
animator = Animator(5, widget=w)

# Create an animation subclass to animate the widget
class SizeAnimation(Animation):
    def __init__(self):
        # Tell the animation to give us values between 20 and
        # 420 during the animation
        Animation.__init__(self, 20, 420)

    def next_frame(self, frame):
        size = int(frame)
        w.set_default_size(size, size)

# Add the animation to the animator
animation = SizeAnimation()
animator.add(animation)

# The animation runs inside a GLib main loop
app.run()

STABLE.

Attributes

EASE_OUT_EXPO

EASE_IN_EXPO

Classes

Animator

The animator class manages the timing for calling the animations.

Animation

The animation class is a base class for creating an animation.

FadeAnimation

A convenience animation class for fading widgets in/out.

ScaleAnimation

A convenience animation class for scaling widgets.

MoveAnimation

A convenience animation class for moving widgets.

ColorAnimation

A convenience animation class for animating colors.

Module Contents

sugar4.graphics.animator.EASE_OUT_EXPO = 0[source]
sugar4.graphics.animator.EASE_IN_EXPO = 1[source]
class sugar4.graphics.animator.Animator(duration, fps=20, easing=EASE_OUT_EXPO, widget=None)[source]

Bases: gi.repository.GObject.GObject

The animator class manages the timing for calling the animations.

The animations can be added using the add function and then started with the start function. If multiple animations are added, then they will be played back at the same time and rate as each other.

The completed signal is emitted upon the completion of the animation and also when the stop function is called.

Parameters:
  • duration (float) – the duration of the animation in seconds

  • fps (int, optional) – the number of animation callbacks to make per second (frames per second). This is used as fallback when frame clock is not available.

  • easing (int) – the desired easing mode, either EASE_OUT_EXPO or EASE_IN_EXPO

  • widget (Gtk.Widget) – one of the widgets that the animation is acting on. If supplied, the animation will run on the frame clock of the widget for smoother animation.

Note

When creating an animation, take into account the limited cpu power on some devices, such as the XO. Setting the fps too high can use significant cpu usage.

__gsignals__[source]
add(animation)[source]

Add an animation to this animator.

Parameters:

animation (sugar4.graphics.animator.Animation) – the animation instance to add

remove_all()[source]

Remove all animations and stop this animator.

start()[source]

Start the animation running. This will stop and restart the animation if the animation is currently running.

stop()[source]

Stop the animation and emit the completed signal.

class sugar4.graphics.animator.Animation(start, end)[source]

Bases: object

The animation class is a base class for creating an animation. It should be subclassed. Subclasses should specify a next_frame function to set the required properties based on the animation progress. The range of the frame value passed to the next_frame function is defined by the start and end values.

Parameters:
  • start (float) – the first frame value for the next_frame method

  • end (float) – the last frame value for the next_frame method

# Create an animation subclass
class MyAnimation(Animation):
    def __init__(self, thing):
        # Tell the animation to give us values between 0.0 and
        # 1.0 during the animation
        Animation.__init__(self, 0.0, 1.0)
        self._thing = thing

    def next_frame(self, frame):
        # Use the `frame` value to set properties
        self._thing.set_green_value(frame)
start[source]
end[source]
do_frame(t, duration, easing)[source]

This method is called by the animator class every frame. This method calculates the frame value to then call next_frame.

Parameters:
  • t (float) – the current time elapsed of the animation in seconds

  • duration (float) – the length of the animation in seconds

  • easing (int) – the easing mode passed to the animator

next_frame(frame)[source]

This method is called every frame and should be overridden by subclasses.

Parameters:

frame (float) – a value between start and end representing the current progress in the animation

do_stop()[source]

This method is called whenever the animation is stopped, either due to the animation ending or being stopped by the animation. next_frame will not be called after do_stop, unless the animation is restarted.

Added in version 0.109.0.3.

This should be used in subclasses if they bind any signals. Eg. if they bind the draw signal for a widget:

class SignalAnimation(Animation):

    def __init__(self, widget):
        Animation.__init__(self, 0, 1)
        self._draw_hid = None
        self._widget = widget

    def next_frame(self, frame):
        self._frame = frame
        if self._draw_hid is None:
            self._draw_hid = self._widget.connect_after(
                'draw', self.__draw_cb)
        self._widget.queue_draw()

    def __draw_cb(self, widget, cr):
        cr.save()
        # Do the draw
        cr.restore()

    def do_stop(self):
        self._widget.disconnect(self._draw_hid)
        self._widget.queue_draw()
class sugar4.graphics.animator.FadeAnimation(widget, start_opacity=0.0, end_opacity=1.0)[source]

Bases: Animation

A convenience animation class for fading widgets in/out.

Parameters:
  • widget (Gtk.Widget) – The widget to animate

  • start_opacity (float) – Starting opacity (0.0 to 1.0)

  • end_opacity (float) – Ending opacity (0.0 to 1.0)

next_frame(frame)[source]

Update widget opacity.

class sugar4.graphics.animator.ScaleAnimation(widget, start_scale=0.0, end_scale=1.0)[source]

Bases: Animation

A convenience animation class for scaling widgets.

Parameters:
  • widget (Gtk.Widget) – The widget to animate

  • start_scale (float) – Starting scale factor

  • end_scale (float) – Ending scale factor

next_frame(frame)[source]

Update widget scale.

class sugar4.graphics.animator.MoveAnimation(widget, start_pos, end_pos)[source]

Bases: Animation

A convenience animation class for moving widgets.

Parameters:
  • widget (Gtk.Widget) – The widget to animate

  • start_pos (tuple) – Starting position (x, y)

  • end_pos (tuple) – Ending position (x, y)

next_frame(frame)[source]

Update widget position.

class sugar4.graphics.animator.ColorAnimation(start_color, end_color, callback)[source]

Bases: Animation

A convenience animation class for animating colors.

Parameters:
  • start_color (tuple) – Starting RGBA color (r, g, b, a)

  • end_color (tuple) – Ending RGBA color (r, g, b, a)

  • callback (function) – Function to call with interpolated color

next_frame(frame)[source]

Interpolate color and call callback.