Rename get_param_vocab to get_children_vocab and use a wrapper for the pure virtual...
[synfig.git] / synfig-core / src / synfig / valuenode_timedswap.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_timedswap.cpp
3 **      \brief Implementation of the "Timed Swap" valuenode conversion.
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007, 2008 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_local_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
93 ValueNode_TimedSwap*
94 ValueNode_TimedSwap::create(const ValueBase& x)
95 {
96         return new ValueNode_TimedSwap(x);
97 }
98
99 LinkableValueNode*
100 ValueNode_TimedSwap::create_new()const
101 {
102         return new ValueNode_TimedSwap(get_type());
103 }
104
105 synfig::ValueNode_TimedSwap::~ValueNode_TimedSwap()
106 {
107         unlink_all();
108 }
109
110 synfig::ValueBase
111 synfig::ValueNode_TimedSwap::operator()(Time t)const
112 {
113         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
114                 printf("%s:%d operator()\n", __FILE__, __LINE__);
115
116         Time swptime=(*swap_time)(t).get(Time());
117         Time swplength=(*swap_length)(t).get(Time());
118
119         if(t>swptime)
120                 return (*after)(t);
121
122         if(t<=swptime && t>swptime-swplength)
123         {
124                 Real amount=(swptime-t)/swplength;
125                 // if amount==0.0, then we are after
126                 // if amount==1.0, then we are before
127
128                 switch(get_type())
129                 {
130                 case ValueBase::TYPE_ANGLE:
131                         {
132                                 Angle a=(*after)(t).get(Angle());
133                                 Angle b=(*before)(t).get(Angle());
134                                 return (b-a)*amount+a;
135                         }
136                 case ValueBase::TYPE_COLOR:
137                         {
138                                 Color a=(*after)(t).get(Color());
139                                 Color b=(*before)(t).get(Color());
140                                 // note: Shouldn't this use a straight blend?
141                                 return (b-a)*amount+a;
142                         }
143                 case ValueBase::TYPE_INTEGER:
144                         {
145                                 float a=(float)(*after)(t).get(int());
146                                 float b=(float)(*before)(t).get(int());
147                                 return round_to_int((b-a)*amount+a);
148                         }
149                 case ValueBase::TYPE_REAL:
150                         {
151                                 Real a=(*after)(t).get(Real());
152                                 Real b=(*before)(t).get(Real());
153                                 return (b-a)*amount+a;
154                         }
155                 case ValueBase::TYPE_TIME:
156                         {
157                                 Time a=(*after)(t).get(Time());
158                                 Time b=(*before)(t).get(Time());
159                                 return (b-a)*amount+a;
160                         }
161                 case ValueBase::TYPE_VECTOR:
162                         {
163                                 Vector a=(*after)(t).get(Vector());
164                                 Vector b=(*before)(t).get(Vector());
165                                 return (b-a)*amount+a;
166                         }
167                 default:
168                         break;
169                 }
170         }
171
172         /*! \todo this should interpolate from
173         **      before to after over the period defined
174         **      by swap_length */
175
176         return (*before)(t);
177 }
178
179 bool
180 ValueNode_TimedSwap::set_link_vfunc(int i,ValueNode::Handle value)
181 {
182         assert(i>=0 && i<link_count());
183
184         switch(i)
185         {
186         case 0: CHECK_TYPE_AND_SET_VALUE(before,      get_type());
187         case 1: CHECK_TYPE_AND_SET_VALUE(after,       get_type());
188         case 2: CHECK_TYPE_AND_SET_VALUE(swap_time,   ValueBase::TYPE_TIME);
189         case 3: CHECK_TYPE_AND_SET_VALUE(swap_length, ValueBase::TYPE_TIME);
190         }
191         return false;
192 }
193
194 ValueNode::LooseHandle
195 ValueNode_TimedSwap::get_link_vfunc(int i)const
196 {
197         assert(i>=0 && i<link_count());
198
199         switch(i)
200         {
201         case 0: return before;
202         case 1: return after;
203         case 2: return swap_time;
204         case 3: return swap_length;
205         }
206         return 0;
207 }
208
209 int
210 ValueNode_TimedSwap::link_count()const
211 {
212         return 4;
213 }
214
215 String
216 ValueNode_TimedSwap::link_local_name(int i)const
217 {
218         assert(i>=0 && i<link_count());
219
220         switch(i)
221         {
222         case 0: return _("Before");
223         case 1: return _("After");
224         case 2: return _("Swap Time");
225         case 3: return _("Swap Duration");
226         default:return String();
227         }
228 }
229
230 String
231 ValueNode_TimedSwap::link_name(int i)const
232 {
233         assert(i>=0 && i<link_count());
234
235         switch(i)
236         {
237         case 0: return "before";
238         case 1: return "after";
239         case 2: return "time";
240         case 3: return "length";
241         default:return String();
242         }
243 }
244
245 int
246 ValueNode_TimedSwap::get_link_index_from_name(const String &name)const
247 {
248         if(name=="before")      return 0;
249         if(name=="after")       return 1;
250         if(name=="time")        return 2;
251         if(name=="length")      return 3;
252
253         throw Exception::BadLinkName(name);
254 }
255
256 String
257 ValueNode_TimedSwap::get_name()const
258 {
259         return "timed_swap";
260 }
261
262 String
263 ValueNode_TimedSwap::get_local_name()const
264 {
265         return _("Timed Swap");
266 }
267
268 bool
269 ValueNode_TimedSwap::check_type(ValueBase::Type type)
270 {
271         return
272                 type==ValueBase::TYPE_ANGLE ||
273                 type==ValueBase::TYPE_COLOR ||
274                 type==ValueBase::TYPE_INTEGER ||
275                 type==ValueBase::TYPE_REAL ||
276                 type==ValueBase::TYPE_TIME ||
277                 type==ValueBase::TYPE_VECTOR;
278 }
279
280 LinkableValueNode::Vocab
281 ValueNode_TimedSwap::get_children_vocab_vfunc()const
282 {
283         LinkableValueNode::Vocab ret;
284
285         ret.push_back(ParamDesc(ValueBase(),"before")
286                 .set_local_name(_("Before"))
287                 .set_description(_("The value node returned when current time is before 'time' - 'length'"))
288         );
289
290         ret.push_back(ParamDesc(ValueBase(),"after")
291                 .set_local_name(_("After"))
292                 .set_description(_("The value node returned when current time is after 'time'"))
293         );
294
295         ret.push_back(ParamDesc(ValueBase(),"time")
296                 .set_local_name(_("Time"))
297                 .set_description(_("The time when the linear interpolation ends"))
298         );
299
300         ret.push_back(ParamDesc(ValueBase(),"length")
301                 .set_local_name(_("Length"))
302                 .set_description(_("The length of time when the linear interpolation between 'Before' and 'After' is made"))
303         );
304
305         return ret;
306 }