1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_timedswap.cpp
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
33 #include "valuenode_timedswap.h"
34 #include "valuenode_const.h"
40 /* === U S I N G =========================================================== */
44 using namespace synfig;
46 /* === M A C R O S ========================================================= */
48 /* === G L O B A L S ======================================================= */
50 /* === P R O C E D U R E S ================================================= */
52 /* === M E T H O D S ======================================================= */
54 ValueNode_TimedSwap::ValueNode_TimedSwap(const ValueBase &value):
55 LinkableValueNode(value.get_type())
59 case ValueBase::TYPE_ANGLE:
60 set_link("before",ValueNode_Const::create(value.get(Angle())));
61 set_link("after",ValueNode_Const::create(value.get(Angle())));
63 case ValueBase::TYPE_COLOR:
64 set_link("before",ValueNode_Const::create(value.get(Color())));
65 set_link("after",ValueNode_Const::create(value.get(Color())));
67 case ValueBase::TYPE_INTEGER:
68 set_link("before",ValueNode_Const::create(value.get(int())));
69 set_link("after",ValueNode_Const::create(value.get(int())));
71 case ValueBase::TYPE_REAL:
72 set_link("before",ValueNode_Const::create(value.get(Real())));
73 set_link("after",ValueNode_Const::create(value.get(Real())));
75 case ValueBase::TYPE_TIME:
76 set_link("before",ValueNode_Const::create(value.get(Time())));
77 set_link("after",ValueNode_Const::create(value.get(Time())));
79 case ValueBase::TYPE_VECTOR:
80 set_link("before",ValueNode_Const::create(value.get(Vector())));
81 set_link("after",ValueNode_Const::create(value.get(Vector())));
84 throw Exception::BadType(ValueBase::type_name(get_type()));
87 set_link("time",ValueNode_Const::create(Time(2)));
88 set_link("length",ValueNode_Const::create(Time(1)));
94 ValueNode_TimedSwap::create(const ValueBase& x)
96 return new ValueNode_TimedSwap(x);
101 ValueNode_TimedSwap::create_new()const
103 return new ValueNode_TimedSwap(get_type());
107 synfig::ValueNode_TimedSwap::~ValueNode_TimedSwap()
115 ValueNode_TimedSwap::set_before(const ValueNode::Handle &x)
117 if(!x || x->get_type()!=get_type()
118 && !PlaceholderValueNode::Handle::cast_dynamic(x))
127 ValueNode_TimedSwap::get_before()const
134 ValueNode_TimedSwap::set_after(const ValueNode::Handle &x)
136 if(!x || x->get_type()!=get_type()
137 && !PlaceholderValueNode::Handle::cast_dynamic(x))
146 ValueNode_TimedSwap::get_after()const
153 ValueNode_TimedSwap::set_swap_time(const ValueNode::Handle &x)
155 if(!x || (!ValueBase(x->get_type()).same_type_as(ValueBase::TYPE_TIME) &&
156 !PlaceholderValueNode::Handle::cast_dynamic(x)))
164 ValueNode_TimedSwap::get_swap_time()const
170 ValueNode_TimedSwap::set_swap_length(const ValueNode::Handle &x)
172 if(!x || (!ValueBase(x->get_type()).same_type_as(ValueBase::TYPE_TIME) &&
173 !PlaceholderValueNode::Handle::cast_dynamic(x)))
181 ValueNode_TimedSwap::get_swap_length()const
189 synfig::ValueNode_TimedSwap::operator()(Time t)const
191 Time swptime=(*swap_time)(t).get(Time());
192 Time swplength=(*swap_length)(t).get(Time());
197 if(t<=swptime && t>swptime-swplength)
199 Real amount=(swptime-t)/swplength;
200 // if amount==0.0, then we are after
201 // if amount==1.0, then we are before
205 case ValueBase::TYPE_ANGLE:
207 Angle a=(*after)(t).get(Angle());
208 Angle b=(*before)(t).get(Angle());
209 return (b-a)*amount+a;
211 case ValueBase::TYPE_COLOR:
213 Color a=(*after)(t).get(Color());
214 Color b=(*before)(t).get(Color());
215 // note: Shouldn't this use a straight blend?
216 return (b-a)*amount+a;
218 case ValueBase::TYPE_INTEGER:
220 float a=(float)(*after)(t).get(int());
221 float b=(float)(*before)(t).get(int());
222 return static_cast<int>((b-a)*amount+a+0.5f);
224 case ValueBase::TYPE_REAL:
226 Real a=(*after)(t).get(Real());
227 Real b=(*before)(t).get(Real());
228 return (b-a)*amount+a;
230 case ValueBase::TYPE_TIME:
232 Time a=(*after)(t).get(Time());
233 Time b=(*before)(t).get(Time());
234 return (b-a)*amount+a;
236 case ValueBase::TYPE_VECTOR:
238 Vector a=(*after)(t).get(Vector());
239 Vector b=(*before)(t).get(Vector());
240 return (b-a)*amount+a;
248 /*! \todo this should interpolate from
249 ** before to after over the period defined
257 ValueNode_TimedSwap::set_link_vfunc(int i,ValueNode::Handle x)
262 case 0: return set_before(x);
263 case 1: return set_after(x);
264 case 2: return set_swap_time(x);
265 case 3: return set_swap_length(x);
270 ValueNode::LooseHandle
271 ValueNode_TimedSwap::get_link_vfunc(int i)const
276 case 0: return get_before();
277 case 1: return get_after();
278 case 2: return get_swap_time();
279 case 3: return get_swap_length();
285 ValueNode_TimedSwap::link_count()const
291 ValueNode_TimedSwap::link_local_name(int i)const
296 case 0: return _("Before");
297 case 1: return _("After");
298 case 2: return _("Swap Time");
299 case 3: return _("Swap Duration");
300 default:return String();
305 ValueNode_TimedSwap::link_name(int i)const
310 case 0: return "before";
311 case 1: return "after";
312 case 2: return "time";
313 case 3: return "length";
314 default:return String();
319 ValueNode_TimedSwap::get_link_index_from_name(const String &name)const
321 if(name=="before") return 0;
322 if(name=="after") return 1;
323 if(name=="time") return 2;
324 if(name=="length") return 3;
326 throw Exception::BadLinkName(name);
330 ValueNode_TimedSwap::get_name()const
336 ValueNode_TimedSwap::get_local_name()const
338 return _("Timed Swap");
342 ValueNode_TimedSwap::check_type(ValueBase::Type type)
345 type==ValueBase::TYPE_ANGLE ||
346 type==ValueBase::TYPE_COLOR ||
347 type==ValueBase::TYPE_INTEGER ||
348 type==ValueBase::TYPE_REAL ||
349 type==ValueBase::TYPE_TIME ||
350 type==ValueBase::TYPE_VECTOR;