Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07 / src / synfig / valuenode_radialcomposite.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_radialcomposite.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 "valuenode_radialcomposite.h"
34 #include "valuenode_const.h"
35 #include <stdexcept>
36 #include "general.h"
37 #include "color.h"
38 #endif
39
40 /* === U S I N G =========================================================== */
41
42 using namespace std;
43 using namespace etl;
44 using namespace synfig;
45
46 /* === M A C R O S ========================================================= */
47
48 /* === G L O B A L S ======================================================= */
49
50 /* === P R O C E D U R E S ================================================= */
51
52 /* === M E T H O D S ======================================================= */
53
54 synfig::ValueNode_RadialComposite::ValueNode_RadialComposite(const ValueBase &value):
55         LinkableValueNode(value.get_type())
56 {
57         switch(get_type())
58         {
59                 case ValueBase::TYPE_VECTOR:
60                 {
61                         Vector vect(value.get(Vector()));
62                         set_link("r",ValueNode_Const::create(vect.mag()));
63                         set_link("t",ValueNode_Const::create(Angle(Angle::tan(vect[1],vect[0]))));
64                 }
65                         break;
66                 case ValueBase::TYPE_COLOR:
67                         set_link("y",ValueNode_Const::create(value.get(Color()).get_y()));
68                         set_link("s",ValueNode_Const::create(value.get(Color()).get_s()));
69                         set_link("h",ValueNode_Const::create(value.get(Color()).get_hue()));
70                         set_link("a",ValueNode_Const::create(value.get(Color()).get_a()));
71                         break;
72                 default:
73                         assert(0);
74                         throw Exception::BadType(ValueBase::type_name(get_type()));
75         }
76 }
77
78 ValueNode_RadialComposite::~ValueNode_RadialComposite()
79 {
80         unlink_all();
81 }
82
83 ValueNode_RadialComposite*
84 ValueNode_RadialComposite::create(const ValueBase &value)
85 {
86         return new ValueNode_RadialComposite(value);
87 }
88
89 LinkableValueNode*
90 ValueNode_RadialComposite::create_new()const
91 {
92         return new ValueNode_RadialComposite(ValueBase(get_type()));
93 }
94
95 ValueBase
96 synfig::ValueNode_RadialComposite::operator()(Time t)const
97 {
98         switch(get_type())
99         {
100                 case ValueBase::TYPE_VECTOR:
101                 {
102                         Real mag;
103                         Angle angle;
104                         assert(components[0] && components[1]);
105                         mag=(*components[0])(t).get(mag);
106                         angle=(*components[1])(t).get(angle);
107                         return Vector(Angle::cos(angle).get()*mag,Angle::sin(angle).get()*mag);
108                 }
109                 case ValueBase::TYPE_COLOR:
110                 {
111                         assert(components[0] && components[1] && components[2] && components[3]);
112                         return Color::YUV(
113                                 (*components[0])(t).get(Real()),
114                                 (*components[1])(t).get(Real()),
115                                 (*components[2])(t).get(Angle()),
116                                 (*components[3])(t).get(Real())
117                         );
118                 }
119                 default:
120                         synfig::error(string("ValueNode_RadialComposite::operator():")+_("Bad type for radialcomposite"));
121                         assert(components[0]);
122                         return (*components[0])(t);
123         }
124 }
125
126 int
127 ValueNode_RadialComposite::link_count()const
128 {
129         switch(get_type())
130         {
131         case ValueBase::TYPE_VECTOR:
132                 return 2;
133         case ValueBase::TYPE_COLOR:
134                 return 4;
135         default:
136                 synfig::warning(string("ValueNode_RadialComposite::component_count():")+_("Bad type for radialcomposite"));
137                 return 1;
138         }
139 }
140
141 bool
142 ValueNode_RadialComposite::set_link_vfunc(int i,ValueNode::Handle x)
143 {
144         assert(i>=0);
145         assert(i<6);
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                         assert(i<2);
157                         if(i==0 && x->get_type()!=ValueBase::TYPE_REAL)
158                                 return false;
159                         if(i==1 && x->get_type()!=ValueBase::TYPE_ANGLE)
160                                 return false;
161                         components[i]=x;
162                         return true;
163                         break;
164
165                 case ValueBase::TYPE_COLOR:
166                         assert(i<4);
167                         if((i==0 || i==1 || i==3) && x->get_type()!=ValueBase::TYPE_REAL)
168                                 return false;
169                         if((i==2) && x->get_type()!=ValueBase::TYPE_ANGLE)
170                                 return false;
171                         components[i]=x;
172                         return true;
173                         break;
174
175
176                 default:
177                         break;
178         }
179         return false;
180 }
181
182 ValueNode::LooseHandle
183 ValueNode_RadialComposite::get_link_vfunc(int i)const
184 {
185         assert(i>=0 && i<6);
186         return components[i];
187 }
188
189 String
190 ValueNode_RadialComposite::link_local_name(int i)const
191 {
192         assert(i>=0 && i<6);
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         return etl::strprintf(_("C%d"),i+1);
218 }
219
220
221 String
222 ValueNode_RadialComposite::link_name(int i)const
223 {
224         assert(i>=0 && i<5);
225         return strprintf("c%d",i);
226 }
227
228 int
229 ValueNode_RadialComposite::get_link_index_from_name(const String &name)const
230 {
231         if(name.empty())
232                 throw Exception::BadLinkName(name);
233
234         if(name[0]=='c')
235                 return name[1]-'0';
236
237         switch(get_type())
238         {
239         case ValueBase::TYPE_COLOR:
240                 if(name[0]=='y')
241                         return 0;
242                 if(name[0]=='s')
243                         return 1;
244                 if(name[0]=='h')
245                         return 2;
246                 if(name[0]=='a')
247                         return 3;
248         case ValueBase::TYPE_VECTOR:
249                 if(name[0]=='r')
250                         return 0;
251                 if(name[0]=='t')
252                         return 1;
253         default:
254                 break;
255         }
256
257         throw Exception::BadLinkName(name);
258 }
259
260 String
261 ValueNode_RadialComposite::get_name()const
262 {
263         return "radial_composite";
264 }
265
266 String
267 ValueNode_RadialComposite::get_local_name()const
268 {
269         return _("Radial Composite");
270 }
271
272 bool
273 ValueNode_RadialComposite::check_type(ValueBase::Type type)
274 {
275         return
276                 type==ValueBase::TYPE_VECTOR ||
277                 type==ValueBase::TYPE_COLOR;
278 }