Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_03 / synfig-core / src / synfig / valuenode_radialcomposite.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file valuenode_radialcomposite.cpp
3 **      \brief Template File
4 **
5 **      $Id: valuenode_radialcomposite.cpp,v 1.1.1.1 2005/01/04 01:23:15 darco Exp $
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **
10 **      This package is free software; you can redistribute it and/or
11 **      modify it under the terms of the GNU General Public License as
12 **      published by the Free Software Foundation; either version 2 of
13 **      the License, or (at your option) any later version.
14 **
15 **      This package is distributed in the hope that it will be useful,
16 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
17 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 **      General Public License for more details.
19 **      \endlegal
20 */
21 /* ========================================================================= */
22
23 /* === H E A D E R S ======================================================= */
24
25 #ifdef USING_PCH
26 #       include "pch.h"
27 #else
28 #ifdef HAVE_CONFIG_H
29 #       include <config.h>
30 #endif
31
32 #include "valuenode_radialcomposite.h"
33 #include "valuenode_const.h"
34 #include <stdexcept>
35 #include "general.h"
36 #include "color.h"
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 synfig::ValueNode_RadialComposite::ValueNode_RadialComposite(const ValueBase &value):
54         LinkableValueNode(value.get_type())
55 {
56         switch(get_type())
57         {
58                 case ValueBase::TYPE_VECTOR:
59                 {
60                         Vector vect(value.get(Vector()));
61                         set_link("r",ValueNode_Const::create(vect.mag()));
62                         set_link("t",ValueNode_Const::create(Angle(Angle::tan(vect[1],vect[0]))));
63                 }
64                         break;
65                 case ValueBase::TYPE_COLOR:
66                         set_link("y",ValueNode_Const::create(value.get(Color()).get_y()));
67                         set_link("s",ValueNode_Const::create(value.get(Color()).get_s()));
68                         set_link("h",ValueNode_Const::create(value.get(Color()).get_hue()));
69                         set_link("a",ValueNode_Const::create(value.get(Color()).get_a()));
70                         break;
71                 default:
72                         assert(0);
73                         throw Exception::BadType(ValueBase::type_name(get_type()));                     
74         }
75 }
76
77 ValueNode_RadialComposite::~ValueNode_RadialComposite()
78 {
79         unlink_all();
80 }
81
82 ValueNode_RadialComposite*
83 ValueNode_RadialComposite::create(const ValueBase &value)
84 {
85         return new ValueNode_RadialComposite(value);
86 }
87         
88 LinkableValueNode*
89 ValueNode_RadialComposite::create_new()const
90 {
91         return new ValueNode_RadialComposite(ValueBase(get_type()));
92 }
93
94 ValueBase
95 synfig::ValueNode_RadialComposite::operator()(Time t)const
96 {
97         switch(get_type())
98         {
99                 case ValueBase::TYPE_VECTOR:
100                 {
101                         Real mag;
102                         Angle angle;
103                         assert(components[0] && components[1]);
104                         mag=(*components[0])(t).get(mag);
105                         angle=(*components[1])(t).get(angle);
106                         return Vector(Angle::cos(angle).get()*mag,Angle::sin(angle).get()*mag);
107                 }
108                 case ValueBase::TYPE_COLOR:
109                 {
110                         assert(components[0] && components[1] && components[2] && components[3]);
111                         return Color::YUV(
112                                 (*components[0])(t).get(Real()),
113                                 (*components[1])(t).get(Real()),
114                                 (*components[2])(t).get(Angle()),
115                                 (*components[3])(t).get(Real())
116                         );
117                 }
118                 default:
119                         synfig::error(string("ValueNode_RadialComposite::operator():")+_("Bad type for radialcomposite"));
120                         assert(components[0]);
121                         return (*components[0])(t);
122         }
123 }
124
125 int
126 ValueNode_RadialComposite::link_count()const
127 {
128         switch(get_type())
129         {
130         case ValueBase::TYPE_VECTOR:
131                 return 2;
132         case ValueBase::TYPE_COLOR:
133                 return 4;
134         default:
135                 synfig::warning(string("ValueNode_RadialComposite::component_count():")+_("Bad type for radialcomposite"));
136                 return 1;
137         }
138 }
139
140 bool
141 ValueNode_RadialComposite::set_link_vfunc(int i,ValueNode::Handle x)
142 {
143         assert(i>=0);
144         assert(i<6);
145         
146         if(PlaceholderValueNode::Handle::cast_dynamic(x))
147         {
148                 components[i]=x;
149                 return true;
150         }
151
152         switch(get_type())
153         {
154                 case ValueBase::TYPE_VECTOR:
155                         assert(i<2);
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                         assert(i<4);
166                         if((i==0 || i==1 || i==3) && x->get_type()!=ValueBase::TYPE_REAL)
167                                 return false;
168                         if((i==2) && x->get_type()!=ValueBase::TYPE_ANGLE)
169                                 return false;
170                         components[i]=x;
171                         return true;
172                         break;
173                         
174                         
175                 default:
176                         break;
177         }
178         return false;   
179 }
180
181 ValueNode::LooseHandle
182 ValueNode_RadialComposite::get_link_vfunc(int i)const
183 {
184         assert(i>=0 && i<6);
185         return components[i];
186 }
187
188 String
189 ValueNode_RadialComposite::link_local_name(int i)const
190 {
191         assert(i>=0 && i<6);
192         switch(get_type())
193         {
194                 case ValueBase::TYPE_VECTOR:
195                         if(i==0)
196                                 return _("Radius");
197                         else if(i==1)
198                                 return _("Theta");
199                         break;
200
201                 case ValueBase::TYPE_COLOR:
202                         if(i==0)
203                                 return _("Luma");
204                         else if(i==1)
205                                 return _("Saturation");
206                         else if(i==2)
207                                 return _("Hue");
208                         else if(i==3)
209                                 return _("Alpha");
210                         break;
211                         
212                 default:
213                         break;
214         }
215         return etl::strprintf(_("C%d"),i+1);    
216 }       
217
218
219 String
220 ValueNode_RadialComposite::link_name(int i)const
221 {
222         assert(i>=0 && i<5);
223         return strprintf("c%d",i);
224 }       
225
226 int
227 ValueNode_RadialComposite::get_link_index_from_name(const String &name)const
228 {
229         if(name.empty())
230                 throw Exception::BadLinkName(name);
231
232         if(name[0]=='c')
233                 return name[1]-'0';
234
235         switch(get_type())
236         {
237         case ValueBase::TYPE_COLOR:
238                 if(name[0]=='y')
239                         return 0;
240                 if(name[0]=='s')
241                         return 1;
242                 if(name[0]=='h')
243                         return 2;
244                 if(name[0]=='a')
245                         return 3;
246         case ValueBase::TYPE_VECTOR:
247                 if(name[0]=='r')
248                         return 0;
249                 if(name[0]=='t')
250                         return 1;
251         default:                        
252                 break;
253         }
254
255         throw Exception::BadLinkName(name);
256 }
257
258 String
259 ValueNode_RadialComposite::get_name()const
260 {
261         return "radial_composite";
262 }
263
264 String
265 ValueNode_RadialComposite::get_local_name()const
266 {
267         return _("RadialComposite");
268 }
269
270 bool
271 ValueNode_RadialComposite::check_type(ValueBase::Type type)
272 {
273         return
274                 type==ValueBase::TYPE_VECTOR ||
275                 type==ValueBase::TYPE_COLOR;
276 }