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(ValueBase::Type type):
55 LinkableValueNode(type)
57 set_before(ValueNode_Const::create(type));
58 set_after(ValueNode_Const::create(type));
59 set_swap_time_real(1.0);
60 set_swap_length_real(1.0);
66 ValueNode_TimedSwap::create(const ValueBase& x)
68 return new ValueNode_TimedSwap(x.get_type());
73 ValueNode_TimedSwap::create_new()const
75 return new ValueNode_TimedSwap(get_type());
79 synfig::ValueNode_TimedSwap::~ValueNode_TimedSwap()
87 ValueNode_TimedSwap::set_before(const ValueNode::Handle &x)
89 if(!x || x->get_type()!=get_type()
90 && !PlaceholderValueNode::Handle::cast_dynamic(x))
99 ValueNode_TimedSwap::get_before()const
106 ValueNode_TimedSwap::set_after(const ValueNode::Handle &x)
108 if(!x || x->get_type()!=get_type()
109 && !PlaceholderValueNode::Handle::cast_dynamic(x))
118 ValueNode_TimedSwap::get_after()const
125 ValueNode_TimedSwap::set_swap_time_real(Time x)
127 set_swap_time(ValueNode_Const::create(x));
131 ValueNode_TimedSwap::set_swap_time(const ValueNode::Handle &x)
134 || !ValueBase(ValueBase::TYPE_TIME).same_type_as(x->get_type())
135 && !PlaceholderValueNode::Handle::cast_dynamic(x)
143 ValueNode_TimedSwap::get_swap_time()const
149 ValueNode_TimedSwap::set_swap_length_real(Time x)
151 set_swap_length(ValueNode_Const::create(x));
155 ValueNode_TimedSwap::set_swap_length(const ValueNode::Handle &x)
158 !ValueBase(ValueBase::TYPE_TIME).same_type_as(x->get_type())
159 && !PlaceholderValueNode::Handle::cast_dynamic(x)
168 ValueNode_TimedSwap::get_swap_length()const
176 synfig::ValueNode_TimedSwap::operator()(Time t)const
178 Time swptime=(*swap_time)(t).get(Time());
179 Time swplength=(*swap_length)(t).get(Time());
184 if(t<=swptime && t>swptime-swplength)
186 Real amount=(swptime-t)/swplength;
187 // if amount==0.0, then we are after
188 // if amount==1.0, then we are before
192 case ValueBase::TYPE_REAL:
194 Real a=(*after)(t).get(Real());
195 Real b=(*before)(t).get(Real());
196 return (b-a)*amount+a;
198 case ValueBase::TYPE_VECTOR:
200 Vector a=(*after)(t).get(Vector());
201 Vector b=(*before)(t).get(Vector());
202 return (b-a)*amount+a;
204 case ValueBase::TYPE_ANGLE:
206 Angle a=(*after)(t).get(Angle());
207 Angle b=(*before)(t).get(Angle());
208 return (b-a)*amount+a;
210 case ValueBase::TYPE_COLOR:
212 Color a=(*after)(t).get(Color());
213 Color b=(*before)(t).get(Color());
214 // note: Shouldn't this use a straight blend?
215 return (b-a)*amount+a;
217 case ValueBase::TYPE_INTEGER:
219 float a=(float)(*after)(t).get(int());
220 float b=(float)(*before)(t).get(int());
221 return static_cast<int>((b-a)*amount+a+0.5f);
229 /*! \todo this should interpolate from
230 ** before to after over the period defined
238 ValueNode_TimedSwap::set_link_vfunc(int i,ValueNode::Handle x)
244 return set_before(x);
248 return set_swap_time(x);
250 return set_swap_length(x);
255 ValueNode::LooseHandle
256 ValueNode_TimedSwap::get_link_vfunc(int i)const
266 return get_swap_time();
268 return get_swap_length();
274 ValueNode_TimedSwap::link_count()const
280 ValueNode_TimedSwap::link_local_name(int i)const
290 return _("Swap Time");
292 return _("Swap Duration");
299 ValueNode_TimedSwap::link_name(int i)const
318 ValueNode_TimedSwap::get_link_index_from_name(const String &name)const
329 throw Exception::BadLinkName(name);
333 ValueNode_TimedSwap::get_name()const
339 ValueNode_TimedSwap::get_local_name()const
341 return _("Timed Swap");
345 ValueNode_TimedSwap::check_type(ValueBase::Type type)