Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / 0.61.09 / src / synfig / keyframe.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file keyframe.cpp
3 **      \brief Template File
4 **
5 **      $Id$
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         //synfig::info("PRE-SORT:");
88         //dump();
89         sort(begin(),end());
90         //synfig::info("POST-SORT:");
91         //dump();
92 }
93
94 KeyframeList::iterator
95 KeyframeList::find(const UniqueID &x)
96 {
97         KeyframeList::iterator iter;
98         iter=std::find(begin(),end(),x);
99         if(iter==end())
100                 throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find UniqueID %d",x.get_uid()));
101         return iter;
102 }
103
104 KeyframeList::const_iterator
105 KeyframeList::find(const UniqueID &x)const
106 {
107         KeyframeList::const_iterator iter;
108         iter=std::find(begin(),end(),x);
109         if(iter==end())
110                 throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find UniqueID %d",x.get_uid()));
111         return iter;
112 }
113
114 KeyframeList::iterator
115 KeyframeList::add(const Keyframe &x)
116 {
117         push_back(x);
118         iterator ret(end());
119         ret--;
120         assert(x==*ret);
121         sync();
122         return ret;
123 }
124
125 void
126 KeyframeList::erase(const UniqueID &x)
127 {
128         std::vector<Keyframe>::erase(find(x));
129 }
130
131 KeyframeList::iterator
132 KeyframeList::find(const Time &x)
133 {
134         KeyframeList::iterator iter;
135         iter=binary_find(begin(),end(),x);
136         if(iter!=end() && iter->get_time().is_equal(x))
137                 return iter;
138 /*      iter++;
139         if(iter!=end() && iter->get_time().is_equal(x))
140                 return iter;
141 */
142         throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find Keyframe %s",x.get_string().c_str()));
143 }
144
145 KeyframeList::iterator
146 KeyframeList::find_next(const Time &x)
147 {
148         KeyframeList::iterator iter(binary_find(begin(),end(),x));
149
150         if(iter!=end())
151         {
152                 if(iter->get_time().is_more_than(x))
153                         return iter;
154                 ++iter;
155                 if(iter!=end())
156                 {
157                         if(iter->get_time().is_more_than(x))
158                                 return iter;
159 /*                      ++iter;
160                         if(iter!=end() && iter->get_time().is_more_than(x))
161                                 return iter;
162 */
163                 }
164         }
165
166         throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find next Keyframe %s",x.get_string().c_str()));
167 }
168
169
170 KeyframeList::iterator
171 KeyframeList::find_prev(const Time &x)
172 {
173         KeyframeList::iterator iter(binary_find(begin(),end(),x));
174
175         if(iter!=end())
176         {
177                 if(iter->get_time()+Time::epsilon()<x)
178                         return iter;
179                 if(iter!=begin() && (--iter)->get_time()+Time::epsilon()<x)
180                         return iter;
181         }
182         throw Exception::NotFound(strprintf("KeyframeList::find(): Can't find prev Keyframe %s",x.get_string().c_str()));
183
184 }
185
186
187
188 KeyframeList::const_iterator
189 KeyframeList::find(const Time &x)const
190 {
191         return const_cast<KeyframeList*>(this)->find(x);
192 }
193
194
195 KeyframeList::const_iterator
196 KeyframeList::find_next(const Time &x)const
197 {
198         return const_cast<KeyframeList*>(this)->find_next(x);
199
200 }
201
202
203 KeyframeList::const_iterator
204 KeyframeList::find_prev(const Time &x)const
205 {
206         return const_cast<KeyframeList*>(this)->find_prev(x);
207
208 }
209
210 void
211 KeyframeList::find_prev_next(const Time& time, Time &prev, Time &next)const
212 {
213         try { prev=find_prev(time)->get_time(); }
214         catch(...) { prev=Time::begin(); }
215         try { next=find_next(time)->get_time(); }
216         catch(...) { next=Time::end(); }
217 }
218
219 void
220 KeyframeList::insert_time(const Time& location, const Time& delta)
221 {
222 //      synfig::info("KeyframeList::insert_time(): loc=%s, delta=%s",location.get_string().c_str(),delta.get_string().c_str());
223         if(!delta)
224                 return;
225         try
226         {
227                 iterator iter(find_next(location));
228                 for(;iter!=end();++iter)
229                 {
230                         iter->set_time(iter->get_time()+delta);
231                 }
232                 sync();
233         }
234         catch(Exception::NotFound) { }
235 }