+/* === S I N F G =========================================================== */
+/*! \file keyframe.cpp
+** \brief Template File
+**
+** $Id: keyframe.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
+**
+** \legal
+** Copyright (c) 2002 Robert B. Quattlebaum Jr.
+**
+** This software and associated documentation
+** are CONFIDENTIAL and PROPRIETARY property of
+** the above-mentioned copyright holder.
+**
+** You may not copy, print, publish, or in any
+** other way distribute this software without
+** a prior written agreement with
+** the copyright holder.
+** \endlegal
+*/
+/* ========================================================================= */
+
+/* === H E A D E R S ======================================================= */
+
+#ifdef USING_PCH
+# include "pch.h"
+#else
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <algorithm>
+
+#include <ETL/stringf>
+
+#include "keyframe.h"
+#include "exception.h"
+#include "general.h"
+#include <ETL/misc>
+
+#endif
+
+/* === U S I N G =========================================================== */
+
+using namespace std;
+using namespace etl;
+using namespace sinfg;
+
+/* === M A C R O S ========================================================= */
+
+/* === G L O B A L S ======================================================= */
+
+/* === P R O C E D U R E S ================================================= */
+
+/* === M E T H O D S ======================================================= */
+
+Keyframe::Keyframe():
+ time_(0)
+{
+}
+
+Keyframe::Keyframe(const Time &time):
+ time_(time)
+{
+}
+
+Keyframe::~Keyframe()
+{
+}
+
+void
+KeyframeList::dump()const
+{
+ const_iterator iter;
+ int i;
+ sinfg::info(">>>>>>>>BEGIN KEYFRAME DUMP");
+ for(iter=begin(),i=0;iter!=end();++iter,i++)
+ {
+ sinfg::info("#%d, time: %s, desc: %s",i,iter->get_time().get_string().c_str(),iter->get_description().c_str());
+ }
+ sinfg::info("<<<<<<<<END KEYFRAME DUMP");
+}
+
+void
+KeyframeList::sync()
+{
+ //DEBUGPOINT();
+ //sinfg::info("PRE-SORT:");
+ //dump();
+ sort(begin(),end());
+ //sinfg::info("POST-SORT:");
+ //dump();
+ //DEBUGPOINT();
+}
+
+KeyframeList::iterator
+KeyframeList::find(const UniqueID &x)
+{
+ KeyframeList::iterator iter;
+ iter=std::find(begin(),end(),x);
+ if(iter==end())
+ throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find UniqueID %d",x.get_uid()));
+ return iter;
+}
+
+KeyframeList::const_iterator
+KeyframeList::find(const UniqueID &x)const
+{
+ KeyframeList::const_iterator iter;
+ iter=std::find(begin(),end(),x);
+ if(iter==end())
+ throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find UniqueID %d",x.get_uid()));
+ return iter;
+}
+
+KeyframeList::iterator
+KeyframeList::add(const Keyframe &x)
+{
+ push_back(x);
+ iterator ret(end());
+ ret--;
+ assert(x==*ret);
+ sync();
+ return ret;
+}
+
+void
+KeyframeList::erase(const UniqueID &x)
+{
+ std::vector<Keyframe>::erase(find(x));
+}
+
+KeyframeList::iterator
+KeyframeList::find(const Time &x)
+{
+ KeyframeList::iterator iter;
+ iter=binary_find(begin(),end(),x);
+ if(iter!=end() && iter->get_time().is_equal(x))
+ return iter;
+/* iter++;
+ if(iter!=end() && iter->get_time().is_equal(x))
+ return iter;
+*/
+ throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find Keyframe %s",x.get_string().c_str()));
+}
+
+KeyframeList::iterator
+KeyframeList::find_next(const Time &x)
+{
+ KeyframeList::iterator iter(binary_find(begin(),end(),x));
+
+ if(iter!=end())
+ {
+ if(iter->get_time().is_more_than(x))
+ return iter;
+ ++iter;
+ if(iter!=end())
+ {
+ if(iter->get_time().is_more_than(x))
+ return iter;
+/* ++iter;
+ if(iter!=end() && iter->get_time().is_more_than(x))
+ return iter;
+*/
+ }
+ }
+
+ throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find next Keyframe %s",x.get_string().c_str()));
+}
+
+
+KeyframeList::iterator
+KeyframeList::find_prev(const Time &x)
+{
+ KeyframeList::iterator iter(binary_find(begin(),end(),x));
+
+ if(iter!=end())
+ {
+ if(iter->get_time()+Time::epsilon()<x)
+ return iter;
+ if(iter!=begin() && (--iter)->get_time()+Time::epsilon()<x)
+ return iter;
+ }
+ throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find prev Keyframe %s",x.get_string().c_str()));
+
+}
+
+
+
+KeyframeList::const_iterator
+KeyframeList::find(const Time &x)const
+{
+ return const_cast<KeyframeList*>(this)->find(x);
+}
+
+
+KeyframeList::const_iterator
+KeyframeList::find_next(const Time &x)const
+{
+ return const_cast<KeyframeList*>(this)->find_next(x);
+
+}
+
+
+KeyframeList::const_iterator
+KeyframeList::find_prev(const Time &x)const
+{
+ return const_cast<KeyframeList*>(this)->find_prev(x);
+
+}
+
+void
+KeyframeList::find_prev_next(const Time& time, Time &prev, Time &next)const
+{
+ try { prev=find_prev(time)->get_time(); }
+ catch(...) { prev=Time::begin(); }
+ try { next=find_next(time)->get_time(); }
+ catch(...) { next=Time::end(); }
+}
+
+void
+KeyframeList::insert_time(const Time& location, const Time& delta)
+{
+// sinfg::info("KeyframeList::insert_time(): loc=%s, delta=%s",location.get_string().c_str(),delta.get_string().c_str());
+ if(!delta)
+ return;
+ try
+ {
+ iterator iter(find_next(location));
+ for(;iter!=end();++iter)
+ {
+ iter->set_time(iter->get_time()+delta);
+ }
+ sync();
+ }
+ catch(Exception::NotFound) { }
+}