Use the ETL 'round_to_int' function rather than re-writing it in each of these files.
[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 **      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 #include <ETL/misc>
39
40 #endif
41
42 /* === U S I N G =========================================================== */
43
44 using namespace std;
45 using namespace etl;
46 using namespace synfig;
47
48 /* === M A C R O S ========================================================= */
49
50 /* === G L O B A L S ======================================================= */
51
52 /* === P R O C E D U R E S ================================================= */
53
54 /* === M E T H O D S ======================================================= */
55
56 ValueNode_TimedSwap::ValueNode_TimedSwap(const ValueBase &value):
57         LinkableValueNode(value.get_type())
58 {
59         switch(get_type())
60         {
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())));
64                 break;
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())));
68                 break;
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())));
72                 break;
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())));
76                 break;
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())));
80                 break;
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())));
84                 break;
85         default:
86                 throw Exception::BadType(ValueBase::type_name(get_type()));
87         }
88
89         set_link("time",ValueNode_Const::create(Time(2)));
90         set_link("length",ValueNode_Const::create(Time(1)));
91
92         DCAST_HACK_ENABLE();
93 }
94
95 ValueNode_TimedSwap*
96 ValueNode_TimedSwap::create(const ValueBase& x)
97 {
98         return new ValueNode_TimedSwap(x);
99 }
100
101
102 LinkableValueNode*
103 ValueNode_TimedSwap::create_new()const
104 {
105         return new ValueNode_TimedSwap(get_type());
106 }
107
108
109 synfig::ValueNode_TimedSwap::~ValueNode_TimedSwap()
110 {
111         unlink_all();
112 }
113
114
115
116 bool
117 ValueNode_TimedSwap::set_before(const ValueNode::Handle &x)
118 {
119         if(!x || x->get_type()!=get_type()
120                 && !PlaceholderValueNode::Handle::cast_dynamic(x))
121                 return false;
122
123         before=x;
124
125         return true;
126 }
127
128 ValueNode::Handle
129 ValueNode_TimedSwap::get_before()const
130 {
131         return before;
132 }
133
134
135 bool
136 ValueNode_TimedSwap::set_after(const ValueNode::Handle &x)
137 {
138         if(!x || x->get_type()!=get_type()
139                 && !PlaceholderValueNode::Handle::cast_dynamic(x))
140                 return false;
141
142         after=x;
143
144         return true;
145 }
146
147 ValueNode::Handle
148 ValueNode_TimedSwap::get_after()const
149 {
150         return after;
151 }
152
153
154 bool
155 ValueNode_TimedSwap::set_swap_time(const ValueNode::Handle &x)
156 {
157         if(!x || (!ValueBase(x->get_type()).same_type_as(ValueBase::TYPE_TIME) &&
158                           !PlaceholderValueNode::Handle::cast_dynamic(x)))
159                 return false;
160
161         swap_time=x;
162         return true;
163 }
164
165 ValueNode::Handle
166 ValueNode_TimedSwap::get_swap_time()const
167 {
168         return swap_time;
169 }
170
171 bool
172 ValueNode_TimedSwap::set_swap_length(const ValueNode::Handle &x)
173 {
174         if(!x || (!ValueBase(x->get_type()).same_type_as(ValueBase::TYPE_TIME) &&
175                           !PlaceholderValueNode::Handle::cast_dynamic(x)))
176                 return false;
177
178         swap_length=x;
179         return true;
180 }
181
182 ValueNode::Handle
183 ValueNode_TimedSwap::get_swap_length()const
184 {
185         return swap_length;
186 }
187
188
189
190 synfig::ValueBase
191 synfig::ValueNode_TimedSwap::operator()(Time t)const
192 {
193         Time swptime=(*swap_time)(t).get(Time());
194         Time swplength=(*swap_length)(t).get(Time());
195
196         if(t>swptime)
197                 return (*after)(t);
198
199         if(t<=swptime && t>swptime-swplength)
200         {
201                 Real amount=(swptime-t)/swplength;
202                 // if amount==0.0, then we are after
203                 // if amount==1.0, then we are before
204
205                 switch(get_type())
206                 {
207                 case ValueBase::TYPE_ANGLE:
208                         {
209                                 Angle a=(*after)(t).get(Angle());
210                                 Angle b=(*before)(t).get(Angle());
211                                 return (b-a)*amount+a;
212                         }
213                 case ValueBase::TYPE_COLOR:
214                         {
215                                 Color a=(*after)(t).get(Color());
216                                 Color b=(*before)(t).get(Color());
217                                 // note: Shouldn't this use a straight blend?
218                                 return (b-a)*amount+a;
219                         }
220                 case ValueBase::TYPE_INTEGER:
221                         {
222                                 float a=(float)(*after)(t).get(int());
223                                 float b=(float)(*before)(t).get(int());
224                                 return round_to_int((b-a)*amount+a);
225                         }
226                 case ValueBase::TYPE_REAL:
227                         {
228                                 Real a=(*after)(t).get(Real());
229                                 Real b=(*before)(t).get(Real());
230                                 return (b-a)*amount+a;
231                         }
232                 case ValueBase::TYPE_TIME:
233                         {
234                                 Time a=(*after)(t).get(Time());
235                                 Time b=(*before)(t).get(Time());
236                                 return (b-a)*amount+a;
237                         }
238                 case ValueBase::TYPE_VECTOR:
239                         {
240                                 Vector a=(*after)(t).get(Vector());
241                                 Vector b=(*before)(t).get(Vector());
242                                 return (b-a)*amount+a;
243                         }
244                 default:
245                         break;
246                 }
247         }
248
249
250         /*! \todo this should interpolate from
251         **      before to after over the period defined
252         **      by swap_length */
253
254         return (*before)(t);
255 }
256
257
258 bool
259 ValueNode_TimedSwap::set_link_vfunc(int i,ValueNode::Handle x)
260 {
261         assert(i>=0 && i<4);
262         switch(i)
263         {
264         case 0: return set_before(x);
265         case 1: return set_after(x);
266         case 2: return set_swap_time(x);
267         case 3: return set_swap_length(x);
268         }
269         return false;
270 }
271
272 ValueNode::LooseHandle
273 ValueNode_TimedSwap::get_link_vfunc(int i)const
274 {
275         assert(i>=0 && i<4);
276         switch(i)
277         {
278         case 0: return get_before();
279         case 1: return get_after();
280         case 2: return get_swap_time();
281         case 3: return get_swap_length();
282         }
283         return 0;
284 }
285
286 int
287 ValueNode_TimedSwap::link_count()const
288 {
289         return 4;
290 }
291
292 String
293 ValueNode_TimedSwap::link_local_name(int i)const
294 {
295         assert(i>=0 && i<4);
296         switch(i)
297         {
298         case 0: return _("Before");
299         case 1: return _("After");
300         case 2: return _("Swap Time");
301         case 3: return _("Swap Duration");
302         default:return String();
303         }
304 }
305
306 String
307 ValueNode_TimedSwap::link_name(int i)const
308 {
309         assert(i>=0 && i<4);
310         switch(i)
311         {
312         case 0: return "before";
313         case 1: return "after";
314         case 2: return "time";
315         case 3: return "length";
316         default:return String();
317         }
318 }
319
320 int
321 ValueNode_TimedSwap::get_link_index_from_name(const String &name)const
322 {
323         if(name=="before")      return 0;
324         if(name=="after")       return 1;
325         if(name=="time")        return 2;
326         if(name=="length")      return 3;
327
328         throw Exception::BadLinkName(name);
329 }
330
331 String
332 ValueNode_TimedSwap::get_name()const
333 {
334         return "timed_swap";
335 }
336
337 String
338 ValueNode_TimedSwap::get_local_name()const
339 {
340         return _("Timed Swap");
341 }
342
343 bool
344 ValueNode_TimedSwap::check_type(ValueBase::Type type)
345 {
346         return
347                 type==ValueBase::TYPE_ANGLE ||
348                 type==ValueBase::TYPE_COLOR ||
349                 type==ValueBase::TYPE_INTEGER ||
350                 type==ValueBase::TYPE_REAL ||
351                 type==ValueBase::TYPE_TIME ||
352                 type==ValueBase::TYPE_VECTOR;
353 }