1 /* === S Y N F I G ========================================================= */
2 /*! \file valuenode_timedswap.cpp
3 ** \brief Implementation of the "Timed Swap" valuenode conversion.
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007, 2008 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"
42 /* === U S I N G =========================================================== */
46 using namespace synfig;
48 /* === M A C R O S ========================================================= */
50 /* === G L O B A L S ======================================================= */
52 /* === P R O C E D U R E S ================================================= */
54 /* === M E T H O D S ======================================================= */
56 ValueNode_TimedSwap::ValueNode_TimedSwap(const ValueBase &value):
57 LinkableValueNode(value.get_type())
61 case ValueBase::TYPE_ANGLE:
62 set_link("before",ValueNode_Const::create(value.get(Angle())));
63 set_link("after",ValueNode_Const::create(value.get(Angle())));
65 case ValueBase::TYPE_COLOR:
66 set_link("before",ValueNode_Const::create(value.get(Color())));
67 set_link("after",ValueNode_Const::create(value.get(Color())));
69 case ValueBase::TYPE_INTEGER:
70 set_link("before",ValueNode_Const::create(value.get(int())));
71 set_link("after",ValueNode_Const::create(value.get(int())));
73 case ValueBase::TYPE_REAL:
74 set_link("before",ValueNode_Const::create(value.get(Real())));
75 set_link("after",ValueNode_Const::create(value.get(Real())));
77 case ValueBase::TYPE_TIME:
78 set_link("before",ValueNode_Const::create(value.get(Time())));
79 set_link("after",ValueNode_Const::create(value.get(Time())));
81 case ValueBase::TYPE_VECTOR:
82 set_link("before",ValueNode_Const::create(value.get(Vector())));
83 set_link("after",ValueNode_Const::create(value.get(Vector())));
86 throw Exception::BadType(ValueBase::type_local_name(get_type()));
89 set_link("time",ValueNode_Const::create(Time(2)));
90 set_link("length",ValueNode_Const::create(Time(1)));
96 ValueNode_TimedSwap::create(const ValueBase& x)
98 return new ValueNode_TimedSwap(x);
102 ValueNode_TimedSwap::create_new()const
104 return new ValueNode_TimedSwap(get_type());
107 synfig::ValueNode_TimedSwap::~ValueNode_TimedSwap()
113 synfig::ValueNode_TimedSwap::operator()(Time t)const
115 Time swptime=(*swap_time)(t).get(Time());
116 Time swplength=(*swap_length)(t).get(Time());
121 if(t<=swptime && t>swptime-swplength)
123 Real amount=(swptime-t)/swplength;
124 // if amount==0.0, then we are after
125 // if amount==1.0, then we are before
129 case ValueBase::TYPE_ANGLE:
131 Angle a=(*after)(t).get(Angle());
132 Angle b=(*before)(t).get(Angle());
133 return (b-a)*amount+a;
135 case ValueBase::TYPE_COLOR:
137 Color a=(*after)(t).get(Color());
138 Color b=(*before)(t).get(Color());
139 // note: Shouldn't this use a straight blend?
140 return (b-a)*amount+a;
142 case ValueBase::TYPE_INTEGER:
144 float a=(float)(*after)(t).get(int());
145 float b=(float)(*before)(t).get(int());
146 return round_to_int((b-a)*amount+a);
148 case ValueBase::TYPE_REAL:
150 Real a=(*after)(t).get(Real());
151 Real b=(*before)(t).get(Real());
152 return (b-a)*amount+a;
154 case ValueBase::TYPE_TIME:
156 Time a=(*after)(t).get(Time());
157 Time b=(*before)(t).get(Time());
158 return (b-a)*amount+a;
160 case ValueBase::TYPE_VECTOR:
162 Vector a=(*after)(t).get(Vector());
163 Vector b=(*before)(t).get(Vector());
164 return (b-a)*amount+a;
171 /*! \todo this should interpolate from
172 ** before to after over the period defined
179 ValueNode_TimedSwap::set_link_vfunc(int i,ValueNode::Handle value)
181 assert(i>=0 && i<link_count());
185 case 0: CHECK_TYPE_AND_SET_VALUE(before, get_type());
186 case 1: CHECK_TYPE_AND_SET_VALUE(after, get_type());
187 case 2: CHECK_TYPE_AND_SET_VALUE(swap_time, ValueBase::TYPE_TIME);
188 case 3: CHECK_TYPE_AND_SET_VALUE(swap_length, ValueBase::TYPE_TIME);
193 ValueNode::LooseHandle
194 ValueNode_TimedSwap::get_link_vfunc(int i)const
196 assert(i>=0 && i<link_count());
200 case 0: return before;
201 case 1: return after;
202 case 2: return swap_time;
203 case 3: return swap_length;
209 ValueNode_TimedSwap::link_count()const
215 ValueNode_TimedSwap::link_local_name(int i)const
217 assert(i>=0 && i<link_count());
221 case 0: return _("Before");
222 case 1: return _("After");
223 case 2: return _("Swap Time");
224 case 3: return _("Swap Duration");
225 default:return String();
230 ValueNode_TimedSwap::link_name(int i)const
232 assert(i>=0 && i<link_count());
236 case 0: return "before";
237 case 1: return "after";
238 case 2: return "time";
239 case 3: return "length";
240 default:return String();
245 ValueNode_TimedSwap::get_link_index_from_name(const String &name)const
247 if(name=="before") return 0;
248 if(name=="after") return 1;
249 if(name=="time") return 2;
250 if(name=="length") return 3;
252 throw Exception::BadLinkName(name);
256 ValueNode_TimedSwap::get_name()const
262 ValueNode_TimedSwap::get_local_name()const
264 return _("Timed Swap");
268 ValueNode_TimedSwap::check_type(ValueBase::Type type)
271 type==ValueBase::TYPE_ANGLE ||
272 type==ValueBase::TYPE_COLOR ||
273 type==ValueBase::TYPE_INTEGER ||
274 type==ValueBase::TYPE_REAL ||
275 type==ValueBase::TYPE_TIME ||
276 type==ValueBase::TYPE_VECTOR;