Back to index

nux  3.0.0
Timeline.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by Gordon Allott <gord.allott@canonical.com>
00019  *
00020  */
00021 
00022 #include "Nux.h"
00023 
00024 #include "Timeline.h"
00025 
00026 namespace nux
00027 {
00028 
00029   Timeline::Timeline(unsigned int msecs, const char *Caption, NUX_FILE_LINE_DECL)
00030     :   Object(true, NUX_FILE_LINE_PARAM)
00031   {
00032     Looping = false;
00033     IsPlaying = false;
00034     Duration = msecs;
00035     Rewind();
00036     SinkReference(); // get rid of our floating reference
00037     nux::GetWindowThread()->AddTimeline(this);
00038   }
00039 
00040   Timeline::~Timeline()
00041   {
00042   }
00043 
00044   void Timeline::Stop()
00045   {
00046     Pause();
00047     Rewind();
00048   }
00049 
00050   void Timeline::Start()
00051   {
00052     IsPlaying = true;
00053     Started.emit();
00054   }
00055 
00056   void Timeline::Pause()
00057   {
00058     IsPlaying = false;
00059     Paused.emit();
00060   }
00061 
00062   void Timeline::Rewind()
00063   {
00064     _ElapsedTime = 0;
00065   }
00066 
00067   double Timeline::GetProgress()
00068   {
00069     return (float)_ElapsedTime / Duration;
00070   }
00071 
00072   double Timeline::GetEasing()
00073   {
00074     // no easing for the base class
00075     return GetProgress();
00076   }
00077 
00078   void Timeline::DoTick(unsigned long msecs)
00079   {
00080     if (msecs < 1)
00081       return;
00082 
00083     _ElapsedTime += msecs;
00084     if (Looping)
00085       _ElapsedTime %= Duration;
00086 
00087     unsigned long remainder = 0;
00088     if (_ElapsedTime > Duration)
00089     {
00090       remainder = _ElapsedTime - Duration;
00091       _ElapsedTime = Duration;
00092     }
00093 
00094     NewFrame.emit(msecs);
00095 
00096     if (remainder > 0)
00097       {
00098         nux::GetWindowThread()->RemoveTimeline(this);
00099         IsPlaying = false;
00100         Completed.emit();
00101         UnReference();
00102       }
00103   }
00104 }