Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_04 / synfig-core / 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-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include <algorithm>
33
34 #include <ETL/stringf>
35
36 #include "keyframe.h"
37 #include "exception.h"
38 #include "general.h"
39 #include <ETL/misc>
40
41 #endif
42
43 /* === U S I N G =========================================================== */
44
45 using namespace std;
46 using namespace etl;
47 using namespace synfig;
48
49 /* === M A C R O S ========================================================= */
50
51 /* === G L O B A L S ======================================================= */
52
53 /* === P R O C E D U R E S ================================================= */
54
55 /* === M E T H O D S ======================================================= */
56
57 Keyframe::Keyframe():
58         time_(0)
59 {
60 }
61
62 Keyframe::Keyframe(const Time &time):
63         time_(time)
64 {
65 }
66
67 Keyframe::~Keyframe()
68 {
69 }
70
71 void
72 KeyframeList::dump()const
73 {
74         const_iterator iter;
75         int i;
76         synfig::info(">>>>>>>>BEGIN KEYFRAME DUMP");
77         for(iter=begin(),i=0;iter!=end();++iter,i++)
78         {
79                 synfig::info("#%d, time: %s, desc: %s",i,iter->get_time().get_string().c_str(),iter->get_description().c_str());
80         }
81         synfig::info("<<<<<<<<END KEYFRAME DUMP");
82 }
83
84 void
85 KeyframeList::sync()
86 {
87         //DEBUGPOINT();
88         //synfig::info("PRE-SORT:");
89         //dump();       
90         sort(begin(),end());
91         //synfig::info("POST-SORT:");
92         //dump();
93         //DEBUGPOINT();
94 }
95
96 KeyframeList::iterator
97 KeyframeList::find(const UniqueID &x)
98 {
99         KeyframeList::iterator iter;
100         iter=std::find(begin(),end(),x);
101         if(iter==end())
102                 throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find UniqueID %d",x.get_uid()));
103         return iter;
104 }
105
106 KeyframeList::const_iterator
107 KeyframeList::find(const UniqueID &x)const
108 {
109         KeyframeList::const_iterator iter;
110         iter=std::find(begin(),end(),x);
111         if(iter==end())
112                 throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find UniqueID %d",x.get_uid()));
113         return iter;
114 }
115
116 KeyframeList::iterator
117 KeyframeList::add(const Keyframe &x)
118 {
119         push_back(x);
120         iterator ret(end());
121         ret--;
122         assert(x==*ret);
123         sync();
124         return ret;
125 }
126
127 void
128 KeyframeList::erase(const UniqueID &x)
129 {
130         std::vector<Keyframe>::erase(find(x));
131 }
132
133 KeyframeList::iterator
134 KeyframeList::find(const Time &x)
135 {
136         KeyframeList::iterator iter;
137         iter=binary_find(begin(),end(),x);
138         if(iter!=end() && iter->get_time().is_equal(x))
139                 return iter;
140 /*      iter++;
141         if(iter!=end() && iter->get_time().is_equal(x))
142                 return iter;
143 */
144         throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find Keyframe %s",x.get_string().c_str()));
145 }
146
147 KeyframeList::iterator
148 KeyframeList::find_next(const Time &x)
149 {
150         KeyframeList::iterator iter(binary_find(begin(),end(),x));
151
152         if(iter!=end())
153         {
154                 if(iter->get_time().is_more_than(x))
155                         return iter;
156                 ++iter;
157                 if(iter!=end())
158                 {
159                         if(iter->get_time().is_more_than(x))
160                                 return iter;
161 /*                      ++iter;
162                         if(iter!=end() && iter->get_time().is_more_than(x))
163                                 return iter;
164 */
165                 }
166         }
167         
168         throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find next Keyframe %s",x.get_string().c_str()));
169 }
170
171
172 KeyframeList::iterator
173 KeyframeList::find_prev(const Time &x)
174 {
175         KeyframeList::iterator iter(binary_find(begin(),end(),x));
176
177         if(iter!=end())
178         {
179                 if(iter->get_time()+Time::epsilon()<x)
180                         return iter;
181                 if(iter!=begin() && (--iter)->get_time()+Time::epsilon()<x)
182                         return iter;
183         }
184         throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find prev Keyframe %s",x.get_string().c_str()));
185
186 }
187
188
189
190 KeyframeList::const_iterator
191 KeyframeList::find(const Time &x)const
192 {
193         return const_cast<KeyframeList*>(this)->find(x);
194 }
195
196
197 KeyframeList::const_iterator
198 KeyframeList::find_next(const Time &x)const
199 {
200         return const_cast<KeyframeList*>(this)->find_next(x);
201
202 }
203
204
205 KeyframeList::const_iterator
206 KeyframeList::find_prev(const Time &x)const
207 {
208         return const_cast<KeyframeList*>(this)->find_prev(x);
209
210 }
211
212 void
213 KeyframeList::find_prev_next(const Time& time, Time &prev, Time &next)const
214 {
215         try { prev=find_prev(time)->get_time(); }
216         catch(...) { prev=Time::begin(); }
217         try { next=find_next(time)->get_time(); }
218         catch(...) { next=Time::end(); }
219 }
220
221 void
222 KeyframeList::insert_time(const Time& location, const Time& delta)
223 {
224 //      synfig::info("KeyframeList::insert_time(): loc=%s, delta=%s",location.get_string().c_str(),delta.get_string().c_str());
225         if(!delta)
226                 return;
227         try
228         {
229                 iterator iter(find_next(location));
230                 for(;iter!=end();++iter)
231                 {
232                         iter->set_time(iter->get_time()+delta);
233                 }
234                 sync();
235         }
236         catch(Exception::NotFound) { }
237 }