moreupdates
[synfig.git] / synfig-core / trunk / src / synfig / keyframe.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file keyframe.cpp
3 **      \brief Template File
4 **
5 **      $Id: keyframe.cpp,v 1.1.1.1 2005/01/04 01:23:14 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include <algorithm>
32
33 #include <ETL/stringf>
34
35 #include "keyframe.h"
36 #include "exception.h"
37 #include "general.h"
38 #include <ETL/misc>
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 /* === M E T H O D S ======================================================= */
55
56 Keyframe::Keyframe():
57         time_(0)
58 {
59 }
60
61 Keyframe::Keyframe(const Time &time):
62         time_(time)
63 {
64 }
65
66 Keyframe::~Keyframe()
67 {
68 }
69
70 void
71 KeyframeList::dump()const
72 {
73         const_iterator iter;
74         int i;
75         synfig::info(">>>>>>>>BEGIN KEYFRAME DUMP");
76         for(iter=begin(),i=0;iter!=end();++iter,i++)
77         {
78                 synfig::info("#%d, time: %s, desc: %s",i,iter->get_time().get_string().c_str(),iter->get_description().c_str());
79         }
80         synfig::info("<<<<<<<<END KEYFRAME DUMP");
81 }
82
83 void
84 KeyframeList::sync()
85 {
86         //DEBUGPOINT();
87         //synfig::info("PRE-SORT:");
88         //dump();       
89         sort(begin(),end());
90         //synfig::info("POST-SORT:");
91         //dump();
92         //DEBUGPOINT();
93 }
94
95 KeyframeList::iterator
96 KeyframeList::find(const UniqueID &x)
97 {
98         KeyframeList::iterator iter;
99         iter=std::find(begin(),end(),x);
100         if(iter==end())
101                 throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find UniqueID %d",x.get_uid()));
102         return iter;
103 }
104
105 KeyframeList::const_iterator
106 KeyframeList::find(const UniqueID &x)const
107 {
108         KeyframeList::const_iterator iter;
109         iter=std::find(begin(),end(),x);
110         if(iter==end())
111                 throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find UniqueID %d",x.get_uid()));
112         return iter;
113 }
114
115 KeyframeList::iterator
116 KeyframeList::add(const Keyframe &x)
117 {
118         push_back(x);
119         iterator ret(end());
120         ret--;
121         assert(x==*ret);
122         sync();
123         return ret;
124 }
125
126 void
127 KeyframeList::erase(const UniqueID &x)
128 {
129         std::vector<Keyframe>::erase(find(x));
130 }
131
132 KeyframeList::iterator
133 KeyframeList::find(const Time &x)
134 {
135         KeyframeList::iterator iter;
136         iter=binary_find(begin(),end(),x);
137         if(iter!=end() && iter->get_time().is_equal(x))
138                 return iter;
139 /*      iter++;
140         if(iter!=end() && iter->get_time().is_equal(x))
141                 return iter;
142 */
143         throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find Keyframe %s",x.get_string().c_str()));
144 }
145
146 KeyframeList::iterator
147 KeyframeList::find_next(const Time &x)
148 {
149         KeyframeList::iterator iter(binary_find(begin(),end(),x));
150
151         if(iter!=end())
152         {
153                 if(iter->get_time().is_more_than(x))
154                         return iter;
155                 ++iter;
156                 if(iter!=end())
157                 {
158                         if(iter->get_time().is_more_than(x))
159                                 return iter;
160 /*                      ++iter;
161                         if(iter!=end() && iter->get_time().is_more_than(x))
162                                 return iter;
163 */
164                 }
165         }
166         
167         throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find next Keyframe %s",x.get_string().c_str()));
168 }
169
170
171 KeyframeList::iterator
172 KeyframeList::find_prev(const Time &x)
173 {
174         KeyframeList::iterator iter(binary_find(begin(),end(),x));
175
176         if(iter!=end())
177         {
178                 if(iter->get_time()+Time::epsilon()<x)
179                         return iter;
180                 if(iter!=begin() && (--iter)->get_time()+Time::epsilon()<x)
181                         return iter;
182         }
183         throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find prev Keyframe %s",x.get_string().c_str()));
184
185 }
186
187
188
189 KeyframeList::const_iterator
190 KeyframeList::find(const Time &x)const
191 {
192         return const_cast<KeyframeList*>(this)->find(x);
193 }
194
195
196 KeyframeList::const_iterator
197 KeyframeList::find_next(const Time &x)const
198 {
199         return const_cast<KeyframeList*>(this)->find_next(x);
200
201 }
202
203
204 KeyframeList::const_iterator
205 KeyframeList::find_prev(const Time &x)const
206 {
207         return const_cast<KeyframeList*>(this)->find_prev(x);
208
209 }
210
211 void
212 KeyframeList::find_prev_next(const Time& time, Time &prev, Time &next)const
213 {
214         try { prev=find_prev(time)->get_time(); }
215         catch(...) { prev=Time::begin(); }
216         try { next=find_next(time)->get_time(); }
217         catch(...) { next=Time::end(); }
218 }
219
220 void
221 KeyframeList::insert_time(const Time& location, const Time& delta)
222 {
223 //      synfig::info("KeyframeList::insert_time(): loc=%s, delta=%s",location.get_string().c_str(),delta.get_string().c_str());
224         if(!delta)
225                 return;
226         try
227         {
228                 iterator iter(find_next(location));
229                 for(;iter!=end();++iter)
230                 {
231                         iter->set_time(iter->get_time()+delta);
232                 }
233                 sync();
234         }
235         catch(Exception::NotFound) { }
236 }