Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_03 / synfig-core / src / synfig / valuenode_timedswap.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_timedswap.cpp
3 **      \brief Template File
4 **
5 **      $Id: valuenode_timedswap.cpp,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
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(ValueBase::Type type):
55         LinkableValueNode(type)
56 {
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);
61
62         DCAST_HACK_ENABLE();
63 }
64
65 ValueNode_TimedSwap*
66 ValueNode_TimedSwap::create(const ValueBase& x)
67 {
68         return new ValueNode_TimedSwap(x.get_type());
69 }
70
71
72 LinkableValueNode*
73 ValueNode_TimedSwap::create_new()const
74 {
75         return new ValueNode_TimedSwap(get_type());
76 }
77
78
79 synfig::ValueNode_TimedSwap::~ValueNode_TimedSwap()
80 {
81         unlink_all();
82 }
83
84
85
86 bool
87 ValueNode_TimedSwap::set_before(const ValueNode::Handle &x)
88 {
89         if(!x || x->get_type()!=get_type()
90                 && !PlaceholderValueNode::Handle::cast_dynamic(x))
91                 return false;
92
93         before=x;
94
95         return true;
96 }
97
98 ValueNode::Handle
99 ValueNode_TimedSwap::get_before()const
100 {
101         return before;
102 }
103
104
105 bool
106 ValueNode_TimedSwap::set_after(const ValueNode::Handle &x)
107 {
108         if(!x || x->get_type()!=get_type()
109                 && !PlaceholderValueNode::Handle::cast_dynamic(x))
110                 return false;
111
112         after=x;
113
114         return true;
115 }
116
117 ValueNode::Handle
118 ValueNode_TimedSwap::get_after()const
119 {
120         return after;
121 }
122
123
124 void
125 ValueNode_TimedSwap::set_swap_time_real(Time x)
126 {
127         set_swap_time(ValueNode_Const::create(x));
128 }
129
130 bool
131 ValueNode_TimedSwap::set_swap_time(const ValueNode::Handle &x)
132 {
133         if(!x
134                 || !ValueBase(ValueBase::TYPE_TIME).same_as(x->get_type())
135                 && !PlaceholderValueNode::Handle::cast_dynamic(x)
136         )
137                 return false;
138         swap_time=x;
139         return true;
140 }
141
142 ValueNode::Handle
143 ValueNode_TimedSwap::get_swap_time()const
144 {
145         return swap_time;
146 }
147
148 void
149 ValueNode_TimedSwap::set_swap_length_real(Time x)
150 {
151         set_swap_length(ValueNode_Const::create(x));
152 }
153
154 bool
155 ValueNode_TimedSwap::set_swap_length(const ValueNode::Handle &x)
156 {
157         if(!x || (
158                 !ValueBase(ValueBase::TYPE_TIME).same_as(x->get_type())
159                 && !PlaceholderValueNode::Handle::cast_dynamic(x)
160                 )
161         )
162                 return false;
163         swap_length=x;
164         return true;
165 }
166
167 ValueNode::Handle
168 ValueNode_TimedSwap::get_swap_length()const
169 {
170         return swap_length;
171 }
172
173
174
175 synfig::ValueBase
176 synfig::ValueNode_TimedSwap::operator()(Time t)const
177 {
178         Time swptime=(*swap_time)(t).get(Time());
179         Time swplength=(*swap_length)(t).get(Time());
180
181         if(t>swptime)
182                 return (*after)(t);
183         
184         if(t<=swptime && t>swptime-swplength)
185         {
186                 Real amount=(swptime-t)/swplength;
187                 // if amount==0.0, then we are after
188                 // if amount==1.0, then we are before
189                 
190                 switch(get_type())
191                 {
192                 case ValueBase::TYPE_REAL:
193                         {
194                                 Real a=(*after)(t).get(Real());
195                                 Real b=(*before)(t).get(Real());
196                                 return (b-a)*amount+a;
197                         }
198                 case ValueBase::TYPE_VECTOR:
199                         {
200                                 Vector a=(*after)(t).get(Vector());
201                                 Vector b=(*before)(t).get(Vector());
202                                 return (b-a)*amount+a;
203                         }
204                 case ValueBase::TYPE_ANGLE:
205                         {
206                                 Angle a=(*after)(t).get(Angle());
207                                 Angle b=(*before)(t).get(Angle());
208                                 return (b-a)*amount+a;
209                         }
210                 case ValueBase::TYPE_COLOR:
211                         {
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;
216                         }
217                 case ValueBase::TYPE_INTEGER:
218                         {
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);
222                         }
223                 default:
224                         break;
225                 }
226         }
227
228
229         /*! \todo this should interpolate from
230         **      before to after over the period defined
231         **      by swap_length */
232
233         return (*before)(t);
234 }
235
236
237 bool
238 ValueNode_TimedSwap::set_link_vfunc(int i,ValueNode::Handle x)
239 {
240         assert(i>=0 && i<4);
241         switch(i)
242         {
243         case 0:
244                 return set_before(x);
245         case 1:
246                 return set_after(x);
247         case 2:
248                 return set_swap_time(x);
249         case 3:
250                 return set_swap_length(x);
251         }
252         return 0;
253 }
254
255 ValueNode::LooseHandle
256 ValueNode_TimedSwap::get_link_vfunc(int i)const
257 {
258         assert(i>=0 && i<4);
259         switch(i)
260         {
261         case 0:
262                 return get_before();
263         case 1:
264                 return get_after();
265         case 2:
266                 return get_swap_time();
267         case 3:
268                 return get_swap_length();
269         }
270         return 0;
271 }
272
273 int
274 ValueNode_TimedSwap::link_count()const
275 {
276         return 4;
277 }
278
279 String
280 ValueNode_TimedSwap::link_local_name(int i)const
281 {
282         assert(i>=0 && i<4);
283         switch(i)
284         {
285         case 0:
286                 return _("Before");
287         case 1:
288                 return _("After");
289         case 2:
290                 return _("Swap Time");
291         case 3:
292                 return _("Swap Duration");
293         }
294         return 0;
295 }       
296
297 String
298 ValueNode_TimedSwap::link_name(int i)const
299 {
300         assert(i>=0 && i<4);
301         switch(i)
302         {
303         case 0:
304                 return "before";
305         case 1:
306                 return "after";
307         case 2:
308                 return "time";
309         case 3:
310                 return "length";
311         }
312         return 0;
313 }       
314
315 int
316 ValueNode_TimedSwap::get_link_index_from_name(const String &name)const
317 {
318         if(name=="before")
319                 return 0;
320         if(name=="after")
321                 return 1;
322         if(name=="time")
323                 return 2;
324         if(name=="length")
325                 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         if(!type)
346                 return false;
347         return true;
348 }