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