1/*
2 * Copyright © 2010 Yuvaraj Pandian T <yuvipanda@yuvi.in>
3 * Copyright © 2010 daniel g. siegel <dgsiegel@gnome.org>
4 * Copyright © 2008 Filippo Argiolas <filippo.argiolas@gmail.com>
5 *
6 * Licensed under the GNU General Public License Version 2
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22internal class Cheese.Countdown : GLib.Object
23{
24  public delegate void CountdownCallback ();
25  private Clutter.Text countdown_actor;
26  private unowned CountdownCallback completed_callback;
27  private Clutter.PropertyTransition pulse_in;
28  private Clutter.PropertyTransition pulse_out;
29  private int current_value = 0;
30  private GLib.Settings settings;
31  public bool running;
32
33  public Countdown (Clutter.Text countdown_actor)
34  {
35    this.countdown_actor = countdown_actor;
36    settings             = new GLib.Settings("org.gnome.Cheese");
37  }
38
39  ~Countdown ()
40  {
41    stop ();
42  }
43
44  /**
45   * Fade the countdown text out, over 500 milliseconds.
46   *
47   * Once the fade-out is complete, this method calls fade_in().
48   */
49  private void fade_out ()
50  {
51    pulse_out = new Clutter.PropertyTransition ("opacity");
52    pulse_out.set_duration (500);
53    pulse_out.set_from_value (255);
54    pulse_out.set_to_value (0);
55    pulse_out.completed.connect (fade_in);
56    pulse_out.animatable = countdown_actor;
57    pulse_out.start ();
58  }
59
60  /**
61   * Decrement the countdown text and fade it in, over 500 milliseconds.
62   *
63   * Once the fade-in is complete, this method calls fade_out().
64   */
65  private void fade_in ()
66  {
67    if (this.current_value <= 0)
68    {
69      this.completed_callback ();
70      running = false;
71      return;
72    }
73    this.countdown_actor.text = this.current_value.to_string ();
74    this.current_value--;
75
76    pulse_in = new Clutter.PropertyTransition ("opacity");
77    pulse_in.set_duration (500);
78    pulse_in.set_from_value (0);
79    pulse_in.set_to_value (255);
80    pulse_in.completed.connect (fade_out);
81    pulse_in.animatable = countdown_actor;
82    pulse_in.start ();
83  }
84
85  /**
86   * Start the countdown, using the countdown-duration GSetting for the time.
87   *
88   * @param completed_callback the callback to call upon countdown completion
89   */
90  public void start (CountdownCallback completed_callback)
91  {
92    this.completed_callback = completed_callback;
93    this.current_value = settings.get_int("countdown-duration");
94    running = true;
95    countdown_actor.show ();
96    fade_in ();
97  }
98
99  /**
100   * Stop the countdown, for example if it was interrupted by the user.
101   */
102  public void stop ()
103  {
104    countdown_actor.hide ();
105    countdown_actor.remove_all_transitions ();
106    running = false;
107  }
108}
109