Tidying.
[synfig.git] / synfig-core / trunk / 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 **
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
216         return etl::strprintf(_("C%d"),i+1);
217 }
218
219
220 String
221 ValueNode_RadialComposite::link_name(int i)const
222 {
223         assert(i>=0 && i<5);
224         return strprintf("c%d",i);
225 }
226
227 int
228 ValueNode_RadialComposite::get_link_index_from_name(const String &name)const
229 {
230         if(name.empty())
231                 throw Exception::BadLinkName(name);
232
233         if(name[0]=='c')
234                 return name[1]-'0';
235
236         switch(get_type())
237         {
238         case ValueBase::TYPE_COLOR:
239                 if(name[0]=='y')
240                         return 0;
241                 if(name[0]=='s')
242                         return 1;
243                 if(name[0]=='h')
244                         return 2;
245                 if(name[0]=='a')
246                         return 3;
247         case ValueBase::TYPE_VECTOR:
248                 if(name[0]=='r')
249                         return 0;
250                 if(name[0]=='t')
251                         return 1;
252         default:
253                 break;
254         }
255
256         throw Exception::BadLinkName(name);
257 }
258
259 String
260 ValueNode_RadialComposite::get_name()const
261 {
262         return "radial_composite";
263 }
264
265 String
266 ValueNode_RadialComposite::get_local_name()const
267 {
268         return _("RadialComposite");
269 }
270
271 bool
272 ValueNode_RadialComposite::check_type(ValueBase::Type type)
273 {
274         return
275                 type==ValueBase::TYPE_VECTOR ||
276                 type==ValueBase::TYPE_COLOR;
277 }