Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_06 / src / synfig / valuenode_animated.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_animated.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 <vector>
33 #include <list>
34 #include <stdexcept>
35
36 #include <cmath>
37
38 #include <ETL/bezier>
39 #include <ETL/hermite>
40 #include <ETL/spline>
41 #include <ETL/handle>
42 #include <ETL/misc>
43
44 #include <algorithm>
45 #include <typeinfo>
46
47 #include "canvas.h"
48 #include "general.h"
49 #include "valuenode_animated.h"
50 #include "valuenode_const.h"
51 #include "exception.h"
52 #include "gradient.h"
53
54 #endif
55
56 /* === U S I N G =========================================================== */
57
58 using namespace std;
59 using namespace etl;
60 using namespace synfig;
61
62 /* === M A C R O S ========================================================= */
63
64 // Fast binary search implementation
65 /*
66 template<typename I, typename T> inline I
67 binary_find(I begin, I end, const T& value)
68 {
69         I iter(begin+(end-begin)/2);
70
71         while(end-begin>1 && !(*iter==value))
72         {
73                 ((*iter<value)?begin:end) = iter;
74
75                 iter = begin+(end-begin)/2;
76         }
77         return iter;
78 }
79 */
80
81 /*
82 template<typename T> String tangent_info(T a, T b, T v)
83 {
84         return "...";
85 }
86
87 String tangent_info(Vector a, Vector b, Vector v)
88 {
89         if(a==b)
90                 return strprintf("(should be zero) T=[%f,%f], Pp=[%f,%f], Pn=[%f,%f]",v[0],v[1],a[0],a[1],b[0],b[1]);
91         else
92                 return strprintf("(should NOT be zero) T=[%f,%f], Pp=[%f,%f], Pn=[%f,%f]",v[0],v[1],a[0],a[1],b[0],b[1]);
93 }
94 */
95
96 template <class T>
97 struct subtractor : public std::binary_function<T, T, T>
98 {
99         T operator()(const T &a,const T &b)const
100         {
101                 return a-b;
102         }
103 };
104
105 template <>
106 struct subtractor<Angle> : public std::binary_function<Angle, Angle, Angle>
107 {
108         Angle operator()(const Angle &a,const Angle &b)const
109         {
110                 return a.dist(b);
111         }
112 };
113
114 template <class T>
115 struct magnitude : public std::unary_function<float, T>
116 {
117         float operator()(const T &a)const
118         {
119                 return abs(a);
120         }
121 };
122
123 template <>
124 struct magnitude<Angle> : public std::unary_function<float, Angle>
125 {
126         float operator()(const Angle &a)const
127         {
128                 return abs(Angle::rad(a).get());
129         }
130 };
131
132 template <>
133 struct magnitude<Vector> : public std::unary_function<float, Vector>
134 {
135         float operator()(const Vector &a)const
136         {
137                 return a.mag();
138         }
139 };
140
141 template <>
142 struct magnitude<Color> : public std::unary_function<float, Color>
143 {
144         float operator()(const Color &a)const
145         {
146                 return abs(a.get_y());
147         }
148 };
149
150
151
152
153
154 template <class T>
155 struct is_angle_type
156 {
157         bool operator()()const
158         {
159                 return false;
160         }
161 };
162
163 template <>
164 struct is_angle_type<Angle>
165 {
166         bool operator()()const
167         {
168                 return true;
169         }
170 };
171
172 /* === G L O B A L S ======================================================= */
173
174 /* === C L A S S E S ======================================================= */
175
176 template<typename T>
177 class _Hermite : public synfig::ValueNode_Animated
178 {
179 public:
180         typedef T value_type;
181         affine_combo<value_type,Time> affine_combo_func;
182         subtractor<value_type>  subtract_func;
183         magnitude<value_type>   magnitude_func;
184         is_angle_type<value_type>       is_angle;
185 private:
186         struct PathSegment
187         {
188                 is_angle_type<value_type>       is_angle;
189                 subtractor<value_type>  subtract_func;
190
191                 mutable hermite<Time,Time> first;
192                 mutable hermite<value_type,Time> second;
193                 WaypointList::iterator start;
194                 WaypointList::iterator end;
195
196                 value_type resolve(const Time &t)const
197                 {
198                         bool start_static(start->is_static());
199                         bool end_static(end->is_static());
200
201                         if(!start_static || !end_static)
202                         {
203                                 //if(!start_static)
204                                         second.p1()=start->get_value(t).get(value_type());
205                                 if(start->get_after()==INTERPOLATION_CONSTANT || end->get_before()==INTERPOLATION_CONSTANT)
206                                         return second.p1();
207                                 //if(!end_static)
208                                         second.p2()=end->get_value(t).get(value_type());
209
210                                 // At the moment, the only type of non-constant interpolation
211                                 // that we support is linear.
212                                 second.t1()=
213                                 second.t2()=subtract_func(second.p2(),second.p1());
214
215                                 second.sync();
216                         }
217
218                         return second(first(t));
219                 }
220         };
221         typedef vector <
222                 PathSegment
223                 /*
224                 pair <
225                         hermite<Time,Time>,
226                         hermite<value_type,Time>
227                 >
228                 */
229         > curve_list_type;
230
231         curve_list_type curve_list;
232
233         // Bounds of this curve
234         Time r,s;
235
236 public:
237         ValueNode* clone(const GUID& deriv_guid)const
238         {
239                 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
240                 _Hermite<T>* ret(new _Hermite<T>());
241                 ret->set_guid(get_guid()^deriv_guid);
242                 for(WaypointList::const_iterator iter=waypoint_list().begin();iter!=waypoint_list().end();++iter)
243                         ret->add(iter->clone(deriv_guid));
244                 return ret;
245         }
246
247         _Hermite()
248         {
249                 set_type(ValueBase(value_type()).get_type());
250         }
251
252         virtual WaypointList::iterator new_waypoint(Time t, ValueBase value)
253         {
254                 // Make sure we are getting data of the correct type
255                 //if(data.type!=type)
256                 //      return waypoint_list_type::iterator();
257
258                 try { find(t); throw Exception::BadTime(_("A waypoint already exists at this point in time")); } catch(Exception::NotFound) { };
259                 Waypoint waypoint(value,t);
260                 waypoint.set_parent_value_node(this);
261
262                 waypoint_list_.push_back(waypoint);
263                 WaypointList::iterator ret=waypoint_list_.end();
264                 --ret;
265
266                 if(is_angle())
267                 {
268                         ret->set_before(INTERPOLATION_LINEAR);
269                         ret->set_after(INTERPOLATION_LINEAR);
270                 }
271
272                 changed();
273
274                 return ret;
275         }
276
277         virtual WaypointList::iterator new_waypoint(Time t, ValueNode::Handle value_node)
278         {
279                 // Make sure we are getting data of the correct type
280                 //if(data.type!=type)
281                 //      return waypoint_list_type::iterator();
282                 try { find(t); throw Exception::BadTime(_("A waypoint already exists at this point in time")); } catch(Exception::NotFound) { };
283
284                 Waypoint waypoint(value_node,t);
285                 waypoint.set_parent_value_node(this);
286
287                 waypoint_list_.push_back(waypoint);
288                 WaypointList::iterator ret=waypoint_list_.end();
289                 --ret;
290
291                 if(is_angle())
292                 {
293                         ret->set_before(INTERPOLATION_LINEAR);
294                         ret->set_after(INTERPOLATION_LINEAR);
295                 }
296
297                 changed();
298
299                 return ret;
300         }
301
302         virtual void on_changed()
303         {
304                 ValueNode_Animated::on_changed();
305
306                 if(waypoint_list_.size()<=1)
307                         return;
308                 std::sort(waypoint_list_.begin(),waypoint_list_.end());
309                 //waypoint_list_.sort();
310
311                 r=waypoint_list_.front().get_time();
312                 s=waypoint_list_.back().get_time();
313
314                 curve_list.clear();
315
316                 WaypointList::iterator prev,iter,next=waypoint_list_.begin();
317                 int i=0;
318
319                 for(iter=next++;iter!=waypoint_list_.end() && next!=waypoint_list_.end();prev=iter,iter=next++,i++)
320                 {
321                         typename curve_list_type::value_type curve;
322                         WaypointList::iterator after_next(next);
323                         ++after_next;
324
325                         curve.start=iter;
326                         curve.end=next;
327
328                         // Set up the positions
329                         curve.first.set_rs(iter->get_time(), next->get_time());
330                         curve.second.set_rs(iter->get_time(), next->get_time());
331
332                         Waypoint::Interpolation iter_get_after(iter->get_after());
333                         Waypoint::Interpolation next_get_after(next->get_after());
334                         Waypoint::Interpolation iter_get_before(iter->get_before());
335                         Waypoint::Interpolation next_get_before(next->get_before());
336
337                         if(is_angle())
338                         {
339                                 if(iter_get_after==INTERPOLATION_TCB)
340                                         iter_get_after=INTERPOLATION_LINEAR;
341                                 if(next_get_after==INTERPOLATION_TCB)
342                                         next_get_after=INTERPOLATION_LINEAR;
343                                 if(iter_get_before==INTERPOLATION_TCB)
344                                         iter_get_before=INTERPOLATION_LINEAR;
345                                 if(next_get_before==INTERPOLATION_TCB)
346                                         next_get_before=INTERPOLATION_LINEAR;
347                         }
348
349                         if(iter->is_static() && next->is_static())
350                         {
351                                 curve.second.p1()=iter->get_value().get(T());
352                                 curve.second.p2()=next->get_value().get(T());
353                                 if(iter_get_after==INTERPOLATION_CONSTANT || next_get_before==INTERPOLATION_CONSTANT)
354                                 {
355                                         // Sections must be constant on both sides.
356                                         // NOTE: this is commented out because of some
357                                         // user interface issues. Namely, if a section is
358                                         // constant and the user turns off the constant on
359                                         // one waypoint, this will end up turning it back on.
360                                         // Confusing.
361                                         //iter->get_after()=next->get_before()=INTERPOLATION_CONSTANT;
362                                         curve.second.p1()=
363                                         curve.second.p2()=iter->get_value().get(T());
364                                         curve.second.t1()=
365                                         curve.second.t2()=subtract_func(curve.second.p1(),curve.second.p2());
366                                 }
367                                 else
368                                 {
369                                         if(iter_get_after==INTERPOLATION_TCB && iter!=waypoint_list_.begin() && !is_angle())
370                                         {
371                                                 if(iter->get_before()!=INTERPOLATION_TCB && !curve_list.empty())
372                                                 {
373                                                         curve.second.t1()=curve_list.back().second.t2();
374                                                 }
375                                                 else
376                                                 {
377
378                                                 const Real& t(iter->get_tension());             // Tension
379                                                 const Real& c(iter->get_continuity());  // Continuity
380                                                 const Real& b(iter->get_bias());                        // Bias
381
382                                                 // The folloing line works where the previous line fails.
383                                                 value_type Pp; Pp=curve_list.back().second.p1();        // P_{i-1}
384
385                                                 const value_type& Pc(curve.second.p1());        // P_i
386                                                 const value_type& Pn(curve.second.p2());        // P_{i+1}
387
388                                                 // TCB
389                                                 value_type vect(static_cast<value_type>(subtract_func(Pc,Pp)*(((1.0-t)*(1.0+c)*(1.0+b))/2.0)+(Pn-Pc)*(((1.0-t)*(1.0-c)*(1.0-b))/2.0)));
390
391                                                 // Tension Only
392                                                 //value_type vect=(value_type)((Pn-Pp)*(1.0-t));
393
394                                                 // Linear
395                                                 //value_type vect=(value_type)(Pn-Pc);
396
397                                                 // Debugging stuff
398                                                 //synfig::info("%d:t1: %s",i,tangent_info(Pp,Pn,vect).c_str());
399
400                                                 // Adjust for time
401                                                 //vect=value_type(vect*(curve.second.get_dt()*2.0)/(curve.second.get_dt()+curve_list.back().second.get_dt()));
402                                                 //vect=value_type(vect*(curve.second.get_dt())/(curve_list.back().second.get_dt()));
403
404                                                 curve.second.t1()=vect;
405                                                 }
406                                         }
407                                         else if(
408                                                 iter_get_after==INTERPOLATION_LINEAR || iter_get_after==INTERPOLATION_HALT ||
409                                                 (iter_get_after==INTERPOLATION_TCB && iter==waypoint_list_.begin()))
410                                         {
411                                                 curve.second.t1()=subtract_func(curve.second.p2(),curve.second.p1());
412                                         }
413
414                                         if(iter_get_before==INTERPOLATION_TCB && iter->get_after()!=INTERPOLATION_TCB && !curve_list.empty())
415                                         {
416                                                 curve_list.back().second.t2()=curve.second.t1();
417                                                 curve_list.back().second.sync();
418                                         }
419
420
421                                         if(next_get_before==INTERPOLATION_TCB && after_next!=waypoint_list_.end()  && !is_angle())
422                                         {
423                                                 const Real &t(next->get_tension());             // Tension
424                                                 const Real &c(next->get_continuity());  // Continuity
425                                                 const Real &b(next->get_bias());                        // Bias
426                                                 const value_type &Pp(curve.second.p1());        // P_{i-1}
427                                                 const value_type &Pc(curve.second.p2());        // P_i
428                                                 value_type Pn; Pn=after_next->get_value().get(T());     // P_{i+1}
429
430                                                 // TCB
431                                                 value_type vect(static_cast<value_type>(subtract_func(Pc,Pp)*(((1.0-t)*(1.0-c)*(1.0+b))/2.0)+(Pn-Pc)*(((1.0-t)*(1.0+c)*(1.0-b))/2.0)));
432
433                                                 // Tension Only
434                                                 //value_type vect((value_type)((Pn-Pp)*(1.0-t)));
435
436                                                 // Linear
437                                                 //value_type vect=(value_type)(Pc-Pp);
438
439                                                 // Debugging stuff
440                                                 //synfig::info("%d:t2: %s",i,tangent_info(Pp,Pn,vect).c_str());
441
442                                                 // Adjust for time
443                                                 //vect=value_type(vect*(curve.second.get_dt()*2.0)/(curve.second.get_dt()+(after_next->get_time()-next->get_time())));
444                                                 //vect=value_type(vect*(curve.second.get_dt()/((after_next->get_time()-next->get_time()))));
445
446                                                 curve.second.t2()=vect;
447                                         }
448                                         else if(
449                                                 next_get_before==INTERPOLATION_LINEAR || next_get_before==INTERPOLATION_HALT ||
450                                                 (next_get_before==INTERPOLATION_TCB && after_next==waypoint_list_.end()))
451                                         {
452                                                 curve.second.t2()=subtract_func(curve.second.p2(),curve.second.p1());
453                                         }
454
455                                         // Adjust for time
456                                         const float timeadjust(0.5);
457
458                                         if(!curve_list.empty())
459                                                 curve.second.t1()*=(curve.second.get_dt()*(timeadjust+1))/(curve.second.get_dt()*timeadjust+curve_list.back().second.get_dt());
460                                         if(after_next!=waypoint_list_.end())
461                                                 curve.second.t2()*=(curve.second.get_dt()*(timeadjust+1))/(curve.second.get_dt()*timeadjust+(after_next->get_time()-next->get_time()));
462
463                                         if(iter_get_after==INTERPOLATION_HALT)
464                                                 curve.second.t1()*=0;
465
466                                         if(next_get_before==INTERPOLATION_HALT)
467                                                 curve.second.t2()*=0;
468                                 }
469                         }
470
471                         // Set up the time to the default stuff
472                         curve.first.set_rs(iter->get_time(), next->get_time());
473                         curve.first.p1()=iter->get_time();
474                         curve.first.p2()=next->get_time();
475                         curve.first.t1()=(curve.first.p2()-curve.first.p1())*(1.0f-iter->get_time_tension());
476                         curve.first.t2()=(curve.first.p2()-curve.first.p1())*(1.0f-next->get_time_tension());
477
478
479                         curve.first.sync();
480                         curve.second.sync();
481
482                         curve_list.push_back(curve);
483                 }
484         }
485
486         virtual ValueBase operator()(Time t)const
487         {
488                 if(waypoint_list_.empty())
489                         return value_type();    //! \todo Perhaps we should throw something here?
490                 if(waypoint_list_.size()==1)
491                         return waypoint_list_.front().get_value(t);
492                 if(t<=r)
493                         return waypoint_list_.front().get_value(t);
494                 if(t>=s)
495                         return waypoint_list_.back().get_value(t);
496
497                 typename curve_list_type::const_iterator iter;
498
499                 // This next line will set iter to the
500                 // correct iterator for the given time.
501                 for(iter=curve_list.begin();iter<curve_list.end() && t>=iter->first.get_s();++iter)
502                         continue;
503                 if(iter==curve_list.end())
504                         return waypoint_list_.back().get_value(t);
505                 return iter->resolve(t);
506         }
507 };
508
509
510 template<typename T>
511 class _Constant : public synfig::ValueNode_Animated
512 {
513 public:
514         typedef T value_type;
515
516 private:
517
518         // Bounds of this curve
519         Time r,s;
520
521 public:
522         ValueNode* clone(const GUID& deriv_guid)const
523         {
524                 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
525                 _Constant<T>* ret(new _Constant<T>());
526                 ret->set_guid(get_guid()^deriv_guid);
527                 for(WaypointList::const_iterator iter=waypoint_list().begin();iter!=waypoint_list().end();++iter)
528                         ret->add(iter->clone(deriv_guid));
529                 return ret;
530         }
531
532         _Constant()
533         {
534                 set_type(ValueBase(value_type()).get_type());
535         }
536
537         virtual WaypointList::iterator new_waypoint(Time t, ValueBase value)
538         {
539                 // Make sure we are getting data of the correct type
540                 //if(data.type!=type)
541                 //      return waypoint_list_type::iterator();
542                 try { find(t); throw Exception::BadTime(_("A waypoint already exists at this point in time")); } catch(Exception::NotFound) { };
543
544                 Waypoint waypoint(value,t);
545                 waypoint.set_parent_value_node(this);
546
547                 waypoint_list_.push_back(waypoint);
548                 WaypointList::iterator ret=waypoint_list_.end();
549                 --ret;
550                 changed();
551
552                 return ret;
553         }
554
555         virtual WaypointList::iterator new_waypoint(Time t, ValueNode::Handle value_node)
556         {
557                 // Make sure we are getting data of the correct type
558                 //if(data.type!=type)
559                 //      return waypoint_list_type::iterator();
560                 try { find(t); throw Exception::BadTime(_("A waypoint already exists at this point in time")); } catch(Exception::NotFound) { };
561
562                 Waypoint waypoint(value_node,t);
563                 waypoint.set_parent_value_node(this);
564
565                 waypoint_list_.push_back(waypoint);
566                 WaypointList::iterator ret=waypoint_list_.end();
567                 --ret;
568                 changed();
569
570                 return ret;
571         }
572
573         virtual void on_changed()
574         {
575                 ValueNode_Animated::on_changed();
576
577                 if(waypoint_list_.size()<=1)
578                         return;
579                 std::sort(waypoint_list_.begin(),waypoint_list_.end());
580                 //waypoint_list_.sort();
581                 r=waypoint_list_.front().get_time();
582                 s=waypoint_list_.back().get_time();
583
584         }
585
586         virtual ValueBase operator()(Time t)const
587         {
588                 if(waypoint_list_.size()==1)
589                         return waypoint_list_.front().get_value(t);
590                 if(waypoint_list_.empty())
591                         return value_type();
592                 if(t<=r)
593                         return waypoint_list_.front().get_value(t);
594                 if(t>=s)
595                         return waypoint_list_.back().get_value(t);
596
597                 typename WaypointList::const_iterator iter;
598                 typename WaypointList::const_iterator next;
599
600                 // This next line will set iter to the
601                 // correct iterator for the given time.
602                 for(next=waypoint_list_.begin(),iter=next++;next!=waypoint_list_.end() && t>=next->get_time();iter=next++)
603                         continue;
604
605                 return iter->get_value(t);
606         }
607 };
608
609 class _AnimBool : public synfig::ValueNode_Animated
610 {
611 public:
612         typedef bool value_type;
613
614 private:
615
616         // Bounds of this curve
617         Time r,s;
618
619 public:
620         ValueNode* clone(const GUID& deriv_guid)const
621         {
622                 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
623                 _AnimBool* ret(new _AnimBool());
624                 ret->set_guid(get_guid()^deriv_guid);
625                 for(WaypointList::const_iterator iter=waypoint_list().begin();iter!=waypoint_list().end();++iter)
626                         ret->add(iter->clone(deriv_guid));
627                 return ret;
628         }
629
630         _AnimBool()
631         {
632                 set_type(ValueBase(value_type()).get_type());
633         }
634
635         virtual WaypointList::iterator new_waypoint(Time t, ValueBase value)
636         {
637                 // Make sure we are getting data of the correct type
638                 //if(data.type!=type)
639                 //      return waypoint_list_type::iterator();
640                 try { find(t); throw Exception::BadTime(_("A waypoint already exists at this point in time")); } catch(Exception::NotFound) { };
641
642
643                 Waypoint waypoint(value,t);
644                 waypoint.set_parent_value_node(this);
645
646                 waypoint_list_.push_back(waypoint);
647                 WaypointList::iterator ret=waypoint_list_.end();
648                 --ret;
649                 changed();
650
651                 return ret;
652         }
653
654         virtual WaypointList::iterator new_waypoint(Time t, ValueNode::Handle value_node)
655         {
656                 // Make sure we are getting data of the correct type
657                 //if(data.type!=type)
658                 //      return waypoint_list_type::iterator();
659                 try { find(t); throw Exception::BadTime(_("A waypoint already exists at this point in time")); } catch(Exception::NotFound) { };
660
661                 Waypoint waypoint(value_node,t);
662                 waypoint.set_parent_value_node(this);
663
664                 waypoint_list_.push_back(waypoint);
665                 WaypointList::iterator ret=waypoint_list_.end();
666                 --ret;
667                 changed();
668
669                 return ret;
670         }
671
672         virtual void on_changed()
673         {
674                 ValueNode_Animated::on_changed();
675
676                 if(waypoint_list_.size()<=1)
677                         return;
678                 std::sort(waypoint_list_.begin(),waypoint_list_.end());
679                 //waypoint_list_.sort();
680                 r=waypoint_list_.front().get_time();
681                 s=waypoint_list_.back().get_time();
682
683         }
684
685         virtual ValueBase operator()(Time t)const
686         {
687                 if(waypoint_list_.size()==1)
688                         return waypoint_list_.front().get_value(t);
689                 if(waypoint_list_.empty())
690                         return false;
691                 if(t<r)
692                         return waypoint_list_.front().get_value(t);
693                 if(t>s)
694                         return waypoint_list_.back().get_value(t);
695
696                 WaypointList::const_iterator iter;
697                 WaypointList::const_iterator next;
698
699                 // This next line will set iter to the
700                 // correct iterator for the given time.
701                 for(next=waypoint_list_.begin(),iter=next++;next!=waypoint_list_.end() && t>=next->get_time();iter=next++)
702                         if(iter->get_time()==t)
703                                 return iter->get_value(t);
704
705                 if(iter->get_time()==t)
706                         return iter->get_value(t);
707
708                 if(next!=waypoint_list_.end())
709                         return iter->get_value(t).get(bool()) || next->get_value(t).get(bool());
710                 return iter->get_value(t);
711         }
712 };
713
714 /* === M E T H O D S ======================================================= */
715
716 ValueNode_Animated::ValueNode_Animated()
717 {
718         DCAST_HACK_ENABLE();
719 }
720
721 int
722 ValueNode_Animated::find(const Time& begin,const Time& end,std::vector<Waypoint*>& selected)
723 {
724         Time curr_time(begin);
725         int ret(0);
726
727         // try to grab first waypoint
728         try
729         {
730                 WaypointList::iterator iter;
731                 iter=find(curr_time);
732                 selected.push_back(&*iter);
733                 ret++;
734         }
735         catch(...) { }
736
737         try
738         {
739                 WaypointList::iterator iter;
740                 while(true)
741                 {
742                         iter=find_next(curr_time);
743                         curr_time=iter->get_time();
744                         if(curr_time>=end)
745                                 break;
746                         selected.push_back(&*iter);
747                         ret++;
748                 }
749         }
750         catch(...) { }
751
752         return ret;
753 }
754
755 /*
756 void
757 ValueNode_Animated::manipulate_time(const Time& old_begin,const Time& old_end,const Time& new_begin,const Time& new_end)
758 {
759 #define old_2_new(x)    (((x)-old_begin)/(old_end-old_begin)*(new_end-new_begin)+new_begin)
760         std::vector<Waypoint*> selected;
761         std::vector<Waypoint*>::iterator iter;
762
763         if(find(old_begin,old_end,selected))
764         {
765                 // check to make sure this operation is OK
766                 for(iter=selected.begin();iter!=selected.end();++iter)
767                 {
768                         try
769                         {
770                                 Time new_time(old_2_new((*iter)->get_time()));
771                                 if(new_time>=old_begin && new_time<old_end)
772                                         continue;
773                                 find(new_time);
774                                 // If we found a waypoint already at that time, then
775                                 // we need to abort
776                                 throw Exception::BadTime(_("Waypoint Conflict"));
777                         }
778                         catch(Exception::NotFound) { }
779
780                         selected.back()->set_time(old_2_new(selected.back()->get_time()));
781                         selected.pop_back();
782                 }
783
784
785                 while(!selected.empty())
786                 {
787                         selected.back()->set_time(old_2_new(selected.back()->get_time()));
788                         selected.pop_back();
789                 }
790
791                 changed();
792         }
793 #undef old_2_new
794 }
795 */
796
797 Waypoint
798 ValueNode_Animated::new_waypoint_at_time(const Time& time)const
799 {
800         Waypoint waypoint;
801         try
802         {
803                 // Trivial case, we are sitting on a waypoint
804                 waypoint=*find(time);
805                 waypoint.make_unique();
806         }
807         catch(...)
808         {
809                 if(waypoint_list().empty())
810                 {
811                         waypoint.set_value((*this)(time));
812                 }
813                 else
814                 {
815                         WaypointList::const_iterator prev;
816                         WaypointList::const_iterator next;
817
818                         bool has_prev(false), has_next(false);
819
820                         try { prev=find_prev(time); has_prev=true; } catch(...) { }
821                         try { next=find_next(time); has_next=true; } catch(...) { }
822
823                         /*
824                         WaypointList::const_iterator closest;
825
826                         if(has_prev&&!has_next)
827                                 closest=prev;
828                         else if(has_next&&!has_prev)
829                                 closest=next;
830                         else if(time-prev->get_time()<next->get_time()-time)
831                                 closest=prev;
832                         else
833                                 closest=next;
834
835                         for(iter=waypoint_list().begin();iter!=waypoint_list().end();++iter)
836                         {
837                                 const Real dist(abs(iter->get_time()-time));
838                                 if(dist<abs(closest->get_time()-time))
839                                         closest=iter;
840                         }
841                         */
842
843                         if(has_prev && !prev->is_static())
844                                 waypoint.set_value_node(prev->get_value_node());
845                         if(has_next && !next->is_static())
846                                 waypoint.set_value_node(next->get_value_node());
847                         else
848                                 waypoint.set_value((*this)(time));
849
850                         /*if(has_prev)
851                                 waypoint.set_after(prev->get_before());
852                         if(has_next)
853                                 waypoint.set_before(next->get_after());
854                         */
855                 }
856         }
857         waypoint.set_time(time);
858         waypoint.set_parent_value_node(const_cast<ValueNode_Animated*>(this));
859 //      synfig::info("waypoint.get_after()=set to %d",waypoint.get_after());
860 //      synfig::info("waypoint.get_before()=set to %d",waypoint.get_before());
861
862         return waypoint;
863 }
864
865 ValueNode_Animated::WaypointList::iterator
866 ValueNode_Animated::find(const UniqueID &x)
867 {
868         ValueNode_Animated::WaypointList::iterator iter;
869         iter=std::find(waypoint_list().begin(),waypoint_list().end(),x);
870         if(iter==waypoint_list().end() || iter->get_uid()!=x.get_uid())
871                 throw Exception::NotFound(strprintf("ValueNode_Animated::find(): Can't find UniqueID %d",x.get_uid()));
872         return iter;
873 }
874
875 ValueNode_Animated::WaypointList::const_iterator
876 ValueNode_Animated::find(const UniqueID &x)const
877 {
878         return const_cast<ValueNode_Animated*>(this)->find(x);
879         /*
880         ValueNode_Animated::WaypointList::const_iterator iter;
881         iter=std::find(waypoint_list().begin(),waypoint_list().end(),x);
882         if(iter!=waypoint_list().end() && iter->get_uid()!=x.get_uid())
883                 throw Exception::NotFound(strprintf("ValueNode_Animated::find()const: Can't find UniqueID %d",x.get_uid()));
884         return iter;
885         */
886 }
887
888 ValueNode_Animated::WaypointList::iterator
889 ValueNode_Animated::find(const Time &x)
890 {
891         WaypointList::iterator iter(binary_find(waypoint_list().begin(),waypoint_list().end(),x));
892
893         if(iter!=waypoint_list().end() && x.is_equal(iter->get_time()))
894                 return iter;
895
896         throw Exception::NotFound(strprintf("ValueNode_Animated::find(): Can't find Waypoint at %s",x.get_string().c_str()));
897 }
898
899 ValueNode_Animated::WaypointList::const_iterator
900 ValueNode_Animated::find(const Time &x)const
901 {
902         return const_cast<ValueNode_Animated*>(this)->find(x);
903         /*
904         WaypointList::const_iterator iter(binary_find(waypoint_list().begin(),waypoint_list().end(),x));
905
906         if(iter!=waypoint_list().end() && x.is_equal(iter->get_time()))
907                 return iter;
908
909         throw Exception::NotFound(strprintf("ValueNode_Animated::find(): Can't find Waypoint at %s",x.get_string().c_str()));
910         */
911 }
912
913 ValueNode_Animated::WaypointList::iterator
914 ValueNode_Animated::find_next(const Time &x)
915 {
916         WaypointList::iterator iter(binary_find(waypoint_list().begin(),waypoint_list().end(),x));
917
918         if(iter!=waypoint_list().end())
919         {
920                 if(iter->get_time().is_more_than(x))
921                         return iter;
922                 ++iter;
923                 if(iter!=waypoint_list().end() && iter->get_time().is_more_than(x))
924                         return iter;
925         }
926
927         throw Exception::NotFound(strprintf("ValueNode_Animated::find_next(): Can't find Waypoint after %s",x.get_string().c_str()));
928 }
929
930 ValueNode_Animated::WaypointList::const_iterator
931 ValueNode_Animated::find_next(const Time &x)const
932 {
933         return const_cast<ValueNode_Animated*>(this)->find_next(x);
934         /*
935         WaypointList::const_iterator iter(binary_find(waypoint_list().begin(),waypoint_list().end(),x));
936
937         if(iter!=waypoint_list().end())
938         {
939                 if(iter->get_time()-Time::epsilon()>x)
940                         return iter;
941                 ++iter;
942                 if(iter!=waypoint_list().end() && iter->get_time()-Time::epsilon()>x)
943                         return iter;
944         }
945
946         throw Exception::NotFound(strprintf("ValueNode_Animated::find_next(): Can't find Waypoint after %s",x.get_string().c_str()));
947 */
948 }
949
950 ValueNode_Animated::WaypointList::iterator
951 ValueNode_Animated::find_prev(const Time &x)
952 {
953         WaypointList::iterator iter(binary_find(waypoint_list().begin(),waypoint_list().end(),x));
954
955         if(iter!=waypoint_list().end())
956         {
957                 if(iter->get_time().is_less_than(x))
958                         return iter;
959                 if(iter!=waypoint_list().begin() && (--iter)->get_time().is_less_than(x))
960                         return iter;
961         }
962
963         throw Exception::NotFound(strprintf("ValueNode_Animated::find_prev(): Can't find Waypoint after %s",x.get_string().c_str()));
964 }
965
966 ValueNode_Animated::WaypointList::const_iterator
967 ValueNode_Animated::find_prev(const Time &x)const
968 {
969         return const_cast<ValueNode_Animated*>(this)->find_prev(x);
970         /*
971         WaypointList::const_iterator iter(binary_find(waypoint_list().begin(),waypoint_list().end(),x));
972
973         if(iter!=waypoint_list().end())
974         {
975                 if(iter->get_time()+Time::epsilon()<x)
976                         return iter;
977                 if(iter!=waypoint_list().begin() && (--iter)->get_time()+Time::epsilon()<x)
978                         return iter;
979         }
980         throw Exception::NotFound(strprintf("ValueNode_Animated::find_prev(): Can't find Waypoint after %s",x.get_string().c_str()));
981         */
982 }
983
984 void
985 ValueNode_Animated::erase(const UniqueID &x)
986 {
987         waypoint_list().erase(find(x));
988 }
989
990 ValueNode_Animated::WaypointList::iterator
991 ValueNode_Animated::add(const Waypoint &x)
992 {
993         Waypoint waypoint(x);
994         waypoint.set_parent_value_node(this);
995         waypoint_list_.push_back(waypoint);
996         //assert(waypoint_list_.back().get_parent_value_node()==this);
997         WaypointList::iterator ret=waypoint_list_.end();
998         --ret;
999         changed();
1000         return ret;
1001 }
1002
1003 void
1004 ValueNode_Animated::set_type(ValueBase::Type t)
1005 {
1006         ValueNode::set_type(t);
1007 }
1008
1009 ValueNode_Animated::Handle
1010 synfig::ValueNode_Animated::create(ValueBase::Type type)
1011 {
1012         switch(type)
1013         {
1014                 case ValueBase::TYPE_TIME:
1015                         return ValueNode_Animated::Handle(new _Hermite<Time>);
1016                 case ValueBase::TYPE_REAL:
1017                         return ValueNode_Animated::Handle(new _Hermite<Vector::value_type>);
1018                 case ValueBase::TYPE_INTEGER:
1019                         return ValueNode_Animated::Handle(new _Hermite<int>);
1020                 case ValueBase::TYPE_ANGLE:
1021                         return ValueNode_Animated::Handle(new _Hermite<Angle>);
1022                 case ValueBase::TYPE_VECTOR:
1023                         return ValueNode_Animated::Handle(new _Hermite<Vector>);
1024                 case ValueBase::TYPE_COLOR:
1025                         return ValueNode_Animated::Handle(new _Hermite<Color>);
1026
1027                 case ValueBase::TYPE_STRING:
1028                         return ValueNode_Animated::Handle(new _Constant<String>);
1029                 case ValueBase::TYPE_GRADIENT:
1030                         return ValueNode_Animated::Handle(new _Constant<Gradient>);
1031                 case ValueBase::TYPE_BOOL:
1032                         return ValueNode_Animated::Handle(new _AnimBool);
1033                 case ValueBase::TYPE_CANVAS:
1034                         return ValueNode_Animated::Handle(new _Constant<Canvas::LooseHandle>);
1035                 default:
1036                         throw
1037                                 Exception::BadType(strprintf(_("%s: You cannot use a %s in an animated ValueNode"),"synfig::ValueNode_Animated::create()",
1038                                         ValueBase::type_name(type).c_str())
1039                                 );
1040                         break;
1041         }
1042         return ValueNode_Animated::Handle();
1043 }
1044
1045 ValueNode_Animated::Handle
1046 ValueNode_Animated::create(const ValueBase& value, const Time& time)
1047 {
1048         return create(ValueNode::Handle(ValueNode_Const::create(value)),time);
1049 }
1050
1051 ValueNode_Animated::Handle
1052 ValueNode_Animated::create(ValueNode::Handle value_node, const Time& time)
1053 {
1054         ValueNode_Animated::Handle ret(create(value_node->get_type()));
1055         ret->new_waypoint(time,value_node);
1056         return ret;
1057 }
1058
1059 ValueNode_Animated::~ValueNode_Animated()
1060 {
1061 }
1062
1063 String
1064 ValueNode_Animated::get_name()const
1065 {
1066         return "animated";
1067 }
1068
1069 String
1070 ValueNode_Animated::get_local_name()const
1071 {
1072         return _("Animated");
1073 }
1074
1075 void ValueNode_Animated::get_times_vfunc(Node::time_set &set) const
1076 {
1077         //add all the way point times to the value node...
1078
1079         WaypointList::const_iterator    i = waypoint_list().begin(),
1080                                                                         end = waypoint_list().end();
1081
1082         for(; i != end; ++i)
1083         {
1084                 TimePoint t;
1085                 t.set_time(i->get_time());
1086                 t.set_before(i->get_before());
1087                 t.set_after(i->get_after());
1088                 t.set_guid(i->get_guid());
1089                 set.insert(t);
1090         }
1091 }
1092 struct timecmp
1093  {
1094         Time t;
1095
1096         timecmp(const Time &c) :t(c) {}
1097
1098         bool operator()(const Waypoint &rhs) const
1099         {
1100                 return t.is_equal(rhs.get_time());
1101         }
1102  };
1103
1104  ValueNode_Animated::findresult
1105  ValueNode_Animated::find_uid(const UniqueID &x)
1106  {
1107         findresult      f;
1108         f.second = false;
1109
1110         //search for it... and set the bool part of the return value to true if we found it!
1111         f.first = std::find(waypoint_list_.begin(), waypoint_list_.end(), x);
1112         if(f.first != waypoint_list_.end())
1113                 f.second = true;
1114
1115         return f;
1116  }
1117
1118  ValueNode_Animated::const_findresult
1119  ValueNode_Animated::find_uid(const UniqueID &x)const
1120  {
1121         const_findresult        f;
1122         f.second = false;
1123
1124         //search for it... and set the bool part of the return value to true if we found it!
1125         f.first = std::find(waypoint_list_.begin(), waypoint_list_.end(), x);
1126         if(f.first != waypoint_list_.end())
1127                 f.second = true;
1128
1129         return f;
1130  }
1131
1132  ValueNode_Animated::findresult
1133  ValueNode_Animated::find_time(const Time &x)
1134  {
1135         findresult      f;
1136         f.second = false;
1137
1138         //search for it... and set the bool part of the return value to true if we found it!
1139         f.first = std::find_if(waypoint_list_.begin(), waypoint_list_.end(), timecmp(x));
1140         if(f.first != waypoint_list_.end())
1141                 f.second = true;
1142
1143         return f;
1144  }
1145
1146 ValueNode_Animated::const_findresult
1147 ValueNode_Animated::find_time(const Time &x)const
1148 {
1149         const_findresult        f;
1150         f.second = false;
1151
1152         //search for it... and set the bool part of the return value to true if we found it!
1153         f.first = std::find_if(waypoint_list_.begin(), waypoint_list_.end(), timecmp(x));
1154         if(f.first != waypoint_list_.end())
1155                 f.second = true;
1156
1157         return f;
1158 }
1159
1160 void
1161 ValueNode_Animated::insert_time(const Time& location, const Time& delta)
1162 {
1163         if(!delta)
1164                 return;
1165         try
1166         {
1167                 WaypointList::iterator iter(find_next(location));
1168                 for(;iter!=waypoint_list().end();++iter)
1169                 {
1170                         iter->set_time(iter->get_time()+delta);
1171                 }
1172                 changed();
1173         }
1174         catch(Exception::NotFound) { }
1175 }