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
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 ======================================================= */
34 #include "valuenode_timedswap.h"
35 #include "valuenode_const.h"
41 /* === U S I N G =========================================================== */
45 using namespace synfig;
47 /* === M A C R O S ========================================================= */
49 /* === G L O B A L S ======================================================= */
51 /* === P R O C E D U R E S ================================================= */
53 /* === M E T H O D S ======================================================= */
55 ValueNode_TimedSwap::ValueNode_TimedSwap(const ValueBase &value):
56 LinkableValueNode(value.get_type())
60 case ValueBase::TYPE_ANGLE:
61 set_link("before",ValueNode_Const::create(value.get(Angle())));
62 set_link("after",ValueNode_Const::create(value.get(Angle())));
64 case ValueBase::TYPE_COLOR:
65 set_link("before",ValueNode_Const::create(value.get(Color())));
66 set_link("after",ValueNode_Const::create(value.get(Color())));
68 case ValueBase::TYPE_INTEGER:
69 set_link("before",ValueNode_Const::create(value.get(int())));
70 set_link("after",ValueNode_Const::create(value.get(int())));
72 case ValueBase::TYPE_REAL:
73 set_link("before",ValueNode_Const::create(value.get(Real())));
74 set_link("after",ValueNode_Const::create(value.get(Real())));
76 case ValueBase::TYPE_TIME:
77 set_link("before",ValueNode_Const::create(value.get(Time())));
78 set_link("after",ValueNode_Const::create(value.get(Time())));
80 case ValueBase::TYPE_VECTOR:
81 set_link("before",ValueNode_Const::create(value.get(Vector())));
82 set_link("after",ValueNode_Const::create(value.get(Vector())));
85 throw Exception::BadType(ValueBase::type_name(get_type()));
88 set_link("time",ValueNode_Const::create(Time(2)));
89 set_link("length",ValueNode_Const::create(Time(1)));
95 ValueNode_TimedSwap::create(const ValueBase& x)
97 return new ValueNode_TimedSwap(x);
102 ValueNode_TimedSwap::create_new()const
104 return new ValueNode_TimedSwap(get_type());
108 synfig::ValueNode_TimedSwap::~ValueNode_TimedSwap()
116 ValueNode_TimedSwap::set_before(const ValueNode::Handle &x)
118 if(!x || x->get_type()!=get_type()
119 && !PlaceholderValueNode::Handle::cast_dynamic(x))
128 ValueNode_TimedSwap::get_before()const
135 ValueNode_TimedSwap::set_after(const ValueNode::Handle &x)
137 if(!x || x->get_type()!=get_type()
138 && !PlaceholderValueNode::Handle::cast_dynamic(x))
147 ValueNode_TimedSwap::get_after()const
154 ValueNode_TimedSwap::set_swap_time(const ValueNode::Handle &x)
156 if(!x || (!ValueBase(x->get_type()).same_type_as(ValueBase::TYPE_TIME) &&
157 !PlaceholderValueNode::Handle::cast_dynamic(x)))
165 ValueNode_TimedSwap::get_swap_time()const
171 ValueNode_TimedSwap::set_swap_length(const ValueNode::Handle &x)
173 if(!x || (!ValueBase(x->get_type()).same_type_as(ValueBase::TYPE_TIME) &&
174 !PlaceholderValueNode::Handle::cast_dynamic(x)))
182 ValueNode_TimedSwap::get_swap_length()const
190 synfig::ValueNode_TimedSwap::operator()(Time t)const
192 Time swptime=(*swap_time)(t).get(Time());
193 Time swplength=(*swap_length)(t).get(Time());
198 if(t<=swptime && t>swptime-swplength)
200 Real amount=(swptime-t)/swplength;
201 // if amount==0.0, then we are after
202 // if amount==1.0, then we are before
206 case ValueBase::TYPE_ANGLE:
208 Angle a=(*after)(t).get(Angle());
209 Angle b=(*before)(t).get(Angle());
210 return (b-a)*amount+a;
212 case ValueBase::TYPE_COLOR:
214 Color a=(*after)(t).get(Color());
215 Color b=(*before)(t).get(Color());
216 // note: Shouldn't this use a straight blend?
217 return (b-a)*amount+a;
219 case ValueBase::TYPE_INTEGER:
221 float a=(float)(*after)(t).get(int());
222 float b=(float)(*before)(t).get(int());
223 return static_cast<int>((b-a)*amount+a+0.5f);
225 case ValueBase::TYPE_REAL:
227 Real a=(*after)(t).get(Real());
228 Real b=(*before)(t).get(Real());
229 return (b-a)*amount+a;
231 case ValueBase::TYPE_TIME:
233 Time a=(*after)(t).get(Time());
234 Time b=(*before)(t).get(Time());
235 return (b-a)*amount+a;
237 case ValueBase::TYPE_VECTOR:
239 Vector a=(*after)(t).get(Vector());
240 Vector b=(*before)(t).get(Vector());
241 return (b-a)*amount+a;
249 /*! \todo this should interpolate from
250 ** before to after over the period defined
258 ValueNode_TimedSwap::set_link_vfunc(int i,ValueNode::Handle x)
263 case 0: return set_before(x);
264 case 1: return set_after(x);
265 case 2: return set_swap_time(x);
266 case 3: return set_swap_length(x);
271 ValueNode::LooseHandle
272 ValueNode_TimedSwap::get_link_vfunc(int i)const
277 case 0: return get_before();
278 case 1: return get_after();
279 case 2: return get_swap_time();
280 case 3: return get_swap_length();
286 ValueNode_TimedSwap::link_count()const
292 ValueNode_TimedSwap::link_local_name(int i)const
297 case 0: return _("Before");
298 case 1: return _("After");
299 case 2: return _("Swap Time");
300 case 3: return _("Swap Duration");
301 default:return String();
306 ValueNode_TimedSwap::link_name(int i)const
311 case 0: return "before";
312 case 1: return "after";
313 case 2: return "time";
314 case 3: return "length";
315 default:return String();
320 ValueNode_TimedSwap::get_link_index_from_name(const String &name)const
322 if(name=="before") return 0;
323 if(name=="after") return 1;
324 if(name=="time") return 2;
325 if(name=="length") return 3;
327 throw Exception::BadLinkName(name);
331 ValueNode_TimedSwap::get_name()const
337 ValueNode_TimedSwap::get_local_name()const
339 return _("Timed Swap");
343 ValueNode_TimedSwap::check_type(ValueBase::Type type)
346 type==ValueBase::TYPE_ANGLE ||
347 type==ValueBase::TYPE_COLOR ||
348 type==ValueBase::TYPE_INTEGER ||
349 type==ValueBase::TYPE_REAL ||
350 type==ValueBase::TYPE_TIME ||
351 type==ValueBase::TYPE_VECTOR;