moreupdates
[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: valuenode_radialcomposite.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 "valuenode_radialcomposite.h"
32 #include "valuenode_const.h"
33 #include <stdexcept>
34 #include "general.h"
35 #include "color.h"
36 #endif
37
38 /* === U S I N G =========================================================== */
39
40 using namespace std;
41 using namespace etl;
42 using namespace synfig;
43
44 /* === M A C R O S ========================================================= */
45
46 /* === G L O B A L S ======================================================= */
47
48 /* === P R O C E D U R E S ================================================= */
49
50 /* === M E T H O D S ======================================================= */
51
52 synfig::ValueNode_RadialComposite::ValueNode_RadialComposite(const ValueBase &value):
53         LinkableValueNode(value.get_type())
54 {
55         switch(get_type())
56         {
57                 case ValueBase::TYPE_VECTOR:
58                 {
59                         Vector vect(value.get(Vector()));
60                         set_link("r",ValueNode_Const::create(vect.mag()));
61                         set_link("t",ValueNode_Const::create(Angle(Angle::tan(vect[1],vect[0]))));
62                 }
63                         break;
64                 case ValueBase::TYPE_COLOR:
65                         set_link("y",ValueNode_Const::create(value.get(Color()).get_y()));
66                         set_link("s",ValueNode_Const::create(value.get(Color()).get_s()));
67                         set_link("h",ValueNode_Const::create(value.get(Color()).get_hue()));
68                         set_link("a",ValueNode_Const::create(value.get(Color()).get_a()));
69                         break;
70                 default:
71                         assert(0);
72                         throw Exception::BadType(ValueBase::type_name(get_type()));                     
73         }
74 }
75
76 ValueNode_RadialComposite::~ValueNode_RadialComposite()
77 {
78         unlink_all();
79 }
80
81 ValueNode_RadialComposite*
82 ValueNode_RadialComposite::create(const ValueBase &value)
83 {
84         return new ValueNode_RadialComposite(value);
85 }
86         
87 LinkableValueNode*
88 ValueNode_RadialComposite::create_new()const
89 {
90         return new ValueNode_RadialComposite(ValueBase(get_type()));
91 }
92
93 ValueBase
94 synfig::ValueNode_RadialComposite::operator()(Time t)const
95 {
96         switch(get_type())
97         {
98                 case ValueBase::TYPE_VECTOR:
99                 {
100                         Real mag;
101                         Angle angle;
102                         assert(components[0] && components[1]);
103                         mag=(*components[0])(t).get(mag);
104                         angle=(*components[1])(t).get(angle);
105                         return Vector(Angle::cos(angle).get()*mag,Angle::sin(angle).get()*mag);
106                 }
107                 case ValueBase::TYPE_COLOR:
108                 {
109                         assert(components[0] && components[1] && components[2] && components[3]);
110                         return Color::YUV(
111                                 (*components[0])(t).get(Real()),
112                                 (*components[1])(t).get(Real()),
113                                 (*components[2])(t).get(Angle()),
114                                 (*components[3])(t).get(Real())
115                         );
116                 }
117                 default:
118                         synfig::error(string("ValueNode_RadialComposite::operator():")+_("Bad type for radialcomposite"));
119                         assert(components[0]);
120                         return (*components[0])(t);
121         }
122 }
123
124 int
125 ValueNode_RadialComposite::link_count()const
126 {
127         switch(get_type())
128         {
129         case ValueBase::TYPE_VECTOR:
130                 return 2;
131         case ValueBase::TYPE_COLOR:
132                 return 4;
133         default:
134                 synfig::warning(string("ValueNode_RadialComposite::component_count():")+_("Bad type for radialcomposite"));
135                 return 1;
136         }
137 }
138
139 bool
140 ValueNode_RadialComposite::set_link_vfunc(int i,ValueNode::Handle x)
141 {
142         assert(i>=0);
143         assert(i<6);
144         
145         if(PlaceholderValueNode::Handle::cast_dynamic(x))
146         {
147                 components[i]=x;
148                 return true;
149         }
150
151         switch(get_type())
152         {
153                 case ValueBase::TYPE_VECTOR:
154                         assert(i<2);
155                         if(i==0 && x->get_type()!=ValueBase::TYPE_REAL)
156                                 return false;
157                         if(i==1 && x->get_type()!=ValueBase::TYPE_ANGLE)
158                                 return false;
159                         components[i]=x;
160                         return true;
161                         break;
162
163                 case ValueBase::TYPE_COLOR:
164                         assert(i<4);
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<6);
184         return components[i];
185 }
186
187 String
188 ValueNode_RadialComposite::link_local_name(int i)const
189 {
190         assert(i>=0 && i<6);
191         switch(get_type())
192         {
193                 case ValueBase::TYPE_VECTOR:
194                         if(i==0)
195                                 return _("Radius");
196                         else if(i==1)
197                                 return _("Theta");
198                         break;
199
200                 case ValueBase::TYPE_COLOR:
201                         if(i==0)
202                                 return _("Luma");
203                         else if(i==1)
204                                 return _("Saturation");
205                         else if(i==2)
206                                 return _("Hue");
207                         else if(i==3)
208                                 return _("Alpha");
209                         break;
210                         
211                 default:
212                         break;
213         }
214         return etl::strprintf(_("C%d"),i+1);    
215 }       
216
217
218 String
219 ValueNode_RadialComposite::link_name(int i)const
220 {
221         assert(i>=0 && i<5);
222         return strprintf("c%d",i);
223 }       
224
225 int
226 ValueNode_RadialComposite::get_link_index_from_name(const String &name)const
227 {
228         if(name.empty())
229                 throw Exception::BadLinkName(name);
230
231         if(name[0]=='c')
232                 return name[1]-'0';
233
234         switch(get_type())
235         {
236         case ValueBase::TYPE_COLOR:
237                 if(name[0]=='y')
238                         return 0;
239                 if(name[0]=='s')
240                         return 1;
241                 if(name[0]=='h')
242                         return 2;
243                 if(name[0]=='a')
244                         return 3;
245         case ValueBase::TYPE_VECTOR:
246                 if(name[0]=='r')
247                         return 0;
248                 if(name[0]=='t')
249                         return 1;
250         default:                        
251                 break;
252         }
253
254         throw Exception::BadLinkName(name);
255 }
256
257 String
258 ValueNode_RadialComposite::get_name()const
259 {
260         return "radial_composite";
261 }
262
263 String
264 ValueNode_RadialComposite::get_local_name()const
265 {
266         return _("RadialComposite");
267 }
268
269 bool
270 ValueNode_RadialComposite::check_type(ValueBase::Type type)
271 {
272         return
273                 type==ValueBase::TYPE_VECTOR ||
274                 type==ValueBase::TYPE_COLOR;
275 }