Use LinkableValueNode members functions when possible in the derived valuenodes.
[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         Vocab ret(get_children_vocab());
60         set_children_vocab(ret);
61         switch(get_type())
62         {
63         case ValueBase::TYPE_ANGLE:
64                 set_link("before",ValueNode_Const::create(value.get(Angle())));
65                 set_link("after",ValueNode_Const::create(value.get(Angle())));
66                 break;
67         case ValueBase::TYPE_COLOR:
68                 set_link("before",ValueNode_Const::create(value.get(Color())));
69                 set_link("after",ValueNode_Const::create(value.get(Color())));
70                 break;
71         case ValueBase::TYPE_INTEGER:
72                 set_link("before",ValueNode_Const::create(value.get(int())));
73                 set_link("after",ValueNode_Const::create(value.get(int())));
74                 break;
75         case ValueBase::TYPE_REAL:
76                 set_link("before",ValueNode_Const::create(value.get(Real())));
77                 set_link("after",ValueNode_Const::create(value.get(Real())));
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         case ValueBase::TYPE_VECTOR:
84                 set_link("before",ValueNode_Const::create(value.get(Vector())));
85                 set_link("after",ValueNode_Const::create(value.get(Vector())));
86                 break;
87         default:
88                 throw Exception::BadType(ValueBase::type_local_name(get_type()));
89         }
90
91         set_link("time",ValueNode_Const::create(Time(2)));
92         set_link("length",ValueNode_Const::create(Time(1)));
93 }
94
95 ValueNode_TimedSwap*
96 ValueNode_TimedSwap::create(const ValueBase& x)
97 {
98         return new ValueNode_TimedSwap(x);
99 }
100
101 LinkableValueNode*
102 ValueNode_TimedSwap::create_new()const
103 {
104         return new ValueNode_TimedSwap(get_type());
105 }
106
107 synfig::ValueNode_TimedSwap::~ValueNode_TimedSwap()
108 {
109         unlink_all();
110 }
111
112 synfig::ValueBase
113 synfig::ValueNode_TimedSwap::operator()(Time t)const
114 {
115         if (getenv("SYNFIG_DEBUG_VALUENODE_OPERATORS"))
116                 printf("%s:%d operator()\n", __FILE__, __LINE__);
117
118         Time swptime=(*swap_time)(t).get(Time());
119         Time swplength=(*swap_length)(t).get(Time());
120
121         if(t>swptime)
122                 return (*after)(t);
123
124         if(t<=swptime && t>swptime-swplength)
125         {
126                 Real amount=(swptime-t)/swplength;
127                 // if amount==0.0, then we are after
128                 // if amount==1.0, then we are before
129
130                 switch(get_type())
131                 {
132                 case ValueBase::TYPE_ANGLE:
133                         {
134                                 Angle a=(*after)(t).get(Angle());
135                                 Angle b=(*before)(t).get(Angle());
136                                 return (b-a)*amount+a;
137                         }
138                 case ValueBase::TYPE_COLOR:
139                         {
140                                 Color a=(*after)(t).get(Color());
141                                 Color b=(*before)(t).get(Color());
142                                 // note: Shouldn't this use a straight blend?
143                                 return (b-a)*amount+a;
144                         }
145                 case ValueBase::TYPE_INTEGER:
146                         {
147                                 float a=(float)(*after)(t).get(int());
148                                 float b=(float)(*before)(t).get(int());
149                                 return round_to_int((b-a)*amount+a);
150                         }
151                 case ValueBase::TYPE_REAL:
152                         {
153                                 Real a=(*after)(t).get(Real());
154                                 Real b=(*before)(t).get(Real());
155                                 return (b-a)*amount+a;
156                         }
157                 case ValueBase::TYPE_TIME:
158                         {
159                                 Time a=(*after)(t).get(Time());
160                                 Time b=(*before)(t).get(Time());
161                                 return (b-a)*amount+a;
162                         }
163                 case ValueBase::TYPE_VECTOR:
164                         {
165                                 Vector a=(*after)(t).get(Vector());
166                                 Vector b=(*before)(t).get(Vector());
167                                 return (b-a)*amount+a;
168                         }
169                 default:
170                         break;
171                 }
172         }
173
174         /*! \todo this should interpolate from
175         **      before to after over the period defined
176         **      by swap_length */
177
178         return (*before)(t);
179 }
180
181 bool
182 ValueNode_TimedSwap::set_link_vfunc(int i,ValueNode::Handle value)
183 {
184         assert(i>=0 && i<link_count());
185
186         switch(i)
187         {
188         case 0: CHECK_TYPE_AND_SET_VALUE(before,      get_type());
189         case 1: CHECK_TYPE_AND_SET_VALUE(after,       get_type());
190         case 2: CHECK_TYPE_AND_SET_VALUE(swap_time,   ValueBase::TYPE_TIME);
191         case 3: CHECK_TYPE_AND_SET_VALUE(swap_length, ValueBase::TYPE_TIME);
192         }
193         return false;
194 }
195
196 ValueNode::LooseHandle
197 ValueNode_TimedSwap::get_link_vfunc(int i)const
198 {
199         assert(i>=0 && i<link_count());
200
201         switch(i)
202         {
203         case 0: return before;
204         case 1: return after;
205         case 2: return swap_time;
206         case 3: return swap_length;
207         }
208         return 0;
209 }
210
211 String
212 ValueNode_TimedSwap::get_name()const
213 {
214         return "timed_swap";
215 }
216
217 String
218 ValueNode_TimedSwap::get_local_name()const
219 {
220         return _("Timed Swap");
221 }
222
223 bool
224 ValueNode_TimedSwap::check_type(ValueBase::Type type)
225 {
226         return
227                 type==ValueBase::TYPE_ANGLE ||
228                 type==ValueBase::TYPE_COLOR ||
229                 type==ValueBase::TYPE_INTEGER ||
230                 type==ValueBase::TYPE_REAL ||
231                 type==ValueBase::TYPE_TIME ||
232                 type==ValueBase::TYPE_VECTOR;
233 }
234
235 LinkableValueNode::Vocab
236 ValueNode_TimedSwap::get_children_vocab_vfunc()const
237 {
238         if(children_vocab.size())
239                 return children_vocab;
240
241         LinkableValueNode::Vocab ret;
242
243         ret.push_back(ParamDesc(ValueBase(),"before")
244                 .set_local_name(_("Before"))
245                 .set_description(_("The value node returned when current time is before 'time' - 'length'"))
246         );
247
248         ret.push_back(ParamDesc(ValueBase(),"after")
249                 .set_local_name(_("After"))
250                 .set_description(_("The value node returned when current time is after 'time'"))
251         );
252
253         ret.push_back(ParamDesc(ValueBase(),"time")
254                 .set_local_name(_("Time"))
255                 .set_description(_("The time when the linear interpolation ends"))
256         );
257
258         ret.push_back(ParamDesc(ValueBase(),"length")
259                 .set_local_name(_("Length"))
260                 .set_description(_("The length of time when the linear interpolation between 'Before' and 'After' is made"))
261         );
262
263         return ret;
264 }