1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_animated.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
11 ** This package is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU General Public License as
13 ** published by the Free Software Foundation; either version 2 of
14 ** the License, or (at your option) any later version.
16 ** This package is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** General Public License for more details.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
40 #include <ETL/hermite>
50 #include "valuenode_animated.h"
51 #include "valuenode_const.h"
52 #include "exception.h"
57 /* === U S I N G =========================================================== */
61 using namespace synfig;
63 /* === M A C R O S ========================================================= */
65 // Fast binary search implementation
67 template<typename I, typename T> inline I
68 binary_find(I begin, I end, const T& value)
70 I iter(begin+(end-begin)/2);
72 while(end-begin>1 && !(*iter==value))
74 ((*iter<value)?begin:end) = iter;
76 iter = begin+(end-begin)/2;
83 template<typename T> String tangent_info(T a, T b, T v)
88 String tangent_info(Vector a, Vector b, Vector v)
91 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]);
93 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]);
98 struct subtractor : public std::binary_function<T, T, T>
100 T operator()(const T &a,const T &b)const
107 struct subtractor<Angle> : public std::binary_function<Angle, Angle, Angle>
109 Angle operator()(const Angle &a,const Angle &b)const
116 struct magnitude : public std::unary_function<float, T>
118 float operator()(const T &a)const
125 struct magnitude<Angle> : public std::unary_function<float, Angle>
127 float operator()(const Angle &a)const
129 return abs(Angle::rad(a).get());
134 struct magnitude<Vector> : public std::unary_function<float, Vector>
136 float operator()(const Vector &a)const
143 struct magnitude<Color> : public std::unary_function<float, Color>
145 float operator()(const Color &a)const
147 return abs(a.get_y());
158 bool operator()()const
164 #ifdef ANGLES_USE_LINEAR_INTERPOLATION
166 struct is_angle_type<Angle>
168 bool operator()()const
173 #endif // ANGLES_USE_LINEAR_INTERPOLATION
175 /* === G L O B A L S ======================================================= */
177 /* === C L A S S E S ======================================================= */
180 class _Hermite : public synfig::ValueNode_Animated
183 typedef T value_type;
184 affine_combo<value_type,Time> affine_combo_func;
185 subtractor<value_type> subtract_func;
186 magnitude<value_type> magnitude_func;
187 is_angle_type<value_type> is_angle;
191 is_angle_type<value_type> is_angle;
192 subtractor<value_type> subtract_func;
194 mutable hermite<Time,Time> first;
195 mutable hermite<value_type,Time> second;
196 WaypointList::iterator start;
197 WaypointList::iterator end;
199 value_type resolve(const Time &t)const
201 bool start_static(start->is_static());
202 bool end_static(end->is_static());
204 if(!start_static || !end_static)
207 second.p1()=start->get_value(t).get(value_type());
208 if(start->get_after()==INTERPOLATION_CONSTANT || end->get_before()==INTERPOLATION_CONSTANT)
211 second.p2()=end->get_value(t).get(value_type());
213 // At the moment, the only type of non-constant interpolation
214 // that we support is linear.
216 second.t2()=subtract_func(second.p2(),second.p1());
221 return second(first(t));
229 hermite<value_type,Time>
234 curve_list_type curve_list;
236 // Bounds of this curve
240 ValueNode* clone(const GUID& deriv_guid)const
242 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
243 _Hermite<T>* ret(new _Hermite<T>());
244 ret->set_guid(get_guid()^deriv_guid);
245 for(WaypointList::const_iterator iter=waypoint_list().begin();iter!=waypoint_list().end();++iter)
246 ret->add(iter->clone(deriv_guid));
252 set_type(ValueBase(value_type()).get_type());
255 virtual WaypointList::iterator new_waypoint(Time t, ValueBase value)
257 // Make sure we are getting data of the correct type
258 //if(data.type!=type)
259 // return waypoint_list_type::iterator();
261 try { find(t); throw Exception::BadTime(_("A waypoint already exists at this point in time")); } catch(Exception::NotFound) { };
262 Waypoint waypoint(value,t);
263 waypoint.set_parent_value_node(this);
265 waypoint_list_.push_back(waypoint);
266 WaypointList::iterator ret=waypoint_list_.end();
271 ret->set_before(INTERPOLATION_LINEAR);
272 ret->set_after(INTERPOLATION_LINEAR);
280 virtual WaypointList::iterator new_waypoint(Time t, ValueNode::Handle value_node)
282 // Make sure we are getting data of the correct type
283 //if(data.type!=type)
284 // return waypoint_list_type::iterator();
285 try { find(t); throw Exception::BadTime(_("A waypoint already exists at this point in time")); } catch(Exception::NotFound) { };
287 Waypoint waypoint(value_node,t);
288 waypoint.set_parent_value_node(this);
290 waypoint_list_.push_back(waypoint);
291 WaypointList::iterator ret=waypoint_list_.end();
296 ret->set_before(INTERPOLATION_LINEAR);
297 ret->set_after(INTERPOLATION_LINEAR);
305 virtual void on_changed()
307 ValueNode_Animated::on_changed();
309 if(waypoint_list_.size()<=1)
311 std::sort(waypoint_list_.begin(),waypoint_list_.end());
312 //waypoint_list_.sort();
314 r=waypoint_list_.front().get_time();
315 s=waypoint_list_.back().get_time();
319 WaypointList::iterator prev,iter,next=waypoint_list_.begin();
322 for(iter=next++;iter!=waypoint_list_.end() && next!=waypoint_list_.end();prev=iter,iter=next++,i++)
324 typename curve_list_type::value_type curve;
325 WaypointList::iterator after_next(next);
331 // Set up the positions
332 curve.first.set_rs(iter->get_time(), next->get_time());
333 curve.second.set_rs(iter->get_time(), next->get_time());
335 Waypoint::Interpolation iter_get_after(iter->get_after());
336 Waypoint::Interpolation next_get_after(next->get_after());
337 Waypoint::Interpolation iter_get_before(iter->get_before());
338 Waypoint::Interpolation next_get_before(next->get_before());
342 if(iter_get_after==INTERPOLATION_TCB)
343 iter_get_after=INTERPOLATION_LINEAR;
344 if(next_get_after==INTERPOLATION_TCB)
345 next_get_after=INTERPOLATION_LINEAR;
346 if(iter_get_before==INTERPOLATION_TCB)
347 iter_get_before=INTERPOLATION_LINEAR;
348 if(next_get_before==INTERPOLATION_TCB)
349 next_get_before=INTERPOLATION_LINEAR;
352 if(iter->is_static() && next->is_static())
354 curve.second.p1()=iter->get_value().get(T());
355 curve.second.p2()=next->get_value().get(T());
356 if(iter_get_after==INTERPOLATION_CONSTANT || next_get_before==INTERPOLATION_CONSTANT)
358 // Sections must be constant on both sides.
359 // NOTE: this is commented out because of some
360 // user interface issues. Namely, if a section is
361 // constant and the user turns off the constant on
362 // one waypoint, this will end up turning it back on.
364 //iter->get_after()=next->get_before()=INTERPOLATION_CONSTANT;
366 curve.second.p2()=iter->get_value().get(T());
368 curve.second.t2()=subtract_func(curve.second.p1(),curve.second.p2());
372 if(iter_get_after==INTERPOLATION_TCB && iter!=waypoint_list_.begin() && !is_angle())
374 if(iter->get_before()!=INTERPOLATION_TCB && !curve_list.empty())
376 curve.second.t1()=curve_list.back().second.t2();
381 const Real& t(iter->get_tension()); // Tension
382 const Real& c(iter->get_continuity()); // Continuity
383 const Real& b(iter->get_bias()); // Bias
385 // The following line works where the previous line fails.
386 value_type Pp; Pp=curve_list.back().second.p1(); // P_{i-1}
388 const value_type& Pc(curve.second.p1()); // P_i
389 const value_type& Pn(curve.second.p2()); // P_{i+1}
392 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)));
395 //value_type vect=(value_type)((Pn-Pp)*(1.0-t));
398 //value_type vect=(value_type)(Pn-Pc);
401 //synfig::info("%d:t1: %s",i,tangent_info(Pp,Pn,vect).c_str());
404 //vect=value_type(vect*(curve.second.get_dt()*2.0)/(curve.second.get_dt()+curve_list.back().second.get_dt()));
405 //vect=value_type(vect*(curve.second.get_dt())/(curve_list.back().second.get_dt()));
407 curve.second.t1()=vect;
411 iter_get_after==INTERPOLATION_LINEAR || iter_get_after==INTERPOLATION_HALT ||
412 (iter_get_after==INTERPOLATION_TCB && iter==waypoint_list_.begin()))
414 curve.second.t1()=subtract_func(curve.second.p2(),curve.second.p1());
417 if(iter_get_before==INTERPOLATION_TCB && iter->get_after()!=INTERPOLATION_TCB && !curve_list.empty())
419 curve_list.back().second.t2()=curve.second.t1();
420 curve_list.back().second.sync();
424 if(next_get_before==INTERPOLATION_TCB && after_next!=waypoint_list_.end() && !is_angle())
426 const Real &t(next->get_tension()); // Tension
427 const Real &c(next->get_continuity()); // Continuity
428 const Real &b(next->get_bias()); // Bias
429 const value_type &Pp(curve.second.p1()); // P_{i-1}
430 const value_type &Pc(curve.second.p2()); // P_i
431 value_type Pn; Pn=after_next->get_value().get(T()); // P_{i+1}
434 value_type vect(static_cast<value_type>(subtract_func(Pc,Pp) * (((1.0-t)*(1.0-c)*(1.0+b))/2.0) +
435 (Pn-Pc) * (((1.0-t)*(1.0+c)*(1.0-b))/2.0)));
438 //value_type vect((value_type)((Pn-Pp)*(1.0-t)));
441 //value_type vect=(value_type)(Pc-Pp);
444 //synfig::info("%d:t2: %s",i,tangent_info(Pp,Pn,vect).c_str());
447 //vect=value_type(vect*(curve.second.get_dt()*2.0)/(curve.second.get_dt()+(after_next->get_time()-next->get_time())));
448 //vect=value_type(vect*(curve.second.get_dt()/((after_next->get_time()-next->get_time()))));
450 curve.second.t2()=vect;
453 next_get_before==INTERPOLATION_LINEAR || next_get_before==INTERPOLATION_HALT ||
454 (next_get_before==INTERPOLATION_TCB && after_next==waypoint_list_.end()))
456 curve.second.t2()=subtract_func(curve.second.p2(),curve.second.p1());
460 const float timeadjust(0.5);
462 if(iter_get_after==INTERPOLATION_HALT)
463 curve.second.t1()*=0;
464 // if this isn't the first curve
465 else if(iter_get_after != INTERPOLATION_LINEAR && !curve_list.empty())
466 // adjust it for the curve that came before it
468 // (time span of this curve) * 1.5
469 // -----------------------------------------------------------------
470 // ((time span of this curve) * 0.5) + (time span of previous curve)
471 (curve.second.get_dt()*(timeadjust+1)) /
472 (curve.second.get_dt()*timeadjust + curve_list.back().second.get_dt());
474 if(next_get_before==INTERPOLATION_HALT)
475 curve.second.t2()*=0;
476 // if this isn't the last curve
477 else if(next_get_before != INTERPOLATION_LINEAR && after_next!=waypoint_list_.end())
478 // adjust it for the curve that came after it
480 // (time span of this curve) * 1.5
481 // -------------------------------------------------------------
482 // ((time span of this curve) * 0.5) + (time span of next curve)
483 (curve.second.get_dt()*(timeadjust+1)) /
484 (curve.second.get_dt()*timeadjust+(after_next->get_time()-next->get_time()));
488 // Set up the time to the default stuff
489 curve.first.set_rs(iter->get_time(), next->get_time());
490 curve.first.p1()=iter->get_time();
491 curve.first.p2()=next->get_time();
492 curve.first.t1()=(curve.first.p2()-curve.first.p1())*(1.0f-iter->get_time_tension());
493 curve.first.t2()=(curve.first.p2()-curve.first.p1())*(1.0f-next->get_time_tension());
499 curve_list.push_back(curve);
503 virtual ValueBase operator()(Time t)const
505 if(waypoint_list_.empty())
506 return value_type(); //! \todo Perhaps we should throw something here?
507 if(waypoint_list_.size()==1)
508 return waypoint_list_.front().get_value(t);
510 return waypoint_list_.front().get_value(t);
512 return waypoint_list_.back().get_value(t);
514 typename curve_list_type::const_iterator iter;
516 // This next line will set iter to the
517 // correct iterator for the given time.
518 for(iter=curve_list.begin();iter<curve_list.end() && t>=iter->first.get_s();++iter)
520 if(iter==curve_list.end())
521 return waypoint_list_.back().get_value(t);
522 return iter->resolve(t);
528 class _Constant : public synfig::ValueNode_Animated
531 typedef T value_type;
535 // Bounds of this curve
539 ValueNode* clone(const GUID& deriv_guid)const
541 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
542 _Constant<T>* ret(new _Constant<T>());
543 ret->set_guid(get_guid()^deriv_guid);
544 for(WaypointList::const_iterator iter=waypoint_list().begin();iter!=waypoint_list().end();++iter)
545 ret->add(iter->clone(deriv_guid));
551 set_type(ValueBase(value_type()).get_type());
554 virtual WaypointList::iterator new_waypoint(Time t, ValueBase value)
556 // Make sure we are getting data of the correct type
557 //if(data.type!=type)
558 // return waypoint_list_type::iterator();
559 try { find(t); throw Exception::BadTime(_("A waypoint already exists at this point in time")); } catch(Exception::NotFound) { };
561 Waypoint waypoint(value,t);
562 waypoint.set_parent_value_node(this);
564 waypoint_list_.push_back(waypoint);
565 WaypointList::iterator ret=waypoint_list_.end();
572 virtual WaypointList::iterator new_waypoint(Time t, ValueNode::Handle value_node)
574 // Make sure we are getting data of the correct type
575 //if(data.type!=type)
576 // return waypoint_list_type::iterator();
577 try { find(t); throw Exception::BadTime(_("A waypoint already exists at this point in time")); } catch(Exception::NotFound) { };
579 Waypoint waypoint(value_node,t);
580 waypoint.set_parent_value_node(this);
582 waypoint_list_.push_back(waypoint);
583 WaypointList::iterator ret=waypoint_list_.end();
590 virtual void on_changed()
592 ValueNode_Animated::on_changed();
594 if(waypoint_list_.size()<=1)
596 std::sort(waypoint_list_.begin(),waypoint_list_.end());
597 //waypoint_list_.sort();
598 r=waypoint_list_.front().get_time();
599 s=waypoint_list_.back().get_time();
603 virtual ValueBase operator()(Time t)const
605 if(waypoint_list_.size()==1)
606 return waypoint_list_.front().get_value(t);
607 if(waypoint_list_.empty())
610 return waypoint_list_.front().get_value(t);
612 return waypoint_list_.back().get_value(t);
614 typename WaypointList::const_iterator iter;
615 typename WaypointList::const_iterator next;
617 // This next line will set iter to the
618 // correct iterator for the given time.
619 for(next=waypoint_list_.begin(),iter=next++;next!=waypoint_list_.end() && t>=next->get_time();iter=next++)
622 return iter->get_value(t);
626 class _AnimBool : public synfig::ValueNode_Animated
629 typedef bool value_type;
633 // Bounds of this curve
637 ValueNode* clone(const GUID& deriv_guid)const
639 { ValueNode* x(find_value_node(get_guid()^deriv_guid).get()); if(x)return x; }
640 _AnimBool* ret(new _AnimBool());
641 ret->set_guid(get_guid()^deriv_guid);
642 for(WaypointList::const_iterator iter=waypoint_list().begin();iter!=waypoint_list().end();++iter)
643 ret->add(iter->clone(deriv_guid));
649 set_type(ValueBase(value_type()).get_type());
652 virtual WaypointList::iterator new_waypoint(Time t, ValueBase value)
654 // Make sure we are getting data of the correct type
655 //if(data.type!=type)
656 // return waypoint_list_type::iterator();
657 try { find(t); throw Exception::BadTime(_("A waypoint already exists at this point in time")); } catch(Exception::NotFound) { };
660 Waypoint waypoint(value,t);
661 waypoint.set_parent_value_node(this);
663 waypoint_list_.push_back(waypoint);
664 WaypointList::iterator ret=waypoint_list_.end();
671 virtual WaypointList::iterator new_waypoint(Time t, ValueNode::Handle value_node)
673 // Make sure we are getting data of the correct type
674 //if(data.type!=type)
675 // return waypoint_list_type::iterator();
676 try { find(t); throw Exception::BadTime(_("A waypoint already exists at this point in time")); } catch(Exception::NotFound) { };
678 Waypoint waypoint(value_node,t);
679 waypoint.set_parent_value_node(this);
681 waypoint_list_.push_back(waypoint);
682 WaypointList::iterator ret=waypoint_list_.end();
689 virtual void on_changed()
691 ValueNode_Animated::on_changed();
693 if(waypoint_list_.size()<=1)
695 std::sort(waypoint_list_.begin(),waypoint_list_.end());
696 //waypoint_list_.sort();
697 r=waypoint_list_.front().get_time();
698 s=waypoint_list_.back().get_time();
702 virtual ValueBase operator()(Time t)const
704 if(waypoint_list_.size()==1)
705 return waypoint_list_.front().get_value(t);
706 if(waypoint_list_.empty())
709 return waypoint_list_.front().get_value(t);
711 return waypoint_list_.back().get_value(t);
713 WaypointList::const_iterator iter;
714 WaypointList::const_iterator next;
716 // This next line will set iter to the
717 // correct iterator for the given time.
718 for(next=waypoint_list_.begin(),iter=next++;next!=waypoint_list_.end() && t>=next->get_time();iter=next++)
719 if(iter->get_time()==t)
720 return iter->get_value(t);
722 if(iter->get_time()==t)
723 return iter->get_value(t);
725 if(next!=waypoint_list_.end())
726 return iter->get_value(t).get(bool()) || next->get_value(t).get(bool());
727 return iter->get_value(t);
731 /* === M E T H O D S ======================================================= */
733 ValueNode_Animated::ValueNode_Animated()
739 ValueNode_Animated::find(const Time& begin,const Time& end,std::vector<Waypoint*>& selected)
741 Time curr_time(begin);
744 // try to grab first waypoint
747 WaypointList::iterator iter;
748 iter=find(curr_time);
749 selected.push_back(&*iter);
756 WaypointList::iterator iter;
759 iter=find_next(curr_time);
760 curr_time=iter->get_time();
763 selected.push_back(&*iter);
774 ValueNode_Animated::manipulate_time(const Time& old_begin,const Time& old_end,const Time& new_begin,const Time& new_end)
776 #define old_2_new(x) (((x)-old_begin)/(old_end-old_begin)*(new_end-new_begin)+new_begin)
777 std::vector<Waypoint*> selected;
778 std::vector<Waypoint*>::iterator iter;
780 if(find(old_begin,old_end,selected))
782 // check to make sure this operation is OK
783 for(iter=selected.begin();iter!=selected.end();++iter)
787 Time new_time(old_2_new((*iter)->get_time()));
788 if(new_time>=old_begin && new_time<old_end)
791 // If we found a waypoint already at that time, then
793 throw Exception::BadTime(_("Waypoint Conflict"));
795 catch(Exception::NotFound) { }
797 selected.back()->set_time(old_2_new(selected.back()->get_time()));
802 while(!selected.empty())
804 selected.back()->set_time(old_2_new(selected.back()->get_time()));
815 ValueNode_Animated::new_waypoint_at_time(const Time& time)const
820 // Trivial case, we are sitting on a waypoint
821 waypoint=*find(time);
822 waypoint.make_unique();
826 if(waypoint_list().empty())
828 waypoint.set_value((*this)(time));
832 WaypointList::const_iterator prev;
833 WaypointList::const_iterator next;
835 bool has_prev(false), has_next(false);
837 try { prev=find_prev(time); has_prev=true; } catch(...) { }
838 try { next=find_next(time); has_next=true; } catch(...) { }
841 WaypointList::const_iterator closest;
843 if(has_prev&&!has_next)
845 else if(has_next&&!has_prev)
847 else if(time-prev->get_time()<next->get_time()-time)
852 for(iter=waypoint_list().begin();iter!=waypoint_list().end();++iter)
854 const Real dist(abs(iter->get_time()-time));
855 if(dist<abs(closest->get_time()-time))
860 if(has_prev && !prev->is_static())
861 waypoint.set_value_node(prev->get_value_node());
862 if(has_next && !next->is_static())
863 waypoint.set_value_node(next->get_value_node());
865 waypoint.set_value((*this)(time));
868 waypoint.set_after(prev->get_before());
870 waypoint.set_before(next->get_after());
874 waypoint.set_time(time);
875 waypoint.set_parent_value_node(const_cast<ValueNode_Animated*>(this));
876 // synfig::info("waypoint.get_after()=set to %d",waypoint.get_after());
877 // synfig::info("waypoint.get_before()=set to %d",waypoint.get_before());
882 ValueNode_Animated::WaypointList::iterator
883 ValueNode_Animated::find(const UniqueID &x)
885 ValueNode_Animated::WaypointList::iterator iter;
886 iter=std::find(waypoint_list().begin(),waypoint_list().end(),x);
887 if(iter==waypoint_list().end() || iter->get_uid()!=x.get_uid())
888 throw Exception::NotFound(strprintf("ValueNode_Animated::find(): Can't find UniqueID %d",x.get_uid()));
892 ValueNode_Animated::WaypointList::const_iterator
893 ValueNode_Animated::find(const UniqueID &x)const
895 return const_cast<ValueNode_Animated*>(this)->find(x);
897 ValueNode_Animated::WaypointList::const_iterator iter;
898 iter=std::find(waypoint_list().begin(),waypoint_list().end(),x);
899 if(iter!=waypoint_list().end() && iter->get_uid()!=x.get_uid())
900 throw Exception::NotFound(strprintf("ValueNode_Animated::find()const: Can't find UniqueID %d",x.get_uid()));
905 ValueNode_Animated::WaypointList::iterator
906 ValueNode_Animated::find(const Time &x)
908 WaypointList::iterator iter(binary_find(waypoint_list().begin(),waypoint_list().end(),x));
910 if(iter!=waypoint_list().end() && x.is_equal(iter->get_time()))
913 throw Exception::NotFound(strprintf("ValueNode_Animated::find(): Can't find Waypoint at %s",x.get_string().c_str()));
916 ValueNode_Animated::WaypointList::const_iterator
917 ValueNode_Animated::find(const Time &x)const
919 return const_cast<ValueNode_Animated*>(this)->find(x);
921 WaypointList::const_iterator iter(binary_find(waypoint_list().begin(),waypoint_list().end(),x));
923 if(iter!=waypoint_list().end() && x.is_equal(iter->get_time()))
926 throw Exception::NotFound(strprintf("ValueNode_Animated::find(): Can't find Waypoint at %s",x.get_string().c_str()));
930 ValueNode_Animated::WaypointList::iterator
931 ValueNode_Animated::find_next(const Time &x)
933 WaypointList::iterator iter(binary_find(waypoint_list().begin(),waypoint_list().end(),x));
935 if(iter!=waypoint_list().end())
937 if(iter->get_time().is_more_than(x))
940 if(iter!=waypoint_list().end() && iter->get_time().is_more_than(x))
944 throw Exception::NotFound(strprintf("ValueNode_Animated::find_next(): Can't find Waypoint after %s",x.get_string().c_str()));
947 ValueNode_Animated::WaypointList::const_iterator
948 ValueNode_Animated::find_next(const Time &x)const
950 return const_cast<ValueNode_Animated*>(this)->find_next(x);
952 WaypointList::const_iterator iter(binary_find(waypoint_list().begin(),waypoint_list().end(),x));
954 if(iter!=waypoint_list().end())
956 if(iter->get_time()-Time::epsilon()>x)
959 if(iter!=waypoint_list().end() && iter->get_time()-Time::epsilon()>x)
963 throw Exception::NotFound(strprintf("ValueNode_Animated::find_next(): Can't find Waypoint after %s",x.get_string().c_str()));
967 ValueNode_Animated::WaypointList::iterator
968 ValueNode_Animated::find_prev(const Time &x)
970 WaypointList::iterator iter(binary_find(waypoint_list().begin(),waypoint_list().end(),x));
972 if(iter!=waypoint_list().end())
974 if(iter->get_time().is_less_than(x))
976 if(iter!=waypoint_list().begin() && (--iter)->get_time().is_less_than(x))
980 throw Exception::NotFound(strprintf("ValueNode_Animated::find_prev(): Can't find Waypoint after %s",x.get_string().c_str()));
983 ValueNode_Animated::WaypointList::const_iterator
984 ValueNode_Animated::find_prev(const Time &x)const
986 return const_cast<ValueNode_Animated*>(this)->find_prev(x);
988 WaypointList::const_iterator iter(binary_find(waypoint_list().begin(),waypoint_list().end(),x));
990 if(iter!=waypoint_list().end())
992 if(iter->get_time()+Time::epsilon()<x)
994 if(iter!=waypoint_list().begin() && (--iter)->get_time()+Time::epsilon()<x)
997 throw Exception::NotFound(strprintf("ValueNode_Animated::find_prev(): Can't find Waypoint after %s",x.get_string().c_str()));
1002 ValueNode_Animated::erase(const UniqueID &x)
1004 waypoint_list().erase(find(x));
1007 ValueNode_Animated::WaypointList::iterator
1008 ValueNode_Animated::add(const Waypoint &x)
1010 Waypoint waypoint(x);
1011 waypoint.set_parent_value_node(this);
1012 waypoint_list_.push_back(waypoint);
1013 //assert(waypoint_list_.back().get_parent_value_node()==this);
1014 WaypointList::iterator ret=waypoint_list_.end();
1021 ValueNode_Animated::set_type(ValueBase::Type t)
1023 ValueNode::set_type(t);
1026 ValueNode_Animated::Handle
1027 synfig::ValueNode_Animated::create(ValueBase::Type type)
1031 case ValueBase::TYPE_TIME:
1032 return ValueNode_Animated::Handle(new _Hermite<Time>);
1033 case ValueBase::TYPE_REAL:
1034 return ValueNode_Animated::Handle(new _Hermite<Vector::value_type>);
1035 case ValueBase::TYPE_INTEGER:
1036 return ValueNode_Animated::Handle(new _Hermite<int>);
1037 case ValueBase::TYPE_ANGLE:
1038 return ValueNode_Animated::Handle(new _Hermite<Angle>);
1039 case ValueBase::TYPE_VECTOR:
1040 return ValueNode_Animated::Handle(new _Hermite<Vector>);
1041 case ValueBase::TYPE_COLOR:
1042 return ValueNode_Animated::Handle(new _Hermite<Color>);
1044 case ValueBase::TYPE_STRING:
1045 return ValueNode_Animated::Handle(new _Constant<String>);
1046 case ValueBase::TYPE_GRADIENT:
1047 return ValueNode_Animated::Handle(new _Hermite<Gradient>);
1048 case ValueBase::TYPE_BOOL:
1049 return ValueNode_Animated::Handle(new _AnimBool);
1050 case ValueBase::TYPE_CANVAS:
1051 return ValueNode_Animated::Handle(new _Constant<Canvas::LooseHandle>);
1054 Exception::BadType(strprintf(_("%s: You cannot use a %s in an animated ValueNode"),"synfig::ValueNode_Animated::create()",
1055 ValueBase::type_name(type).c_str())
1059 return ValueNode_Animated::Handle();
1062 ValueNode_Animated::Handle
1063 ValueNode_Animated::create(const ValueBase& value, const Time& time)
1065 return create(ValueNode::Handle(ValueNode_Const::create(value)),time);
1068 ValueNode_Animated::Handle
1069 ValueNode_Animated::create(ValueNode::Handle value_node, const Time& time)
1071 ValueNode_Animated::Handle ret(create(value_node->get_type()));
1072 ret->new_waypoint(time,value_node);
1076 ValueNode_Animated::~ValueNode_Animated()
1081 ValueNode_Animated::get_name()const
1087 ValueNode_Animated::get_local_name()const
1089 return _("Animated");
1092 void ValueNode_Animated::get_times_vfunc(Node::time_set &set) const
1094 //add all the way point times to the value node...
1096 WaypointList::const_iterator i = waypoint_list().begin(),
1097 end = waypoint_list().end();
1099 for(; i != end; ++i)
1102 t.set_time(i->get_time());
1103 t.set_before(i->get_before());
1104 t.set_after(i->get_after());
1105 t.set_guid(i->get_guid());
1113 timecmp(const Time &c) :t(c) {}
1115 bool operator()(const Waypoint &rhs) const
1117 return t.is_equal(rhs.get_time());
1121 ValueNode_Animated::findresult
1122 ValueNode_Animated::find_uid(const UniqueID &x)
1127 //search for it... and set the bool part of the return value to true if we found it!
1128 f.first = std::find(waypoint_list_.begin(), waypoint_list_.end(), x);
1129 if(f.first != waypoint_list_.end())
1135 ValueNode_Animated::const_findresult
1136 ValueNode_Animated::find_uid(const UniqueID &x)const
1141 //search for it... and set the bool part of the return value to true if we found it!
1142 f.first = std::find(waypoint_list_.begin(), waypoint_list_.end(), x);
1143 if(f.first != waypoint_list_.end())
1149 ValueNode_Animated::findresult
1150 ValueNode_Animated::find_time(const Time &x)
1155 //search for it... and set the bool part of the return value to true if we found it!
1156 f.first = std::find_if(waypoint_list_.begin(), waypoint_list_.end(), timecmp(x));
1157 if(f.first != waypoint_list_.end())
1163 ValueNode_Animated::const_findresult
1164 ValueNode_Animated::find_time(const Time &x)const
1169 //search for it... and set the bool part of the return value to true if we found it!
1170 f.first = std::find_if(waypoint_list_.begin(), waypoint_list_.end(), timecmp(x));
1171 if(f.first != waypoint_list_.end())
1178 ValueNode_Animated::insert_time(const Time& location, const Time& delta)
1184 WaypointList::iterator iter(find_next(location));
1185 for(;iter!=waypoint_list().end();++iter)
1187 iter->set_time(iter->get_time()+delta);
1191 catch(Exception::NotFound) { }