Back to index

unity  6.0.0
Animator.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2011 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Marco Trevisan (TreviƱo) <mail@3v1n0.net>
00018  */
00019 
00020 #include "Animator.h"
00021 
00022 namespace unity
00023 {
00024 
00025 Animator::Animator(unsigned int default_duration, unsigned int fps_rate)
00026   : start_time_(0)
00027   , rate_(1)
00028   , duration_(0)
00029   , one_time_duration_(0)
00030   , start_progress_(0.0f)
00031   , progress_(0.0f)
00032 {
00033   SetDuration(default_duration);
00034   SetRate(fps_rate);
00035 }
00036 
00037 Animator::~Animator()
00038 {
00039   Stop();
00040 }
00041 
00042 void Animator::SetRate(unsigned int fps_rate)
00043 {
00044   if (fps_rate != 0)
00045     rate_ = 1000 / fps_rate;
00046 }
00047 
00048 void Animator::SetDuration(unsigned int duration)
00049 {
00050   duration_ = duration * 1000;
00051 }
00052 
00053 unsigned int Animator::GetRate() const
00054 {
00055   if (rate_ != 0)
00056     return 1000 / rate_;
00057 
00058   return rate_;
00059 }
00060 
00061 unsigned int Animator::GetDuration() const
00062 {
00063   return (one_time_duration_ > 0 ? one_time_duration_ : duration_) / 1000;
00064 }
00065 
00066 bool Animator::IsRunning() const
00067 {
00068   return bool(timeout_);
00069 }
00070 
00071 double Animator::GetProgress() const
00072 {
00073   return progress_;
00074 }
00075 
00076 void Animator::Start(unsigned int one_time_duration, double start_progress)
00077 {
00078   if (!timeout_ && start_progress < 1.0f)
00079   {
00080     if (start_progress < 0.0f)
00081       start_progress = 0.0f;
00082 
00083     one_time_duration_ = one_time_duration * 1000;
00084     start_progress_ = start_progress;
00085     progress_ = start_progress_;
00086     start_time_ = g_get_monotonic_time();
00087     timeout_.reset(new glib::Timeout(rate_, sigc::mem_fun(this, &Animator::DoStep)));
00088     animation_started.emit();
00089   }
00090 }
00091 
00092 void Animator::Start(double start_progress)
00093 {
00094   Start(0, start_progress);
00095 }
00096 
00097 void Animator::Stop()
00098 {
00099   if (timeout_)
00100   {
00101     timeout_.reset();
00102     animation_updated.emit(progress_);
00103     animation_ended.emit();
00104     animation_stopped.emit(progress_);
00105     one_time_duration_ = 0;
00106   }
00107 }
00108 
00109 bool Animator::DoStep()
00110 {
00111   const gint64 current_time = g_get_monotonic_time();
00112   const gint64 duration = one_time_duration_ > 0 ? one_time_duration_ : duration_;
00113   const gint64 end_time = start_time_ + duration;
00114 
00115   if (current_time < end_time && progress_ < 1.0f && duration > 0)
00116   {
00117     const double diff_time = current_time - start_time_;
00118     progress_ = CLAMP(start_progress_ + (diff_time / duration), 0.0f, 1.0f);
00119     animation_updated.emit(progress_);
00120 
00121     return true;
00122   }
00123   else
00124   {
00125     progress_ = 1.0f;
00126     animation_updated.emit(1.0f);
00127     animation_ended.emit();
00128     one_time_duration_ = 0;
00129     timeout_.reset();
00130 
00131     return false;
00132   }
00133 }
00134 
00135 } //namespace