moreupdates
[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: valuenode_timedswap.cpp,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002 Robert B. Quattlebaum Jr.
9 **
10 **      This software and associated documentation
11 **      are CONFIDENTIAL and PROPRIETARY property of
12 **      the above-mentioned copyright holder.
13 **
14 **      You may not copy, print, publish, or in any
15 **      other way distribute this software without
16 **      a prior written agreement with
17 **      the copyright holder.
18 **      \endlegal
19 */
20 /* ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #ifdef USING_PCH
25 #       include "pch.h"
26 #else
27 #ifdef HAVE_CONFIG_H
28 #       include <config.h>
29 #endif
30
31 #include "general.h"
32 #include "valuenode_timedswap.h"
33 #include "valuenode_const.h"
34 #include <stdexcept>
35 #include "color.h"
36
37 #endif
38
39 /* === U S I N G =========================================================== */
40
41 using namespace std;
42 using namespace etl;
43 using namespace synfig;
44
45 /* === M A C R O S ========================================================= */
46
47 /* === G L O B A L S ======================================================= */
48
49 /* === P R O C E D U R E S ================================================= */
50
51 /* === M E T H O D S ======================================================= */
52
53 ValueNode_TimedSwap::ValueNode_TimedSwap(ValueBase::Type type):
54         LinkableValueNode(type)
55 {
56         set_before(ValueNode_Const::create(type));
57         set_after(ValueNode_Const::create(type));
58         set_swap_time_real(1.0);
59         set_swap_length_real(1.0);
60
61         DCAST_HACK_ENABLE();
62 }
63
64 ValueNode_TimedSwap*
65 ValueNode_TimedSwap::create(const ValueBase& x)
66 {
67         return new ValueNode_TimedSwap(x.get_type());
68 }
69
70
71 LinkableValueNode*
72 ValueNode_TimedSwap::create_new()const
73 {
74         return new ValueNode_TimedSwap(get_type());
75 }
76
77
78 synfig::ValueNode_TimedSwap::~ValueNode_TimedSwap()
79 {
80         unlink_all();
81 }
82
83
84
85 bool
86 ValueNode_TimedSwap::set_before(const ValueNode::Handle &x)
87 {
88         if(!x || x->get_type()!=get_type()
89                 && !PlaceholderValueNode::Handle::cast_dynamic(x))
90                 return false;
91
92         before=x;
93
94         return true;
95 }
96
97 ValueNode::Handle
98 ValueNode_TimedSwap::get_before()const
99 {
100         return before;
101 }
102
103
104 bool
105 ValueNode_TimedSwap::set_after(const ValueNode::Handle &x)
106 {
107         if(!x || x->get_type()!=get_type()
108                 && !PlaceholderValueNode::Handle::cast_dynamic(x))
109                 return false;
110
111         after=x;
112
113         return true;
114 }
115
116 ValueNode::Handle
117 ValueNode_TimedSwap::get_after()const
118 {
119         return after;
120 }
121
122
123 void
124 ValueNode_TimedSwap::set_swap_time_real(Time x)
125 {
126         set_swap_time(ValueNode_Const::create(x));
127 }
128
129 bool
130 ValueNode_TimedSwap::set_swap_time(const ValueNode::Handle &x)
131 {
132         if(!x
133                 || !ValueBase(ValueBase::TYPE_TIME).same_as(x->get_type())
134                 && !PlaceholderValueNode::Handle::cast_dynamic(x)
135         )
136                 return false;
137         swap_time=x;
138         return true;
139 }
140
141 ValueNode::Handle
142 ValueNode_TimedSwap::get_swap_time()const
143 {
144         return swap_time;
145 }
146
147 void
148 ValueNode_TimedSwap::set_swap_length_real(Time x)
149 {
150         set_swap_length(ValueNode_Const::create(x));
151 }
152
153 bool
154 ValueNode_TimedSwap::set_swap_length(const ValueNode::Handle &x)
155 {
156         if(!x || (
157                 !ValueBase(ValueBase::TYPE_TIME).same_as(x->get_type())
158                 && !PlaceholderValueNode::Handle::cast_dynamic(x)
159                 )
160         )
161                 return false;
162         swap_length=x;
163         return true;
164 }
165
166 ValueNode::Handle
167 ValueNode_TimedSwap::get_swap_length()const
168 {
169         return swap_length;
170 }
171
172
173
174 synfig::ValueBase
175 synfig::ValueNode_TimedSwap::operator()(Time t)const
176 {
177         Time swptime=(*swap_time)(t).get(Time());
178         Time swplength=(*swap_length)(t).get(Time());
179
180         if(t>swptime)
181                 return (*after)(t);
182         
183         if(t<=swptime && t>swptime-swplength)
184         {
185                 Real amount=(swptime-t)/swplength;
186                 // if amount==0.0, then we are after
187                 // if amount==1.0, then we are before
188                 
189                 switch(get_type())
190                 {
191                 case ValueBase::TYPE_REAL:
192                         {
193                                 Real a=(*after)(t).get(Real());
194                                 Real b=(*before)(t).get(Real());
195                                 return (b-a)*amount+a;
196                         }
197                 case ValueBase::TYPE_VECTOR:
198                         {
199                                 Vector a=(*after)(t).get(Vector());
200                                 Vector b=(*before)(t).get(Vector());
201                                 return (b-a)*amount+a;
202                         }
203                 case ValueBase::TYPE_ANGLE:
204                         {
205                                 Angle a=(*after)(t).get(Angle());
206                                 Angle b=(*before)(t).get(Angle());
207                                 return (b-a)*amount+a;
208                         }
209                 case ValueBase::TYPE_COLOR:
210                         {
211                                 Color a=(*after)(t).get(Color());
212                                 Color b=(*before)(t).get(Color());
213                                 // note: Shouldn't this use a straight blend?
214                                 return (b-a)*amount+a;
215                         }
216                 case ValueBase::TYPE_INTEGER:
217                         {
218                                 float a=(float)(*after)(t).get(int());
219                                 float b=(float)(*before)(t).get(int());
220                                 return static_cast<int>((b-a)*amount+a+0.5f);
221                         }
222                 default:
223                         break;
224                 }
225         }
226
227
228         /*! \todo this should interpolate from
229         **      before to after over the period defined
230         **      by swap_length */
231
232         return (*before)(t);
233 }
234
235
236 bool
237 ValueNode_TimedSwap::set_link_vfunc(int i,ValueNode::Handle x)
238 {
239         assert(i>=0 && i<4);
240         switch(i)
241         {
242         case 0:
243                 return set_before(x);
244         case 1:
245                 return set_after(x);
246         case 2:
247                 return set_swap_time(x);
248         case 3:
249                 return set_swap_length(x);
250         }
251         return 0;
252 }
253
254 ValueNode::LooseHandle
255 ValueNode_TimedSwap::get_link_vfunc(int i)const
256 {
257         assert(i>=0 && i<4);
258         switch(i)
259         {
260         case 0:
261                 return get_before();
262         case 1:
263                 return get_after();
264         case 2:
265                 return get_swap_time();
266         case 3:
267                 return get_swap_length();
268         }
269         return 0;
270 }
271
272 int
273 ValueNode_TimedSwap::link_count()const
274 {
275         return 4;
276 }
277
278 String
279 ValueNode_TimedSwap::link_local_name(int i)const
280 {
281         assert(i>=0 && i<4);
282         switch(i)
283         {
284         case 0:
285                 return _("Before");
286         case 1:
287                 return _("After");
288         case 2:
289                 return _("Swap Time");
290         case 3:
291                 return _("Swap Duration");
292         }
293         return 0;
294 }       
295
296 String
297 ValueNode_TimedSwap::link_name(int i)const
298 {
299         assert(i>=0 && i<4);
300         switch(i)
301         {
302         case 0:
303                 return "before";
304         case 1:
305                 return "after";
306         case 2:
307                 return "time";
308         case 3:
309                 return "length";
310         }
311         return 0;
312 }       
313
314 int
315 ValueNode_TimedSwap::get_link_index_from_name(const String &name)const
316 {
317         if(name=="before")
318                 return 0;
319         if(name=="after")
320                 return 1;
321         if(name=="time")
322                 return 2;
323         if(name=="length")
324                 return 3;
325
326         throw Exception::BadLinkName(name);
327 }
328
329 String
330 ValueNode_TimedSwap::get_name()const
331 {
332         return "timed_swap";
333 }
334
335 String
336 ValueNode_TimedSwap::get_local_name()const
337 {
338         return _("Timed Swap");
339 }
340
341 bool
342 ValueNode_TimedSwap::check_type(ValueBase::Type type)
343 {
344         if(!type)
345                 return false;
346         return true;
347 }