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