Fix and re-enable the "Timed Swap" convert. Prevent loadcanvas from converting ...
[synfig.git] / synfig-core / trunk / src / synfig / valuenode_timedswap.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_timedswap.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
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.
14 **
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.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "general.h"
33 #include "valuenode_timedswap.h"
34 #include "valuenode_const.h"
35 #include <stdexcept>
36 #include "color.h"
37
38 #endif
39
40 /* === U S I N G =========================================================== */
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 ValueNode_TimedSwap::ValueNode_TimedSwap(const ValueBase &value):
55         LinkableValueNode(value.get_type())
56 {
57         switch(get_type())
58         {
59         case ValueBase::TYPE_REAL:
60                 set_link("before",ValueNode_Const::create(value.get(Real())));
61                 set_link("after",ValueNode_Const::create(value.get(Real())));
62                 break;
63         case ValueBase::TYPE_VECTOR:
64                 set_link("before",ValueNode_Const::create(value.get(Vector())));
65                 set_link("after",ValueNode_Const::create(value.get(Vector())));
66                 break;
67         case ValueBase::TYPE_ANGLE:
68                 set_link("before",ValueNode_Const::create(value.get(Angle())));
69                 set_link("after",ValueNode_Const::create(value.get(Angle())));
70                 break;
71         case ValueBase::TYPE_COLOR:
72                 set_link("before",ValueNode_Const::create(value.get(Color())));
73                 set_link("after",ValueNode_Const::create(value.get(Color())));
74                 break;
75         case ValueBase::TYPE_INTEGER:
76                 set_link("before",ValueNode_Const::create(value.get(int())));
77                 set_link("after",ValueNode_Const::create(value.get(int())));
78                 break;
79         case ValueBase::TYPE_TIME:
80                 set_link("before",ValueNode_Const::create(value.get(Time())));
81                 set_link("after",ValueNode_Const::create(value.get(Time())));
82                 break;
83         default:
84                 throw Exception::BadType(ValueBase::type_name(get_type()));
85         }
86
87         set_link("time",ValueNode_Const::create(Time(2)));
88         set_link("length",ValueNode_Const::create(Time(1)));
89
90         DCAST_HACK_ENABLE();
91 }
92
93 ValueNode_TimedSwap*
94 ValueNode_TimedSwap::create(const ValueBase& x)
95 {
96         return new ValueNode_TimedSwap(x);
97 }
98
99
100 LinkableValueNode*
101 ValueNode_TimedSwap::create_new()const
102 {
103         return new ValueNode_TimedSwap(get_type());
104 }
105
106
107 synfig::ValueNode_TimedSwap::~ValueNode_TimedSwap()
108 {
109         unlink_all();
110 }
111
112
113
114 bool
115 ValueNode_TimedSwap::set_before(const ValueNode::Handle &x)
116 {
117         if(!x || x->get_type()!=get_type()
118                 && !PlaceholderValueNode::Handle::cast_dynamic(x))
119                 return false;
120
121         before=x;
122
123         return true;
124 }
125
126 ValueNode::Handle
127 ValueNode_TimedSwap::get_before()const
128 {
129         return before;
130 }
131
132
133 bool
134 ValueNode_TimedSwap::set_after(const ValueNode::Handle &x)
135 {
136         if(!x || x->get_type()!=get_type()
137                 && !PlaceholderValueNode::Handle::cast_dynamic(x))
138                 return false;
139
140         after=x;
141
142         return true;
143 }
144
145 ValueNode::Handle
146 ValueNode_TimedSwap::get_after()const
147 {
148         return after;
149 }
150
151
152 bool
153 ValueNode_TimedSwap::set_swap_time(const ValueNode::Handle &x)
154 {
155         if(!x || (!ValueBase(x->get_type()).same_type_as(ValueBase::TYPE_TIME) &&
156                           !PlaceholderValueNode::Handle::cast_dynamic(x)))
157                 return false;
158
159         swap_time=x;
160         return true;
161 }
162
163 ValueNode::Handle
164 ValueNode_TimedSwap::get_swap_time()const
165 {
166         return swap_time;
167 }
168
169 bool
170 ValueNode_TimedSwap::set_swap_length(const ValueNode::Handle &x)
171 {
172         if(!x || (!ValueBase(x->get_type()).same_type_as(ValueBase::TYPE_TIME) &&
173                           !PlaceholderValueNode::Handle::cast_dynamic(x)))
174                 return false;
175
176         swap_length=x;
177         return true;
178 }
179
180 ValueNode::Handle
181 ValueNode_TimedSwap::get_swap_length()const
182 {
183         return swap_length;
184 }
185
186
187
188 synfig::ValueBase
189 synfig::ValueNode_TimedSwap::operator()(Time t)const
190 {
191         Time swptime=(*swap_time)(t).get(Time());
192         Time swplength=(*swap_length)(t).get(Time());
193
194         if(t>swptime)
195                 return (*after)(t);
196
197         if(t<=swptime && t>swptime-swplength)
198         {
199                 Real amount=(swptime-t)/swplength;
200                 // if amount==0.0, then we are after
201                 // if amount==1.0, then we are before
202
203                 switch(get_type())
204                 {
205                 case ValueBase::TYPE_REAL:
206                         {
207                                 Real a=(*after)(t).get(Real());
208                                 Real b=(*before)(t).get(Real());
209                                 return (b-a)*amount+a;
210                         }
211                 case ValueBase::TYPE_VECTOR:
212                         {
213                                 Vector a=(*after)(t).get(Vector());
214                                 Vector b=(*before)(t).get(Vector());
215                                 return (b-a)*amount+a;
216                         }
217                 case ValueBase::TYPE_ANGLE:
218                         {
219                                 Angle a=(*after)(t).get(Angle());
220                                 Angle b=(*before)(t).get(Angle());
221                                 return (b-a)*amount+a;
222                         }
223                 case ValueBase::TYPE_COLOR:
224                         {
225                                 Color a=(*after)(t).get(Color());
226                                 Color b=(*before)(t).get(Color());
227                                 // note: Shouldn't this use a straight blend?
228                                 return (b-a)*amount+a;
229                         }
230                 case ValueBase::TYPE_INTEGER:
231                         {
232                                 float a=(float)(*after)(t).get(int());
233                                 float b=(float)(*before)(t).get(int());
234                                 return static_cast<int>((b-a)*amount+a+0.5f);
235                         }
236                 case ValueBase::TYPE_TIME:
237                         {
238                                 Time a=(*after)(t).get(Time());
239                                 Time b=(*before)(t).get(Time());
240                                 return (b-a)*amount+a;
241                         }
242                 default:
243                         break;
244                 }
245         }
246
247
248         /*! \todo this should interpolate from
249         **      before to after over the period defined
250         **      by swap_length */
251
252         return (*before)(t);
253 }
254
255
256 bool
257 ValueNode_TimedSwap::set_link_vfunc(int i,ValueNode::Handle x)
258 {
259         assert(i>=0 && i<4);
260         switch(i)
261         {
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);
266         }
267         return false;
268 }
269
270 ValueNode::LooseHandle
271 ValueNode_TimedSwap::get_link_vfunc(int i)const
272 {
273         assert(i>=0 && i<4);
274         switch(i)
275         {
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();
280         }
281         return 0;
282 }
283
284 int
285 ValueNode_TimedSwap::link_count()const
286 {
287         return 4;
288 }
289
290 String
291 ValueNode_TimedSwap::link_local_name(int i)const
292 {
293         assert(i>=0 && i<4);
294         switch(i)
295         {
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();
301         }
302 }
303
304 String
305 ValueNode_TimedSwap::link_name(int i)const
306 {
307         assert(i>=0 && i<4);
308         switch(i)
309         {
310         case 0: return "before";
311         case 1: return "after";
312         case 2: return "time";
313         case 3: return "length";
314         default:return String();
315         }
316 }
317
318 int
319 ValueNode_TimedSwap::get_link_index_from_name(const String &name)const
320 {
321         if(name=="before")      return 0;
322         if(name=="after")       return 1;
323         if(name=="time")        return 2;
324         if(name=="length")      return 3;
325
326         throw Exception::BadLinkName(name);
327 }
328
329 String
330 ValueNode_TimedSwap::get_name()const
331 {
332         return "timed_swap";
333 }
334
335 String
336 ValueNode_TimedSwap::get_local_name()const
337 {
338         return _("Timed Swap");
339 }
340
341 bool
342 ValueNode_TimedSwap::check_type(ValueBase::Type type)
343 {
344         return
345                 type==ValueBase::TYPE_REAL ||
346                 type==ValueBase::TYPE_VECTOR ||
347                 type==ValueBase::TYPE_ANGLE ||
348                 type==ValueBase::TYPE_COLOR ||
349                 type==ValueBase::TYPE_INTEGER ||
350                 type==ValueBase::TYPE_TIME;
351 }