a1875685652a7145334cc506b4a788b669a1837d
[synfig.git] /
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_radialcomposite.cpp
3 **      \brief Implementation of the "Radial Composite" 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 "valuenode_radialcomposite.h"
34 #include "valuenode_const.h"
35 #include <stdexcept>
36 #include "general.h"
37 #include "color.h"
38 #include "savecanvas.h"
39 #endif
40
41 /* === U S I N G =========================================================== */
42
43 using namespace std;
44 using namespace etl;
45 using namespace synfig;
46
47 /* === M A C R O S ========================================================= */
48
49 /* === G L O B A L S ======================================================= */
50
51 /* === P R O C E D U R E S ================================================= */
52
53 /* === M E T H O D S ======================================================= */
54
55 synfig::ValueNode_RadialComposite::ValueNode_RadialComposite(const ValueBase &value):
56         LinkableValueNode(value.get_type())
57 {
58         switch(get_type())
59         {
60                 case ValueBase::TYPE_VECTOR:
61                 {
62                         Vector vect(value.get(Vector()));
63                         set_link("r",ValueNode_Const::create(vect.mag()));
64                         set_link("t",ValueNode_Const::create(Angle(Angle::tan(vect[1],vect[0]))));
65                 }
66                         break;
67                 case ValueBase::TYPE_COLOR:
68                         set_link("y",ValueNode_Const::create(value.get(Color()).get_y()));
69                         set_link("s",ValueNode_Const::create(value.get(Color()).get_s()));
70                         set_link("h",ValueNode_Const::create(value.get(Color()).get_hue()));
71                         set_link("a",ValueNode_Const::create(value.get(Color()).get_a()));
72                         break;
73                 default:
74                         assert(0);
75                         throw Exception::BadType(ValueBase::type_local_name(get_type()));
76         }
77 }
78
79 ValueNode_RadialComposite::~ValueNode_RadialComposite()
80 {
81         unlink_all();
82 }
83
84 ValueNode_RadialComposite*
85 ValueNode_RadialComposite::create(const ValueBase &value)
86 {
87         return new ValueNode_RadialComposite(value);
88 }
89
90 LinkableValueNode*
91 ValueNode_RadialComposite::create_new()const
92 {
93         return new ValueNode_RadialComposite(ValueBase(get_type()));
94 }
95
96 ValueBase
97 synfig::ValueNode_RadialComposite::operator()(Time t)const
98 {
99         switch(get_type())
100         {
101                 case ValueBase::TYPE_VECTOR:
102                 {
103                         Real mag;
104                         Angle angle;
105                         assert(components[0] && components[1]);
106                         mag=(*components[0])(t).get(mag);
107                         angle=(*components[1])(t).get(angle);
108                         return Vector(Angle::cos(angle).get()*mag,Angle::sin(angle).get()*mag);
109                 }
110                 case ValueBase::TYPE_COLOR:
111                 {
112                         assert(components[0] && components[1] && components[2] && components[3]);
113                         return Color::YUV(
114                                 (*components[0])(t).get(Real()),
115                                 (*components[1])(t).get(Real()),
116                                 (*components[2])(t).get(Angle()),
117                                 (*components[3])(t).get(Real())
118                         );
119                 }
120                 default:
121                         synfig::error(string("ValueNode_RadialComposite::operator():")+_("Bad type for radialcomposite"));
122                         assert(components[0]);
123                         return (*components[0])(t);
124         }
125 }
126
127 int
128 ValueNode_RadialComposite::link_count()const
129 {
130         switch(get_type())
131         {
132         case ValueBase::TYPE_VECTOR:
133                 return 2;
134         case ValueBase::TYPE_COLOR:
135                 return 4;
136         default:
137                 synfig::warning(string("ValueNode_RadialComposite::component_count():")+_("Bad type for radialcomposite"));
138                 return 1;
139         }
140 }
141
142 bool
143 ValueNode_RadialComposite::set_link_vfunc(int i,ValueNode::Handle x)
144 {
145         assert(i>=0 && i<link_count());
146
147         if(PlaceholderValueNode::Handle::cast_dynamic(x))
148         {
149                 components[i]=x;
150                 return true;
151         }
152
153         switch(get_type())
154         {
155                 case ValueBase::TYPE_VECTOR:
156                         if(i==0 && x->get_type()!=ValueBase::TYPE_REAL)
157                                 return false;
158                         if(i==1 && x->get_type()!=ValueBase::TYPE_ANGLE)
159                                 return false;
160                         components[i]=x;
161                         return true;
162                         break;
163
164                 case ValueBase::TYPE_COLOR:
165                         if((i==0 || i==1 || i==3) && x->get_type()!=ValueBase::TYPE_REAL)
166                                 return false;
167                         if((i==2) && x->get_type()!=ValueBase::TYPE_ANGLE)
168                                 return false;
169                         components[i]=x;
170                         return true;
171                         break;
172
173
174                 default:
175                         break;
176         }
177         return false;
178 }
179
180 ValueNode::LooseHandle
181 ValueNode_RadialComposite::get_link_vfunc(int i)const
182 {
183         assert(i>=0 && i<link_count());
184
185         return components[i];
186 }
187
188 String
189 ValueNode_RadialComposite::link_local_name(int i)const
190 {
191         assert(i>=0 && i<link_count());
192
193         switch(get_type())
194         {
195                 case ValueBase::TYPE_VECTOR:
196                         if(i==0)
197                                 return _("Radius");
198                         else if(i==1)
199                                 return _("Theta");
200                         break;
201
202                 case ValueBase::TYPE_COLOR:
203                         if(i==0)
204                                 return _("Luma");
205                         else if(i==1)
206                                 return _("Saturation");
207                         else if(i==2)
208                                 return _("Hue");
209                         else if(i==3)
210                                 return _("Alpha");
211                         break;
212
213                 default:
214                         break;
215         }
216
217         assert(0);
218         // notice that Composite counts from 1 and Radial Composite counts
219         // from 0!  we need to keep it like that to correctly load old
220         // animations, but let's not save "c%d" format link names in future
221         return etl::strprintf(_("C%d"),i);
222 }
223
224
225 String
226 ValueNode_RadialComposite::link_name(int i)const
227 {
228         assert(i>=0 && i<link_count());
229
230         if (get_file_version() < RELEASE_VERSION_0_61_08)
231                 return strprintf("c%d",i);
232
233         switch(get_type())
234         {
235         case ValueBase::TYPE_COLOR:
236                 switch(i)
237                 {
238                 case 0: return "y_luma"; // the 'luma' attribute is recognised by the fact that it starts with a 'y'
239                 case 1: return "saturation";
240                 case 2: return "hue";
241                 case 3: return "alpha";
242                 }
243                 break;
244         case ValueBase::TYPE_VECTOR:
245                 switch(i)
246                 {
247                 case 0: return "radius";
248                 case 1: return "theta";
249                 }
250                 break;
251         default:
252                 break;
253         }
254
255         assert(0);
256         // notice that Composite counts from 1 and Radial Composite counts
257         // from 0!  we need to keep it like that to correctly load old
258         // animations, but let's not save "c%d" format link names in future
259         return strprintf("c%d",i);
260 }
261
262 int
263 ValueNode_RadialComposite::get_link_index_from_name(const String &name)const
264 {
265         if(name.empty())
266                 throw Exception::BadLinkName(name);
267
268         if(name[0]=='c' && name.size() == 2 && name[1]-'0' >= 0 && name[1]-'0' < link_count())
269                 return name[1]-'0';
270
271         switch(get_type())
272         {
273         case ValueBase::TYPE_COLOR:
274                 if(name[0]=='y')
275                         return 0;
276                 if(name[0]=='s')
277                         return 1;
278                 if(name[0]=='h')
279                         return 2;
280                 if(name[0]=='a')
281                         return 3;
282         case ValueBase::TYPE_VECTOR:
283                 if(name[0]=='r')
284                         return 0;
285                 if(name[0]=='t')
286                         return 1;
287         default:
288                 break;
289         }
290
291         throw Exception::BadLinkName(name);
292 }
293
294 String
295 ValueNode_RadialComposite::get_name()const
296 {
297         return "radial_composite";
298 }
299
300 String
301 ValueNode_RadialComposite::get_local_name()const
302 {
303         return _("Radial Composite");
304 }
305
306 bool
307 ValueNode_RadialComposite::check_type(ValueBase::Type type)
308 {
309         return
310                 type==ValueBase::TYPE_VECTOR ||
311                 type==ValueBase::TYPE_COLOR;
312 }